Skip to content

Commit

Permalink
- Loosely emulate statFile on FlippyDrive.
Browse files Browse the repository at this point in the history
- Start using statFile more widely.
  • Loading branch information
Extrems committed Jan 20, 2025
1 parent ec8d5aa commit 9bbbb15
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cube/swiss/source/cheats/cheats.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ void loadCheatsSelection() {
concatf_path(cheatsSelFile->name, devices[DEVICE_CHEATS]->initial->name, "swiss/cheats/%.6s.chtsel", trimmedGameId);
print_gecko("Looking for previous cheat selection file [%s].\r\n", cheatsSelFile->name);
// See if we've saved cheat selections for this game before.
if(!devices[DEVICE_CHEATS]->readFile(cheatsSelFile, NULL, 0)) {
if(!devices[DEVICE_CHEATS]->statFile(cheatsSelFile)) {
XXH64_hash_t curCheatsHash = calcCheatsHash();
// We have, but was the cheats file the same as it is now?
devices[DEVICE_CHEATS]->seekFile(cheatsSelFile, -sizeof(old_hash), DEVICE_HANDLER_SEEK_END);
Expand Down
4 changes: 2 additions & 2 deletions cube/swiss/source/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ char* config_file_read(char* filename) {
file_handle *configFile = (file_handle*)calloc(1, sizeof(file_handle));
concat_path(configFile->name, devices[DEVICE_CONFIG]->initial->name, filename);
print_gecko("config_file_read: looking for %s\r\n", configFile->name);
if(devices[DEVICE_CONFIG]->readFile(configFile, NULL, 0) == 0) {
if(!devices[DEVICE_CONFIG]->statFile(configFile)) {
readBuffer = (char*)calloc(1, configFile->size + 1);
if (readBuffer) {
print_gecko("config_file_read: reading %i byte file\r\n", configFile->size);
devices[DEVICE_CONFIG]->readFile(configFile, readBuffer, configFile->size);
devices[DEVICE_CONFIG]->closeFile(configFile);
}
}
devices[DEVICE_CONFIG]->closeFile(configFile);
free(configFile);
return readBuffer;
}
Expand Down
4 changes: 2 additions & 2 deletions cube/swiss/source/devices/dvd/deviceHandler-DVD.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ s32 deviceHandler_DVD_setupFile(file_handle* file, file_handle* file2, Executabl
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
Expand All @@ -485,7 +485,7 @@ s32 deviceHandler_DVD_setupFile(file_handle* file, file_handle* file2, Executabl
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
Expand Down
4 changes: 2 additions & 2 deletions cube/swiss/source/devices/fat/deviceHandler-FAT.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ s32 deviceHandler_FAT_setupFile(file_handle* file, file_handle* file2, Executabl
ensure_path(DEVICE_CUR, "swiss/saves", NULL, false);
devices[DEVICE_CUR]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_CUR]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_CUR]->statFile(&patchFile)) {
devices[DEVICE_CUR]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_CUR]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_CUR]->closeFile(&patchFile);
Expand All @@ -278,7 +278,7 @@ s32 deviceHandler_FAT_setupFile(file_handle* file, file_handle* file2, Executabl
ensure_path(DEVICE_CUR, "swiss/saves", NULL, false);
devices[DEVICE_CUR]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_CUR]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_CUR]->statFile(&patchFile)) {
devices[DEVICE_CUR]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_CUR]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_CUR]->closeFile(&patchFile);
Expand Down
36 changes: 25 additions & 11 deletions cube/swiss/source/devices/flippydrive/deviceHandler-flippydrive.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ s32 deviceHandler_Flippy_readDir(file_handle* ffile, file_handle** dir, u32 type
return i;
}

s32 deviceHandler_Flippy_statFile(file_handle* file) {
flippyfileinfo* fp = memalign(32, sizeof(flippyfileinfo));
int ret = flippy_open(fp, getDevicePath(file->name), FLIPPY_FLAG_DEFAULT);
if(ret == FLIPPY_RESULT_OK) {
file->size = fp->file.size;
file->fileType = IS_FILE;
flippy_close(fp);
}
free(fp);
return ret;
}

s64 deviceHandler_Flippy_seekFile(file_handle* file, s64 where, u32 type) {
if(type == DEVICE_HANDLER_SEEK_SET) file->offset = where;
else if(type == DEVICE_HANDLER_SEEK_CUR) file->offset = file->offset + where;
Expand All @@ -91,7 +103,10 @@ s64 deviceHandler_Flippy_seekFile(file_handle* file, s64 where, u32 type) {
static u32 defaultFlags(file_handle* file) {
if(endsWith(file->name,".fdi") || endsWith(file->name,".gcm") || endsWith(file->name,".iso") || endsWith(file->name,".tgc"))
return FLIPPY_FLAG_DISABLE_DVDSPEED;
return FLIPPY_FLAG_DEFAULT;
else if(endsWith(file->name,".raw"))
return FLIPPY_FLAG_DEFAULT | FLIPPY_FLAG_WRITE;
else
return FLIPPY_FLAG_DEFAULT;
}

s32 deviceHandler_Flippy_readFile(file_handle* file, void* buffer, u32 length) {
Expand All @@ -112,13 +127,13 @@ s32 deviceHandler_Flippy_readFile(file_handle* file, void* buffer, u32 length) {
file->fp = NULL;
return -1;
}
flippyfileinfo* info = file->fp;
flippyfileinfo* fp = file->fp;
if(endsWith(file->name,".fdi") && (getDeviceFromPath(file->name)->quirks & QUIRK_FDI_BYTESWAP_SIZE)) {
if(info->file.size != file->size) {
info->file.size = __builtin_bswap32(info->file.size >> 9 << 8) << 9;
if(fp->file.size != file->size) {
fp->file.size = __builtin_bswap32(fp->file.size >> 9 << 8) << 9;
}
}
file->size = info->file.size;
file->size = fp->file.size;
file->fileType = IS_FILE;
}
if(file->offset > file->size) {
Expand All @@ -144,8 +159,8 @@ s32 deviceHandler_Flippy_writeFile(file_handle* file, const void* buffer, u32 le
file->fp = NULL;
return -1;
}
flippyfileinfo* info = file->fp;
file->size = info->file.size;
flippyfileinfo* fp = file->fp;
file->size = fp->file.size;
file->fileType = IS_FILE;
}
if(flippy_pwrite(file->fp, buffer, length, file->offset) != FLIPPY_RESULT_OK) {
Expand Down Expand Up @@ -217,11 +232,10 @@ s32 deviceHandler_Flippy_setupFile(file_handle* file, file_handle* file2, Execut
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0) == 0 && !patchFile.size) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
}

if(getFragments(DEVICE_PATCHES, &patchFile, &fragList, &numFrags, FRAGS_CARD_A, 0, 0))
Expand All @@ -233,11 +247,10 @@ s32 deviceHandler_Flippy_setupFile(file_handle* file, file_handle* file2, Execut
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0) == 0 && !patchFile.size) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
}

if(getFragments(DEVICE_PATCHES, &patchFile, &fragList, &numFrags, FRAGS_CARD_B, 0, 0))
Expand Down Expand Up @@ -392,6 +405,7 @@ DEVICEHANDLER_INTERFACE __device_flippy = {
.init = deviceHandler_Flippy_init,
.makeDir = deviceHandler_Flippy_makeDir,
.readDir = deviceHandler_Flippy_readDir,
.statFile = deviceHandler_Flippy_statFile,
.seekFile = deviceHandler_Flippy_seekFile,
.readFile = deviceHandler_Flippy_readFile,
.writeFile = deviceHandler_Flippy_writeFile,
Expand Down
4 changes: 2 additions & 2 deletions cube/swiss/source/devices/gcloader/deviceHandler-gcloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ s32 deviceHandler_GCLoader_setupFile(file_handle* file, file_handle* file2, Exec
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
Expand All @@ -190,7 +190,7 @@ s32 deviceHandler_GCLoader_setupFile(file_handle* file, file_handle* file2, Exec
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ s32 deviceHandler_WKF_setupFile(file_handle* file, file_handle* file2, Executabl
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
Expand All @@ -108,7 +108,7 @@ s32 deviceHandler_WKF_setupFile(file_handle* file, file_handle* file2, Executabl
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
Expand Down
4 changes: 2 additions & 2 deletions cube/swiss/source/devices/wode/deviceHandler-WODE.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ s32 deviceHandler_WODE_setupFile(file_handle* file, file_handle* file2, Executab
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
Expand All @@ -191,7 +191,7 @@ s32 deviceHandler_WODE_setupFile(file_handle* file, file_handle* file2, Executab
ensure_path(DEVICE_PATCHES, "swiss/saves", NULL, false);
devices[DEVICE_PATCHES]->renameFile(&patchFile, txtbuffer); // TODO remove this in our next major release

if(devices[DEVICE_PATCHES]->readFile(&patchFile, NULL, 0) != 0) {
if(devices[DEVICE_PATCHES]->statFile(&patchFile)) {
devices[DEVICE_PATCHES]->seekFile(&patchFile, 16*1024*1024, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_PATCHES]->writeFile(&patchFile, NULL, 0);
devices[DEVICE_PATCHES]->closeFile(&patchFile);
Expand Down
2 changes: 1 addition & 1 deletion cube/swiss/source/gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ int patch_gcm(ExecutableFile *filesToPatch, int numToPatch) {
concatf_path(fileToPatch->patchFile->name, devices[DEVICE_PATCHES]->initial->name, "swiss/patches/game/%08x.bin", (u32)fileToPatch->hash);

// See if this file already exists, if it does, match hash
if(!devices[DEVICE_PATCHES]->readFile(fileToPatch->patchFile, NULL, 0)) {
if(!devices[DEVICE_PATCHES]->statFile(fileToPatch->patchFile)) {
if(devices[DEVICE_PATCHES]->seekFile(fileToPatch->patchFile, -sizeof(old_hash), DEVICE_HANDLER_SEEK_END) == sizeToRead &&
devices[DEVICE_PATCHES]->readFile(fileToPatch->patchFile, &old_hash, sizeof(old_hash)) == sizeof(old_hash) &&
XXH128_isEqual(old_hash, new_hash)) {
Expand Down

0 comments on commit 9bbbb15

Please sign in to comment.