diff options
author | WlodekM <[email protected]> | 2024-06-16 10:35:45 +0300 |
---|---|---|
committer | WlodekM <[email protected]> | 2024-06-16 10:35:45 +0300 |
commit | abef6da56913f1c55528103e60a50451a39628b1 (patch) | |
tree | b3c8092471ecbb73e568cd0d336efa0e7871ee8d /src/Platform_Xbox360.c |
initial commit
Diffstat (limited to 'src/Platform_Xbox360.c')
-rw-r--r-- | src/Platform_Xbox360.c | 295 |
1 files changed, 295 insertions, 0 deletions
diff --git a/src/Platform_Xbox360.c b/src/Platform_Xbox360.c new file mode 100644 index 0000000..86d4839 --- /dev/null +++ b/src/Platform_Xbox360.c @@ -0,0 +1,295 @@ +#include "Core.h" +#if defined CC_BUILD_XBOX360 +#include "_PlatformBase.h" +#include "Stream.h" +#include "Funcs.h" +#include "Utils.h" +#include "Errors.h" + +#define LWIP_SOCKET 1 +#include <lwip/sockets.h> +#include <xenos/xe.h> +#include <xenos/xenos.h> +#include <xenos/edram.h> +#include <console/console.h> +#include <network/network.h> +#include <ppc/timebase.h> +#include <time/time.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <dirent.h> +#include <fcntl.h> +#include "_PlatformConsole.h" + +const cc_result ReturnCode_FileShareViolation = 1000000000; /* TODO: not used apparently */ +const cc_result ReturnCode_FileNotFound = ENOENT; +const cc_result ReturnCode_SocketInProgess = EINPROGRESS; +const cc_result ReturnCode_SocketWouldBlock = EWOULDBLOCK; +const cc_result ReturnCode_DirectoryExists = EEXIST; +const char* Platform_AppNameSuffix = " XBox 360"; + + +/*########################################################################################################################* +*------------------------------------------------------Logging/Time-------------------------------------------------------* +*#########################################################################################################################*/ +void Platform_Log(const char* msg, int len) { + char tmp[2048 + 1]; + len = min(len, 2048); + Mem_Copy(tmp, msg, len); tmp[len] = '\0'; + + puts(tmp); +} + +TimeMS DateTime_CurrentUTC(void) { + struct timeval cur; + gettimeofday(&cur, NULL); + return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS; +} + +void DateTime_CurrentLocal(struct DateTime* t) { + struct timeval cur; + struct tm loc_time; + gettimeofday(&cur, NULL); + localtime_r(&cur.tv_sec, &loc_time); + + t->year = loc_time.tm_year + 1900; + t->month = loc_time.tm_mon + 1; + t->day = loc_time.tm_mday; + t->hour = loc_time.tm_hour; + t->minute = loc_time.tm_min; + t->second = loc_time.tm_sec; +} + +cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) { + if (end < beg) return 0; + return tb_diff_usec(end, beg); +} + +cc_uint64 Stopwatch_Measure(void) { + return mftb(); +} + + +/*########################################################################################################################* +*-----------------------------------------------------Directory/File------------------------------------------------------* +*#########################################################################################################################*/ +static char root_buffer[NATIVE_STR_LEN]; +static cc_string root_path = String_FromArray(root_buffer); + +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); +} + +cc_result Directory_Create(const cc_string* path) { + char str[NATIVE_STR_LEN]; + GetNativePath(str, path); + return mkdir(str, 0) == -1 ? errno : 0; +} + +int File_Exists(const cc_string* path) { + char str[NATIVE_STR_LEN]; + struct stat sb; + GetNativePath(str, path); + return stat(str, &sb) == 0 && S_ISREG(sb.st_mode); +} + +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]; + struct dirent* entry; + int res; + + GetNativePath(str, dirPath); + DIR* dirPtr = opendir(str); + if (!dirPtr) return errno; + + // POSIX docs: "When the end of the directory is encountered, a null pointer is returned and errno is not changed." + // errno is sometimes leftover from previous calls, so always reset it before readdir gets called + errno = 0; + String_InitArray(path, pathBuffer); + + while ((entry = readdir(dirPtr))) { + path.length = 0; + String_Format1(&path, "%s/", dirPath); + + // ignore . and .. entry + 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); + int is_dir = entry->d_type == DT_DIR; + // TODO: fallback to stat when this fails + + if (is_dir) { + res = Directory_Enum(&path, obj, callback); + if (res) { closedir(dirPtr); return res; } + } else { + callback(&path, obj); + } + errno = 0; + } + + res = errno; // return code from readdir + closedir(dirPtr); + return res; +} + +static cc_result File_Do(cc_file* file, const cc_string* path, int mode) { + char str[NATIVE_STR_LEN]; + GetNativePath(str, path); + *file = open(str, mode, 0); + return *file == -1 ? errno : 0; +} + +cc_result File_Open(cc_file* file, const cc_string* path) { + return File_Do(file, path, O_RDONLY); +} +cc_result File_Create(cc_file* file, const cc_string* path) { + return File_Do(file, path, O_RDWR | O_CREAT | O_TRUNC); +} +cc_result File_OpenOrCreate(cc_file* file, const cc_string* path) { + return File_Do(file, path, O_RDWR | O_CREAT); +} + +cc_result File_Read(cc_file file, void* data, cc_uint32 count, cc_uint32* bytesRead) { + *bytesRead = read(file, data, count); + return *bytesRead == -1 ? errno : 0; +} + +cc_result File_Write(cc_file file, const void* data, cc_uint32 count, cc_uint32* bytesWrote) { + *bytesWrote = write(file, data, count); + return *bytesWrote == -1 ? errno : 0; +} + +cc_result File_Close(cc_file file) { + return close(file) == -1 ? errno : 0; +} + +cc_result File_Seek(cc_file file, int offset, int seekType) { + static cc_uint8 modes[3] = { SEEK_SET, SEEK_CUR, SEEK_END }; + return lseek(file, offset, modes[seekType]) == -1 ? errno : 0; +} + +cc_result File_Position(cc_file file, cc_uint32* pos) { + *pos = lseek(file, 0, SEEK_CUR); + return *pos == -1 ? errno : 0; +} + +cc_result File_Length(cc_file file, cc_uint32* len) { + struct stat st; + if (fstat(file, &st) == -1) { *len = -1; return errno; } + *len = st.st_size; return 0; +} + +/*########################################################################################################################* +*--------------------------------------------------------Threading--------------------------------------------------------* +*#############################################################################################################p############*/ +void Thread_Sleep(cc_uint32 milliseconds) { mdelay(milliseconds); } + +void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) { + *handle = NULL; // TODO +} + +void Thread_Start2(void* handle, Thread_StartFunc func) {// TODO +} + +void Thread_Detach(void* handle) {// TODO +} + +void Thread_Join(void* handle) {// TODO +} + +void* Mutex_Create(const char* name) { + return Mem_AllocCleared(1, sizeof(int), "mutex"); +} + +void Mutex_Free(void* handle) { + Mem_Free(handle); +} +void Mutex_Lock(void* handle) { } // TODO +void Mutex_Unlock(void* handle) { } // TODO + +void* Waitable_Create(const char* name) { + return NULL; // TODO +} + +void Waitable_Free(void* handle) { + // TODO +} + +void Waitable_Signal(void* handle) { } // TODO } +void Waitable_Wait(void* handle) { + // TODO +} + +void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) { + // TODO +} + + +/*########################################################################################################################* +*---------------------------------------------------------Socket----------------------------------------------------------* +*#########################################################################################################################*/ +cc_result Socket_ParseAddress(const cc_string* address, int port, cc_sockaddr* addrs, int* numValidAddrs) { + return ERR_NOT_SUPPORTED; +} + +cc_result Socket_Connect(cc_socket* s, cc_sockaddr* addr, cc_bool nonblocking) { + return ERR_NOT_SUPPORTED; +} + +cc_result Socket_Read(cc_socket s, cc_uint8* data, cc_uint32 count, cc_uint32* modified) { + return ERR_NOT_SUPPORTED; +} + +cc_result Socket_Write(cc_socket s, const cc_uint8* data, cc_uint32 count, cc_uint32* modified) { + return ERR_NOT_SUPPORTED; +} + +void Socket_Close(cc_socket s) { +} + +cc_result Socket_CheckReadable(cc_socket s, cc_bool* readable) { + return ERR_NOT_SUPPORTED; +} + +cc_result Socket_CheckWritable(cc_socket s, cc_bool* writable) { + return ERR_NOT_SUPPORTED; +} + + +/*########################################################################################################################* +*--------------------------------------------------------Platform---------------------------------------------------------* +*#########################################################################################################################*/ +void Platform_Init(void) { + xenos_init(VIDEO_MODE_AUTO); + console_init(); + //network_init(); +} + +void Platform_Free(void) { +} + +cc_bool Platform_DescribeError(cc_result res, cc_string* dst) { + return false; +} + +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 |