diff --git a/premake5.lua b/premake5.lua index 0152b9308..6d9b19f78 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/src/Linker/Linker.cpp b/src/Linker/Linker.cpp index c6d2940e9..6c05aa653 100644 --- a/src/Linker/Linker.cpp +++ b/src/Linker/Linker.cpp @@ -613,9 +613,13 @@ class LinkerImpl final : public Linker 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 a9b2d318b..1e6c927cb 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(); @@ -62,7 +82,16 @@ bool RawTemplaterArguments::Parse(const int argc, const char** argv) if (m_argument_parser.IsOptionSpecified(OPTION_HELP)) { PrintUsage(); - return false; + shouldContinue = false; + return true; + } + + // 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(); diff --git a/src/RawTemplater/RawTemplaterArguments.h b/src/RawTemplater/RawTemplaterArguments.h index 463ccbc60..dadb1f88f 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 @@ class RawTemplaterArguments 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 2abcf3547..f26cc12de 100644 --- a/src/Unlinker/Unlinker.cpp +++ b/src/Unlinker/Unlinker.cpp @@ -429,9 +429,13 @@ class Unlinker::Impl */ 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 5c7bfaa02..29276c660 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(); @@ -249,7 +264,16 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv) if (m_argument_parser.IsOptionSpecified(OPTION_HELP)) { PrintUsage(); - return false; + shouldContinue = false; + return true; + } + + // 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(); diff --git a/src/Unlinker/UnlinkerArgs.h b/src/Unlinker/UnlinkerArgs.h index d26dca949..358725661 100644 --- a/src/Unlinker/UnlinkerArgs.h +++ b/src/Unlinker/UnlinkerArgs.h @@ -21,6 +21,7 @@ class UnlinkerArgs * \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 @@ class UnlinkerArgs 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 b6b7a5940..cdfcc2097 100644 --- a/src/ZoneCodeGeneratorLib/ZoneCodeGenerator.cpp +++ b/src/ZoneCodeGeneratorLib/ZoneCodeGenerator.cpp @@ -64,9 +64,13 @@ class ZoneCodeGenerator::Impl 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 9f30103ba..2d6995770 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(); @@ -121,7 +139,16 @@ bool ZoneCodeGeneratorArguments::Parse(const int argc, const char** argv) if (m_argument_parser.IsOptionSpecified(OPTION_HELP)) { PrintUsage(); - return false; + shouldContinue = false; + return true; + } + + // Check if the user wants to see the version + if (m_argument_parser.IsOptionSpecified(OPTION_VERSION)) + { + PrintVersion(); + shouldContinue = false; + return true; } // -v; --verbose diff --git a/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.h b/src/ZoneCodeGeneratorLib/ZoneCodeGeneratorArguments.h index 1344d2af9..351efc2e3 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 @@ class ZoneCodeGeneratorArguments 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; diff --git a/tools/scripts/version.lua b/tools/scripts/version.lua new file mode 100644 index 000000000..dc0087f91 --- /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