From fc59a09d02c787a0048b1dca258b233e7afb5cb8 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 6 Mar 2021 12:09:00 +0100 Subject: [PATCH] Make unlinker open a gdt file if not raw specified --- .../Dumping/AssetDumpingContext.cpp | 6 ++ src/ObjWriting/Dumping/AssetDumpingContext.h | 4 + src/Unlinker/Unlinker.cpp | 37 +++++++- src/Unlinker/UnlinkerArgs.cpp | 90 +++++++++++-------- src/Unlinker/UnlinkerArgs.h | 2 + 5 files changed, 99 insertions(+), 40 deletions(-) diff --git a/src/ObjWriting/Dumping/AssetDumpingContext.cpp b/src/ObjWriting/Dumping/AssetDumpingContext.cpp index e69de29b..723ffcbb 100644 --- a/src/ObjWriting/Dumping/AssetDumpingContext.cpp +++ b/src/ObjWriting/Dumping/AssetDumpingContext.cpp @@ -0,0 +1,6 @@ +#include "AssetDumpingContext.h" + +AssetDumpingContext::AssetDumpingContext() + : m_zone(nullptr) +{ +} diff --git a/src/ObjWriting/Dumping/AssetDumpingContext.h b/src/ObjWriting/Dumping/AssetDumpingContext.h index f8af7d72..27c5bd48 100644 --- a/src/ObjWriting/Dumping/AssetDumpingContext.h +++ b/src/ObjWriting/Dumping/AssetDumpingContext.h @@ -2,6 +2,7 @@ #include +#include "Obj/Gdt/GdtStream.h" #include "Zone/Zone.h" class AssetDumpingContext @@ -9,4 +10,7 @@ class AssetDumpingContext public: Zone* m_zone; std::string m_base_path; + std::unique_ptr m_gdt; + + AssetDumpingContext(); }; diff --git a/src/Unlinker/Unlinker.cpp b/src/Unlinker/Unlinker.cpp index 42d747e7..d2d55bd6 100644 --- a/src/Unlinker/Unlinker.cpp +++ b/src/Unlinker/Unlinker.cpp @@ -183,6 +183,22 @@ class Unlinker::Impl return result; } + static bool OpenGdtFile(Zone* zone, const fs::path& zoneDefinitionFileFolder, std::ofstream& stream) + { + auto gdtFilePath(zoneDefinitionFileFolder); + gdtFilePath.append(zone->m_name); + gdtFilePath.replace_extension(".gdt"); + + stream = std::ofstream(gdtFilePath, std::fstream::out | std::fstream::binary); + if (!stream.is_open()) + { + printf("Failed to open file for zone definition file of zone \"%s\".\n", zone->m_name.c_str()); + return false; + } + + return true; + } + /** * \brief Performs the tasks specified by the command line arguments on the specified zone. * \param zone The zone to handle. @@ -204,12 +220,31 @@ class Unlinker::Impl zoneDefinitionFileFolder.append("zone_source"); fs::create_directories(zoneDefinitionFileFolder); - WriteZoneDefinitionFile(zone, zoneDefinitionFileFolder); + if (!WriteZoneDefinitionFile(zone, zoneDefinitionFileFolder)) + return false; + std::ofstream gdtStream; AssetDumpingContext context; context.m_zone = zone; context.m_base_path = outputFolderPath; + + if(!m_args.m_raw) + { + if (!OpenGdtFile(zone, zoneDefinitionFileFolder, gdtStream)) + return false; + auto gdt = std::make_unique(gdtStream); + gdt->BeginStream(); + gdt->WriteVersion(GdtVersion(zone->m_game->GetName(), 1)); + context.m_gdt = std::move(gdt); + } + ObjWriting::DumpZone(context); + + if(!m_args.m_raw) + { + context.m_gdt->EndStream(); + gdtStream.close(); + } } return true; diff --git a/src/Unlinker/UnlinkerArgs.cpp b/src/Unlinker/UnlinkerArgs.cpp index 395da9b4..f2376ab5 100644 --- a/src/Unlinker/UnlinkerArgs.cpp +++ b/src/Unlinker/UnlinkerArgs.cpp @@ -7,54 +7,62 @@ #include "ObjLoading.h" #include "ObjWriting.h" -const CommandLineOption* const OPTION_HELP = CommandLineOption::Builder::Create() - .WithShortName("?") - .WithLongName("help") - .WithDescription("Displays usage information.") - .Build(); - -const CommandLineOption* const OPTION_VERBOSE = CommandLineOption::Builder::Create() -.WithShortName("v") -.WithLongName("verbose") -.WithDescription("Outputs a lot more and more detailed messages.") -.Build(); - -const CommandLineOption* const OPTION_MINIMAL_ZONE_FILE = CommandLineOption::Builder::Create() -.WithShortName("min") -.WithLongName("minimal-zone") -.WithDescription( - "Minimizes the size of the zone file output by only including assets that are not a dependency of another asset.") +const CommandLineOption* const OPTION_HELP = + CommandLineOption::Builder::Create() + .WithShortName("?") + .WithLongName("help") + .WithDescription("Displays usage information.") .Build(); -const CommandLineOption* const OPTION_LIST = CommandLineOption::Builder::Create() -.WithShortName("l") -.WithLongName("list") -.WithDescription( - "Lists the contents of a zone instead of writing them to the disk.") +const CommandLineOption* const OPTION_VERBOSE = + CommandLineOption::Builder::Create() + .WithShortName("v") + .WithLongName("verbose") + .WithDescription("Outputs a lot more and more detailed messages.") .Build(); -const CommandLineOption* const OPTION_OUTPUT_FOLDER = CommandLineOption::Builder::Create() -.WithShortName("o") -.WithLongName("output-folder") -.WithDescription( - "Specifies the output folder containing the contents of the unlinked zones. Defaults to ./%zoneName%") +const CommandLineOption* const OPTION_MINIMAL_ZONE_FILE = + CommandLineOption::Builder::Create() + .WithShortName("min") + .WithLongName("minimal-zone") + .WithDescription("Minimizes the size of the zone file output by only including assets that are not a dependency of another asset.") + .Build(); + +const CommandLineOption* const OPTION_LIST = + CommandLineOption::Builder::Create() + .WithShortName("l") + .WithLongName("list") + .WithDescription("Lists the contents of a zone instead of writing them to the disk.") + .Build(); + +const CommandLineOption* const OPTION_OUTPUT_FOLDER = + CommandLineOption::Builder::Create() + .WithShortName("o") + .WithLongName("output-folder") + .WithDescription("Specifies the output folder containing the contents of the unlinked zones. Defaults to ./%zoneName%") .WithParameter("outputFolderPath") .Build(); -const CommandLineOption* const OPTION_SEARCH_PATH = CommandLineOption::Builder::Create() -.WithLongName("search-path") -.WithDescription( - "Specifies a semi-colon separated list of paths to search for additional game files.") +const CommandLineOption* const OPTION_SEARCH_PATH = + CommandLineOption::Builder::Create() + .WithLongName("search-path") + .WithDescription("Specifies a semi-colon separated list of paths to search for additional game files.") .WithParameter("searchPathString") .Build(); -const CommandLineOption* const OPTION_IMAGE_FORMAT = CommandLineOption::Builder::Create() -.WithLongName("image-format") -.WithDescription( - "Specifies the format of dumped image files. Valid values are: DDS, IWI") +const CommandLineOption* const OPTION_IMAGE_FORMAT = + CommandLineOption::Builder::Create() + .WithLongName("image-format") + .WithDescription("Specifies the format of dumped image files. Valid values are: DDS, IWI") .WithParameter("imageFormatValue") .Build(); +const CommandLineOption* const OPTION_RAW = + CommandLineOption::Builder::Create() + .WithLongName("raw") + .WithDescription("Prevents generation of a GDT and dumps assets as raw whenever possible.") + .Build(); + const CommandLineOption* const COMMAND_LINE_OPTIONS[] { OPTION_HELP, @@ -63,7 +71,8 @@ const CommandLineOption* const COMMAND_LINE_OPTIONS[] OPTION_LIST, OPTION_OUTPUT_FOLDER, OPTION_SEARCH_PATH, - OPTION_IMAGE_FORMAT + OPTION_IMAGE_FORMAT, + OPTION_RAW }; UnlinkerArgs::UnlinkerArgs() @@ -163,13 +172,13 @@ bool UnlinkerArgs::SetImageDumpingMode() for (auto& c : specifiedValue) c = tolower(c); - if(specifiedValue == "dds") + if (specifiedValue == "dds") { ObjWriting::Configuration.ImageOutputFormat = ObjWriting::Configuration_t::ImageOutputFormat_e::DDS; return true; } - if(specifiedValue == "iwi") + if (specifiedValue == "iwi") { ObjWriting::Configuration.ImageOutputFormat = ObjWriting::Configuration_t::ImageOutputFormat_e::IWI; return true; @@ -229,7 +238,7 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv) } // --image-format - if(m_argument_parser.IsOptionSpecified(OPTION_IMAGE_FORMAT)) + if (m_argument_parser.IsOptionSpecified(OPTION_IMAGE_FORMAT)) { if (!SetImageDumpingMode()) { @@ -237,6 +246,9 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv) } } + // --raw + m_raw = m_argument_parser.IsOptionSpecified(OPTION_RAW); + return true; } diff --git a/src/Unlinker/UnlinkerArgs.h b/src/Unlinker/UnlinkerArgs.h index 6e6a17ac..647bab8a 100644 --- a/src/Unlinker/UnlinkerArgs.h +++ b/src/Unlinker/UnlinkerArgs.h @@ -39,6 +39,8 @@ public: std::string m_output_folder; bool m_minimal_zone_def; + bool m_raw; + bool m_verbose; UnlinkerArgs();