From 7919683748ce567a93db43f99fa7cf3af54c7102 Mon Sep 17 00:00:00 2001 From: Jan Date: Tue, 23 Jan 2024 21:53:01 +0100 Subject: [PATCH 1/3] chore: write header with git version --- premake5.lua | 8 ++++++++ tools/scripts/version.lua | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tools/scripts/version.lua diff --git a/premake5.lua b/premake5.lua index 0152b930..6d9b19f7 100644 --- a/premake5.lua +++ b/premake5.lua @@ -3,6 +3,7 @@ include "tools/scripts/including.lua" include "tools/scripts/linking.lua" include "tools/scripts/options.lua" include "tools/scripts/platform.lua" +include "tools/scripts/version.lua" -- ================== -- Workspace @@ -63,6 +64,13 @@ workspace "OpenAssetTools" "__STDC_WANT_LIB_EXT1__=1", "_CRT_SECURE_NO_WARNINGS" } + + -- Write the current version to a header + -- This is better than adding it as macro here since changing a global macro would cause a full rebuild + WriteVersionHeader() + includedirs { + GetVersionHeaderFolder() + } filter "options:debug-structureddatadef" defines { "STRUCTUREDDATADEF_DEBUG" } diff --git a/tools/scripts/version.lua b/tools/scripts/version.lua new file mode 100644 index 00000000..dc0087f9 --- /dev/null +++ b/tools/scripts/version.lua @@ -0,0 +1,36 @@ +local BuildSubFolderFolder = "premake" +local HeaderFileName = "GitVersion.h" + +function GetGitVersion() + result, errorCode = os.outputof("git describe --tags") + + if errorCode == 0 then + return result + end + + return "Unknown" +end + +function GetVersionHeaderFolder() + return path.join(BuildFolder(), BuildSubFolderFolder) +end + +function WriteVersionHeader() + local folder = GetVersionHeaderFolder() + local file = path.join(folder, HeaderFileName) + local content = string.format([[ +#pragma once + +#define GIT_VERSION "%s" + ]], GetGitVersion()) + + if os.isdir(folder) ~= True then + os.mkdir(folder) + end + + local ok, err = os.writefile_ifnotequal(content, file) + + if ok == -1 then + error("Could not create version file: " .. err) + end +end From 05437cfd7df8eca78668507872a75b3537817876 Mon Sep 17 00:00:00 2001 From: Jan Date: Tue, 23 Jan 2024 23:14:09 +0100 Subject: [PATCH 2/3] feat: add version command line arg to all executables --- src/Linker/Linker.cpp | 6 +++- src/Linker/LinkerArgs.cpp | 27 ++++++++++++++-- src/Linker/LinkerArgs.h | 3 +- src/RawTemplater/RawTemplater.cpp | 6 +++- src/RawTemplater/RawTemplaterArguments.cpp | 32 +++++++++++++++++-- src/RawTemplater/RawTemplaterArguments.h | 3 +- src/Unlinker/Unlinker.cpp | 6 +++- src/Unlinker/UnlinkerArgs.cpp | 25 ++++++++++++++- src/Unlinker/UnlinkerArgs.h | 3 +- .../ZoneCodeGenerator.cpp | 6 +++- .../ZoneCodeGeneratorArguments.cpp | 32 +++++++++++++++++-- .../ZoneCodeGeneratorArguments.h | 4 +-- 12 files changed, 136 insertions(+), 17 deletions(-) diff --git a/src/Linker/Linker.cpp b/src/Linker/Linker.cpp index c6d2940e..6c05aa65 100644 --- a/src/Linker/Linker.cpp +++ b/src/Linker/Linker.cpp @@ -613,9 +613,13 @@ public: bool Start(const int argc, const char** argv) override { - if (!m_args.ParseArgs(argc, argv)) + auto shouldContinue = true; + if (!m_args.ParseArgs(argc, argv, shouldContinue)) return false; + if (!shouldContinue) + return true; + if (!m_search_paths.BuildProjectIndependentSearchPaths()) return false; diff --git a/src/Linker/LinkerArgs.cpp b/src/Linker/LinkerArgs.cpp index a9b2d318..90320567 100644 --- a/src/Linker/LinkerArgs.cpp +++ b/src/Linker/LinkerArgs.cpp @@ -1,11 +1,13 @@ #include "LinkerArgs.h" +#include "GitVersion.h" #include "ObjLoading.h" #include "ObjWriting.h" #include "Utils/Arguments/UsageInformation.h" #include "Utils/FileUtils.h" #include +#include #include #include @@ -19,6 +21,12 @@ const CommandLineOption* const OPTION_HELP = .WithDescription("Displays usage information.") .Build(); +const CommandLineOption* const OPTION_VERSION = + CommandLineOption::Builder::Create() + .WithLongName("version") + .WithDescription("Prints the application version.") + .Build(); + const CommandLineOption* const OPTION_VERBOSE = CommandLineOption::Builder::Create() .WithShortName("v") @@ -88,6 +96,7 @@ const CommandLineOption* const OPTION_MENU_NO_OPTIMIZATION = const CommandLineOption* const COMMAND_LINE_OPTIONS[]{ OPTION_HELP, + OPTION_VERSION, OPTION_VERBOSE, OPTION_BASE_FOLDER, OPTION_OUTPUT_FOLDER, @@ -100,7 +109,7 @@ const CommandLineOption* const COMMAND_LINE_OPTIONS[]{ }; LinkerArgs::LinkerArgs() - : m_argument_parser(COMMAND_LINE_OPTIONS, std::extent::value), + : m_argument_parser(COMMAND_LINE_OPTIONS, std::extent_v), m_base_pattern(R"(\?base\?)"), m_game_pattern(R"(\?game\?)"), m_project_pattern(R"(\?project\?)"), @@ -125,6 +134,11 @@ void LinkerArgs::PrintUsage() usage.Print(); } +void LinkerArgs::PrintVersion() +{ + std::cout << "OpenAssetTools Linker " << std::string(GIT_VERSION) << "\n"; +} + void LinkerArgs::SetVerbose(const bool isVerbose) { m_verbose = isVerbose; @@ -198,8 +212,9 @@ std::set LinkerArgs::GetSearchPathsForProject(const std::set +#include + const CommandLineOption* const OPTION_HELP = CommandLineOption::Builder::Create().WithShortName("?").WithLongName("help").WithDescription("Displays usage information.").Build(); +const CommandLineOption* const OPTION_VERSION = + CommandLineOption::Builder::Create().WithLongName("version").WithDescription("Prints the application version.").Build(); + const CommandLineOption* const OPTION_VERBOSE = CommandLineOption::Builder::Create().WithShortName("v").WithLongName("verbose").WithDescription("Outputs a lot more and more detailed messages.").Build(); @@ -30,7 +37,14 @@ const CommandLineOption* const OPTION_DEFINE = CommandLineOption::Builder::Creat .Reusable() .Build(); -const CommandLineOption* const COMMAND_LINE_OPTIONS[]{OPTION_HELP, OPTION_VERBOSE, OPTION_OUTPUT_FOLDER, OPTION_BUILD_LOG, OPTION_DEFINE}; +const CommandLineOption* const COMMAND_LINE_OPTIONS[]{ + OPTION_HELP, + OPTION_VERSION, + OPTION_VERBOSE, + OPTION_OUTPUT_FOLDER, + OPTION_BUILD_LOG, + OPTION_DEFINE, +}; RawTemplaterArguments::RawTemplaterArguments() : m_argument_parser(COMMAND_LINE_OPTIONS, std::extent_v), @@ -50,8 +64,14 @@ void RawTemplaterArguments::PrintUsage() usage.Print(); } -bool RawTemplaterArguments::Parse(const int argc, const char** argv) +void RawTemplaterArguments::PrintVersion() { + std::cout << "OpenAssetTools RawTemplater " << std::string(GIT_VERSION) << "\n"; +} + +bool RawTemplaterArguments::ParseArgs(const int argc, const char** argv, bool& shouldContinue) +{ + shouldContinue = true; if (!m_argument_parser.ParseArguments(argc - 1, &argv[1])) { PrintUsage(); @@ -65,6 +85,14 @@ bool RawTemplaterArguments::Parse(const int argc, const char** argv) return false; } + // Check if the user wants to see the version + if (m_argument_parser.IsOptionSpecified(OPTION_VERSION)) + { + PrintVersion(); + shouldContinue = false; + return true; + } + m_input_files = m_argument_parser.GetArguments(); if (m_input_files.empty()) { diff --git a/src/RawTemplater/RawTemplaterArguments.h b/src/RawTemplater/RawTemplaterArguments.h index 463ccbc6..dadb1f88 100644 --- a/src/RawTemplater/RawTemplaterArguments.h +++ b/src/RawTemplater/RawTemplaterArguments.h @@ -14,6 +14,7 @@ class RawTemplaterArguments * \brief Prints a command line usage help text for the RawTemplater tool to stdout. */ static void PrintUsage(); + static void PrintVersion(); public: bool m_verbose; @@ -27,5 +28,5 @@ public: RawTemplaterArguments(); - bool Parse(int argc, const char** argv); + bool ParseArgs(int argc, const char** argv, bool& shouldContinue); }; diff --git a/src/Unlinker/Unlinker.cpp b/src/Unlinker/Unlinker.cpp index 2abcf354..f26cc12d 100644 --- a/src/Unlinker/Unlinker.cpp +++ b/src/Unlinker/Unlinker.cpp @@ -429,9 +429,13 @@ public: */ bool Start(const int argc, const char** argv) { - if (!m_args.ParseArgs(argc, argv)) + auto shouldContinue = true; + if (!m_args.ParseArgs(argc, argv, shouldContinue)) return false; + if (!shouldContinue) + return true; + if (!BuildSearchPaths()) return false; diff --git a/src/Unlinker/UnlinkerArgs.cpp b/src/Unlinker/UnlinkerArgs.cpp index 5c7bfaa0..79c1a181 100644 --- a/src/Unlinker/UnlinkerArgs.cpp +++ b/src/Unlinker/UnlinkerArgs.cpp @@ -1,10 +1,12 @@ #include "UnlinkerArgs.h" +#include "GitVersion.h" #include "ObjLoading.h" #include "ObjWriting.h" #include "Utils/Arguments/UsageInformation.h" #include "Utils/FileUtils.h" +#include #include #include @@ -16,6 +18,12 @@ const CommandLineOption* const OPTION_HELP = .WithDescription("Displays usage information.") .Build(); +const CommandLineOption* const OPTION_VERSION = + CommandLineOption::Builder::Create() + .WithLongName("version") + .WithDescription("Prints the application version.") + .Build(); + const CommandLineOption* const OPTION_VERBOSE = CommandLineOption::Builder::Create() .WithShortName("v") @@ -113,6 +121,7 @@ const CommandLineOption* const OPTION_LEGACY_MENUS = const CommandLineOption* const COMMAND_LINE_OPTIONS[]{ OPTION_HELP, + OPTION_VERSION, OPTION_VERBOSE, OPTION_MINIMAL_ZONE_FILE, OPTION_LOAD, @@ -155,6 +164,11 @@ void UnlinkerArgs::PrintUsage() usage.Print(); } +void UnlinkerArgs::PrintVersion() +{ + std::cout << "OpenAssetTools Unlinker " << std::string(GIT_VERSION) << "\n"; +} + void UnlinkerArgs::SetVerbose(const bool isVerbose) { m_verbose = isVerbose; @@ -237,8 +251,9 @@ void UnlinkerArgs::ParseCommaSeparatedAssetTypeString(const std::string& input) AddSpecifiedAssetType(std::string(lowerInput, currentPos, lowerInput.size() - currentPos)); } -bool UnlinkerArgs::ParseArgs(const int argc, const char** argv) +bool UnlinkerArgs::ParseArgs(const int argc, const char** argv, bool& shouldContinue) { + shouldContinue = true; if (!m_argument_parser.ParseArguments(argc - 1, &argv[1])) { PrintUsage(); @@ -252,6 +267,14 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv) return false; } + // Check if the user wants to see the version + if (m_argument_parser.IsOptionSpecified(OPTION_VERSION)) + { + PrintVersion(); + shouldContinue = false; + return true; + } + m_zones_to_unlink = m_argument_parser.GetArguments(); const size_t zoneCount = m_zones_to_unlink.size(); if (zoneCount < 1) diff --git a/src/Unlinker/UnlinkerArgs.h b/src/Unlinker/UnlinkerArgs.h index d26dca94..35872566 100644 --- a/src/Unlinker/UnlinkerArgs.h +++ b/src/Unlinker/UnlinkerArgs.h @@ -21,6 +21,7 @@ private: * \brief Prints a command line usage help text for the Unlinker tool to stdout. */ static void PrintUsage(); + static void PrintVersion(); void SetVerbose(bool isVerbose); bool SetImageDumpingMode(); @@ -60,7 +61,7 @@ public: bool m_verbose; UnlinkerArgs(); - bool ParseArgs(int argc, const char** argv); + bool ParseArgs(int argc, const char** argv, bool& shouldContinue); /** * \brief Converts the output path specified by command line arguments to a path applies for the specified zone. diff --git a/src/ZoneCodeGeneratorLib/ZoneCodeGenerator.cpp b/src/ZoneCodeGeneratorLib/ZoneCodeGenerator.cpp index b6b7a594..cdfcc209 100644 --- a/src/ZoneCodeGeneratorLib/ZoneCodeGenerator.cpp +++ b/src/ZoneCodeGeneratorLib/ZoneCodeGenerator.cpp @@ -64,9 +64,13 @@ public: int Run(const int argc, const char** argv) { - if (!m_args.Parse(argc, argv)) + auto shouldContinue = true; + if (!m_args.ParseArgs(argc, argv, shouldContinue)) return 1; + if (!shouldContinue) + return 0; + if (!ReadHeaderData() || !ReadCommandsData()) return 1; diff --git a/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.cpp b/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.cpp index 9f30103b..c2e1b737 100644 --- a/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.cpp +++ b/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.cpp @@ -1,5 +1,6 @@ #include "ZoneCodeGeneratorArguments.h" +#include "GitVersion.h" #include "Utils/Arguments/CommandLineOption.h" #include "Utils/Arguments/UsageInformation.h" @@ -9,6 +10,9 @@ const CommandLineOption* const OPTION_HELP = CommandLineOption::Builder::Create().WithShortName("?").WithLongName("help").WithDescription("Displays usage information.").Build(); +const CommandLineOption* const OPTION_VERSION = + CommandLineOption::Builder::Create().WithLongName("version").WithDescription("Prints the application version.").Build(); + const CommandLineOption* const OPTION_VERBOSE = CommandLineOption::Builder::Create().WithShortName("v").WithLongName("verbose").WithDescription("Outputs a lot more and more detailed messages.").Build(); @@ -70,7 +74,15 @@ const CommandLineOption* const OPTION_GENERATE = .Build(); const CommandLineOption* const COMMAND_LINE_OPTIONS[]{ - OPTION_HELP, OPTION_VERBOSE, OPTION_HEADER, OPTION_COMMANDS_FILE, OPTION_OUTPUT_FOLDER, OPTION_PRINT, OPTION_GENERATE}; + OPTION_HELP, + OPTION_VERSION, + OPTION_VERBOSE, + OPTION_HEADER, + OPTION_COMMANDS_FILE, + OPTION_OUTPUT_FOLDER, + OPTION_PRINT, + OPTION_GENERATE, +}; ZoneCodeGeneratorArguments::GenerationTask::GenerationTask() : m_all_assets(false) @@ -91,7 +103,7 @@ ZoneCodeGeneratorArguments::GenerationTask::GenerationTask(std::string assetName } ZoneCodeGeneratorArguments::ZoneCodeGeneratorArguments() - : m_argument_parser(COMMAND_LINE_OPTIONS, std::extent::value), + : m_argument_parser(COMMAND_LINE_OPTIONS, std::extent_v), m_task_flags(0) { m_verbose = false; @@ -109,8 +121,14 @@ void ZoneCodeGeneratorArguments::PrintUsage() usage.Print(); } -bool ZoneCodeGeneratorArguments::Parse(const int argc, const char** argv) +void ZoneCodeGeneratorArguments::PrintVersion() { + std::cout << "OpenAssetTools ZoneCodeGenerator " << std::string(GIT_VERSION) << "\n"; +} + +bool ZoneCodeGeneratorArguments::ParseArgs(const int argc, const char** argv, bool& shouldContinue) +{ + shouldContinue = true; if (!m_argument_parser.ParseArguments(argc - 1, &argv[1])) { PrintUsage(); @@ -124,6 +142,14 @@ bool ZoneCodeGeneratorArguments::Parse(const int argc, const char** argv) return false; } + // Check if the user wants to see the version + if (m_argument_parser.IsOptionSpecified(OPTION_VERSION)) + { + PrintVersion(); + shouldContinue = false; + return true; + } + // -v; --verbose m_verbose = m_argument_parser.IsOptionSpecified(OPTION_VERBOSE); diff --git a/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.h b/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.h index 1344d2af..351efc2e 100644 --- a/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.h +++ b/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.h @@ -13,6 +13,7 @@ class ZoneCodeGeneratorArguments * \brief Prints a command line usage help text for the Unlinker tool to stdout. */ static void PrintUsage(); + static void PrintVersion(); public: static constexpr unsigned FLAG_TASK_GENERATE = 1 << 0; @@ -40,8 +41,7 @@ public: std::vector m_generation_tasks; ZoneCodeGeneratorArguments(); - - bool Parse(int argc, const char** argv); + bool ParseArgs(int argc, const char** argv, bool& shouldContinue); _NODISCARD bool ShouldGenerate() const; _NODISCARD bool ShouldPrint() const; From 0b0b888b0d21852f60cf3ad306a67d15286f676c Mon Sep 17 00:00:00 2001 From: Jan Date: Tue, 23 Jan 2024 23:14:25 +0100 Subject: [PATCH 3/3] chore: make help command return exit code 0 --- src/Linker/LinkerArgs.cpp | 3 ++- src/RawTemplater/RawTemplaterArguments.cpp | 3 ++- src/Unlinker/UnlinkerArgs.cpp | 3 ++- src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.cpp | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Linker/LinkerArgs.cpp b/src/Linker/LinkerArgs.cpp index 90320567..1e6c927c 100644 --- a/src/Linker/LinkerArgs.cpp +++ b/src/Linker/LinkerArgs.cpp @@ -225,7 +225,8 @@ bool LinkerArgs::ParseArgs(const int argc, const char** argv, bool& shouldContin if (m_argument_parser.IsOptionSpecified(OPTION_HELP)) { PrintUsage(); - return false; + shouldContinue = false; + return true; } // Check if the user wants to see the version diff --git a/src/RawTemplater/RawTemplaterArguments.cpp b/src/RawTemplater/RawTemplaterArguments.cpp index d61de176..d3879b10 100644 --- a/src/RawTemplater/RawTemplaterArguments.cpp +++ b/src/RawTemplater/RawTemplaterArguments.cpp @@ -82,7 +82,8 @@ bool RawTemplaterArguments::ParseArgs(const int argc, const char** argv, bool& s if (m_argument_parser.IsOptionSpecified(OPTION_HELP)) { PrintUsage(); - return false; + shouldContinue = false; + return true; } // Check if the user wants to see the version diff --git a/src/Unlinker/UnlinkerArgs.cpp b/src/Unlinker/UnlinkerArgs.cpp index 79c1a181..29276c66 100644 --- a/src/Unlinker/UnlinkerArgs.cpp +++ b/src/Unlinker/UnlinkerArgs.cpp @@ -264,7 +264,8 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv, bool& shouldCont if (m_argument_parser.IsOptionSpecified(OPTION_HELP)) { PrintUsage(); - return false; + shouldContinue = false; + return true; } // Check if the user wants to see the version diff --git a/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.cpp b/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.cpp index c2e1b737..2d699577 100644 --- a/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.cpp +++ b/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.cpp @@ -139,7 +139,8 @@ bool ZoneCodeGeneratorArguments::ParseArgs(const int argc, const char** argv, bo if (m_argument_parser.IsOptionSpecified(OPTION_HELP)) { PrintUsage(); - return false; + shouldContinue = false; + return true; } // Check if the user wants to see the version