From bfb4769f6fae24b449ec1494eeea754544461689 Mon Sep 17 00:00:00 2001 From: Arisotura Date: Tue, 21 Sep 2021 23:15:52 +0200 Subject: [PATCH] add files into the DLDI volume --- src/CMakeLists.txt | 4 +- src/FATStorage.cpp | 94 +++++++++++++++++++++++++++++++++++++--------- src/FATStorage.h | 1 + src/NDSCart.cpp | 6 +-- 4 files changed, 83 insertions(+), 22 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index adad3222..4e703910 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -121,7 +121,7 @@ if (ENABLE_OGLRENDERER) target_include_directories(core PRIVATE ${EPOXY_INCLUDE_DIRS}) if (WIN32) - target_link_libraries(core ole32 comctl32 ws2_32 shlwapi ${EPOXY_LIBRARIES}) + target_link_libraries(core ole32 comctl32 ws2_32 ${EPOXY_LIBRARIES}) elseif (APPLE) target_link_libraries(core ${EPOXY_LIBRARIES}) else() @@ -129,7 +129,7 @@ if (ENABLE_OGLRENDERER) endif() else() if (WIN32) - target_link_libraries(core ole32 comctl32 ws2_32 shlwapi) + target_link_libraries(core ole32 comctl32 ws2_32) else() target_link_libraries(core rt) endif() diff --git a/src/FATStorage.cpp b/src/FATStorage.cpp index c185136d..3829ff78 100644 --- a/src/FATStorage.cpp +++ b/src/FATStorage.cpp @@ -17,7 +17,7 @@ */ #ifdef __WIN32__ -#include +#include #else #include #endif // __WIN32__ @@ -28,11 +28,14 @@ #include "Platform.h" -static int GetDirEntryType(struct dirent* entry) +static int GetDirEntryType(const char* path, struct dirent* entry) { #ifdef __WIN32__ - BOOL res = PathIsDirectoryA(entry->d_name); - return res ? 1:0; + DWORD res = GetFileAttributesA(path); + if (res == INVALID_FILE_ATTRIBUTES) return -1; + if (res & FILE_ATTRIBUTE_DIRECTORY) return 1; + if (res & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DEVICE | FILE_ATTRIBUTE_VIRTUAL)) return -1; + return 0; #else if (entry->d_type == DT_DIR) return 1; if (entry->d_type == DT_REG) return 0; @@ -44,7 +47,7 @@ static int GetDirEntryType(struct dirent* entry) FATStorage::FATStorage() { printf("FATStorage begin\n"); - bool res = Build(".", 0x40000000, "melonDLDI.bin"); + bool res = Build("dldi", 0x20000000, "melonDLDI.bin"); printf("FATStorage result: %d\n", res); } @@ -107,6 +110,47 @@ UINT FATStorage::FF_WriteStorage(BYTE* buf, LBA_t sector, UINT num) } +bool FATStorage::ImportFile(const char* path, const char* in) +{ + FIL file; + FILE* fin; + FRESULT res; + + fin = fopen(in, "rb"); + if (!fin) + return false; + + fseek(fin, 0, SEEK_END); + u32 len = (u32)ftell(fin); + fseek(fin, 0, SEEK_SET); + + res = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE); + if (res != FR_OK) + { + fclose(fin); + return false; + } + + u8 buf[0x1000]; + for (u32 i = 0; i < len; i += 0x1000) + { + u32 blocklen; + if ((i + 0x1000) > len) + blocklen = len - i; + else + blocklen = 0x1000; + + u32 nwrite; + fread(buf, blocklen, 1, fin); + f_write(&file, buf, blocklen, &nwrite); + } + + fclose(fin); + f_close(&file); + + return true; +} + bool FATStorage::BuildSubdirectory(const char* sourcedir, const char* path, int level) { if (level >= 32) @@ -116,7 +160,7 @@ bool FATStorage::BuildSubdirectory(const char* sourcedir, const char* path, int } char fullpath[1024] = {0}; - snprintf(fullpath, 1023, "%s/%s", sourcedir, path); + snprintf(fullpath, 1023, "%s%s", sourcedir, path); DIR* dir = opendir(fullpath); if (!dir) return false; @@ -138,23 +182,30 @@ bool FATStorage::BuildSubdirectory(const char* sourcedir, const char* path, int if (entry->d_name[1] == '.' && entry->d_name[2] == '\0') continue; } - int entrytype = GetDirEntryType(entry); + snprintf(fullpath, 1023, "%s%s/%s", sourcedir, path, entry->d_name); + + int entrytype = GetDirEntryType(fullpath, entry); if (entrytype == -1) continue; if (entrytype == 1) // directory { - snprintf(fullpath, 1023, "%s/%s", path, entry->d_name); - - printf("DIR: %s/%s\n", sourcedir, fullpath); - - if (!BuildSubdirectory(sourcedir, fullpath, level+1)) + snprintf(fullpath, 1023, "0:%s/%s", path, entry->d_name); + FRESULT fres = f_mkdir(fullpath); + if (fres == FR_OK) + { + if (!BuildSubdirectory(sourcedir, &fullpath[2], level+1)) + res = false; + } + else res = false; } else // file { - snprintf(fullpath, 1023, "%s/%s/%s", sourcedir, path, entry->d_name); - - printf("FILE: %s\n", fullpath); + char importpath[1024] = {0}; + snprintf(importpath, 1023, "0:%s/%s", path, entry->d_name); + printf("importing %s to %s\n", fullpath, importpath); + if (!ImportFile(importpath, fullpath)) + res = false; } } @@ -167,7 +218,7 @@ bool FATStorage::Build(const char* sourcedir, u64 size, const char* filename) { filesize = size; - FF_File = Platform::OpenLocalFile(filename, "wb"); + FF_File = Platform::OpenLocalFile(filename, "w+b"); if (!FF_File) return false; @@ -176,8 +227,11 @@ bool FATStorage::Build(const char* sourcedir, u64 size, const char* filename) FRESULT res; + // TODO: determine proper FAT type! + // for example: libfat tries to determine the FAT type from the number of clusters + // which doesn't match the way fatfs handles autodetection MKFS_PARM fsopt; - fsopt.fmt = FM_FAT | FM_FAT32; + fsopt.fmt = FM_FAT;// | FM_FAT32; fsopt.au_size = 0; fsopt.align = 1; fsopt.n_fat = 1; @@ -187,8 +241,14 @@ bool FATStorage::Build(const char* sourcedir, u64 size, const char* filename) res = f_mkfs("0:", &fsopt, workbuf, sizeof(workbuf)); printf("MKFS RES %d\n", res); + FATFS fs; + res = f_mount(&fs, "0:", 0); + printf("MOUNT RES %d\n", res); + BuildSubdirectory(sourcedir, "", 0); + f_unmount("0:"); + ff_disk_close(); fclose(FF_File); FF_File = nullptr; diff --git a/src/FATStorage.h b/src/FATStorage.h index 4f2ff07b..5eabe79a 100644 --- a/src/FATStorage.h +++ b/src/FATStorage.h @@ -40,6 +40,7 @@ private: static UINT FF_ReadStorage(BYTE* buf, LBA_t sector, UINT num); static UINT FF_WriteStorage(BYTE* buf, LBA_t sector, UINT num); + bool ImportFile(const char* path, const char* in); bool BuildSubdirectory(const char* sourcedir, const char* path, int level); bool Build(const char* sourcedir, u64 size, const char* filename); }; diff --git a/src/NDSCart.cpp b/src/NDSCart.cpp index 6d7ec21d..e614bfcf 100644 --- a/src/NDSCart.cpp +++ b/src/NDSCart.cpp @@ -1168,8 +1168,8 @@ CartHomebrew::CartHomebrew(u8* rom, u32 len, u32 chipid) : CartCommon(rom, len, if (Config::DLDIEnable) { ApplyDLDIPatch(melonDLDI, sizeof(melonDLDI)); - SDFile = Platform::OpenLocalFile(Config::DLDISDPath, "r+b"); test = new FATStorage(); + SDFile = Platform::OpenLocalFile(/*Config::DLDISDPath*/"melonDLDI.bin", "r+b"); } else SDFile = nullptr; @@ -1188,7 +1188,7 @@ void CartHomebrew::Reset() if (SDFile) fclose(SDFile); if (Config::DLDIEnable) - SDFile = Platform::OpenLocalFile(Config::DLDISDPath, "r+b"); + SDFile = Platform::OpenLocalFile(/*Config::DLDISDPath*/"melonDLDI.bin", "r+b"); else SDFile = nullptr; } @@ -1224,7 +1224,7 @@ int CartHomebrew::ROMCommandStart(u8* cmd, u8* data, u32 len) { u32 sector = (cmd[1]<<24) | (cmd[2]<<16) | (cmd[3]<<8) | cmd[4]; u64 addr = sector * 0x200ULL; - +//printf("SD READ: %08X, %p\n", sector, SDFile); if (SDFile) { fseek(SDFile, addr, SEEK_SET);