Implemented better error handling

This commit is contained in:
Luca D'Amico 2024-08-25 17:59:54 +02:00
parent ac5df00de3
commit 872514cce5
12 changed files with 251 additions and 224 deletions

View File

@ -22,107 +22,95 @@
#define ROOT_DIRECTORY_ADDRESS 0xF000 #define ROOT_DIRECTORY_ADDRESS 0xF000
NDSFactory::NDSFactory() NFResult NDSFactory::loadRomHeader(const std::string& romPath, std::vector<char>& romHeader)
{
}
bool NDSFactory::loadRomHeader(const std::string& romPath, std::vector<char>& romHeader)
{ {
std::streampos headerSize = sizeof(NDSHeader); std::streampos headerSize = sizeof(NDSHeader);
std::ifstream romFile (romPath, std::ios::binary); std::ifstream romFile (romPath, std::ios::binary);
if (romFile.is_open()) if (!romFile.is_open())
{ return NFResult({ false, "Error opening file: " + romPath });
romHeader.resize(static_cast<unsigned long>(headerSize));
romHeader.resize(static_cast<unsigned long>(headerSize));
romFile.read (romHeader.data(), headerSize); romFile.read (romHeader.data(), headerSize);
romFile.close(); romFile.close();
return NFResult({ true, "" });
return true;
}
return false;
} }
bool NDSFactory::dumpDataFromFile(const std::string& romPath, const std::string& savePath, uint32_t startAddr, uint32_t size) NFResult NDSFactory::dumpDataFromFile(const std::string& romPath, const std::string& savePath, uint32_t startAddr, uint32_t size)
{ {
std::ifstream romFile (romPath, std::ios::binary); std::ifstream romFile (romPath, std::ios::binary);
std::ofstream savedFile (savePath, std::ios::binary); std::ofstream savedFile (savePath, std::ios::binary);
if (romFile.is_open() && savedFile.is_open()) if (!romFile.is_open()) return NFResult({ false, "Error opening file: " + romPath });
{ if (!savedFile.is_open()) return NFResult({ false, "Error creating file: " + savePath });
std::vector<char> dumpBuffer(size);
romFile.seekg (startAddr, std::ios::beg);
romFile.read (dumpBuffer.data(), size);
romFile.close();
savedFile.write(dumpBuffer.data(), size); std::vector<char> dumpBuffer(size);
savedFile.close(); romFile.seekg (startAddr, std::ios::beg);
return true; romFile.read (dumpBuffer.data(), size);
} romFile.close();
return false;
savedFile.write(dumpBuffer.data(), size);
savedFile.close();
return NFResult({ true, "" });
} }
bool NDSFactory::logToFile(const std::string& logPath, const std::string& log) bool NDSFactory::logToFile(const std::string& logPath, const std::string& log)
{ {
std::ofstream savedFile(logPath, std::ios::out | std::ios::binary | std::ios::app); std::ofstream savedFile(logPath, std::ios::out | std::ios::binary | std::ios::app);
if (savedFile.is_open()) if (!savedFile.is_open())
{ return false;
savedFile.write(log.c_str(), log.size());
savedFile.close(); savedFile.write(log.c_str(), log.size());
return true; savedFile.close();
} return true;
return false;
} }
bool NDSFactory::readBytesFromFile(std::vector<char>& byteBuffer, const std::string& romPath, uint32_t startAddr, uint32_t size) NFResult NDSFactory::readBytesFromFile(std::vector<char>& byteBuffer, const std::string& romPath, uint32_t startAddr, uint32_t size)
{ {
std::ifstream romFile (romPath, std::ios::binary); std::ifstream romFile (romPath, std::ios::binary);
if (romFile.is_open()) if (!romFile.is_open())
{ return NFResult({ false, "Error opening file: " + romPath });
romFile.seekg (startAddr, std::ios::beg);
romFile.read (byteBuffer.data(), size); romFile.seekg (startAddr, std::ios::beg);
romFile.close(); romFile.read (byteBuffer.data(), size);
return true; romFile.close();
} return NFResult({ true, "" });
return false;
} }
bool NDSFactory::writeSectionToFile(const std::string& sectionPath, const std::string& savePath, uint32_t startAddr, uint32_t size) NFResult NDSFactory::writeSectionToFile(const std::string& sectionPath, const std::string& savePath, uint32_t startAddr, uint32_t size)
{ {
std::ifstream sectionFile (sectionPath, std::ios::binary); std::ifstream sectionFile (sectionPath, std::ios::binary);
if (sectionFile.is_open()) if (!sectionFile.is_open())
{ return NFResult({ false, "Error opening file: " + sectionPath });
std::vector<char> dumpBuffer(size);
sectionFile.read (dumpBuffer.data(), size); std::vector<char> dumpBuffer(size);
sectionFile.close(); sectionFile.read (dumpBuffer.data(), size);
return writeBytesToFile(dumpBuffer, savePath, startAddr, size); sectionFile.close();
} return writeBytesToFile(dumpBuffer, savePath, startAddr, size);
return false;
} }
bool NDSFactory::writeBytesToFile(std::vector<char>& byteBuffer, const std::string& savePath, uint32_t startAddr, uint32_t size) NFResult NDSFactory::writeBytesToFile(std::vector<char>& byteBuffer, const std::string& savePath, uint32_t startAddr, uint32_t size)
{ {
std::ofstream savedFile (savePath, std::ios::in | std::ios::out | std::ios::binary); std::ofstream savedFile (savePath, std::ios::in | std::ios::out | std::ios::binary);
if (!savedFile.is_open()) if (!savedFile.is_open())
{ {
savedFile.open(savePath, std::ios::out | std::ios::binary); savedFile.open(savePath, std::ios::out | std::ios::binary);
if (!savedFile.is_open()) { if (!savedFile.is_open()) {
return false; return NFResult({ false, "Error creating file: " + savePath });
} }
savedFile.close(); savedFile.close();
savedFile.open(savePath, std::ios::in | std::ios::out | std::ios::binary); savedFile.open(savePath, std::ios::in | std::ios::out | std::ios::binary);
if (!savedFile.is_open()) if (!savedFile.is_open())
return false; return NFResult({ false, "Error opening file: " + savePath });
} }
savedFile.seekp(startAddr); savedFile.seekp(startAddr);
savedFile.write(byteBuffer.data(), size); savedFile.write(byteBuffer.data(), size);
savedFile.close(); savedFile.close();
return true; return NFResult({ true, "" });
} }
bool NDSFactory::writePaddingToFile(char paddingChar, const std::string& filePath, uint32_t startAddr, const uint32_t size) NFResult NDSFactory::writePaddingToFile(char paddingChar, const std::string& filePath, uint32_t startAddr, const uint32_t size)
{ {
std::vector<char> paddingBytes(size, paddingChar); std::vector<char> paddingBytes(size, paddingChar);
return writeBytesToFile(paddingBytes, filePath, startAddr, size); return writeBytesToFile(paddingBytes, filePath, startAddr, size);
@ -148,7 +136,7 @@ bool NDSFactory::checkArm9FooterPresence(const std::string& sectionPath, uint32_
return false; return false;
} }
bool NDSFactory::extractFatData(const std::string& fatDataSectionPath, const std::string& fatSectionPath, NFResult NDSFactory::extractFatData(const std::string& fatDataSectionPath, const std::string& fatSectionPath,
const std::string& fntSectionPath, uint32_t originalFatDataAddr, const std::string& savePath, bool logFileIDs) const std::string& fntSectionPath, uint32_t originalFatDataAddr, const std::string& savePath, bool logFileIDs)
{ {
std::vector<char> fatDataBytes; std::vector<char> fatDataBytes;
@ -156,7 +144,7 @@ bool NDSFactory::extractFatData(const std::string& fatDataSectionPath, const std
std::vector<char> fntBytes; std::vector<char> fntBytes;
std::ifstream fatDataSectionFile(fatDataSectionPath, std::ios::in | std::ios::binary | std::ios::ate); std::ifstream fatDataSectionFile(fatDataSectionPath, std::ios::in | std::ios::binary | std::ios::ate);
if (!fatDataSectionFile.is_open()) return false; if (!fatDataSectionFile.is_open()) return NFResult({ false, "Error opening file: " + fatDataSectionPath });
std::streamoff fatDataSectionSize = fatDataSectionFile.tellg(); std::streamoff fatDataSectionSize = fatDataSectionFile.tellg();
fatDataBytes.resize(fatDataSectionSize); fatDataBytes.resize(fatDataSectionSize);
fatDataSectionFile.seekg(0, std::ios::beg); fatDataSectionFile.seekg(0, std::ios::beg);
@ -164,7 +152,7 @@ bool NDSFactory::extractFatData(const std::string& fatDataSectionPath, const std
fatDataSectionFile.close(); fatDataSectionFile.close();
std::ifstream fatSectionFile(fatSectionPath, std::ios::in | std::ios::binary | std::ios::ate); std::ifstream fatSectionFile(fatSectionPath, std::ios::in | std::ios::binary | std::ios::ate);
if (!fatSectionFile.is_open()) return false; if (!fatSectionFile.is_open()) return NFResult({ false, "Error opening file: " + fatSectionPath });
std::streamoff fatSectionSize = fatSectionFile.tellg(); std::streamoff fatSectionSize = fatSectionFile.tellg();
fatBytes.resize(fatSectionSize); fatBytes.resize(fatSectionSize);
fatSectionFile.seekg(0, std::ios::beg); fatSectionFile.seekg(0, std::ios::beg);
@ -172,7 +160,7 @@ bool NDSFactory::extractFatData(const std::string& fatDataSectionPath, const std
fatSectionFile.close(); fatSectionFile.close();
std::ifstream fntSectionFile(fntSectionPath, std::ios::in | std::ios::binary | std::ios::ate); std::ifstream fntSectionFile(fntSectionPath, std::ios::in | std::ios::binary | std::ios::ate);
if (!fntSectionFile.is_open()) return false; if (!fntSectionFile.is_open()) return NFResult({ false, "Error opening file: " + fntSectionPath });
std::streamoff fntSectionSize = fntSectionFile.tellg(); std::streamoff fntSectionSize = fntSectionFile.tellg();
fntBytes.resize(fntSectionSize); fntBytes.resize(fntSectionSize);
fntSectionFile.seekg(0, std::ios::beg); fntSectionFile.seekg(0, std::ios::beg);
@ -248,16 +236,16 @@ bool NDSFactory::extractFatData(const std::string& fatDataSectionPath, const std
currentOffset += 2; currentOffset += 2;
if (!std::filesystem::exists(newPath)) if (!std::filesystem::exists(newPath))
if (!std::filesystem::create_directory(newPath)) return false; if (!std::filesystem::create_directory(newPath)) return NFResult({ false, "Error creating directory: " + newPath });
if (logFileIDs) if (logFileIDs)
{ {
std::string log = std::format("{:x}",subFolderId) + ":::" + newPath.substr(savePath.size()+1) + '\n'; std::string log = std::format("{:x}",subFolderId) + ":::" + newPath.substr(savePath.size()+1) + '\n';
if (!logToFile(savePath + "/_file_IDs.txt", log)) return false; if (!logToFile(savePath + "/_file_IDs.txt", log)) return NFResult({ false, "Error writing to file: " + savePath + "/_file_IDs.txt" });
} }
// Jump back to the FNT header and repeat the process for subdirectory ! // Jump back to the FNT header and repeat the process for subdirectory !
if (!parseFolder(subFolderId, newPath, parseFolder)) return false; if (!parseFolder(subFolderId, newPath, parseFolder).result) return NFResult({ false, "Error parsing folder: " + newPath });
} }
else else
{ {
@ -267,31 +255,31 @@ bool NDSFactory::extractFatData(const std::string& fatDataSectionPath, const std
uint32_t fileStartAddr = (pfatrange + fatOffset)->startAddr - originalFatDataAddr; uint32_t fileStartAddr = (pfatrange + fatOffset)->startAddr - originalFatDataAddr;
uint32_t fileSize = (pfatrange + fatOffset)->endAddr - (pfatrange + fatOffset)->startAddr; uint32_t fileSize = (pfatrange + fatOffset)->endAddr - (pfatrange + fatOffset)->startAddr;
if (!dumpDataFromFile(fatDataSectionPath, newPath, fileStartAddr, fileSize)) return false; if (!dumpDataFromFile(fatDataSectionPath, newPath, fileStartAddr, fileSize).result) return NFResult({ false, "Error dumping file: " + newPath });
if (logFileIDs) if (logFileIDs)
{ {
std::string log = std::format("{:x}", fatOffset) + ":::" + newPath.substr(savePath.size()+1) + '\n'; std::string log = std::format("{:x}", fatOffset) + ":::" + newPath.substr(savePath.size()+1) + '\n';
if (!logToFile(savePath + "/_file_IDs.txt", log)) return false; if (!logToFile(savePath + "/_file_IDs.txt", log)) return NFResult({ false, "Error writing to file: " + savePath + "/_file_IDs.txt" });
} }
fatOffset++; fatOffset++;
} }
} }
return true; return NFResult({ true, "" });
}; };
return parseFolder(ROOT_DIRECTORY_ADDRESS, savePath, parseFolder); return parseFolder(ROOT_DIRECTORY_ADDRESS, savePath, parseFolder);
} }
bool NDSFactory::patchFat(const std::string& fatSectionPath, uint32_t shiftSize, const std::string& savePath) NFResult NDSFactory::patchFat(const std::string& fatSectionPath, uint32_t shiftSize, const std::string& savePath)
{ {
std::vector<char> fatBytes; std::vector<char> fatBytes;
std::ifstream sectionFile (fatSectionPath, std::ios::in|std::ios::binary|std::ios::ate); std::ifstream sectionFile (fatSectionPath, std::ios::in|std::ios::binary|std::ios::ate);
if (!sectionFile.is_open()) if (!sectionFile.is_open())
return false; return { false, "Error opening file: " + fatSectionPath };
std::streamoff sectionSize = sectionFile.tellg(); std::streamoff sectionSize = sectionFile.tellg();
fatBytes.resize(sectionSize); fatBytes.resize(sectionSize);
@ -313,7 +301,7 @@ bool NDSFactory::patchFat(const std::string& fatSectionPath, uint32_t shiftSize,
return writeBytesToFile(fatBytes, savePath, 0, static_cast<uint32_t>(sectionSize)); return writeBytesToFile(fatBytes, savePath, 0, static_cast<uint32_t>(sectionSize));
} }
bool NDSFactory::buildFatData(const std::string& fatDataDirPath, const std::string& originalFatPath, uint32_t fatDataAddr, const std::string& savePath) NFResult NDSFactory::buildFatData(const std::string& fatDataDirPath, const std::string& originalFatPath, uint32_t fatDataAddr, const std::string& savePath)
{ {
std::vector<char> fatDataBytes; std::vector<char> fatDataBytes;
std::vector<FatRange> fat; std::vector<FatRange> fat;
@ -321,7 +309,7 @@ bool NDSFactory::buildFatData(const std::string& fatDataDirPath, const std::stri
std::vector<FatFileID> fileIDs; std::vector<FatFileID> fileIDs;
std::ifstream fileIDsFile(fatDataDirPath + "/_file_IDs.txt", std::ios::in | std::ios::beg); std::ifstream fileIDsFile(fatDataDirPath + "/_file_IDs.txt", std::ios::in | std::ios::beg);
if (!fileIDsFile.is_open()) return false; if (!fileIDsFile.is_open()) return { false, "Error opening file: " + fatDataDirPath + "/_file_IDs.txt" };
std::string fileIDsLine; std::string fileIDsLine;
while (std::getline(fileIDsFile, fileIDsLine)) while (std::getline(fileIDsFile, fileIDsLine))
{ {
@ -344,7 +332,7 @@ bool NDSFactory::buildFatData(const std::string& fatDataDirPath, const std::stri
if (firstFileId > 0) if (firstFileId > 0)
{ {
std::ifstream originalFatFile(originalFatPath, std::ios::in | std::ios::binary | std::ios::beg); std::ifstream originalFatFile(originalFatPath, std::ios::in | std::ios::binary | std::ios::beg);
if (!originalFatFile.is_open()) return false; if (!originalFatFile.is_open()) return { false, "Error opening file: " + originalFatPath };
std::vector<char> ovrBytes; std::vector<char> ovrBytes;
uint32_t requiredBytes = firstFileId * sizeof(FatRange); uint32_t requiredBytes = firstFileId * sizeof(FatRange);
ovrBytes.resize(requiredBytes); ovrBytes.resize(requiredBytes);
@ -368,7 +356,7 @@ bool NDSFactory::buildFatData(const std::string& fatDataDirPath, const std::stri
fatRange.startAddr = fatDataAddr + static_cast<uint32_t>(fatDataBytes.size()); fatRange.startAddr = fatDataAddr + static_cast<uint32_t>(fatDataBytes.size());
std::ifstream currentFatDataFile(currentFile, std::ios::in | std::ios::binary | std::ios::ate); std::ifstream currentFatDataFile(currentFile, std::ios::in | std::ios::binary | std::ios::ate);
if (!currentFatDataFile.is_open()) return false; if (!currentFatDataFile.is_open()) return NFResult({ false, "Error opening file: " + currentFile });
std::streamsize size = currentFatDataFile.tellg(); std::streamsize size = currentFatDataFile.tellg();
currentFatDataFile.seekg(0, std::ios::beg); currentFatDataFile.seekg(0, std::ios::beg);
@ -377,25 +365,26 @@ bool NDSFactory::buildFatData(const std::string& fatDataDirPath, const std::stri
fatDataBytes.insert(fatDataBytes.end(), buffer.begin(), buffer.end()); fatDataBytes.insert(fatDataBytes.end(), buffer.begin(), buffer.end());
else else
{ {
currentFatDataFile.close(); currentFatDataFile.close();
return false; return NFResult({ false, "Error reading file: " + currentFile });
} }
currentFatDataFile.close(); currentFatDataFile.close();
fatRange.endAddr = fatDataAddr + static_cast<uint32_t>(fatDataBytes.size()); fatRange.endAddr = fatDataAddr + static_cast<uint32_t>(fatDataBytes.size());
fat.push_back(fatRange); fat.push_back(fatRange);
} }
const char* fat_ptr = reinterpret_cast<const char*>(fat.data()); const char* fat_ptr = reinterpret_cast<const char*>(fat.data());
std::vector<char> fatBytes(fat_ptr, fat_ptr + fat.size() * sizeof(FatRange)); std::vector<char> fatBytes(fat_ptr, fat_ptr + fat.size() * sizeof(FatRange));
std::remove((savePath + "/fat_data.bin").c_str()); std::remove((savePath + "/fat_data.bin").c_str());
std::remove((savePath + "/fat.bin").c_str()); std::remove((savePath + "/fat.bin").c_str());
bool res = true; if (!writeBytesToFile(fatDataBytes, savePath + "/fat_data.bin", 0, static_cast<uint32_t>(fatDataBytes.size())).result)
res &= writeBytesToFile(fatDataBytes, savePath + "/fat_data.bin", 0, static_cast<uint32_t>(fatDataBytes.size())); return NFResult({ false, "Error writing to file: " + savePath + "/fat_data.bin" });
res &= writeBytesToFile(fatBytes, savePath + "/fat.bin", 0, static_cast<uint32_t>(fatBytes.size())); if (!writeBytesToFile(fatBytes, savePath + "/fat.bin", 0, static_cast<uint32_t>(fatBytes.size())).result)
return res; return NFResult({ false, "Error writing to file: " + savePath + "/fat.bin" });
return NFResult({ true, "" });
} }
uint16_t NDSFactory::calcHeaderCrc16(const std::vector<char>& romHeader) uint16_t NDSFactory::calcHeaderCrc16(const std::vector<char>& romHeader)

View File

@ -5,29 +5,28 @@
#include <cstdint> #include <cstdint>
#include "ndsheader.h" #include "ndsheader.h"
#include "fatstruct.h" #include "fatstruct.h"
#include "nfresult.h"
class NDSFactory class NDSFactory
{ {
public: public:
NDSFactory(); NFResult loadRomHeader(const std::string& romPath, std::vector<char>& header);
bool loadRomHeader(const std::string& romPath, std::vector<char>& header); NFResult dumpDataFromFile(const std::string& romPath, const std::string& savePath, uint32_t startAddr, uint32_t size);
bool dumpDataFromFile(const std::string& romPath, const std::string& savePath, uint32_t startAddr, uint32_t size); NFResult readBytesFromFile(std::vector<char>& byteBuffer, const std::string& romPath, uint32_t startAddr, uint32_t size);
bool logToFile(const std::string& logPath, const std::string& log); NFResult writeSectionToFile(const std::string& sectionPath, const std::string& savePath, uint32_t startAddr, uint32_t size);
bool readBytesFromFile(std::vector<char>& byteBuffer, const std::string& romPath, uint32_t startAddr, uint32_t size); NFResult writeBytesToFile(std::vector<char>& byteBuffer, const std::string& savePath, uint32_t startAddr, uint32_t size);
bool writeSectionToFile(const std::string& sectionPath, const std::string& savePath, uint32_t startAddr, uint32_t size); NFResult writePaddingToFile(char paddingChar, const std::string& savePath, uint32_t startAddr, uint32_t size);
bool writeBytesToFile(std::vector<char>& byteBuffer, const std::string& savePath, uint32_t startAddr, uint32_t size);
bool writePaddingToFile(char paddingChar, const std::string& savePath, uint32_t startAddr, uint32_t size);
int getCardSizeInBytes(int cardType); int getCardSizeInBytes(int cardType);
bool checkArm9FooterPresence(const std::string& sectionPath, uint32_t size); bool checkArm9FooterPresence(const std::string& sectionPath, uint32_t size);
bool extractFatData(const std::string& fatDataSectionPath, const std::string& fatSectionPath, NFResult extractFatData(const std::string& fatDataSectionPath, const std::string& fatSectionPath,
const std::string& fntSectionPath, uint32_t originalFatDataAddr, const std::string& savePath, bool logFileIDs); const std::string& fntSectionPath, uint32_t originalFatDataAddr, const std::string& savePath, bool logFileIDs);
bool patchFat(const std::string& sectionPath, uint32_t shiftSize, const std::string& savePath); NFResult patchFat(const std::string& sectionPath, uint32_t shiftSize, const std::string& savePath);
bool buildFatData(const std::string& fatDataDirPath, const std::string& originalFatPath, uint32_t fatDataAddr, const std::string& savePath); NFResult buildFatData(const std::string& fatDataDirPath, const std::string& originalFatPath, uint32_t fatDataAddr, const std::string& savePath);
uint16_t calcHeaderCrc16(const std::vector<char>& romHeader); uint16_t calcHeaderCrc16(const std::vector<char>& romHeader);
private: private:
bool logToFile(const std::string& logPath, const std::string& log);
}; };

9
ndsfactory/nfresult.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <string>
struct NFResult
{
bool result;
std::string message;
};

View File

@ -1,6 +1,6 @@
#ifndef REVISION_H #ifndef REVISION_H
#define REVISION_H #define REVISION_H
#define GIT_COMMIT_HASH "f46e527" #define GIT_COMMIT_HASH "ac5df00"
#endif #endif

View File

@ -69,48 +69,48 @@ private:
void populateHeader(NDSHeader* ndsHeader); void populateHeader(NDSHeader* ndsHeader);
void enableExtractionButtons(); void enableExtractionButtons();
void disableExtractionButtons(); void disableExtractionButtons();
bool dumpHeader(const std::string& dirPath); NFResult dumpHeader(const std::string& dirPath);
bool dumpArm9Bin(const std::string& dirPath, bool dumpExtraBytes); NFResult dumpArm9Bin(const std::string& dirPath, bool dumpExtraBytes);
bool dumpArm7Bin(const std::string& dirPath); NFResult dumpArm7Bin(const std::string& dirPath);
bool dumpFnt(const std::string& dirPath); NFResult dumpFnt(const std::string& dirPath);
bool dumpFat(const std::string& dirPath); NFResult dumpFat(const std::string& dirPath);
bool dumpArm9Overlay(const std::string& dirPath); NFResult dumpArm9Overlay(const std::string& dirPath);
bool dumpArm9OverlayFiles(const std::string& dirPath); NFResult dumpArm9OverlayFiles(const std::string& dirPath);
bool dumpArm7Overlay(const std::string& dirPath); NFResult dumpArm7Overlay(const std::string& dirPath);
bool dumpArm7OverlayFiles(const std::string& dirPath); NFResult dumpArm7OverlayFiles(const std::string& dirPath);
bool dumpIconTitle(const std::string& dirPath); NFResult dumpIconTitle(const std::string& dirPath);
bool dumpFatFiles(const std::string& dirPath); NFResult dumpFatFiles(const std::string& dirPath);
bool dumpEverything(QString dirPath); NFResult dumpEverything(QString dirPath);
void populatePackerSectionHeader(NDSHeader *ndsHeader); void populatePackerSectionHeader(NDSHeader *ndsHeader);
void enableCalcCrcButton(); void enableCalcCrcButton();
void enableBuildRomButton(); void enableBuildRomButton();
void generateHeader(NDSHeader* pRomHeader); void generateHeader(NDSHeader* pRomHeader);
bool writeHeader(const std::string& savePath); NFResult writeHeader(const std::string& savePath);
void calcHeaderCrc16(); void calcHeaderCrc16();
bool writeArm9Bin(const std::string& savePath, bool isArm9FooterPresent); NFResult writeArm9Bin(const std::string& savePath, bool isArm9FooterPresent);
bool writeArm7Bin(const std::string& savePath); NFResult writeArm7Bin(const std::string& savePath);
bool writeFnt(const std::string& savePath); NFResult writeFnt(const std::string& savePath);
bool writeFat(const std::string& savePath); NFResult writeFat(const std::string& savePath);
bool writeArm9Overlay(const std::string& savePath); NFResult writeArm9Overlay(const std::string& savePath);
bool writeArm9OverlayFiles(const std::string& savePath); NFResult writeArm9OverlayFiles(const std::string& savePath);
bool writeArm7Overlay(const std::string& savePath); NFResult writeArm7Overlay(const std::string& savePath);
bool writeArm7OverlayFiles(const std::string& savePath); NFResult writeArm7OverlayFiles(const std::string& savePath);
bool writeIconTitle(const std::string& savePath); NFResult writeIconTitle(const std::string& savePath);
bool writeFatFiles(const std::string& savePath); NFResult writeFatFiles(const std::string& savePath);
bool writeEverything(const std::string& savePath); NFResult writeEverything(const std::string& savePath);
bool writeHeaderPadding(char paddingByte, const std::string& savePath); NFResult writeHeaderPadding(char paddingByte, const std::string& savePath);
bool writeArm9BinPadding(char paddingByte, const std::string& savePath, bool isArm9FooterPresent); NFResult writeArm9BinPadding(char paddingByte, const std::string& savePath, bool isArm9FooterPresent);
bool writeArm7BinPadding(char paddingByte, const std::string& savePath); NFResult writeArm7BinPadding(char paddingByte, const std::string& savePath);
bool writeFntPadding(char paddingByte, const std::string& savePath); NFResult writeFntPadding(char paddingByte, const std::string& savePath);
bool writeFatPadding(char paddingByte, const std::string& savePath); NFResult writeFatPadding(char paddingByte, const std::string& savePath);
bool writeRomPadding(const std::string& savePath); NFResult writeRomPadding(const std::string& savePath);
QString extractPackerHeaderTableData(int index); QString extractPackerHeaderTableData(int index);
bool extractFatData(const std::string& fatDataSectionPath, const std::string& fatSectionPath, NFResult extractFatData(const std::string& fatDataSectionPath, const std::string& fatSectionPath,
const std::string& fntSectionPath, uint32_t originalFatDataAddr, const std::string& savePath, bool logFileIDs); const std::string& fntSectionPath, uint32_t originalFatDataAddr, const std::string& savePath, bool logFileIDs);
bool patchFat(const std::string& loadPath, uint32_t shiftSize, const std::string& savePath); NFResult patchFat(const std::string& loadPath, uint32_t shiftSize, const std::string& savePath);
bool buildFatData(const std::string& fatDataDirPath, const std::string& originalFatPath, uint32_t fatDataAddr, const std::string& savePath); NFResult buildFatData(const std::string& fatDataDirPath, const std::string& originalFatPath, uint32_t fatDataAddr, const std::string& savePath);
}; };

View File

@ -2,19 +2,18 @@
#include "./../../mainwindow.h" #include "./../../mainwindow.h"
bool MainWindow::extractFatData(const std::string& fatDataSectionPath, const std::string& fatSectionPath, NFResult MainWindow::extractFatData(const std::string& fatDataSectionPath, const std::string& fatSectionPath,
const std::string& fntSectionPath, uint32_t originalFatDataAddr, const std::string& savePath, bool logFileIDs) const std::string& fntSectionPath, uint32_t originalFatDataAddr, const std::string& savePath, bool logFileIDs)
{ {
return ndsFactory.extractFatData(fatDataSectionPath, fatSectionPath, fntSectionPath, originalFatDataAddr, savePath, logFileIDs); return ndsFactory.extractFatData(fatDataSectionPath, fatSectionPath, fntSectionPath, originalFatDataAddr, savePath, logFileIDs);
} }
bool MainWindow::patchFat(const std::string& loadPath, uint32_t shiftSize, const std::string& savePath) NFResult MainWindow::patchFat(const std::string& loadPath, uint32_t shiftSize, const std::string& savePath)
{ {
return ndsFactory.patchFat(loadPath, shiftSize, savePath); return ndsFactory.patchFat(loadPath, shiftSize, savePath);
} }
bool MainWindow::buildFatData(const std::string& fatDataDirPath, const std::string& originalFatPath, uint32_t fatDataAddr, const std::string& savePath) NFResult MainWindow::buildFatData(const std::string& fatDataDirPath, const std::string& originalFatPath, uint32_t fatDataAddr, const std::string& savePath)
{ {
if (!std::filesystem::exists(fatDataDirPath + "/_file_IDs.txt")) return false;
return ndsFactory.buildFatData(fatDataDirPath, originalFatPath, fatDataAddr, savePath); return ndsFactory.buildFatData(fatDataDirPath, originalFatPath, fatDataAddr, savePath);
} }

View File

@ -55,14 +55,16 @@ void MainWindow::on_fatExtractorExtractBtn_clicked()
"", "",
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!dirPath.isNull()) if (!dirPath.isNull()) {
extractFatData(ui->fatExtractorFatDataPathEdt->text().toStdString(), NFResult nfResult = extractFatData(ui->fatExtractorFatDataPathEdt->text().toStdString(),
ui->fatExtractorFatPathEdt->text().toStdString(), ui->fatExtractorFatPathEdt->text().toStdString(),
ui->fatExtractorFntPathEdt->text().toStdString(), ui->fatExtractorFntPathEdt->text().toStdString(),
ui->fatExtractorOriginalFatFilesAddrEdt->text().toUInt(nullptr, 16), ui->fatExtractorOriginalFatFilesAddrEdt->text().toUInt(nullptr, 16),
dirPath.toStdString(), dirPath.toStdString(),
ui->fatExtractorSaveFileIDsCbx->isChecked()) ? QMessageBox::information(this, tr("NDSFactory"), tr("FAT files extraction completed!")) ui->fatExtractorSaveFileIDsCbx->isChecked());
: QMessageBox::critical(this, tr("NDSFactory"), tr("Error extracting FAT files!")); nfResult.result? QMessageBox::information(this, tr("NDSFactory"), tr("FAT files extraction completed!"))
: QMessageBox::critical(this, tr("NDSFactory"), nfResult.message.c_str());
}
ui->fatExtractorExtractBtn->setEnabled(true); ui->fatExtractorExtractBtn->setEnabled(true);
@ -99,9 +101,9 @@ void MainWindow::on_fatPatcherPatchFatBtn_clicked()
else else
positionDiff = originalPos - newPos; positionDiff = originalPos - newPos;
patchFat(ui->fatPatcherFatPathEdt->text().toStdString(), positionDiff, dirPath.toStdString()) NFResult nfResult = patchFat(ui->fatPatcherFatPathEdt->text().toStdString(), positionDiff, dirPath.toStdString());
? QMessageBox::information(this, tr("NDSFactory"), tr("FAT patching completed!")) nfResult.result ? QMessageBox::information(this, tr("NDSFactory"), tr("FAT patching completed!"))
: QMessageBox::critical(this, tr("NDSFactory"), tr("Error patching FAT!")); : QMessageBox::critical(this, tr("NDSFactory"), nfResult.message.c_str());
} }
ui->fatPatcherPatchFatBtn->setEnabled(true); ui->fatPatcherPatchFatBtn->setEnabled(true);
@ -140,11 +142,14 @@ void MainWindow::on_fatBuilderBuildBtn_clicked()
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!dirPath.isNull()) if (!dirPath.isNull())
buildFatData(ui->fatBuilderFatDirPathEdt->text().toStdString(), {
ui->fatBuilderOriginalFatPathEdt->text().toStdString(), NFResult nfResult = buildFatData(ui->fatBuilderFatDirPathEdt->text().toStdString(),
ui->fatBuilderOriginalFatPathEdt->text().toStdString(),
ui->fatBuilderFatAddrEdt->text().toUInt(nullptr, 16), ui->fatBuilderFatAddrEdt->text().toUInt(nullptr, 16),
dirPath.toStdString()) ? QMessageBox::information(this, tr("NDSFactory"), tr("fat_data.bin and fat.bin correctly built!")) dirPath.toStdString());
: QMessageBox::critical(this, tr("NDSFactory"), tr("Error building FAT!")); nfResult.result? QMessageBox::information(this, tr("NDSFactory"), tr("fat_data.bin and fat.bin correctly built!"))
: QMessageBox::critical(this, tr("NDSFactory"), nfResult.message.c_str());
}
ui->fatBuilderOpenFatDataDirBtn->setEnabled(true); ui->fatBuilderOpenFatDataDirBtn->setEnabled(true);
} }

View File

@ -102,17 +102,17 @@ void MainWindow::calcHeaderCrc16()
ui->packerHeaderDataTable->model()->setData(headerCrcIndex, QString::number(ndsFactory.calcHeaderCrc16(romHeaderBuffer), 16), Qt::EditRole); ui->packerHeaderDataTable->model()->setData(headerCrcIndex, QString::number(ndsFactory.calcHeaderCrc16(romHeaderBuffer), 16), Qt::EditRole);
} }
bool MainWindow::writeHeader(const std::string& savePath) NFResult MainWindow::writeHeader(const std::string& savePath)
{ {
std::vector<char> romHeaderBuffer(sizeof(NDSHeader)); std::vector<char> romHeaderBuffer(sizeof(NDSHeader));
NDSHeader* pRomHeader = reinterpret_cast<NDSHeader*>(romHeaderBuffer.data()); NDSHeader* pRomHeader = reinterpret_cast<NDSHeader*>(romHeaderBuffer.data());
generateHeader(pRomHeader); generateHeader(pRomHeader);
return ndsFactory.writeBytesToFile(romHeaderBuffer, savePath, 0, sizeof(NDSHeader));; return ndsFactory.writeBytesToFile(romHeaderBuffer, savePath, 0, sizeof(NDSHeader));
} }
bool MainWindow::writeHeaderPadding(char paddingType, const std::string& savePath) NFResult MainWindow::writeHeaderPadding(char paddingType, const std::string& savePath)
{ {
uint32_t startAddr = sizeof(NDSHeader); uint32_t startAddr = sizeof(NDSHeader);
uint32_t size = extractPackerHeaderTableData(NDSHeaderNames::ARM9RomAddress).toUInt(nullptr, 16) - startAddr; uint32_t size = extractPackerHeaderTableData(NDSHeaderNames::ARM9RomAddress).toUInt(nullptr, 16) - startAddr;
@ -124,7 +124,7 @@ bool MainWindow::writeHeaderPadding(char paddingType, const std::string& savePat
size); size);
} }
bool MainWindow::writeArm9Bin(const std::string& savePath, bool isArm9FooterPresent) NFResult MainWindow::writeArm9Bin(const std::string& savePath, bool isArm9FooterPresent)
{ {
uint32_t size = extractPackerHeaderTableData(NDSHeaderNames::ARM9Size).toUInt(nullptr, 16); uint32_t size = extractPackerHeaderTableData(NDSHeaderNames::ARM9Size).toUInt(nullptr, 16);
if (isArm9FooterPresent) if (isArm9FooterPresent)
@ -137,7 +137,7 @@ bool MainWindow::writeArm9Bin(const std::string& savePath, bool isArm9FooterPres
} }
bool MainWindow::writeArm9BinPadding(char paddingType, const std::string& savePath, bool isFooterPresent) NFResult MainWindow::writeArm9BinPadding(char paddingType, const std::string& savePath, bool isFooterPresent)
{ {
uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::ARM9RomAddress).toUInt(nullptr, 16) + uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::ARM9RomAddress).toUInt(nullptr, 16) +
extractPackerHeaderTableData(NDSHeaderNames::ARM9Size).toUInt(nullptr, 16); extractPackerHeaderTableData(NDSHeaderNames::ARM9Size).toUInt(nullptr, 16);
@ -160,7 +160,7 @@ bool MainWindow::writeArm9BinPadding(char paddingType, const std::string& savePa
size); size);
} }
bool MainWindow::writeArm9Overlay(const std::string& savePath) NFResult MainWindow::writeArm9Overlay(const std::string& savePath)
{ {
return ndsFactory.writeSectionToFile( return ndsFactory.writeSectionToFile(
ui->loadedArm9OverlayPathEdt->text().toStdString(), ui->loadedArm9OverlayPathEdt->text().toStdString(),
@ -169,7 +169,7 @@ bool MainWindow::writeArm9Overlay(const std::string& savePath)
extractPackerHeaderTableData(NDSHeaderNames::ARM9OverlaySize).toUInt(nullptr, 16)); extractPackerHeaderTableData(NDSHeaderNames::ARM9OverlaySize).toUInt(nullptr, 16));
} }
bool MainWindow::writeArm9OverlayFiles(const std::string& savePath) NFResult MainWindow::writeArm9OverlayFiles(const std::string& savePath)
{ {
uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::ARM9OverlayAddress).toUInt(nullptr, 16) + uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::ARM9OverlayAddress).toUInt(nullptr, 16) +
extractPackerHeaderTableData(NDSHeaderNames::ARM9OverlaySize).toUInt(nullptr, 16); extractPackerHeaderTableData(NDSHeaderNames::ARM9OverlaySize).toUInt(nullptr, 16);
@ -181,7 +181,7 @@ bool MainWindow::writeArm9OverlayFiles(const std::string& savePath)
size); size);
} }
bool MainWindow::writeArm7Bin(const std::string& savePath) NFResult MainWindow::writeArm7Bin(const std::string& savePath)
{ {
return ndsFactory.writeSectionToFile( return ndsFactory.writeSectionToFile(
ui->loadedArm7BinPathEdt->text().toStdString(), ui->loadedArm7BinPathEdt->text().toStdString(),
@ -190,7 +190,7 @@ bool MainWindow::writeArm7Bin(const std::string& savePath)
extractPackerHeaderTableData(NDSHeaderNames::ARM7Size).toUInt(nullptr, 16)); extractPackerHeaderTableData(NDSHeaderNames::ARM7Size).toUInt(nullptr, 16));
} }
bool MainWindow::writeArm7BinPadding(char paddingType, const std::string& savePath) NFResult MainWindow::writeArm7BinPadding(char paddingType, const std::string& savePath)
{ {
uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::ARM7RomAddress).toUInt(nullptr, 16) + uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::ARM7RomAddress).toUInt(nullptr, 16) +
extractPackerHeaderTableData(NDSHeaderNames::ARM7Size).toUInt(nullptr, 16); extractPackerHeaderTableData(NDSHeaderNames::ARM7Size).toUInt(nullptr, 16);
@ -207,7 +207,7 @@ bool MainWindow::writeArm7BinPadding(char paddingType, const std::string& savePa
size); size);
} }
bool MainWindow::writeArm7Overlay(const std::string& savePath) NFResult MainWindow::writeArm7Overlay(const std::string& savePath)
{ {
return ndsFactory.writeSectionToFile( return ndsFactory.writeSectionToFile(
ui->loadedArm7OverlayPathEdt->text().toStdString(), ui->loadedArm7OverlayPathEdt->text().toStdString(),
@ -216,7 +216,7 @@ bool MainWindow::writeArm7Overlay(const std::string& savePath)
extractPackerHeaderTableData(NDSHeaderNames::ARM7OverlaySize).toUInt(nullptr, 16)); extractPackerHeaderTableData(NDSHeaderNames::ARM7OverlaySize).toUInt(nullptr, 16));
} }
bool MainWindow::writeArm7OverlayFiles(const std::string& savePath) NFResult MainWindow::writeArm7OverlayFiles(const std::string& savePath)
{ {
uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::ARM7OverlayAddress).toUInt(nullptr, 16) + uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::ARM7OverlayAddress).toUInt(nullptr, 16) +
extractPackerHeaderTableData(NDSHeaderNames::ARM7OverlaySize).toUInt(nullptr, 16); extractPackerHeaderTableData(NDSHeaderNames::ARM7OverlaySize).toUInt(nullptr, 16);
@ -228,7 +228,7 @@ bool MainWindow::writeArm7OverlayFiles(const std::string& savePath)
size); size);
} }
bool MainWindow::writeFnt(const std::string& savePath) NFResult MainWindow::writeFnt(const std::string& savePath)
{ {
return ndsFactory.writeSectionToFile( return ndsFactory.writeSectionToFile(
ui->loadedFntPathEdt->text().toStdString(), ui->loadedFntPathEdt->text().toStdString(),
@ -237,7 +237,7 @@ bool MainWindow::writeFnt(const std::string& savePath)
extractPackerHeaderTableData(NDSHeaderNames::FilenameTableSize).toUInt(nullptr, 16)); extractPackerHeaderTableData(NDSHeaderNames::FilenameTableSize).toUInt(nullptr, 16));
} }
bool MainWindow::writeFntPadding(char paddingType, const std::string& savePath) NFResult MainWindow::writeFntPadding(char paddingType, const std::string& savePath)
{ {
uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::FilenameTableAddress).toUInt(nullptr, 16) + uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::FilenameTableAddress).toUInt(nullptr, 16) +
extractPackerHeaderTableData(NDSHeaderNames::FilenameTableSize).toUInt(nullptr, 16); extractPackerHeaderTableData(NDSHeaderNames::FilenameTableSize).toUInt(nullptr, 16);
@ -250,7 +250,7 @@ bool MainWindow::writeFntPadding(char paddingType, const std::string& savePath)
size); size);
} }
bool MainWindow::writeFat(const std::string& savePath) NFResult MainWindow::writeFat(const std::string& savePath)
{ {
return ndsFactory.writeSectionToFile( return ndsFactory.writeSectionToFile(
ui->loadedFatPathEdt->text().toStdString(), ui->loadedFatPathEdt->text().toStdString(),
@ -259,7 +259,7 @@ bool MainWindow::writeFat(const std::string& savePath)
extractPackerHeaderTableData(NDSHeaderNames::FATSize).toUInt(nullptr, 16)); extractPackerHeaderTableData(NDSHeaderNames::FATSize).toUInt(nullptr, 16));
} }
bool MainWindow::writeFatPadding(char paddingType, const std::string& savePath) NFResult MainWindow::writeFatPadding(char paddingType, const std::string& savePath)
{ {
uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::FATAddress).toUInt(nullptr, 16) + uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::FATAddress).toUInt(nullptr, 16) +
extractPackerHeaderTableData(NDSHeaderNames::FATSize).toUInt(nullptr, 16); extractPackerHeaderTableData(NDSHeaderNames::FATSize).toUInt(nullptr, 16);
@ -272,7 +272,7 @@ bool MainWindow::writeFatPadding(char paddingType, const std::string& savePath)
size); size);
} }
bool MainWindow::writeIconTitle(const std::string& savePath) NFResult MainWindow::writeIconTitle(const std::string& savePath)
{ {
return ndsFactory.writeSectionToFile( return ndsFactory.writeSectionToFile(
ui->loadedIconTitlePathEdt->text().toStdString(), ui->loadedIconTitlePathEdt->text().toStdString(),
@ -281,7 +281,7 @@ bool MainWindow::writeIconTitle(const std::string& savePath)
IconTitleSize); IconTitleSize);
} }
bool MainWindow::writeFatFiles(const std::string& savePath) NFResult MainWindow::writeFatFiles(const std::string& savePath)
{ {
uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::IconTitleAddress).toUInt(nullptr, 16) + IconTitleSize; uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::IconTitleAddress).toUInt(nullptr, 16) + IconTitleSize;
uint32_t size = extractPackerHeaderTableData(NDSHeaderNames::UsedRomSize).toUInt(nullptr, 16) - startAddr; uint32_t size = extractPackerHeaderTableData(NDSHeaderNames::UsedRomSize).toUInt(nullptr, 16) - startAddr;
@ -293,7 +293,7 @@ bool MainWindow::writeFatFiles(const std::string& savePath)
size); size);
} }
bool MainWindow::writeRomPadding(const std::string& savePath) NFResult MainWindow::writeRomPadding(const std::string& savePath)
{ {
uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::UsedRomSize).toUInt(nullptr, 16); uint32_t startAddr = extractPackerHeaderTableData(NDSHeaderNames::UsedRomSize).toUInt(nullptr, 16);
uint32_t size = static_cast<uint32_t>(ndsFactory.getCardSizeInBytes(extractPackerHeaderTableData(NDSHeaderNames::CardSize).toInt())) - startAddr; uint32_t size = static_cast<uint32_t>(ndsFactory.getCardSizeInBytes(extractPackerHeaderTableData(NDSHeaderNames::CardSize).toInt())) - startAddr;
@ -305,9 +305,9 @@ bool MainWindow::writeRomPadding(const std::string& savePath)
size); size);
} }
bool MainWindow::writeEverything(const std::string& savePath) NFResult MainWindow::writeEverything(const std::string& savePath)
{ {
bool res = true; NFResult nfResult;
char paddingType; char paddingType;
bool isArm9FooterPresent = ndsFactory.checkArm9FooterPresence(ui->loadedArm9BinPathEdt->text().toStdString(), bool isArm9FooterPresent = ndsFactory.checkArm9FooterPresence(ui->loadedArm9BinPathEdt->text().toStdString(),
extractPackerHeaderTableData(NDSHeaderNames::ARM9Size).toUInt(nullptr, 16)); extractPackerHeaderTableData(NDSHeaderNames::ARM9Size).toUInt(nullptr, 16));
@ -318,32 +318,49 @@ bool MainWindow::writeEverything(const std::string& savePath)
std::remove(savePath.c_str()); std::remove(savePath.c_str());
res &= writeHeader(savePath); nfResult = writeHeader(savePath);
res &= writeHeaderPadding(paddingType, savePath); if (!nfResult.result) return nfResult;
res &= writeArm9Bin(savePath, isArm9FooterPresent); nfResult = writeHeaderPadding(paddingType, savePath);
res &= writeArm9BinPadding(paddingType, savePath, isArm9FooterPresent); if (!nfResult.result) return nfResult;
nfResult = writeArm9Bin(savePath, isArm9FooterPresent);
if (!nfResult.result) return nfResult;
nfResult = writeArm9BinPadding(paddingType, savePath, isArm9FooterPresent);
if (!nfResult.result) return nfResult;
if (extractPackerHeaderTableData(NDSHeaderNames::ARM9OverlayAddress).toUInt(nullptr, 16) != 0) if (extractPackerHeaderTableData(NDSHeaderNames::ARM9OverlayAddress).toUInt(nullptr, 16) != 0)
{ {
res &= writeArm9Overlay(savePath); nfResult = writeArm9Overlay(savePath);
res &= writeArm9OverlayFiles(savePath); if (!nfResult.result) return nfResult;
nfResult = writeArm9OverlayFiles(savePath);
if (!nfResult.result) return nfResult;
} }
res &= writeArm7Bin(savePath); nfResult = writeArm7Bin(savePath);
res &= writeArm7BinPadding(paddingType, savePath); if (!nfResult.result) return nfResult;
nfResult = writeArm7BinPadding(paddingType, savePath);
if (!nfResult.result) return nfResult;
if (extractPackerHeaderTableData(NDSHeaderNames::ARM7OverlayAddress).toUInt(nullptr, 16) != 0) { if (extractPackerHeaderTableData(NDSHeaderNames::ARM7OverlayAddress).toUInt(nullptr, 16) != 0) {
res &= writeArm7Overlay(savePath); nfResult = writeArm7Overlay(savePath);
res &= writeArm7OverlayFiles(savePath); if (!nfResult.result) return nfResult;
nfResult = writeArm7OverlayFiles(savePath);
if (!nfResult.result) return nfResult;
} }
res &= writeFnt(savePath); nfResult = writeFnt(savePath);
res &= writeFntPadding(paddingType, savePath); if (!nfResult.result) return nfResult;
res &= writeFat(savePath); nfResult = writeFntPadding(paddingType, savePath);
res &= writeFatPadding(paddingType, savePath); if (!nfResult.result) return nfResult;
res &= writeIconTitle(savePath); nfResult = writeFat(savePath);
res &= writeFatFiles(savePath); if (!nfResult.result) return nfResult;
nfResult = writeFatPadding(paddingType, savePath);
if (!nfResult.result) return nfResult;
nfResult = writeIconTitle(savePath);
if (!nfResult.result) return nfResult;
nfResult = writeFatFiles(savePath);
if (!nfResult.result) return nfResult;
if(!ui->packerTrimRomsCbx->isChecked()) if(!ui->packerTrimRomsCbx->isChecked())
{ {
res &= writeRomPadding(savePath); nfResult = writeRomPadding(savePath);
if (!nfResult.result) return nfResult;
} }
return res; return { true, "" };
} }
QString MainWindow::extractPackerHeaderTableData(int index) QString MainWindow::extractPackerHeaderTableData(int index)

View File

@ -16,13 +16,9 @@ void MainWindow::on_packerLoadHeaderBtn_clicked()
QDir::currentPath(), QDir::currentPath(),
"NDS Header (*.bin)"); "NDS Header (*.bin)");
if(headerPath.isNull()) if (headerPath.isNull()) return;
{
QMessageBox::critical(this, tr("NDSFactory"), tr("Unable to open file!"));
return;
}
if (ndsFactory.loadRomHeader(headerPath.toStdString(), romHeader)) if (ndsFactory.loadRomHeader(headerPath.toStdString(), romHeader).result)
{ {
pNDSHeader = reinterpret_cast<NDSHeader*>(romHeader.data()); pNDSHeader = reinterpret_cast<NDSHeader*>(romHeader.data());
populatePackerSectionHeader(pNDSHeader); populatePackerSectionHeader(pNDSHeader);
@ -180,9 +176,11 @@ void MainWindow::on_packerBuildNDSRomBtn_clicked()
"NDS ROM (*.nds)"); "NDS ROM (*.nds)");
if (!dirPath.isNull()) if (!dirPath.isNull())
writeEverything(dirPath.toStdString()) ? QMessageBox::information(this, tr("NDSFactory"), tr("Creation completed!")) {
: QMessageBox::critical(this, tr("NDSFactory"), tr("Error during the creation!")); NFResult nfResult = writeEverything(dirPath.toStdString());
nfResult.result? QMessageBox::information(this, tr("NDSFactory"), tr("Creation completed!"))
: QMessageBox::critical(this, tr("NDSFactory"), nfResult.message.c_str());
}
ui->packerBuildNDSRomBtn->setEnabled(true); ui->packerBuildNDSRomBtn->setEnabled(true);
} }

View File

@ -51,7 +51,7 @@ void MainWindow::disableExtractionButtons()
ui->unpackerExtraGbx->setEnabled(false); ui->unpackerExtraGbx->setEnabled(false);
} }
bool MainWindow::dumpHeader(const std::string& dirPath) NFResult MainWindow::dumpHeader(const std::string& dirPath)
{ {
return ndsFactory.dumpDataFromFile( return ndsFactory.dumpDataFromFile(
ui->loadedRomPath->text().toStdString(), ui->loadedRomPath->text().toStdString(),
@ -60,7 +60,7 @@ bool MainWindow::dumpHeader(const std::string& dirPath)
ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::HeaderSize, 1).data().toString().toUInt(nullptr,16)); ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::HeaderSize, 1).data().toString().toUInt(nullptr,16));
} }
bool MainWindow::dumpArm9Bin(const std::string& dirPath, bool dumpExtraBytes) NFResult MainWindow::dumpArm9Bin(const std::string& dirPath, bool dumpExtraBytes)
{ {
uint32_t size = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9Size, 1).data().toString().toUInt(nullptr,16); uint32_t size = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9Size, 1).data().toString().toUInt(nullptr,16);
if (dumpExtraBytes) if (dumpExtraBytes)
@ -72,7 +72,7 @@ bool MainWindow::dumpArm9Bin(const std::string& dirPath, bool dumpExtraBytes)
size); size);
} }
bool MainWindow::dumpArm7Bin(const std::string& dirPath) NFResult MainWindow::dumpArm7Bin(const std::string& dirPath)
{ {
return ndsFactory.dumpDataFromFile( return ndsFactory.dumpDataFromFile(
ui->loadedRomPath->text().toStdString(), ui->loadedRomPath->text().toStdString(),
@ -81,7 +81,7 @@ bool MainWindow::dumpArm7Bin(const std::string& dirPath)
ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7Size, 1).data().toString().toUInt(nullptr,16)); ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7Size, 1).data().toString().toUInt(nullptr,16));
} }
bool MainWindow::dumpFnt(const std::string& dirPath) NFResult MainWindow::dumpFnt(const std::string& dirPath)
{ {
return ndsFactory.dumpDataFromFile( return ndsFactory.dumpDataFromFile(
ui->loadedRomPath->text().toStdString(), ui->loadedRomPath->text().toStdString(),
@ -90,7 +90,7 @@ bool MainWindow::dumpFnt(const std::string& dirPath)
ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::FilenameTableSize, 1).data().toString().toUInt(nullptr,16)); ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::FilenameTableSize, 1).data().toString().toUInt(nullptr,16));
} }
bool MainWindow::dumpFat(const std::string& dirPath) NFResult MainWindow::dumpFat(const std::string& dirPath)
{ {
return ndsFactory.dumpDataFromFile( return ndsFactory.dumpDataFromFile(
ui->loadedRomPath->text().toStdString(), ui->loadedRomPath->text().toStdString(),
@ -99,7 +99,7 @@ bool MainWindow::dumpFat(const std::string& dirPath)
ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::FATSize, 1).data().toString().toUInt(nullptr,16)); ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::FATSize, 1).data().toString().toUInt(nullptr,16));
} }
bool MainWindow::dumpArm9Overlay(const std::string& dirPath) NFResult MainWindow::dumpArm9Overlay(const std::string& dirPath)
{ {
return ndsFactory.dumpDataFromFile( return ndsFactory.dumpDataFromFile(
ui->loadedRomPath->text().toStdString(), ui->loadedRomPath->text().toStdString(),
@ -108,7 +108,7 @@ bool MainWindow::dumpArm9Overlay(const std::string& dirPath)
ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9OverlaySize, 1).data().toString().toUInt(nullptr,16)); ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9OverlaySize, 1).data().toString().toUInt(nullptr,16));
} }
bool MainWindow::dumpArm9OverlayFiles(const std::string& dirPath) NFResult MainWindow::dumpArm9OverlayFiles(const std::string& dirPath)
{ {
uint32_t startAddr = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9OverlayAddress, 1).data().toString().toUInt(nullptr, 16) + uint32_t startAddr = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9OverlayAddress, 1).data().toString().toUInt(nullptr, 16) +
ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9OverlaySize, 1).data().toString().toUInt(nullptr, 16); ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9OverlaySize, 1).data().toString().toUInt(nullptr, 16);
@ -121,7 +121,7 @@ bool MainWindow::dumpArm9OverlayFiles(const std::string& dirPath)
size); size);
} }
bool MainWindow::dumpArm7Overlay(const std::string& dirPath) NFResult MainWindow::dumpArm7Overlay(const std::string& dirPath)
{ {
return ndsFactory.dumpDataFromFile( return ndsFactory.dumpDataFromFile(
ui->loadedRomPath->text().toStdString(), ui->loadedRomPath->text().toStdString(),
@ -130,7 +130,7 @@ bool MainWindow::dumpArm7Overlay(const std::string& dirPath)
ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7OverlaySize, 1).data().toString().toUInt(nullptr,16)); ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7OverlaySize, 1).data().toString().toUInt(nullptr,16));
} }
bool MainWindow::dumpArm7OverlayFiles(const std::string& dirPath) NFResult MainWindow::dumpArm7OverlayFiles(const std::string& dirPath)
{ {
uint32_t startAddr = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7OverlayAddress, 1).data().toString().toUInt(nullptr, 16) + uint32_t startAddr = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7OverlayAddress, 1).data().toString().toUInt(nullptr, 16) +
ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7OverlaySize, 1).data().toString().toUInt(nullptr, 16); ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7OverlaySize, 1).data().toString().toUInt(nullptr, 16);
@ -143,7 +143,7 @@ bool MainWindow::dumpArm7OverlayFiles(const std::string& dirPath)
size); size);
} }
bool MainWindow::dumpIconTitle(const std::string& dirPath) NFResult MainWindow::dumpIconTitle(const std::string& dirPath)
{ {
return ndsFactory.dumpDataFromFile( return ndsFactory.dumpDataFromFile(
ui->loadedRomPath->text().toStdString(), ui->loadedRomPath->text().toStdString(),
@ -152,7 +152,7 @@ bool MainWindow::dumpIconTitle(const std::string& dirPath)
ICON_TITLE_SIZE); ICON_TITLE_SIZE);
} }
bool MainWindow::dumpFatFiles(const std::string& dirPath) NFResult MainWindow::dumpFatFiles(const std::string& dirPath)
{ {
uint32_t startAddr = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::IconTitleAddress, 1).data().toString().toUInt(nullptr,16) + IconTitleSize ; uint32_t startAddr = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::IconTitleAddress, 1).data().toString().toUInt(nullptr,16) + IconTitleSize ;
uint32_t size = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::UsedRomSize, 1).data().toString().toUInt(nullptr,16) - startAddr; uint32_t size = ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::UsedRomSize, 1).data().toString().toUInt(nullptr,16) - startAddr;
@ -165,23 +165,34 @@ bool MainWindow::dumpFatFiles(const std::string& dirPath)
} }
bool MainWindow::dumpEverything(QString dirPath) NFResult MainWindow::dumpEverything(QString dirPath)
{ {
bool result = true; NFResult nfResult;
result &= dumpHeader(QDir::toNativeSeparators(dirPath+"/header.bin").toStdString()); nfResult = dumpHeader(QDir::toNativeSeparators(dirPath + "/header.bin").toStdString());
result &= dumpArm9Bin(QDir::toNativeSeparators(dirPath+"/arm9.bin").toStdString(), true); if (!nfResult.result) return nfResult;
result &= dumpArm7Bin(QDir::toNativeSeparators(dirPath+"/arm7.bin").toStdString()); nfResult = dumpArm9Bin(QDir::toNativeSeparators(dirPath+"/arm9.bin").toStdString(), true);
result &= dumpFnt(QDir::toNativeSeparators(dirPath+"/fnt.bin").toStdString()); if (!nfResult.result) return nfResult;
result &= dumpFat(QDir::toNativeSeparators(dirPath+"/fat.bin").toStdString()); nfResult = dumpArm7Bin(QDir::toNativeSeparators(dirPath+"/arm7.bin").toStdString());
if (!nfResult.result) return nfResult;
nfResult = dumpFnt(QDir::toNativeSeparators(dirPath+"/fnt.bin").toStdString());
if (!nfResult.result) return nfResult;
nfResult = dumpFat(QDir::toNativeSeparators(dirPath+"/fat.bin").toStdString());
if (!nfResult.result) return nfResult;
if(ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9OverlayAddress, 1).data().toString().toUInt(nullptr,16) != 0) { if(ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM9OverlayAddress, 1).data().toString().toUInt(nullptr,16) != 0) {
result &= dumpArm9Overlay(QDir::toNativeSeparators(dirPath+"/a9ovr.bin").toStdString()); nfResult = dumpArm9Overlay(QDir::toNativeSeparators(dirPath+"/a9ovr.bin").toStdString());
result &= dumpArm9OverlayFiles(QDir::toNativeSeparators(dirPath+"/a9ovr_data.bin").toStdString()); if (!nfResult.result) return nfResult;
nfResult = dumpArm9OverlayFiles(QDir::toNativeSeparators(dirPath+"/a9ovr_data.bin").toStdString());
if (!nfResult.result) return nfResult;
} }
if(ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7OverlayAddress, 1).data().toString().toUInt(nullptr,16) != 0) { if(ui->unpackerHeaderDataTable->model()->index(NDSHeaderNames::ARM7OverlayAddress, 1).data().toString().toUInt(nullptr,16) != 0) {
result &= dumpArm7Overlay(QDir::toNativeSeparators(dirPath+"/a7ovr.bin").toStdString()); nfResult = dumpArm7Overlay(QDir::toNativeSeparators(dirPath+"/a7ovr.bin").toStdString());
result &= dumpArm7OverlayFiles(QDir::toNativeSeparators(dirPath+"/a7ovr_data.bin").toStdString()); if (!nfResult.result) return nfResult;
} nfResult = dumpArm7OverlayFiles(QDir::toNativeSeparators(dirPath+"/a7ovr_data.bin").toStdString());
result &= dumpIconTitle(QDir::toNativeSeparators(dirPath+"/itl.bin").toStdString()); if (!nfResult.result) return nfResult;
result &= dumpFatFiles(QDir::toNativeSeparators(dirPath+"/fat_data.bin").toStdString()); }
return result; nfResult = dumpIconTitle(QDir::toNativeSeparators(dirPath+"/itl.bin").toStdString());
if (!nfResult.result) return nfResult;
nfResult = dumpFatFiles(QDir::toNativeSeparators(dirPath+"/fat_data.bin").toStdString());
if (!nfResult.result) return nfResult;
return { true, "" };
} }

View File

@ -17,12 +17,11 @@ void MainWindow::on_loadRomBtn_clicked()
QDir::currentPath(), QDir::currentPath(),
"NDS Rom (*.nds)"); "NDS Rom (*.nds)");
if( !romPath.isNull() ) if (romPath.isNull()) return;
{
ui->loadedRomPath->setText(romPath.toUtf8());
}
if (ndsFactory.loadRomHeader(ui->loadedRomPath->text().toStdString(), romHeader)) ui->loadedRomPath->setText(romPath.toUtf8());
if (ndsFactory.loadRomHeader(ui->loadedRomPath->text().toStdString(), romHeader).result)
{ {
pNDSHeader = reinterpret_cast<NDSHeader*>(romHeader.data()); pNDSHeader = reinterpret_cast<NDSHeader*>(romHeader.data());
populateHeader(pNDSHeader); populateHeader(pNDSHeader);

View File

@ -1,15 +1,16 @@
#pragma once #pragma once
#include <QMessageBox> #include <QMessageBox>
#include "../../ndsfactory/nfresult.h"
void notifyExtractionResult(bool result) void notifyExtractionResult(NFResult nfResult)
{ {
if (result) if (nfResult.result)
{ {
QMessageBox::information(Q_NULLPTR, "NDSFactory", "Extraction completed!"); QMessageBox::information(Q_NULLPTR, "NDSFactory", "Extraction completed!");
} }
else else
{ {
QMessageBox::critical(Q_NULLPTR, "NDSFactory", "Error during the extraction!"); QMessageBox::critical(Q_NULLPTR, "NDSFactory", nfResult.message.c_str());
} }
} }