diff options
Diffstat (limited to 'ZAPD/Main.cpp')
| -rw-r--r-- | ZAPD/Main.cpp | 302 |
1 files changed, 258 insertions, 44 deletions
diff --git a/ZAPD/Main.cpp b/ZAPD/Main.cpp index 2563154..a2030aa 100644 --- a/ZAPD/Main.cpp +++ b/ZAPD/Main.cpp @@ -1,11 +1,73 @@ #include "Globals.h" #include "Utils/Directory.h" -#include "Utils/File.h" +#include <Utils/DiskFile.h> #include "Utils/Path.h" #include "WarningHandler.h" + #include "ZAnimation.h" +ZNormalAnimation nAnim(nullptr); +ZCurveAnimation cAnim(nullptr); +ZLinkAnimation lAnim(nullptr); +ZLegacyAnimation lAnim2(nullptr); + +#include "ZArray.h" +ZArray arr(nullptr); + +#include "ZAudio.h" +ZAudio audio(nullptr); + #include "ZBackground.h" +ZBackground back(nullptr); + #include "ZBlob.h" +ZBlob blob(nullptr); + +#include "ZCollision.h" +ZCollisionHeader colHeader(nullptr); + +#include "ZCutscene.h" +ZCutscene cs(nullptr); + +#include "ZLimb.h" +ZLimb limb(nullptr); + +#include "ZMtx.h" +ZMtx mtx(nullptr); + +#include "ZPath.h" +ZPath path(nullptr); + +#include "ZPlayerAnimationData.h" +ZPlayerAnimationData pAnimData(nullptr); + +#include "ZScalar.h" +ZScalar scalar(nullptr); + +#include "ZSkeleton.h" +ZLimbTable limbTbl(nullptr); +ZSkeleton skel(nullptr); + +#include "ZString.h" +ZString str(nullptr); + +#include "ZSymbol.h" +ZSymbol sym(nullptr); + +#include "ZText.h" +ZText txt(nullptr); + +#include "ZTexture.h" +ZTexture tex(nullptr); + +#include "ZVector.h" +ZVector vec(nullptr); + +#include "ZVtx.h" +ZVtx vtx(nullptr); + +#include "ZRoom/ZRoom.h" +ZRoom room(nullptr); + #include "ZFile.h" #include "ZTexture.h" @@ -14,17 +76,24 @@ #include <string> #include <string_view> #include "tinyxml2.h" +#include <ctpl_stl.h> + +//extern const char gBuildHash[]; +const char gBuildHash[] = ""; bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path& outPath, - ZFileMode fileMode); + ZFileMode fileMode, int workerID); void BuildAssetTexture(const fs::path& pngFilePath, TextureType texType, const fs::path& outPath); void BuildAssetBackground(const fs::path& imageFilePath, const fs::path& outPath); void BuildAssetBlob(const fs::path& blobFilePath, const fs::path& outPath); +int ExtractFunc(int workerID, int fileListSize, std::string fileListItem, ZFileMode fileMode); + +volatile int numWorkersLeft = 0; -extern const char gBuildHash[]; +extern void ImportExporters(); -int main(int argc, char* argv[]) +extern "C" int zapd_main(int argc, char* argv[]) { // Syntax: ZAPD.out [mode (btex/bovl/e)] (Arbritrary Number of Arguments) @@ -110,6 +179,10 @@ int main(int argc, char* argv[]) { Globals::Instance->texType = ZTexture::GetTextureTypeFromString(argv[++i]); } + else if (arg == "-fl") // Set baserom filelist path + { + Globals::Instance->fileListPath = argv[++i]; + } else if (arg == "-rconf") // Read Config File { Globals::Instance->cfg.ReadConfigFile(argv[++i]); @@ -128,6 +201,7 @@ int main(int argc, char* argv[]) } else if (arg == "-se" || arg == "--set-exporter") // Set Current Exporter { + ImportExporters(); Globals::Instance->currentExporter = argv[++i]; } else if (arg == "--gcc-compat") // GCC compatibility @@ -142,10 +216,27 @@ int main(int argc, char* argv[]) { Globals::Instance->forceUnaccountedStatic = true; } + else if (arg == "-brt" || arg == "--build-raw-texture") + { + Globals::Instance->buildRawTexture = true; + } + else if (arg == "--norom") + { + Globals::Instance->onlyGenSohOtr = true; + } } + // Parse File Mode ExporterSet* exporterSet = Globals::Instance->GetExporterSet(); + + if(Globals::Instance->onlyGenSohOtr) { + exporterSet->endProgramFunc(); + + delete g; + return 0; + } + std::string buildMode = argv[1]; ZFileMode fileMode = ZFileMode::Invalid; @@ -159,6 +250,8 @@ int main(int argc, char* argv[]) fileMode = ZFileMode::BuildBlob; else if (buildMode == "e") fileMode = ZFileMode::Extract; + else if (buildMode == "ed") + fileMode = ZFileMode::ExtractDirectory; else if (exporterSet != nullptr && exporterSet->parseFileModeFunc != nullptr) exporterSet->parseFileModeFunc(buildMode, fileMode); @@ -168,6 +261,11 @@ int main(int argc, char* argv[]) return 1; } + Globals::Instance->fileMode = fileMode; + + if (fileMode == ZFileMode::ExtractDirectory) + Globals::Instance->rom = new ZRom(Globals::Instance->baseRomPath.string()); + // We've parsed through our commands once. If an exporter exists, it's been set by now. // Now we'll parse through them again but pass them on to our exporter if one is available. @@ -186,7 +284,7 @@ int main(int argc, char* argv[]) } // TODO: switch - if (fileMode == ZFileMode::Extract || fileMode == ZFileMode::BuildSourceFile) + if (fileMode == ZFileMode::Extract || fileMode == ZFileMode::BuildSourceFile || fileMode == ZFileMode::ExtractDirectory) { bool procFileModeSuccess = false; @@ -195,30 +293,85 @@ int main(int argc, char* argv[]) if (!procFileModeSuccess) { - bool parseSuccessful; - - for (auto& extFile : Globals::Instance->cfg.externalFiles) + if (fileMode == ZFileMode::ExtractDirectory) { - fs::path externalXmlFilePath = - Globals::Instance->cfg.externalXmlFolder / extFile.xmlPath; + std::vector<std::string> fileList = + Directory::ListFiles(Globals::Instance->inputPath.string()); - if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_INFO) + const int num_threads = std::thread::hardware_concurrency(); + ctpl::thread_pool pool(num_threads > 1 ? num_threads / 2 : 1); + + bool parseSuccessful; + + auto start = std::chrono::steady_clock::now(); + int fileListSize = fileList.size(); + Globals::Instance->singleThreaded = false; + + for (int i = 0; i < fileListSize; i++) + Globals::Instance->workerData[i] = new FileWorker(); + + numWorkersLeft = fileListSize; + + for (int i = 0; i < fileListSize; i++) + { + if (Globals::Instance->singleThreaded) + { + ExtractFunc(i, fileList.size(), fileList[i], fileMode); + } + else + { + std::string fileListItem = fileList[i]; + pool.push([i, fileListSize, fileListItem, fileMode](int) { + ExtractFunc(i, fileListSize, fileListItem, fileMode); + }); + } + } + + if (!Globals::Instance->singleThreaded) { - printf("Parsing external file from config: '%s'\n", - externalXmlFilePath.c_str()); + while (true) + { + if (numWorkersLeft <= 0) + break; + + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + } } - parseSuccessful = Parse(externalXmlFilePath, Globals::Instance->baseRomPath, - extFile.outPath, ZFileMode::ExternalFile); + auto end = std::chrono::steady_clock::now(); + auto diff = + std::chrono::duration_cast<std::chrono::seconds>(end - start).count(); + + printf("Generated OTR File Data in %i seconds\n", diff); + } + else + { + bool parseSuccessful; + for (auto& extFile : Globals::Instance->cfg.externalFiles) + { + fs::path externalXmlFilePath = + Globals::Instance->cfg.externalXmlFolder / extFile.xmlPath; + + if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_INFO) + { + printf("Parsing external file from config: '%s'\n", + externalXmlFilePath.c_str()); + } + + parseSuccessful = Parse(externalXmlFilePath, Globals::Instance->baseRomPath, + extFile.outPath, ZFileMode::ExternalFile, 0); + + if (!parseSuccessful) + return 1; + } + + parseSuccessful = + Parse(Globals::Instance->inputPath, Globals::Instance->baseRomPath, + Globals::Instance->outputPath, fileMode, 0); if (!parseSuccessful) return 1; } - - parseSuccessful = Parse(Globals::Instance->inputPath, Globals::Instance->baseRomPath, - Globals::Instance->outputPath, fileMode); - if (!parseSuccessful) - return 1; } } else if (fileMode == ZFileMode::BuildTexture) @@ -235,12 +388,81 @@ int main(int argc, char* argv[]) BuildAssetBlob(Globals::Instance->inputPath, Globals::Instance->outputPath); } + if (exporterSet != nullptr && exporterSet->endProgramFunc != nullptr) + exporterSet->endProgramFunc(); + +end: + delete exporterSet; + + //Globals::Instance->GetExporterSet() = nullptr; //TODO NULL this out. Compiler complains about lvalue assignment. + delete g; return 0; } +int ExtractFunc(int workerID, int fileListSize, std::string fileListItem, ZFileMode fileMode) +{ + bool parseSuccessful; + + printf("(%i / %i): %s\n", (workerID + 1), fileListSize, fileListItem.c_str()); + + for (auto& extFile : Globals::Instance->cfg.externalFiles) + { + fs::path externalXmlFilePath = Globals::Instance->cfg.externalXmlFolder / extFile.xmlPath; + + if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_INFO) + { + printf("Parsing external file from config: '%s'\n", externalXmlFilePath.c_str()); + } + + parseSuccessful = Parse(externalXmlFilePath, Globals::Instance->baseRomPath, + extFile.outPath, ZFileMode::ExternalFile, workerID); + + if (!parseSuccessful) + return 1; + } + + parseSuccessful = Parse(fileListItem, Globals::Instance->baseRomPath, + Globals::Instance->outputPath, fileMode, workerID); + + if (!parseSuccessful) + return 1; + + if (Globals::Instance->singleThreaded) + { + for (int i = 0; i < Globals::Instance->files.size(); i++) + { + delete Globals::Instance->files[i]; + Globals::Instance->files.erase(Globals::Instance->files.begin() + i); + i--; + } + + Globals::Instance->externalFiles.clear(); + Globals::Instance->segments.clear(); + Globals::Instance->cfg.segmentRefFiles.clear(); + } + else + { + for (int i = 0; i < Globals::Instance->workerData[workerID]->files.size(); i++) + { + delete Globals::Instance->workerData[workerID]->files[i]; + Globals::Instance->workerData[workerID]->files.erase( + Globals::Instance->workerData[workerID]->files.begin() + + i); + i--; + } + + Globals::Instance->workerData[workerID]->externalFiles.clear(); + Globals::Instance->workerData[workerID]->segments.clear(); + Globals::Instance->workerData[workerID]->segmentRefFiles.clear(); + + numWorkersLeft--; + } + return 0; +} + bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path& outPath, - ZFileMode fileMode) + ZFileMode fileMode, int workerID) { tinyxml2::XMLDocument doc; tinyxml2::XMLError eResult = doc.LoadFile(xmlFilePath.string().c_str()); @@ -268,11 +490,11 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path { if (std::string_view(child->Name()) == "File") { - ZFile* file = new ZFile(fileMode, child, basePath, outPath, "", xmlFilePath); - Globals::Instance->files.push_back(file); + ZFile* file = new ZFile(fileMode, child, basePath, outPath, "", xmlFilePath, workerID); + Globals::Instance->AddFile(file, workerID); if (fileMode == ZFileMode::ExternalFile) { - Globals::Instance->externalFiles.push_back(file); + Globals::Instance->AddExternalFile(file, workerID); file->isExternalFile = true; } } @@ -305,7 +527,7 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path } // Recursion. What can go wrong? - Parse(externalXmlFilePath, basePath, externalOutFilePath, ZFileMode::ExternalFile); + Parse(externalXmlFilePath, basePath, externalOutFilePath, ZFileMode::ExternalFile, workerID); } else { @@ -324,7 +546,14 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path if (exporterSet != nullptr && exporterSet->beginXMLFunc != nullptr) exporterSet->beginXMLFunc(); - for (ZFile* file : Globals::Instance->files) + std::vector<ZFile*> files; + + if (Globals::Instance->singleThreaded) + files = Globals::Instance->files; + else + files = Globals::Instance->workerData[workerID]->files; + + for (ZFile* file : files) { if (fileMode == ZFileMode::BuildSourceFile) file->BuildSourceFile(); @@ -341,22 +570,7 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path void BuildAssetTexture(const fs::path& pngFilePath, TextureType texType, const fs::path& outPath) { - std::string name = outPath.stem().string(); - - ZTexture tex(nullptr); - - if (name.find("u32") != std::string::npos) - tex.dWordAligned = false; - - tex.FromPNG(pngFilePath.string(), texType); - std::string cfgPath = StringHelper::Split(pngFilePath.string(), ".")[0] + ".cfg"; - - if (File::Exists(cfgPath)) - name = File::ReadAllText(cfgPath); - - std::string src = tex.GetBodySourceCode(); - - File::WriteAllText(outPath.string(), src); + return Globals::Instance->BuildAssetTexture(pngFilePath, texType, outPath); } void BuildAssetBackground(const fs::path& imageFilePath, const fs::path& outPath) @@ -364,7 +578,7 @@ void BuildAssetBackground(const fs::path& imageFilePath, const fs::path& outPath ZBackground background(nullptr); background.ParseBinaryFile(imageFilePath.string(), false); - File::WriteAllText(outPath.string(), background.GetBodySourceCode()); + DiskFile::WriteAllText(outPath.string(), background.GetBodySourceCode()); } void BuildAssetBlob(const fs::path& blobFilePath, const fs::path& outPath) @@ -374,7 +588,7 @@ void BuildAssetBlob(const fs::path& blobFilePath, const fs::path& outPath) std::string src = blob->GetBodySourceCode(); - File::WriteAllText(outPath.string(), src); + DiskFile::WriteAllText(outPath.string(), src); delete blob; } |
