1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
#include "Core.h"
#if defined CC_BUILD_PSVITA
#include "_PlatformBase.h"
#include "Stream.h"
#include "ExtMath.h"
#include "Funcs.h"
#include "Window.h"
#include "Utils.h"
#include "Errors.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <vitasdk.h>
#include "_PlatformConsole.h"
const cc_result ReturnCode_FileShareViolation = 1000000000; // not used
const cc_result ReturnCode_FileNotFound = ENOENT;
const cc_result ReturnCode_SocketInProgess = SCE_NET_ERROR_EINPROGRESS;
const cc_result ReturnCode_SocketWouldBlock = SCE_NET_ERROR_EWOULDBLOCK;
const cc_result ReturnCode_DirectoryExists = EEXIST;
const char* Platform_AppNameSuffix = " PS Vita";
static int epoll_id;
static int stdout_fd;
/*########################################################################################################################*
*------------------------------------------------------Logging/Time-------------------------------------------------------*
*#########################################################################################################################*/
cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) {
if (end < beg) return 0;
return end - beg;
}
void Platform_Log(const char* msg, int len) {
if (!stdout_fd) stdout_fd = sceKernelGetStdout();
sceIoWrite(stdout_fd, msg, len);
}
TimeMS DateTime_CurrentUTC(void) {
struct SceKernelTimeval cur;
sceKernelLibcGettimeofday(&cur, NULL);
return (cc_uint64)cur.sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {
SceDateTime curTime;
sceRtcGetCurrentClockLocalTime(&curTime);
t->year = curTime.year;
t->month = curTime.month;
t->day = curTime.day;
t->hour = curTime.hour;
t->minute = curTime.minute;
t->second = curTime.second;
}
#define US_PER_SEC 1000000ULL
cc_uint64 Stopwatch_Measure(void) {
// TODO: sceKernelGetSystemTimeWide
struct SceKernelTimeval cur;
sceKernelLibcGettimeofday(&cur, NULL);
return (cc_uint64)cur.sec * US_PER_SEC + cur.usec;
}
/*########################################################################################################################*
*-----------------------------------------------------Directory/File------------------------------------------------------*
*#########################################################################################################################*/
static const cc_string root_path = String_FromConst("ux0:data/ClassiCube/");
static void GetNativePath(char* str, const cc_string* path) {
Mem_Copy(str, root_path.buffer, root_path.length);
str += root_path.length;
String_EncodeUtf8(str, path);
}
#define GetSCEResult(result) (result >= 0 ? 0 : result & 0xFFFF)
cc_result Directory_Create(const cc_string* path) {
char str[NATIVE_STR_LEN];
GetNativePath(str, path);
int result = sceIoMkdir(str, 0777);
return GetSCEResult(result);
}
int File_Exists(const cc_string* path) {
char str[NATIVE_STR_LEN];
SceIoStat sb;
GetNativePath(str, path);
return sceIoGetstat(str, &sb) == 0 && SCE_S_ISREG(sb.st_mode) != 0;
}
cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) {
cc_string path; char pathBuffer[FILENAME_SIZE];
char str[NATIVE_STR_LEN];
int res;
GetNativePath(str, dirPath);
SceUID uid = sceIoDopen(str);
if (uid < 0) return GetSCEResult(uid); // error
String_InitArray(path, pathBuffer);
SceIoDirent entry;
while ((res = sceIoDread(uid, &entry)) > 0) {
path.length = 0;
String_Format1(&path, "%s/", dirPath);
// ignore . and .. entry (PSP does return them)
char* src = entry.d_name;
if (src[0] == '.' && src[1] == '\0') continue;
if (src[0] == '.' && src[1] == '.' && src[2] == '\0') continue;
int len = String_Length(src);
String_AppendUtf8(&path, src, len);
if (entry.d_stat.st_attr & SCE_SO_IFDIR) {
res = Directory_Enum(&path, obj, callback);
if (res) break;
} else {
callback(&path, obj);
}
}
sceIoDclose(uid);
return GetSCEResult(res);
}
static cc_result File_Do(cc_file* file, const cc_string* path, int mode) {
char str[NATIVE_STR_LEN];
GetNativePath(str, path);
int result = sceIoOpen(str, mode, 0777);
*file = result;
return GetSCEResult(result);
}
cc_result File_Open(cc_file* file, const cc_string* path) {
return File_Do(file, path, SCE_O_RDONLY);
}
cc_result File_Create(cc_file* file, const cc_string* path) {
return File_Do(file, path, SCE_O_RDWR | SCE_O_CREAT | SCE_O_TRUNC);
}
cc_result File_OpenOrCreate(cc_file* file, const cc_string* path) {
return File_Do(file, path, SCE_O_RDWR | SCE_O_CREAT);
}
cc_result File_Read(cc_file file, void* data, cc_uint32 count, cc_uint32* bytesRead) {
int result = sceIoRead(file, data, count);
*bytesRead = result;
return GetSCEResult(result);
}
cc_result File_Write(cc_file file, const void* data, cc_uint32 count, cc_uint32* bytesWrote) {
int result = sceIoWrite(file, data, count);
*bytesWrote = result;
return GetSCEResult(result);
}
cc_result File_Close(cc_file file) {
int result = sceIoClose(file);
return GetSCEResult(result);
}
cc_result File_Seek(cc_file file, int offset, int seekType) {
static cc_uint8 modes[3] = { SCE_SEEK_SET, SCE_SEEK_CUR, SCE_SEEK_END };
int result = sceIoLseek32(file, offset, modes[seekType]);
return GetSCEResult(result);
}
cc_result File_Position(cc_file file, cc_uint32* pos) {
int result = sceIoLseek32(file, 0, SCE_SEEK_CUR);
*pos = result;
return GetSCEResult(result);
}
cc_result File_Length(cc_file file, cc_uint32* len) {
int curPos = sceIoLseek32(file, 0, SCE_SEEK_CUR);
if (curPos < 0) { *len = -1; return GetSCEResult(curPos); }
*len = sceIoLseek32(file, 0, SCE_SEEK_END);
sceIoLseek32(file, curPos, SCE_SEEK_SET); // restore position
return 0;
}
/*########################################################################################################################*
*--------------------------------------------------------Threading--------------------------------------------------------*
*#########################################################################################################################*/
// !!! NOTE: PSP uses cooperative multithreading (not preemptive multithreading) !!!
void Thread_Sleep(cc_uint32 milliseconds) {
sceKernelDelayThread(milliseconds * 1000);
}
static int ExecThread(unsigned int argc, void *argv) {
Thread_StartFunc* func = (Thread_StartFunc*)argv;
(*func)();
return 0;
}
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
#define CC_THREAD_PRIORITY 0x10000100
#define CC_THREAD_ATTRS 0 // TODO PSP_THREAD_ATTR_VFPU?
Thread_StartFunc func_ = func;
int threadID = sceKernelCreateThread(name, ExecThread, CC_THREAD_PRIORITY,
stackSize, CC_THREAD_ATTRS, 0, NULL);
*handle = (int)threadID;
sceKernelStartThread(threadID, sizeof(func_), (void*)&func_);
}
void Thread_Detach(void* handle) {
sceKernelDeleteThread((int)handle); // TODO don't call this??
}
void Thread_Join(void* handle) {
sceKernelWaitThreadEnd((int)handle, NULL, NULL);
sceKernelDeleteThread((int)handle);
}
void* Mutex_Create(const char* name) {
SceKernelLwMutexWork* ptr = (SceKernelLwMutexWork*)Mem_Alloc(1, sizeof(SceKernelLwMutexWork), "mutex");
int res = sceKernelCreateLwMutex(ptr, name, 0, 0, NULL);
if (res) Logger_Abort2(res, "Creating mutex");
return ptr;
}
void Mutex_Free(void* handle) {
int res = sceKernelDeleteLwMutex((SceKernelLwMutexWork*)handle);
if (res) Logger_Abort2(res, "Destroying mutex");
Mem_Free(handle);
}
void Mutex_Lock(void* handle) {
int res = sceKernelLockLwMutex((SceKernelLwMutexWork*)handle, 1, NULL);
if (res) Logger_Abort2(res, "Locking mutex");
}
void Mutex_Unlock(void* handle) {
int res = sceKernelUnlockLwMutex((SceKernelLwMutexWork*)handle, 1);
if (res) Logger_Abort2(res, "Unlocking mutex");
}
void* Waitable_Create(const char* name) {
int evid = sceKernelCreateEventFlag(name, SCE_EVENT_WAITMULTIPLE, 0, NULL);
if (evid < 0) Logger_Abort2(evid, "Creating waitable");
return (void*)evid;
}
void Waitable_Free(void* handle) {
sceKernelDeleteEventFlag((int)handle);
}
void Waitable_Signal(void* handle) {
int res = sceKernelSetEventFlag((int)handle, 0x1);
if (res < 0) Logger_Abort2(res, "Signalling event");
}
void Waitable_Wait(void* handle) {
unsigned int match;
int res = sceKernelWaitEventFlag((int)handle, 0x1, SCE_EVENT_WAITAND | SCE_EVENT_WAITCLEAR, &match, NULL);
if (res < 0) Logger_Abort2(res, "Event wait");
}
void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) {
SceUInt timeout = milliseconds * 1000;
unsigned int match;
int res = sceKernelWaitEventFlag((int)handle, 0x1, SCE_EVENT_WAITAND | SCE_EVENT_WAITCLEAR, &match, &timeout);
if (res < 0) Logger_Abort2(res, "Event timed wait");
}
/*########################################################################################################################*
*---------------------------------------------------------Socket----------------------------------------------------------*
*#########################################################################################################################*/
cc_result Socket_ParseAddress(const cc_string* address, int port, cc_sockaddr* addrs, int* numValidAddrs) {
struct SceNetSockaddrIn* addr4 = (struct SceNetSockaddrIn*)addrs[0].data;
char str[NATIVE_STR_LEN];
char buf[1024];
int rid, ret;
String_EncodeUtf8(str, address);
*numValidAddrs = 1;
if (sceNetInetPton(SCE_NET_AF_INET, str, &addr4->sin_addr) <= 0) {
/* Fallback to resolving as DNS name */
rid = sceNetResolverCreate("CC resolver", NULL, 0);
if (rid < 0) return ERR_INVALID_ARGUMENT;
ret = sceNetResolverStartNtoa(rid, str, &addr4->sin_addr, 0, 0, 0);
sceNetResolverDestroy(rid);
if (ret) return ret;
}
addr4->sin_family = SCE_NET_AF_INET;
addr4->sin_port = sceNetHtons(port);
addrs[0].size = sizeof(*addr4);
return 0;
}
cc_result Socket_Connect(cc_socket* s, cc_sockaddr* addr, cc_bool nonblocking) {
struct SceNetSockaddr* raw = (struct SceNetSockaddr*)addr->data;
int res;
*s = sceNetSocket("CC socket", raw->sa_family, SCE_NET_SOCK_STREAM, SCE_NET_IPPROTO_TCP);
if (*s < 0) return *s;
if (nonblocking) {
int on = 1;
sceNetSetsockopt(*s, SCE_NET_SOL_SOCKET, SCE_NET_SO_NBIO, &on, sizeof(int));
}
res = sceNetConnect(*s, raw, addr->size);
return res;
}
cc_result Socket_Read(cc_socket s, cc_uint8* data, cc_uint32 count, cc_uint32* modified) {
int recvCount = sceNetRecv(s, data, count, 0);
if (recvCount >= 0) { *modified = recvCount; return 0; }
*modified = 0;
return recvCount;
}
cc_result Socket_Write(cc_socket s, const cc_uint8* data, cc_uint32 count, cc_uint32* modified) {
int sentCount = sceNetSend(s, data, count, 0);
if (sentCount >= 0) { *modified = sentCount; return 0; }
*modified = 0;
return sentCount;
}
void Socket_Close(cc_socket s) {
sceNetShutdown(s, SCE_NET_SHUT_RDWR);
sceNetSocketClose(s);
}
static cc_result Socket_Poll(cc_socket s, int mode, cc_bool* success) {
SceNetEpollEvent ev = { 0 };
// to match select, closed socket still counts as readable
int flags = mode == SOCKET_POLL_READ ? (SCE_NET_EPOLLIN | SCE_NET_EPOLLHUP) : SCE_NET_EPOLLOUT;
int res, num_events;
ev.data.fd = s;
ev.events = flags;
if ((res = sceNetEpollControl(epoll_id, SCE_NET_EPOLL_CTL_ADD, s, &ev))) return res;
num_events = sceNetEpollWait(epoll_id, &ev, 1, 0);
sceNetEpollControl(epoll_id, SCE_NET_EPOLL_CTL_DEL, s, NULL);
if (num_events < 0) return num_events;
if (num_events == 0) { *success = false; return 0; }
*success = (ev.events & flags) != 0;
return 0;
}
cc_result Socket_CheckReadable(cc_socket s, cc_bool* readable) {
return Socket_Poll(s, SOCKET_POLL_READ, readable);
}
cc_result Socket_CheckWritable(cc_socket s, cc_bool* writable) {
uint32_t resultSize = sizeof(uint32_t);
cc_result res = Socket_Poll(s, SOCKET_POLL_WRITE, writable);
if (res || *writable) return res;
// https://stackoverflow.com/questions/29479953/so-error-value-after-successful-socket-operation
sceNetGetsockopt(s, SCE_NET_SOL_SOCKET, SCE_NET_SO_ERROR, &res, &resultSize);
return res;
}
/*########################################################################################################################*
*--------------------------------------------------------Platform---------------------------------------------------------*
*#########################################################################################################################*/
static char net_memory[512 * 1024] __attribute__ ((aligned (16))); // TODO is just 256 kb enough ?
static void InitNetworking(void) {
sceSysmoduleLoadModule(SCE_SYSMODULE_NET);
SceNetInitParam param;
param.memory = net_memory;
param.size = sizeof(net_memory);
param.flags = 0;
int ret = sceNetInit(¶m);
if (ret < 0) Platform_Log1("Network init failed: %i", &ret);
}
void Platform_Init(void) {
/*pspDebugSioInit();*/
InitNetworking();
epoll_id = sceNetEpollCreate("CC poll", 0);
// Create root directory
Directory_Create(&String_Empty);
}
void Platform_Free(void) { }
cc_bool Platform_DescribeError(cc_result res, cc_string* dst) {
char chars[NATIVE_STR_LEN];
int len;
/* For unrecognised error codes, strerror_r might return messages */
/* such as 'No error information', which is not very useful */
/* (could check errno here but quicker just to skip entirely) */
if (res >= 1000) return false;
len = strerror_r(res, chars, NATIVE_STR_LEN);
if (len == -1) return false;
len = String_CalcLen(chars, NATIVE_STR_LEN);
String_AppendUtf8(dst, chars, len);
return true;
}
cc_bool Process_OpenSupported = false;
cc_result Process_StartOpen(const cc_string* args) {
return ERR_NOT_SUPPORTED;
}
/*########################################################################################################################*
*-------------------------------------------------------Encryption--------------------------------------------------------*
*#########################################################################################################################*/
static cc_result GetMachineID(cc_uint32* key) {
return ERR_NOT_SUPPORTED;
}
#endif
|