diff options
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | files/poketool/personal/growtbl/.knarcignore | 3 | ||||
-rw-r--r-- | filesystem.mk | 4 | ||||
-rw-r--r-- | tools/csv2bin/Makefile (renamed from files/poketool/personal/growtbl/Makefile) | 8 | ||||
-rw-r--r-- | tools/csv2bin/csv2bin.c (renamed from files/poketool/personal/growtbl/grow2bin.c) | 0 | ||||
-rw-r--r-- | tools/knarc/Narc.cpp | 70 | ||||
-rw-r--r-- | tools/knarc/Source.cpp | 61 |
8 files changed, 116 insertions, 35 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index abf430db..243b0cca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 2.8.11) project(PokeDiamond) -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) # TODO: Add commands @@ -15,3 +15,5 @@ target_include_directories(PokeDiamond PRIVATE include include-mw arm9/lib/inclu add_executable(calcrom .travis/calcrom/calcrom.cpp) target_include_directories(calcrom PRIVATE /usr/local/include) + +add_executable(knarc tools/knarc/Source.cpp tools/knarc/Narc.cpp) @@ -199,6 +199,7 @@ LDFLAGS = -map -nodead -w off -proc v5te -interworking -map -symtab -m _start # DS TOOLS TOOLS_DIR = tools SHA1SUM = sha1sum +CSV2BIN = $(TOOLS_DIR)/csv2bin/csv2bin JSONPROC = $(TOOLS_DIR)/jsonproc/jsonproc GFX = $(TOOLS_DIR)/nitrogfx/nitrogfx MWASMARM_PATCHER = $(TOOLS_DIR)/mwasmarm_patcher/mwasmarm_patcher$(EXE) -q diff --git a/files/poketool/personal/growtbl/.knarcignore b/files/poketool/personal/growtbl/.knarcignore new file mode 100644 index 00000000..d633d071 --- /dev/null +++ b/files/poketool/personal/growtbl/.knarcignore @@ -0,0 +1,3 @@ +*.txt +*.exe +grow2bin diff --git a/filesystem.mk b/filesystem.mk index b43877db..f557e59d 100644 --- a/filesystem.mk +++ b/filesystem.mk @@ -283,5 +283,5 @@ HOSTFS_FILES = $(NITROFS_FILES:%=files/%) files/poketool/personal/pms.narc: ; files/poketool/personal/growtbl.narc: $(wildcard files/poketool/personal/growtbl/*.txt) - $(MAKE) -C $(<D) - $(NARCCOMP) -o $@ -p 255 $(^:%.txt=%.bin) + $(foreach file,$^,$(CSV2BIN) $(file);) + $(KNARC) -d $(basename $@)/ -p $@ diff --git a/files/poketool/personal/growtbl/Makefile b/tools/csv2bin/Makefile index 4bbb579f..949ec4e4 100644 --- a/files/poketool/personal/growtbl/Makefile +++ b/tools/csv2bin/Makefile @@ -6,14 +6,14 @@ growth_rates_bin = $(growth_rates_txt:%.txt=%.bin) .PHONY: all clean -all: grow2bin $(growth_rates_bin) +all: csv2bin $(growth_rates_bin) @: clean: - $(RM) grow2bin $(growth_rates_bin) + $(RM) csv2bin $(growth_rates_bin) -grow2bin: grow2bin.c +csv2bin: csv2bin.c $(CC) $(CFLAGS) -o $@ $^ %.bin: %.txt - ./grow2bin $< + ./csv2bin $< diff --git a/files/poketool/personal/growtbl/grow2bin.c b/tools/csv2bin/csv2bin.c index 5c8b5111..5c8b5111 100644 --- a/files/poketool/personal/growtbl/grow2bin.c +++ b/tools/csv2bin/csv2bin.c diff --git a/tools/knarc/Narc.cpp b/tools/knarc/Narc.cpp index 506e050f..7ccdcf0e 100644 --- a/tools/knarc/Narc.cpp +++ b/tools/knarc/Narc.cpp @@ -13,6 +13,8 @@ #include <stack> #include <string> #include <vector> +#include <iostream> +#include <fnmatch.h> #if __GNUC__ <= 7 #include <experimental/filesystem> @@ -24,6 +26,9 @@ namespace fs = std::filesystem; using namespace std; +extern bool debug; +extern bool pack_no_fnt; + void Narc::AlignDword(ofstream& ofs, uint8_t paddingChar) { if ((ofs.tellp() % 4) != 0) @@ -104,6 +109,26 @@ NarcError Narc::GetError() const return error; } +class WildcardVector : public vector<string> { +public: + WildcardVector(fs::path fp) { + fstream infile; + if (!fs::exists(fp)) return; + infile.open(fp, ios_base::in); + string line; + while (getline(infile, line)) { + push_back(line); + } + } + bool matches(string fp) { + for (string& pattern : *this) { + if (fnmatch(pattern.c_str(), fp.c_str(), FNM_PERIOD) == 0) + return true; + } + return false; + } +}; + bool Narc::Pack(const fs::path& fileName, const fs::path& directory) { ofstream ofs(fileName, ios::binary); @@ -113,14 +138,22 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) vector<FileAllocationTableEntry> fatEntries; uint16_t directoryCounter = 1; + WildcardVector ignore_patterns(directory / ".knarcignore"); + ignore_patterns.push_back(".*ignore"); + ignore_patterns.push_back(".*keep"); + WildcardVector keep_patterns(directory / ".knarckeep"); + for (const auto& de : OrderedDirectoryIterator(directory, true)) { if (is_directory(de)) { ++directoryCounter; } - else + else if (keep_patterns.matches(de.path().filename()) || !ignore_patterns.matches(de.path().filename())) { + if (debug) { + cerr << "DEBUG: adding file " << de.path() << endl; + } fatEntries.push_back(FileAllocationTableEntry { .Start = 0x0, @@ -143,8 +176,8 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) FileAllocationTable fat { - .Id = 0x46415442, - .ChunkSize = sizeof(FileAllocationTable) + ((uint32_t)fatEntries.size() * sizeof(FileAllocationTableEntry)), + .Id = 0x46415442, // BTAF + .ChunkSize = static_cast<uint32_t>(sizeof(FileAllocationTable) + ((uint32_t)fatEntries.size() * sizeof(FileAllocationTableEntry))), .FileCount = static_cast<uint16_t>(fatEntries.size()), .Reserved = 0x0 }; @@ -156,7 +189,7 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) for (const auto& de : OrderedDirectoryIterator(directory, true)) { - if (!subTables.count(de.path().parent_path())) + if (!subTables.count(de.path().parent_path()) && (keep_patterns.matches(de.path().filename()) || !ignore_patterns.matches(de.path().filename()))) { subTables.insert({ de.path().parent_path(), "" }); paths.push_back(de.path().parent_path()); @@ -171,7 +204,7 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) subTables[de.path().parent_path()] += (0xF000 + directoryCounter) & 0xFF; subTables[de.path().parent_path()] += (0xF000 + directoryCounter) >> 8; } - else + else if (keep_patterns.matches(de.path().filename()) || !ignore_patterns.matches(de.path().filename())) { subTables[de.path().parent_path()] += static_cast<uint8_t>(de.path().filename().string().size()); subTables[de.path().parent_path()] += de.path().filename().string(); @@ -185,11 +218,11 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) vector<FileNameTableEntry> fntEntries; - if (!regex_match(fs::directory_iterator(directory)->path().string(), regex(".*_\\d{4,8}\\.bin"))) + if (!pack_no_fnt) { fntEntries.push_back( { - .Offset = (directoryCounter + 1) * sizeof(FileNameTableEntry), + .Offset = static_cast<uint32_t>((directoryCounter + 1) * sizeof(FileNameTableEntry)), .FirstFileId = 0x0, .Utility = static_cast<uint16_t>(directoryCounter + 1) }); @@ -198,7 +231,7 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) { fntEntries.push_back( { - .Offset = fntEntries.back().Offset + subTables[paths[i]].size(), + .Offset = static_cast<uint32_t>(fntEntries.back().Offset + subTables[paths[i]].size()), .FirstFileId = fntEntries.back().FirstFileId, .Utility = 0x0 }); @@ -231,11 +264,11 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) FileNameTable fnt { - .Id = 0x464E5442, - .ChunkSize = sizeof(FileNameTable) + (fntEntries.size() * sizeof(FileNameTableEntry)) + .Id = 0x464E5442, // BTNF + .ChunkSize = static_cast<uint32_t>(sizeof(FileNameTable) + (fntEntries.size() * sizeof(FileNameTableEntry))) }; - if (!regex_match(fs::directory_iterator(directory)->path().string(), regex(".*_\\d{4,8}\\.bin"))) + if (!pack_no_fnt) { for (const auto& subTable : subTables) { @@ -250,8 +283,8 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) FileImages fi { - .Id = 0x46494D47, - .ChunkSize = sizeof(FileImages) + fatEntries.back().End + .Id = 0x46494D47, // GMIF + .ChunkSize = static_cast<uint32_t>(sizeof(FileImages) + fatEntries.back().End) }; if ((fi.ChunkSize % 4) != 0) @@ -261,10 +294,10 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) Header header { - .Id = 0x4352414E, + .Id = 0x4352414E, // NARC .ByteOrderMark = 0xFFFE, .Version = 0x100, - .FileSize = sizeof(Header) + fat.ChunkSize + fnt.ChunkSize + fi.ChunkSize, + .FileSize = static_cast<uint32_t>(sizeof(Header) + fat.ChunkSize + fnt.ChunkSize + fi.ChunkSize), .ChunkSize = sizeof(Header), .ChunkCount = 0x3 }; @@ -284,7 +317,7 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) ofs.write(reinterpret_cast<char*>(&entry), sizeof(FileNameTableEntry)); } - if (!regex_match(fs::directory_iterator(directory)->path().string(), regex(".*_\\d{4,8}\\.bin"))) + if (!pack_no_fnt) { for (const auto& path : paths) { @@ -303,6 +336,11 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) continue; } + if (!(keep_patterns.matches(de.path().filename()) || !ignore_patterns.matches(de.path().filename()))) + { + continue; + } + ifstream ifs(de.path(), ios::binary | ios::ate); if (!ifs.good()) diff --git a/tools/knarc/Source.cpp b/tools/knarc/Source.cpp index 51bb5320..21020891 100644 --- a/tools/knarc/Source.cpp +++ b/tools/knarc/Source.cpp @@ -6,6 +6,9 @@ using namespace std; +bool debug = false; +bool pack_no_fnt = true; + void PrintError(NarcError error) { switch (error) @@ -27,20 +30,20 @@ void PrintError(NarcError error) } } +static inline void usage() { + cout << "OVERVIEW: Knarc" << endl << endl; + cout << "USAGE: knarc [options] -d DIRECTORY [-p TARGET | -u SOURCE]" << endl << endl; + cout << "OPTIONS:" << endl; + cout << "\t-d DIRECTORY\tDirectory to pack from/unpack to" << endl; + cout << "\t-p TARGET\tPack to the target NARC" << endl; + cout << "\t-u SOURCE\tUnpack from the source NARC" << endl; + cout << "\t-n\tBuild the filename table (default: discards filenames)" << endl; + cout << "\t-D/--debug\tPrint additional debug messages" << endl; + cout << "\t-h/--help\tPrint this message and exit" << endl; +} + int main(int argc, char* argv[]) { - if (argc != 5) - { - cout << "OVERVIEW: Knarc" << endl << endl; - cout << "USAGE: knarc [options] <inputs>" << endl << endl; - cout << "OPTIONS:" << endl; - cout << "\t-d\tDirectory to pack from/unpack to" << endl; - cout << "\t-p\tPack" << endl; - cout << "\t-u\tUnpack" << endl; - - return 1; - } - string directory = ""; string fileName = ""; bool pack = false; @@ -56,6 +59,10 @@ int main(int argc, char* argv[]) return 1; } + if (!directory.empty()) { + cerr << "ERROR: Multiple directories specified" << endl; + return 1; + } directory = argv[++i]; } else if (!strcmp(argv[i], "-p")) @@ -67,6 +74,10 @@ int main(int argc, char* argv[]) return 1; } + if (!fileName.empty()) { + cerr << "ERROR: Multiple files specified" << endl; + return 1; + } fileName = argv[++i]; pack = true; } @@ -79,10 +90,36 @@ int main(int argc, char* argv[]) return 1; } + if (!fileName.empty()) { + cerr << "ERROR: Multiple files specified" << endl; + return 1; + } fileName = argv[++i]; + } else if (!strcmp(argv[i], "-D") || !strcmp(argv[i], "--debug")) { + debug = true; + } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + usage(); + return 0; + } + else if (!strcmp(argv[i], "-n")) { + pack_no_fnt = false; + } + else { + usage(); + cerr << "ERROR: Unrecognized argument: " << argv[i] << endl; + return 1; } } + if (fileName.empty()) { + cerr << "ERROR: Missing -u or -p" << endl; + return 1; + } + if (directory.empty()) { + cerr << "ERROR: Missing -d" << endl; + return 1; + } + Narc narc; if (pack) |