From c09c6853690a88d7d3bd1f6b7e4d047d1c7102bd Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 1 Nov 2020 23:12:28 +0100 Subject: [PATCH] Create project for ZoneCodeGenerator rewrite in c++ --- premake5.lua | 2 + src/ZoneCodeGeneratorNew.lua | 42 +++++ src/ZoneCodeGeneratorNew/Interface/CLI.cpp | 0 src/ZoneCodeGeneratorNew/Interface/CLI.h | 0 .../Interface/PrettyPrinter.cpp | 0 .../Interface/PrettyPrinter.h | 0 .../Interface/Session.cpp | 10 + src/ZoneCodeGeneratorNew/Interface/Session.h | 11 ++ .../Interface/ZoneCodeGeneratorArguments.cpp | 171 ++++++++++++++++++ .../Interface/ZoneCodeGeneratorArguments.h | 44 +++++ src/ZoneCodeGeneratorNew/main.cpp | 18 ++ 11 files changed, 298 insertions(+) create mode 100644 src/ZoneCodeGeneratorNew.lua create mode 100644 src/ZoneCodeGeneratorNew/Interface/CLI.cpp create mode 100644 src/ZoneCodeGeneratorNew/Interface/CLI.h create mode 100644 src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.cpp create mode 100644 src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.h create mode 100644 src/ZoneCodeGeneratorNew/Interface/Session.cpp create mode 100644 src/ZoneCodeGeneratorNew/Interface/Session.h create mode 100644 src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.cpp create mode 100644 src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.h create mode 100644 src/ZoneCodeGeneratorNew/main.cpp diff --git a/premake5.lua b/premake5.lua index 415c9483..c8d65865 100644 --- a/premake5.lua +++ b/premake5.lua @@ -132,6 +132,7 @@ include "src/Unlinker.lua" include "src/Utils.lua" include "src/ZoneCode.lua" include "src/ZoneCodeGenerator.lua" +include "src/ZoneCodeGeneratorNew.lua" include "src/ZoneCommon.lua" include "src/ZoneLoading.lua" include "src/ZoneWriting.lua" @@ -146,6 +147,7 @@ group "Components" Utils:project() ZoneCode:project() ZoneCodeGenerator:project() + ZoneCodeGeneratorNew:project() ZoneCommon:project() ZoneLoading:project() ZoneWriting:project() diff --git a/src/ZoneCodeGeneratorNew.lua b/src/ZoneCodeGeneratorNew.lua new file mode 100644 index 00000000..108db2ae --- /dev/null +++ b/src/ZoneCodeGeneratorNew.lua @@ -0,0 +1,42 @@ +ZoneCodeGeneratorNew = {} + +function ZoneCodeGeneratorNew:include() + if References:include(self:name()) then + includedirs { + path.join(ProjectFolder(), "ZoneCodeGeneratorNew") + } + end +end + +function ZoneCodeGeneratorNew:link() + +end + +function ZoneCodeGeneratorNew:use() + dependson(self:name()) +end + +function ZoneCodeGeneratorNew:name() + return "ZoneCodeGeneratorNew" +end + +function ZoneCodeGeneratorNew:project() + References:reset() + local folder = ProjectFolder(); + + project(self:name()) + targetdir(TargetDirectoryLib) + location "%{wks.location}/src/%{prj.name}" + kind "ConsoleApp" + language "C++" + + files { + path.join(folder, "ZoneCodeGeneratorNew/**.h"), + path.join(folder, "ZoneCodeGeneratorNew/**.cpp") + } + + self:include() + Utils:include() + + Utils:link() +end diff --git a/src/ZoneCodeGeneratorNew/Interface/CLI.cpp b/src/ZoneCodeGeneratorNew/Interface/CLI.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/ZoneCodeGeneratorNew/Interface/CLI.h b/src/ZoneCodeGeneratorNew/Interface/CLI.h new file mode 100644 index 00000000..e69de29b diff --git a/src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.cpp b/src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.h b/src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.h new file mode 100644 index 00000000..e69de29b diff --git a/src/ZoneCodeGeneratorNew/Interface/Session.cpp b/src/ZoneCodeGeneratorNew/Interface/Session.cpp new file mode 100644 index 00000000..dfc77fbe --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Interface/Session.cpp @@ -0,0 +1,10 @@ +#include "Session.h" + +Session::Session() += default; + +Session::Session(ZoneCodeGeneratorArguments args) + : m_args(std::move(args)) +{ + +} diff --git a/src/ZoneCodeGeneratorNew/Interface/Session.h b/src/ZoneCodeGeneratorNew/Interface/Session.h new file mode 100644 index 00000000..da71ed39 --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Interface/Session.h @@ -0,0 +1,11 @@ +#pragma once +#include "ZoneCodeGeneratorArguments.h" + +class Session +{ + ZoneCodeGeneratorArguments m_args; + +public: + Session(); + explicit Session(ZoneCodeGeneratorArguments args); +}; diff --git a/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.cpp b/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.cpp new file mode 100644 index 00000000..8569692d --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.cpp @@ -0,0 +1,171 @@ +#include "ZoneCodeGeneratorArguments.h" + +#include "Utils/Arguments/CommandLineOption.h" +#include "Utils/Arguments/UsageInformation.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(); + +// ------ +// INPUT +// ------ + +constexpr const char* CATEGORY_INPUT = "Input"; + +const CommandLineOption* const OPTION_CREATE = CommandLineOption::Builder::Create() + .WithShortName("h") + .WithLongName("header") + .WithDescription("Create a new database from the specified header file.") + .WithCategory(CATEGORY_INPUT) + .WithParameter("headerFile") + .Build(); + +// ------ +// EDITING +// ------ +constexpr const char* CATEGORY_EDITING = "Editing"; + +const CommandLineOption* const OPTION_EDITING_COMMANDS = CommandLineOption::Builder::Create() + .WithShortName("e") + .WithLongName("editing-commands") + .WithDescription("Specifies the editing command file. Defaults to stdin.") + .WithCategory(CATEGORY_EDITING) + .WithParameter("commandFile") + .Build(); + +// ------ +// OUTPUT +// ------ +constexpr const char* CATEGORY_OUTPUT = "Output"; + +const CommandLineOption* const OPTION_OUTPUT_FOLDER = CommandLineOption::Builder::Create() + .WithShortName("o") + .WithLongName("output") + .WithDescription("Specify the folder to save the generate code files to. Defaults to the current directory.") + .WithCategory(CATEGORY_OUTPUT) + .WithParameter("outputPath") + .Build(); + +const CommandLineOption* const OPTION_PRINT = CommandLineOption::Builder::Create() + .WithShortName("p") + .WithLongName("print") + .WithDescription("Print the loaded data.") + .WithCategory(CATEGORY_OUTPUT) + .Build(); + +const CommandLineOption* const OPTION_GENERATE = CommandLineOption::Builder::Create() + .WithShortName("g") + .WithLongName("generate") + .WithDescription("Generates a specified asset/preset combination. Can be used multiple times. Available presets: " + "ZoneLoad, ZoneWrite, AssetStructTests") + .WithCategory(CATEGORY_OUTPUT) + .WithParameter("assetName") + .WithParameter("preset") + .Reusable() + .Build(); + +const CommandLineOption* const COMMAND_LINE_OPTIONS[] +{ + OPTION_HELP, + OPTION_VERBOSE, + OPTION_CREATE, + OPTION_EDITING_COMMANDS, + OPTION_OUTPUT_FOLDER, + OPTION_PRINT, + OPTION_GENERATE +}; + +ZoneCodeGeneratorArguments::GenerationTask::GenerationTask() += default; + +ZoneCodeGeneratorArguments::GenerationTask::GenerationTask(std::string assetName, std::string presetName) + : m_asset_name(std::move(assetName)), + m_preset_name(std::move(presetName)) +{ +} + +ZoneCodeGeneratorArguments::ZoneCodeGeneratorArguments() + : m_argument_parser(COMMAND_LINE_OPTIONS, _countof(COMMAND_LINE_OPTIONS)) +{ + m_verbose = false; + m_task = ProcessingTask::GENERATE_CODE; +} + +void ZoneCodeGeneratorArguments::PrintUsage() +{ + UsageInformation usage("ZoneCodeGenerator.exe"); + + for (const auto* commandLineOption : COMMAND_LINE_OPTIONS) + { + usage.AddCommandLineOption(commandLineOption); + } + + usage.Print(); +} + +bool ZoneCodeGeneratorArguments::Parse(const int argc, const char** argv) +{ + if (!m_argument_parser.ParseArguments(argc - 1, &argv[1])) + { + PrintUsage(); + return false; + } + + // Check if the user requested help + if (m_argument_parser.IsOptionSpecified(OPTION_HELP)) + { + PrintUsage(); + return false; + } + + // -v; --verbose + m_verbose = m_argument_parser.IsOptionSpecified(OPTION_VERBOSE); + + // -p; --print + if (m_argument_parser.IsOptionSpecified(OPTION_PRINT)) + m_task = ProcessingTask::PRINT_DATA; + + // -o; --output + if (m_argument_parser.IsOptionSpecified(OPTION_OUTPUT_FOLDER)) + m_output_directory = m_argument_parser.GetValueForOption(OPTION_OUTPUT_FOLDER); + + // -h; --header + if (m_argument_parser.IsOptionSpecified(OPTION_CREATE)) + m_header_path = m_argument_parser.GetValueForOption(OPTION_CREATE); + + // -e; --editing-commands + if (m_argument_parser.IsOptionSpecified(OPTION_EDITING_COMMANDS)) + m_commands_path = m_argument_parser.GetValueForOption(OPTION_EDITING_COMMANDS); + + if (m_task == ProcessingTask::GENERATE_CODE) + { + if (!m_argument_parser.IsOptionSpecified(OPTION_GENERATE)) + { + printf("A generate parameter needs to be specified when generating code\n"); + PrintUsage(); + return false; + } + + const auto generateParameterValues = m_argument_parser.GetParametersForOption(OPTION_GENERATE); + const auto generateCount = generateParameterValues.size() / 2; + for(auto i = 0u; i < generateCount; i++) + m_generation_tasks.emplace_back(generateParameterValues[i], generateParameterValues[i + 1]); + } + else if (m_argument_parser.IsOptionSpecified(OPTION_GENERATE)) + { + printf("Cannot specify generate parameter when not generating code\n"); + PrintUsage(); + return false; + } + + return true; +} diff --git a/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.h b/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.h new file mode 100644 index 00000000..73bf03e4 --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.h @@ -0,0 +1,44 @@ +#pragma once +#include "Utils/Arguments/ArgumentParser.h" + +#include + +class ZoneCodeGeneratorArguments +{ + ArgumentParser m_argument_parser; + + /** + * \brief Prints a command line usage help text for the Unlinker tool to stdout. + */ + static void PrintUsage(); + +public: + enum class ProcessingTask + { + GENERATE_CODE, + PRINT_DATA + }; + + class GenerationTask + { + public: + std::string m_asset_name; + std::string m_preset_name; + + GenerationTask(); + GenerationTask(std::string assetName, std::string presetName); + }; + + bool m_verbose; + + std::string m_header_path; + std::string m_commands_path; + std::string m_output_directory; + + ProcessingTask m_task; + std::vector m_generation_tasks; + + ZoneCodeGeneratorArguments(); + + bool Parse(int argc, const char** argv); +}; diff --git a/src/ZoneCodeGeneratorNew/main.cpp b/src/ZoneCodeGeneratorNew/main.cpp new file mode 100644 index 00000000..74a6be51 --- /dev/null +++ b/src/ZoneCodeGeneratorNew/main.cpp @@ -0,0 +1,18 @@ +#include +#include + +#include "Interface/Session.h" +#include "Interface/ZoneCodeGeneratorArguments.h" + +int main(const int argc, const char** argv) +{ + ZoneCodeGeneratorArguments args; + + if(!args.Parse(argc, argv)) + return 1; + + Session session(args); + + const std::string asdf = "Hello World"; + printf("%s\n", asdf.c_str()); +}