chore: add base for image converter tool

This commit is contained in:
Jan 2024-09-23 19:29:58 +02:00
parent fabefc8cd5
commit c37e9984ba
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
7 changed files with 249 additions and 0 deletions

View File

@ -112,6 +112,7 @@ group ""
-- ======================== -- ========================
include "src/Common.lua" include "src/Common.lua"
include "src/Crypto.lua" include "src/Crypto.lua"
include "src/ImageConverter.lua"
include "src/Linker.lua" include "src/Linker.lua"
include "src/Parser.lua" include "src/Parser.lua"
include "src/RawTemplater.lua" include "src/RawTemplater.lua"
@ -155,6 +156,7 @@ group ""
group "Tools" group "Tools"
Linker:project() Linker:project()
Unlinker:project() Unlinker:project()
ImageConverter:project()
group "" group ""
group "Raw" group "Raw"

50
src/ImageConverter.lua Normal file
View File

@ -0,0 +1,50 @@
ImageConverter = {}
function ImageConverter:include(includes)
if includes:handle(self:name()) then
includedirs {
path.join(ProjectFolder(), "ImageConverter")
}
end
end
function ImageConverter:link(links)
end
function ImageConverter:use()
dependson(self:name())
end
function ImageConverter:name()
return "ImageConverter"
end
function ImageConverter:project()
local folder = ProjectFolder()
local includes = Includes:create()
local links = Links:create()
project(self:name())
targetdir(TargetDirectoryBin)
location "%{wks.location}/src/%{prj.name}"
kind "ConsoleApp"
language "C++"
files {
path.join(folder, "ImageConverter/**.h"),
path.join(folder, "ImageConverter/**.cpp")
}
self:include(includes)
Utils:include(includes)
ObjLoading:include(includes)
ObjWriting:include(includes)
Raw:use()
links:linkto(Utils)
links:linkto(ObjLoading)
links:linkto(ObjWriting)
links:linkall()
end

View File

@ -0,0 +1,27 @@
#include "ImageConverter.h"
#include "ImageConverterArgs.h"
class ImageConverterImpl final : public ImageConverter
{
public:
bool Start(const int argc, const char** argv) override
{
auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue))
return false;
if (!shouldContinue)
return true;
return true;
}
private:
ImageConverterArgs m_args;
};
std::unique_ptr<ImageConverter> ImageConverter::Create()
{
return std::make_unique<ImageConverterImpl>();
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <memory>
class ImageConverter
{
public:
ImageConverter() = default;
virtual ~ImageConverter() = default;
ImageConverter(const ImageConverter& other) = delete;
ImageConverter(ImageConverter&& other) noexcept = delete;
ImageConverter& operator=(const ImageConverter& other) = delete;
ImageConverter& operator=(ImageConverter&& other) noexcept = delete;
/**
* \brief Starts the ImageConverter application logic.
* \param argc The amount of command line arguments specified.
* \param argv The command line arguments.
* \return \c true if the application was successful or \c false if an error occurred.
*/
virtual bool Start(int argc, const char** argv) = 0;
static std::unique_ptr<ImageConverter> Create();
};

View File

@ -0,0 +1,110 @@
#include "ImageConverterArgs.h"
#include "GitVersion.h"
#include "ObjLoading.h"
#include "ObjWriting.h"
#include "Utils/Arguments/UsageInformation.h"
#include <format>
#include <iostream>
#include <type_traits>
// clang-format off
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();
// clang-format on
const CommandLineOption* const COMMAND_LINE_OPTIONS[]{
OPTION_HELP,
OPTION_VERSION,
OPTION_VERBOSE,
};
ImageConverterArgs::ImageConverterArgs()
: m_verbose(false),
m_argument_parser(COMMAND_LINE_OPTIONS, std::extent_v<decltype(COMMAND_LINE_OPTIONS)>)
{
}
void ImageConverterArgs::PrintUsage()
{
UsageInformation usage("ImageConverter.exe");
for (const auto* commandLineOption : COMMAND_LINE_OPTIONS)
{
usage.AddCommandLineOption(commandLineOption);
}
usage.AddArgument("fileToConvert");
usage.SetVariableArguments(true);
usage.Print();
}
void ImageConverterArgs::PrintVersion()
{
std::cout << std::format("OpenAssetTools ImageConverter {}\n", GIT_VERSION);
}
void ImageConverterArgs::SetVerbose(const bool isVerbose)
{
m_verbose = isVerbose;
ObjLoading::Configuration.Verbose = isVerbose;
ObjWriting::Configuration.Verbose = isVerbose;
}
bool ImageConverterArgs::ParseArgs(const int argc, const char** argv, bool& shouldContinue)
{
shouldContinue = true;
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();
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_files_to_convert = m_argument_parser.GetArguments();
if (m_files_to_convert.empty())
{
// No files to convert specified...
PrintUsage();
return false;
}
// -v; --verbose
SetVerbose(m_argument_parser.IsOptionSpecified(OPTION_VERBOSE));
return true;
}

View File

@ -0,0 +1,28 @@
#pragma once
#include "Utils/Arguments/ArgumentParser.h"
#include "Utils/ClassUtils.h"
#include <string>
#include <vector>
class ImageConverterArgs
{
public:
ImageConverterArgs();
bool ParseArgs(int argc, const char** argv, bool& shouldContinue);
bool m_verbose;
std::vector<std::string> m_files_to_convert;
private:
/**
* \brief Prints a command line usage help text for the ImageConverter tool to stdout.
*/
static void PrintUsage();
static void PrintVersion();
void SetVerbose(bool isVerbose);
ArgumentParser m_argument_parser;
};

View File

@ -0,0 +1,8 @@
#include "ImageConverter.h"
int main(const int argc, const char** argv)
{
const auto imageConverter = ImageConverter::Create();
return imageConverter->Start(argc, argv) ? 0 : 1;
}