diff --git a/arm9/source/fileOperations.cpp b/arm9/source/fileOperations.cpp index 428c236..7ebe039 100644 --- a/arm9/source/fileOperations.cpp +++ b/arm9/source/fileOperations.cpp @@ -8,6 +8,7 @@ #include "date.h" #include "file_browse.h" #include "font.h" +#include "ndsheaderbanner.h" #define copyBufSize 0x8000 #define shaChunkSize 0x10000 @@ -105,6 +106,25 @@ bool calculateSHA1(const char *fileName, u8 *sha1) { return true; } +int trimNds(const char *fileName) { + FILE *file = fopen(fileName, "rb"); + if(file) { + sNDSHeaderExt ndsCardHeader; + + fread(&ndsCardHeader, 1, sizeof(ndsCardHeader), file); + fclose(file); + + u32 romSize = ((ndsCardHeader.unitCode != 0) && (ndsCardHeader.twlRomSize > 0)) + ? ndsCardHeader.twlRomSize : ndsCardHeader.romSize + 0x88; + + truncate(fileName, romSize); + + return romSize; + } + + return -1; +} + void dirCopy(const DirEntry &entry, int i, const char *destinationPath, const char *sourcePath) { std::vector dirContents; dirContents.clear(); diff --git a/arm9/source/fileOperations.h b/arm9/source/fileOperations.h index c07647f..b7c5d49 100644 --- a/arm9/source/fileOperations.h +++ b/arm9/source/fileOperations.h @@ -23,6 +23,7 @@ extern std::string getBytes(int bytes); extern off_t getFileSize(const char *fileName); extern bool calculateSHA1(const char *fileName, u8 *sha1); +extern int trimNds(const char *fileName); extern int fcopy(const char *sourcePath, const char *destinationPath); void changeFileAttribs(const DirEntry *entry); diff --git a/arm9/source/file_browse.cpp b/arm9/source/file_browse.cpp index 7333d3f..7e4ed2c 100644 --- a/arm9/source/file_browse.cpp +++ b/arm9/source/file_browse.cpp @@ -191,6 +191,7 @@ FileOperation fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) { if(extension(entry->name, {"nds", "dsi", "ids", "app"})) { operations.push_back(FileOperation::mountNitroFS); operations.push_back(FileOperation::ndsInfo); + operations.push_back(FileOperation::trimNds); } else if(extension(entry->name, {"sav", "sav1", "sav2", "sav3", "sav4", "sav5", "sav6", "sav7", "sav8", "sav9"})) { operations.push_back(FileOperation::restoreSave); } else if(extension(entry->name, {"img", "sd"})) { @@ -236,6 +237,9 @@ FileOperation fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) { case FileOperation::ndsInfo: font->print(3, row++, false, "Show NDS file info"); break; + case FileOperation::trimNds: + font->print(3, row++, false, "Trim NDS file"); + break; case FileOperation::restoreSave: font->print(3, row++, false, "Restore save"); break; @@ -380,6 +384,9 @@ FileOperation fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) { } case FileOperation::ndsInfo: { ndsInfo(entry->name.c_str()); break; + } case FileOperation::trimNds: { + entry->size = trimNds(entry->name.c_str()); + break; } case FileOperation::showInfo: { changeFileAttribs(entry); break; diff --git a/arm9/source/file_browse.h b/arm9/source/file_browse.h index a7deb72..a4c83cd 100644 --- a/arm9/source/file_browse.h +++ b/arm9/source/file_browse.h @@ -38,6 +38,8 @@ enum class FileOperation { bootFile, bootstrapFile, mountNitroFS, + ndsInfo, + trimNds, mountImg, restoreSave, showInfo, @@ -45,7 +47,6 @@ enum class FileOperation { copyFatOut, calculateSHA1, hexEdit, - ndsInfo, }; bool extension(const std::string &filename, const std::vector &extensions);