From 4e37936525fa5292f168d7fcd78c2ea5d38f347e Mon Sep 17 00:00:00 2001 From: Phil564 <93886661+Phil564@users.noreply.github.com> Date: Sun, 16 Mar 2025 16:12:19 -0400 Subject: [PATCH] N64: Filesystem support for flashcarts --- src/Core.h | 1 - src/Platform_N64.c | 113 +++++++++++++++++++++++++++------------------ 2 files changed, 68 insertions(+), 46 deletions(-) diff --git a/src/Core.h b/src/Core.h index 83f53a3fd..c51ad19bd 100644 --- a/src/Core.h +++ b/src/Core.h @@ -425,7 +425,6 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_SPLITSCREEN #undef CC_BUILD_RESOURCES #undef CC_BUILD_NETWORKING - #undef CC_BUILD_FILESYSTEM #elif defined PLAT_PS2 #define CC_BUILD_PS2 #define CC_BUILD_CONSOLE diff --git a/src/Platform_N64.c b/src/Platform_N64.c index 2ab38b560..ccff4cac4 100644 --- a/src/Platform_N64.c +++ b/src/Platform_N64.c @@ -19,8 +19,14 @@ //#include #include #include +#include #include "_PlatformConsole.h" +// These functions are private in libdragon. FatFs functions are instead recommanded. +int open( const char *file, int flags, ... ); +int stat( const char *file, struct stat *st ); +int mkdir( const char * path, mode_t mode ); + const cc_result ReturnCode_FileShareViolation = 1000000000; // not used const cc_result ReturnCode_FileNotFound = ENOENT; const cc_result ReturnCode_DirectoryExists = EEXIST; @@ -29,7 +35,7 @@ const cc_result ReturnCode_SocketWouldBlock = EWOULDBLOCK; const cc_result ReturnCode_SocketDropped = EPIPE; const char* Platform_AppNameSuffix = " N64"; -cc_bool Platform_ReadonlyFilesystem = true; +cc_bool Platform_ReadonlyFilesystem = false; /*########################################################################################################################* @@ -81,14 +87,16 @@ void Process_Abort2(cc_result result, const char* raw_msg) { /*########################################################################################################################* *-----------------------------------------------------Directory/File------------------------------------------------------* *#########################################################################################################################*/ -static const cc_string root_path = String_FromConst("/"); +static cc_string root_path = String_FromConst("sd:/ClassiCube/"); +static cc_string root_path_rom = String_FromConst("rom:/"); void Platform_EncodePath(cc_filepath* dst, const cc_string* path) { char* str = dst->buffer; - // TODO temp hack cc_string path_ = *path; - int idx = String_IndexOf(path, '/'); - if (idx >= 0) path_ = String_UNSAFE_SubstringAt(&path_, idx + 1); + if (Platform_ReadonlyFilesystem) { // read from rom root + int idx = String_IndexOf(path, '/'); + if (idx >= 0) path_ = String_UNSAFE_SubstringAt(&path_, idx + 1); + } Mem_Copy(str, root_path.buffer, root_path.length); str += root_path.length; @@ -96,80 +104,90 @@ void Platform_EncodePath(cc_filepath* dst, const cc_string* path) { } cc_result Directory_Create(const cc_filepath* path) { - return ERR_NOT_SUPPORTED; + return mkdir(path->buffer, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1 ? errno : 0; } int File_Exists(const cc_filepath* path) { - return false; + struct stat sb; + return stat(path->buffer, &sb) == 0 && S_ISREG(sb.st_mode); } cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) { - return ERR_NOT_SUPPORTED; // TODO add support + cc_string path; char pathBuffer[FILENAME_SIZE]; + cc_filepath str; + dir_t entry; + + Platform_EncodePath(&str, dirPath); + int res = dir_findfirst(str.buffer, &entry); + if (res != 0) return errno; + + errno = 0; + String_InitArray(path, pathBuffer); + + while (res == 0) { + 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; + callback(&path, obj, is_dir); + errno = 0; + res = dir_findnext(str.buffer, &entry); + } + + return errno; // no clue if libdragon actually sets errno... } -static cc_result File_Do(cc_file* file, const char* path) { - //*file = -1; - //return ReturnCode_FileNotFound; - // TODO: Why does trying this code break everything - - int ret = dfs_open(path); - Platform_Log2("Opened %c = %i", path, &ret); - if (ret < 0) { *file = -1; return ret; } - - *file = ret; - return 0; +static cc_result File_Do(cc_file* file, const char* path, int mode) { + *file = open(path, mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + return *file == -1 ? errno : 0; } cc_result File_Open(cc_file* file, const cc_filepath* path) { - return File_Do(file, path->buffer); + return File_Do(file, path->buffer, O_RDONLY); } cc_result File_Create(cc_file* file, const cc_filepath* path) { - *file = -1; - return ERR_NOT_SUPPORTED; - //return File_Do(file, path->buffer); + return File_Do(file, path->buffer, O_RDWR | O_CREAT | O_TRUNC); } cc_result File_OpenOrCreate(cc_file* file, const cc_filepath* path) { - *file = -1; - return ERR_NOT_SUPPORTED; - //return File_Do(file, path->buffer); + return File_Do(file, path->buffer, O_RDWR | O_CREAT); } cc_result File_Read(cc_file file, void* data, cc_uint32 count, cc_uint32* bytesRead) { - int ret = dfs_read(data, 1, count, file); - if (ret < 0) { *bytesRead = -1; return ret; } - - *bytesRead = ret; - return 0; + *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) { - return ERR_NOT_SUPPORTED; + *bytesWrote = write(file, data, count); + return *bytesWrote == -1 ? errno : 0; } cc_result File_Close(cc_file file) { - if (file < 0) return 0; - return dfs_close(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 dfs_seek(file, offset, modes[seekType]); + return lseek(file, offset, modes[seekType]) == -1 ? errno : 0; } cc_result File_Position(cc_file file, cc_uint32* pos) { - int ret = dfs_tell(file); - if (ret < 0) { *pos = -1; return ret; } - - *pos = ret; - return 0; + *pos = lseek(file, 0, SEEK_CUR); + return *pos == -1 ? errno : 0; } cc_result File_Length(cc_file file, cc_uint32* len) { - int ret = dfs_size(file); - if (ret < 0) { *len = -1; return ret; } - - *len = ret; - return 0; + struct stat st; + if (fstat(file, &st) == -1) { *len = -1; return errno; } + *len = st.st_size; return 0; } @@ -277,6 +295,11 @@ void Platform_Init(void) { dfs_init(DFS_DEFAULT_LOCATION); timer_init(); rtc_init(); + if (!debug_init_sdfs("sd:/", -1)) { + String_Copy(&root_path, &root_path_rom); + Platform_ReadonlyFilesystem = true; + Platform_LogConst("Couldn't access SD card."); + } } void Platform_Free(void) { }