diff options
| -rw-r--r-- | ZAPD/Directory.h | 2 | ||||
| -rw-r--r-- | ZAPD/File.h | 13 | ||||
| -rw-r--r-- | ZAPD/Globals.cpp | 3 | ||||
| -rw-r--r-- | ZAPD/Main.cpp | 18 | ||||
| -rw-r--r-- | ZAPD/ZArray.cpp | 2 | ||||
| -rw-r--r-- | ZAPD/ZFile.cpp | 67 | ||||
| -rw-r--r-- | ZAPD/ZFile.h | 13 | ||||
| -rw-r--r-- | docs/zapd_extraction_xml_reference.md | 1 |
8 files changed, 60 insertions, 59 deletions
diff --git a/ZAPD/Directory.h b/ZAPD/Directory.h index 74bb14e..adb2e17 100644 --- a/ZAPD/Directory.h +++ b/ZAPD/Directory.h @@ -19,7 +19,7 @@ class Directory public: static std::string GetCurrentDirectory() { return fs::current_path().u8string(); } - static bool Exists(const std::string& path) { return fs::exists(fs::path(path)); } + static bool Exists(const fs::path& path) { return fs::exists(path); } static void CreateDirectory(const std::string& path) { diff --git a/ZAPD/File.h b/ZAPD/File.h index 69aa044..4cb7b56 100644 --- a/ZAPD/File.h +++ b/ZAPD/File.h @@ -5,18 +5,19 @@ #include <stdio.h> #include <string> #include <vector> +#include "Directory.h" #include "StringHelper.h" class File { public: - static bool Exists(const std::string& filePath) + static bool Exists(const fs::path& filePath) { std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate); return file.good(); } - static std::vector<uint8_t> ReadAllBytes(const std::string& filePath) + static std::vector<uint8_t> ReadAllBytes(const fs::path& filePath) { std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate); int32_t fileSize = (int32_t)file.tellg(); @@ -29,7 +30,7 @@ public: return result; }; - static std::string ReadAllText(const std::string& filePath) + static std::string ReadAllText(const fs::path& filePath) { std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate); int32_t fileSize = (int32_t)file.tellg(); @@ -43,7 +44,7 @@ public: return str; }; - static std::vector<std::string> ReadAllLines(const std::string& filePath) + static std::vector<std::string> ReadAllLines(const fs::path& filePath) { std::string text = ReadAllText(filePath); std::vector<std::string> lines = StringHelper::Split(text, "\n"); @@ -51,13 +52,13 @@ public: return lines; }; - static void WriteAllBytes(const std::string& filePath, const std::vector<uint8_t>& data) + static void WriteAllBytes(const fs::path& filePath, const std::vector<uint8_t>& data) { std::ofstream file(filePath, std::ios::binary); file.write((char*)data.data(), data.size()); }; - static void WriteAllText(const std::string& filePath, const std::string& text) + static void WriteAllText(const fs::path& filePath, const std::string& text) { std::ofstream file(filePath, std::ios::out); file.write(text.c_str(), text.size()); diff --git a/ZAPD/Globals.cpp b/ZAPD/Globals.cpp index a8676cc..5de7843 100644 --- a/ZAPD/Globals.cpp +++ b/ZAPD/Globals.cpp @@ -25,6 +25,7 @@ Globals::Globals() useExternalResources = true; lastScene = nullptr; verbosity = VerbosityLevel::VERBOSITY_SILENT; + outputPath = Directory::GetCurrentDirectory(); } std::string Globals::FindSymbolSegRef(int32_t segNumber, uint32_t symbolAddress) @@ -50,7 +51,7 @@ std::string Globals::FindSymbolSegRef(int32_t segNumber, uint32_t symbolAddress) { if (std::string(child->Name()) == "File") { - ZFile* file = new ZFile(fileMode, child, "", "", "", filePath, true); + ZFile* file = new ZFile(fileMode, child, "", "", filePath, true); file->GeneratePlaceholderDeclarations(); segmentRefFiles[segNumber] = file; break; diff --git a/ZAPD/Main.cpp b/ZAPD/Main.cpp index 4728c7a..54ea447 100644 --- a/ZAPD/Main.cpp +++ b/ZAPD/Main.cpp @@ -27,8 +27,7 @@ using namespace tinyxml2; -bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path& outPath, - ZFileMode fileMode); +bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, ZFileMode fileMode); void BuildAssetTexture(const fs::path& pngFilePath, TextureType texType, const fs::path& outPath); void BuildAssetBackground(const fs::path& imageFilePath, const fs::path& outPath); @@ -249,8 +248,8 @@ int main(int argc, char* argv[]) if (fileMode == ZFileMode::Extract || fileMode == ZFileMode::BuildSourceFile) { - bool parseSuccessful = Parse(Globals::Instance->inputPath, Globals::Instance->baseRomPath, - Globals::Instance->outputPath, fileMode); + bool parseSuccessful = + Parse(Globals::Instance->inputPath, Globals::Instance->baseRomPath, fileMode); if (!parseSuccessful) return 1; @@ -292,8 +291,7 @@ int main(int argc, char* argv[]) return 0; } -bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path& outPath, - ZFileMode fileMode) +bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, ZFileMode fileMode) { XMLDocument doc; XMLError eResult = doc.LoadFile(xmlFilePath.string().c_str()); @@ -317,7 +315,7 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path { if (std::string(child->Name()) == "File") { - ZFile* file = new ZFile(fileMode, child, basePath, outPath, "", xmlFilePath, false); + ZFile* file = new ZFile(fileMode, child, basePath, "", xmlFilePath, false); Globals::Instance->files.push_back(file); } else @@ -332,9 +330,9 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path for (ZFile* file : Globals::Instance->files) { if (fileMode == ZFileMode::BuildSourceFile) - file->BuildSourceFile(outPath); + file->BuildSourceFile(); else - file->ExtractResources(outPath); + file->ExtractResources(); } // All done, free files @@ -397,7 +395,7 @@ void BuildAssetModelIntermediette(const fs::path& outPath) void BuildAssetAnimationIntermediette(const fs::path& animPath, const fs::path& outPath) { std::vector<std::string> split = StringHelper::Split(outPath.string(), "/"); - ZFile* file = new ZFile("", split[split.size() - 2]); + ZFile* file = new ZFile(split[split.size() - 2]); HLAnimationIntermediette* anim = HLAnimationIntermediette::FromXML(animPath.string()); ZAnimation* zAnim = anim->ToZAnimation(); zAnim->SetName(Path::GetFileNameWithoutExtension(split[split.size() - 1])); diff --git a/ZAPD/ZArray.cpp b/ZAPD/ZArray.cpp index e904c99..e514960 100644 --- a/ZAPD/ZArray.cpp +++ b/ZAPD/ZArray.cpp @@ -22,7 +22,7 @@ void ZArray::ParseXML(tinyxml2::XMLElement* reader) { ZResource::ParseXML(reader); - arrayCnt = StringHelper::StrToL(registeredAttributes.at("Count").value, 0); + arrayCnt = reader->IntAttribute("Count", 0); // TODO: do a better check. assert(arrayCnt > 0); diff --git a/ZAPD/ZFile.cpp b/ZAPD/ZFile.cpp index 38eba56..9e5bb94 100644 --- a/ZAPD/ZFile.cpp +++ b/ZAPD/ZFile.cpp @@ -31,7 +31,6 @@ ZFile::ZFile() { resources = std::vector<ZResource*>(); basePath = ""; - outputPath = Directory::GetCurrentDirectory(); declarations = std::map<uint32_t, Declaration*>(); defines = ""; baseAddress = 0; @@ -39,15 +38,13 @@ ZFile::ZFile() rangeEnd = 0xFFFFFFFF; } -ZFile::ZFile(const fs::path& nOutPath, std::string nName) : ZFile() +ZFile::ZFile(std::string nName) : ZFile() { - outputPath = nOutPath; name = nName; } ZFile::ZFile(ZFileMode mode, tinyxml2::XMLElement* reader, const fs::path& nBasePath, - const fs::path& nOutPath, std::string filename, const fs::path& nXmlFilePath, - bool placeholderMode) + std::string filename, const fs::path& nXmlFilePath, bool placeholderMode) : ZFile() { xmlFilePath = nXmlFilePath; @@ -56,11 +53,6 @@ ZFile::ZFile(ZFileMode mode, tinyxml2::XMLElement* reader, const fs::path& nBase else basePath = nBasePath; - if (nOutPath == "") - outputPath = Directory::GetCurrentDirectory(); - else - outputPath = nOutPath; - ParseXML(mode, reader, filename, placeholderMode); DeclareResourceSubReferences(); } @@ -85,6 +77,11 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b else name = filename; + outName = name; + const char* outNameXml = reader->Attribute("OutName"); + if (outNameXml != nullptr) + outName = outNameXml; + // TODO: This should be a variable on the ZFile, but it is a large change in order to force all // ZResource types to have a parent ZFile. const char* gameStr = reader->Attribute("Game"); @@ -125,8 +122,6 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b Globals::Instance->AddSegment(segment, this); } - std::string folderName = (basePath / Path::GetFileNameWithoutExtension(name)).string(); - if (mode == ZFileMode::Extract) { if (!File::Exists((basePath / name).string())) @@ -228,14 +223,12 @@ void ZFile::DeclareResourceSubReferences() } } -void ZFile::BuildSourceFile(fs::path outputDir) +void ZFile::BuildSourceFile() { - std::string folderName = Path::GetFileNameWithoutExtension(outputPath.string()); - - if (!Directory::Exists(outputPath.string())) - Directory::CreateDirectory(outputPath.string()); + if (!Directory::Exists(Globals::Instance->outputPath)) + Directory::CreateDirectory(Globals::Instance->outputPath); - GenerateSourceFiles(outputDir); + GenerateSourceFiles(Globals::Instance->outputPath); } std::string ZFile::GetVarName(uint32_t address) @@ -264,28 +257,26 @@ const std::vector<uint8_t>& ZFile::GetRawData() const return rawData; } -void ZFile::ExtractResources(fs::path outputDir) +void ZFile::ExtractResources() { - std::string folderName = Path::GetFileNameWithoutExtension(outputPath.string()); - - if (!Directory::Exists(outputPath.string())) - Directory::CreateDirectory(outputPath.string()); + if (!Directory::Exists(Globals::Instance->outputPath)) + Directory::CreateDirectory(Globals::Instance->outputPath); - if (!Directory::Exists(Globals::Instance->sourceOutputPath.string())) - Directory::CreateDirectory(Globals::Instance->sourceOutputPath.string()); + if (!Directory::Exists(GetSourceOutputFolderPath().string())) + Directory::CreateDirectory(GetSourceOutputFolderPath().string()); for (ZResource* res : resources) res->PreGenSourceFiles(); if (Globals::Instance->genSourceFile) - GenerateSourceFiles(outputDir); + GenerateSourceFiles(Globals::Instance->outputPath); for (ZResource* res : resources) { if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_INFO) printf("Saving resource %s\n", res->GetName().c_str()); - res->Save(outputPath); + res->Save(Globals::Instance->outputPath); } if (Globals::Instance->testMode) @@ -623,9 +614,10 @@ void ZFile::GenerateSourceFiles(fs::path outputDir) sourceOutput += ProcessDeclarations(); - std::string outPath = - (Globals::Instance->sourceOutputPath / (Path::GetFileNameWithoutExtension(name) + ".c")) - .string(); + fs::path outPath = GetSourceOutputFolderPath() / outName.stem().concat(".c"); + + if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_INFO) + printf("Writing C file: %s\n", outPath.c_str()); OutputFormatter formatter; formatter.Write(sourceOutput); @@ -650,10 +642,12 @@ void ZFile::GenerateSourceHeaderFiles() formatter.Write(ProcessExterns()); - fs::path headerFilename = - Globals::Instance->sourceOutputPath / (Path::GetFileNameWithoutExtension(name) + ".h"); + fs::path headerFilename = GetSourceOutputFolderPath() / outName.stem().concat(".h"); + + if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_INFO) + printf("Writing H file: %s\n", headerFilename.c_str()); - File::WriteAllText(headerFilename.string(), formatter.GetOutput()); + File::WriteAllText(headerFilename, formatter.GetOutput()); } void ZFile::GenerateHLIntermediette() @@ -675,7 +669,7 @@ void ZFile::GenerateHLIntermediette() std::string ZFile::GetHeaderInclude() { return StringHelper::Sprintf("#include \"%s\"\n\n", - (Path::GetFileNameWithoutExtension(name) + ".h").c_str()); + (outName.parent_path() / outName.stem().concat(".h")).c_str()); } void ZFile::GeneratePlaceholderDeclarations() @@ -706,6 +700,11 @@ ZTexture* ZFile::GetTextureResource(uint32_t offset) const return nullptr; } +fs::path ZFile::GetSourceOutputFolderPath() const +{ + return Globals::Instance->sourceOutputPath / outName.parent_path(); +} + std::map<std::string, ZResourceFactoryFunc*>* ZFile::GetNodeMap() { static std::map<std::string, ZResourceFactoryFunc*> nodeMap; diff --git a/ZAPD/ZFile.h b/ZAPD/ZFile.h index a3765a0..c8f9291 100644 --- a/ZAPD/ZFile.h +++ b/ZAPD/ZFile.h @@ -36,18 +36,17 @@ public: int32_t segment; uint32_t baseAddress, rangeStart, rangeEnd; - ZFile(const fs::path& nOutPath, std::string nName); + ZFile(std::string nName); ZFile(ZFileMode mode, tinyxml2::XMLElement* reader, const fs::path& nBasePath, - const fs::path& nOutPath, std::string filename, const fs::path& nXmlFilePath, - bool placeholderMode); + std::string filename, const fs::path& nXmlFilePath, bool placeholderMode); ~ZFile(); std::string GetVarName(uint32_t address); std::string GetName() const; const fs::path& GetXmlFilePath() const; const std::vector<uint8_t>& GetRawData() const; - void ExtractResources(fs::path outputDir); - void BuildSourceFile(fs::path outputDir); + void ExtractResources(); + void BuildSourceFile(); void AddResource(ZResource* res); ZResource* FindResource(uint32_t rawDataIndex); std::vector<ZResource*> GetResourcesOfType(ZResourceType resType); @@ -89,14 +88,16 @@ public: void AddTextureResource(uint32_t offset, ZTexture* tex); ZTexture* GetTextureResource(uint32_t offset) const; + fs::path GetSourceOutputFolderPath() const; + static std::map<std::string, ZResourceFactoryFunc*>* GetNodeMap(); static void RegisterNode(std::string nodeName, ZResourceFactoryFunc* nodeFunc); protected: std::vector<uint8_t> rawData; std::string name; + fs::path outName = ""; fs::path basePath; - fs::path outputPath; fs::path xmlFilePath; // Keep track of every texture of this ZFile. // The pointers declared here are "borrowed" (somebody else is the owner), diff --git a/docs/zapd_extraction_xml_reference.md b/docs/zapd_extraction_xml_reference.md index 8767512..8863fb8 100644 --- a/docs/zapd_extraction_xml_reference.md +++ b/docs/zapd_extraction_xml_reference.md @@ -78,6 +78,7 @@ It's worth noting that every tag expects a `Name="gNameOfTheAsset"`. This is wil - Attributes: - `Name`: Required. The name of the file in `baserom/` which will be extracted. + - `OutName`: Optional. The output name of the generated C source file. Defaults to the value passed to `Name`. - `Segment`: Required. This is the segment number of the current file. Expects a decimal number, usually 6 if it is an object, or 128 for overlays (It's kinda a whacky hack to get around of the `0x80` addresses). - `BaseAddress`: Optional. RAM address of the file. Expects a hex number (with `0x` prefix). Default value: `0`. - `RangeStart`: Optional. File offset where the extraction will begin. Hex. Default value: `0x000000000`. |
