Merge pull request #1346 from Phil564/master

N64: Filesystem support for flashcarts
This commit is contained in:
UnknownShadow200 2025-03-18 22:04:56 +11:00 committed by GitHub
commit d7e1525ff4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 46 deletions

View File

@ -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

View File

@ -19,8 +19,14 @@
//#include <timer.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/fcntl.h>
#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) { }