diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/knarc/Narc.cpp | 46 | ||||
-rw-r--r-- | tools/knarc/Source.cpp | 5 | ||||
-rw-r--r-- | tools/o2narc/o2narc.cpp | 64 |
3 files changed, 108 insertions, 7 deletions
diff --git a/tools/knarc/Narc.cpp b/tools/knarc/Narc.cpp index 7e698571..cce7cdea 100644 --- a/tools/knarc/Narc.cpp +++ b/tools/knarc/Narc.cpp @@ -29,6 +29,7 @@ using namespace std; extern bool debug; extern bool pack_no_fnt; +extern bool output_header; void Narc::AlignDword(ofstream& ofs, uint8_t paddingChar) { @@ -145,6 +146,39 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) if (!ofs.good()) { return Cleanup(ofs, NarcError::InvalidOutputFile); } + ofstream ofhs; + string stem; + string stem_upper; + // Pikalax 29 May 2021 + // Output an includable header that enumerates the NARC contents + if (output_header) + { + fs::path naixfname = fileName; + naixfname.replace_extension(".naix"); + + ofhs.open(naixfname); + if (!ofhs.good()) + { + ofhs.close(); + return Cleanup(ofs, NarcError::InvalidOutputFile); + } + + stem = fileName.stem().string(); + stem_upper = stem; + for (char &c : stem_upper) + { c = toupper(c); } + + ofhs << "/*\n" + " * THIS FILE WAS AUTOMATICALLY\n" + " * GENERATED BY tools/knarc\n" + " * DO NOT MODIFY!!!\n" + " */\n" + "\n" + "#ifndef NARC_" << stem_upper << "_NAIX_\n" + "#define NARC_" << stem_upper << "_NAIX_\n" + "\n" + "enum {\n"; + } vector<FileAllocationTableEntry> fatEntries; uint16_t directoryCounter = 1; @@ -153,6 +187,7 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) ignore_patterns.push_back(".*keep"); WildcardVector keep_patterns(directory / ".knarckeep"); + int memberNo = 0; for (const auto& de : OrderedDirectoryIterator(directory, true)) { if (is_directory(de)) @@ -164,6 +199,12 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) if (debug) { cerr << "DEBUG: adding file " << de.path() << endl; } + if (output_header) + { + string de_stem = de.path().filename().string(); + std::replace(de_stem.begin(), de_stem.end(), '.', '_'); + ofhs << "\tNARC_" << stem << "_" << de_stem << " = " << (memberNo++) << ",\n"; + } fatEntries.push_back(FileAllocationTableEntry { .Start = 0x0, @@ -183,6 +224,11 @@ bool Narc::Pack(const fs::path& fileName, const fs::path& directory) fatEntries.back().End = fatEntries.back().Start + static_cast<uint32_t>(file_size(de)); } } + if (output_header) + { + ofhs << "};\n\n#endif //NARC_" << stem_upper << "_NAIX_\n"; + ofhs.close(); + } FileAllocationTable fat { diff --git a/tools/knarc/Source.cpp b/tools/knarc/Source.cpp index d9a5cf83..587c8527 100644 --- a/tools/knarc/Source.cpp +++ b/tools/knarc/Source.cpp @@ -8,6 +8,7 @@ using namespace std; bool debug = false; bool pack_no_fnt = true; +bool output_header = false; void PrintError(NarcError error) { @@ -40,6 +41,7 @@ static inline void usage() { 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; + cout << "\t-i\tOutput a .naix header" << endl; } int main(int argc, char* argv[]) @@ -104,6 +106,9 @@ int main(int argc, char* argv[]) else if (!strcmp(argv[i], "-n")) { pack_no_fnt = false; } + else if (!strcmp(argv[i], "-i")) { + output_header = true; + } else { usage(); cerr << "ERROR: Unrecognized argument: " << argv[i] << endl; diff --git a/tools/o2narc/o2narc.cpp b/tools/o2narc/o2narc.cpp index 1381788a..60e4e242 100644 --- a/tools/o2narc/o2narc.cpp +++ b/tools/o2narc/o2narc.cpp @@ -3,6 +3,7 @@ #include <getopt.h> #include <cstring> #include <vector> +#include <iomanip> #include "elf.h" #include "Narc.h" @@ -49,6 +50,7 @@ class Elf { public: ShdrTab shdr; Symtab symtab; + Elf(string const& filename) : Elf(filename.c_str()) {} Elf(const char * filename) { // Read the ELF header phdr = nullptr; @@ -155,25 +157,29 @@ static inline void usage() { cout << "\toutfile\tOutput NARC file" << endl; cout << "Options:" << endl; cout << "\t-f|--flatten\tDon't generate NARC headers" << endl; + cout << "\t-i|--output-header\tCreate a .naix file" << endl; } int main(int argc, char ** argv) { // CLI arguments - int flatten = 0; + int flatten = 0, output_header = 0; char padding = '\xFF'; static option options [] { { "flatten", no_argument, &flatten, 1 }, { "padding", required_argument, nullptr, 'p' }, + { "output_header", no_argument, &output_header, 1 }, {nullptr, 0, nullptr, 0} }; int opt_index; int c; - while ((c = getopt_long(argc, argv, "fp:", options, &opt_index)) != -1) + while ((c = getopt_long(argc, argv, "fp:i", options, &opt_index)) != -1) { if (c == 'f') { flatten = 1; } else if (c == 'p') { padding = strtol(optarg, NULL, 0); + } else if (c == 'i') { + output_header = 1; } } argv += optind; @@ -188,8 +194,13 @@ int main(int argc, char ** argv) { cerr << "Excess arguments: first unrecognized '" << argv[2] << "'" << endl; return 1; } - char * infname = argv[0]; - char * outfname = argv[1]; + if (output_header && flatten) { + usage(); + cerr << "Incompatible flags: -i, -f" << endl; + return 1; + } + const char * infname = argv[0]; + const char * outfname = argv[1]; // Read the ELF file Elf elf(infname); @@ -201,15 +212,45 @@ int main(int argc, char ** argv) { exit(1); } - fstream ofile; - ofile.open(outfname, ios_base::out | ios_base::binary); + ofstream ofile; + ofile.open(outfname, ios_base::binary); if (!ofile.good()) { cerr << "ERROR: Unable to open '" << outfname << "' for writing" << endl; exit(1); } - if (!flatten) // then build the NARC chunks { + string stem, stem_upper; + ofstream ofheader; + if (output_header) + { + string outhname = outfname; + outhname.replace(outhname.find(".narc"), 5, ".naix"); + ofheader.open(outhname); + if (!ofheader.good()) + { + ofile.close(); + cerr << "ERROR: Unable to open '" << outhname << "' for writing" << endl; + exit(1); + } + stem = outfname; + stem = stem.substr(0, stem.rfind(".")); + stem = stem.substr(stem.rfind("/") + 1); + stem_upper = stem; + for (char &_c : stem_upper) + { _c = toupper(_c); } + + ofheader << "/*\n" + " * THIS FILE WAS AUTOMATICALLY\n" + " * GENERATED BY tools/o2narc\n" + " * DO NOT MODIFY!!!\n" + " */\n" + "\n" + "#ifndef NARC_" << stem_upper << "_NAIX_\n" + "#define NARC_" << stem_upper << "_NAIX_\n" + "\n" + "enum {\n"; + } // .data contains the size table Elf32_Shdr & data_sec = elf.shdr[".data"]; uint32_t * _data = (uint32_t *)elf.read(data_sec); @@ -275,6 +316,15 @@ int main(int argc, char ** argv) { { _rodata[fat_entries[i].Start + j] = padding; } + if (output_header) + { + ofheader << "\tNARC_" << stem << "_narc_" << setw(4) << setfill('0') << i << " = " << i << ",\n"; + } + } + if (output_header) + { + ofheader << "};\n\n#endif //NARC_" << stem_upper << "_NAIX_\n"; + ofheader.close(); } // These NARCs have empty FNTs FileNameTable fnt{ |