Allow to setup/override module configuration from commandline

This commit is contained in:
SirLynix 2023-07-30 12:44:43 +02:00
parent 218b75558a
commit 816d9d1174
13 changed files with 150 additions and 21 deletions

View File

@ -16,6 +16,8 @@
namespace Nz
{
class CommandLineParameters;
class NAZARA_AUDIO_API Audio : public ModuleBase<Audio>
{
friend ModuleBase;
@ -45,8 +47,10 @@ namespace Nz
Audio& operator=(const Audio&) = delete;
Audio& operator=(Audio&&) = delete;
struct Config
struct NAZARA_AUDIO_API Config
{
void Override(const CommandLineParameters& parameters);
bool allowDummyDevice = true;
bool noAudio = false;
};

View File

@ -25,16 +25,8 @@ namespace Nz
if constexpr (ModuleHasRegister<Module, C>::value)
modules.template Get<Module>().RegisterComponent(component);
ModuleRegisterer<TypeList<Rest...>>::Register(modules, component);
}
};
template<>
struct ModuleRegisterer<TypeList<>>
{
template<typename T, typename C>
static void Register(T& /*modules*/, C& /*component*/)
{
if constexpr (sizeof...(Rest) > 0)
ModuleRegisterer<TypeList<Rest...>>::Register(modules, component);
}
};
}
@ -50,7 +42,7 @@ namespace Nz
template<typename... ModuleConfig>
Application<ModuleList...>::Application(int argc, char** argv, ModuleConfig&&... configs) :
ApplicationBase(argc, argv),
m_modules(std::forward<ModuleConfig>(configs)...)
m_modules(GetCommandLineParameters(), std::forward<ModuleConfig>(configs)...)
{
}
@ -58,7 +50,7 @@ namespace Nz
template<typename... ModuleConfig>
Application<ModuleList...>::Application(int argc, const Pointer<const char>* argv, ModuleConfig&&... configs) :
ApplicationBase(argc, argv),
m_modules(std::forward<ModuleConfig>(configs)...)
m_modules(GetCommandLineParameters(), std::forward<ModuleConfig>(configs)...)
{
}

View File

@ -11,7 +11,9 @@
#include <Nazara/Core/ApplicationComponent.hpp>
#include <Nazara/Core/ApplicationUpdater.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/CommandLineParameters.hpp>
#include <atomic>
#include <string>
#include <vector>
namespace Nz
@ -36,6 +38,7 @@ namespace Nz
inline void ClearComponents();
inline const CommandLineParameters& GetCommandLineParameters() const;
template<typename T> T& GetComponent();
template<typename T> const T& GetComponent() const;
@ -66,6 +69,7 @@ namespace Nz
std::atomic_bool m_running;
std::vector<std::unique_ptr<ApplicationComponent>> m_components;
std::vector<Updater> m_updaters;
CommandLineParameters m_commandLineParams;
HighPrecisionClock m_clock;
Time m_currentTime;

View File

@ -50,6 +50,11 @@ namespace Nz
m_components.clear();
}
inline const CommandLineParameters& ApplicationBase::GetCommandLineParameters() const
{
return m_commandLineParams;
}
template<typename T>
T& ApplicationBase::GetComponent()
{

View File

@ -11,13 +11,15 @@
namespace Nz
{
class CommandLineParameters;
namespace Detail
{
template<typename Module, typename... Modules>
struct ModuleTuple : ModuleTuple<Module>, ModuleTuple<Modules...>
{
template<typename... ModuleConfig>
ModuleTuple(ModuleConfig&&... configs);
template<typename... ModuleConfig> ModuleTuple(ModuleConfig&&... configs);
template<typename... ModuleConfig> ModuleTuple(const CommandLineParameters& parameters, ModuleConfig&&... configs);
template<typename T> T& Get();
};
@ -25,8 +27,8 @@ namespace Nz
template<typename Module>
struct ModuleTuple<Module>
{
template<typename... ModuleConfig>
ModuleTuple(ModuleConfig&&... configs);
template<typename... ModuleConfig> ModuleTuple(ModuleConfig&&... configs);
template<typename... ModuleConfig> ModuleTuple(const CommandLineParameters& parameters, ModuleConfig&&... configs);
template<typename T> T& Get();
@ -42,8 +44,8 @@ namespace Nz
class Modules
{
public:
template<typename... ModuleConfig>
Modules(ModuleConfig&&... configs);
template<typename... ModuleConfig> Modules(ModuleConfig&&... configs);
template<typename... ModuleConfig> Modules(const CommandLineParameters& parameters, ModuleConfig&&... configs);
~Modules() = default;
template<typename T> T& Get();

View File

@ -9,6 +9,21 @@ namespace Nz
{
namespace Detail
{
template<typename, typename = void>
struct ModuleConfigHasOverride : std::false_type {};
template<typename T>
struct ModuleConfigHasOverride<T, std::void_t<decltype(std::declval<T>().Override(std::declval<const CommandLineParameters&>()))>> : std::true_type {};
template<typename T>
auto OverrideModuleConfig(T&& module, const CommandLineParameters& params)
{
if constexpr (!std::is_const_v<T> && ModuleConfigHasOverride<T>::value)
module.Override(params);
return std::forward<T>(module);
}
template<typename T>
struct Pick
{
@ -24,10 +39,27 @@ namespace Nz
}
}
template<typename First, typename... Args>
static auto Get(const CommandLineParameters& parameters, First&& first, Args&&... args)
{
if constexpr (std::is_same_v<T, std::decay_t<First>>)
return OverrideModuleConfig<First>(first, parameters);
else
{
NazaraUnused(first);
return Get(parameters, std::forward<Args>(args)...);
}
}
static auto Get()
{
return T{};
}
static auto Get(const CommandLineParameters& parameters)
{
return OverrideModuleConfig(T{}, parameters);
}
};
template<typename Module, typename... Modules>
@ -37,6 +69,14 @@ namespace Nz
ModuleTuple<Modules...>(std::forward<ModuleConfig>(configs)...)
{
}
template<typename Module, typename... Modules>
template<typename... ModuleConfig>
ModuleTuple<Module, Modules...>::ModuleTuple(const CommandLineParameters& parameters, ModuleConfig&&... configs) :
ModuleTuple<Module>(parameters, std::forward<ModuleConfig>(configs)...),
ModuleTuple<Modules...>(parameters, std::forward<ModuleConfig>(configs)...)
{
}
template<typename Module, typename... Modules>
template<typename T>
@ -55,6 +95,13 @@ namespace Nz
m(Pick<typename Module::Config>::Get(std::forward<ModuleConfig>(configs)...))
{
}
template<typename Module>
template<typename... ModuleConfig>
ModuleTuple<Module>::ModuleTuple(const CommandLineParameters& parameters, ModuleConfig&&... configs) :
m(Pick<typename Module::Config>::Get(parameters, std::forward<ModuleConfig>(configs)...))
{
}
template<typename Module>
template<typename T>
@ -71,6 +118,13 @@ namespace Nz
m_modules(std::forward<ModuleConfig>(configs)...)
{
}
template<typename... ModuleList>
template<typename... ModuleConfig>
Modules<ModuleList...>::Modules(const CommandLineParameters& parameters, ModuleConfig&&... configs) :
m_modules(parameters, std::forward<ModuleConfig>(configs)...)
{
}
template<typename... ModuleList>
template<typename T>

View File

@ -23,6 +23,7 @@
namespace Nz
{
class AppFilesystemComponent;
class CommandLineParameters;
class RenderBuffer;
class NAZARA_GRAPHICS_API Graphics : public ModuleBase<Graphics>
@ -59,8 +60,10 @@ namespace Nz
void RegisterComponent(AppFilesystemComponent& component);
struct Config
struct NAZARA_GRAPHICS_API Config
{
void Override(const CommandLineParameters& parameters);
RenderDeviceFeatures forceDisableFeatures;
bool useDedicatedRenderDevice = true;
};

View File

@ -18,6 +18,7 @@
namespace Nz
{
class Buffer;
class CommandLineParameters;
class RendererImpl;
class NAZARA_RENDERER_API Renderer : public ModuleBase<Renderer>
@ -42,8 +43,10 @@ namespace Nz
const std::vector<RenderDeviceInfo>& QueryRenderDevices() const;
struct Config
struct NAZARA_RENDERER_API Config
{
void Override(const CommandLineParameters& parameters);
ParameterList customParameters;
RenderAPI preferredAPI = RenderAPI::Unknown;
#ifdef NAZARA_DEBUG

View File

@ -14,6 +14,7 @@
#include <Nazara/Audio/Formats/libflacLoader.hpp>
#include <Nazara/Audio/Formats/libvorbisLoader.hpp>
#include <Nazara/Audio/Formats/minimp3Loader.hpp>
#include <Nazara/Core/CommandLineParameters.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
@ -155,4 +156,10 @@ namespace Nz
}
Audio* Audio::s_instance = nullptr;
void Audio::Config::Override(const CommandLineParameters& parameters)
{
if (parameters.HasFlag("no-audio"))
noAudio = true;
}
}

View File

@ -13,6 +13,7 @@ namespace Nz
{
ApplicationBase::ApplicationBase(int argc, const Pointer<const char>* argv) :
m_running(true),
m_commandLineParams(CommandLineParameters::Parse(argc, argv)),
m_currentTime(Time::Zero())
{
NazaraAssert(s_instance == nullptr, "only one instance of ApplicationBase can exist at a given time");

View File

@ -4,6 +4,7 @@
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Core/AppFilesystemComponent.hpp>
#include <Nazara/Core/CommandLineParameters.hpp>
#include <Nazara/Graphics/GuillotineTextureAtlas.hpp>
#include <Nazara/Graphics/MaterialInstance.hpp>
#include <Nazara/Graphics/MaterialPipeline.hpp>
@ -451,4 +452,13 @@ namespace Nz
}
Graphics* Graphics::s_instance = nullptr;
void Graphics::Config::Override(const CommandLineParameters& parameters)
{
if (parameters.HasFlag("use-dedicated-gpu"))
useDedicatedRenderDevice = true;
if (parameters.HasFlag("use-integrated-gpu"))
useDedicatedRenderDevice = false;
}
}

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Core/CommandLineParameters.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Core/StringExt.hpp>
@ -14,6 +15,8 @@
#include <Nazara/Utility/Utility.hpp>
#include <NazaraUtils/CallOnExit.hpp>
#include <NazaraUtils/EnumArray.hpp>
#include <frozen/string.h>
#include <frozen/unordered_map.h>
#include <filesystem>
#include <stdexcept>
@ -211,4 +214,44 @@ namespace Nz
}
Renderer* Renderer::s_instance = nullptr;
void Renderer::Config::Override(const CommandLineParameters& parameters)
{
std::string_view value;
if (parameters.GetParameter("render-api", &value))
{
constexpr auto renderAPIStr = frozen::make_unordered_map<frozen::string, RenderAPI>({
{ "auto", RenderAPI::Unknown },
{ "direct3d", RenderAPI::Direct3D },
{ "mantle", RenderAPI::Mantle },
{ "metal", RenderAPI::Metal },
{ "opengl", RenderAPI::OpenGL },
{ "opengles", RenderAPI::OpenGL_ES },
{ "vulkan", RenderAPI::Vulkan }
});
if (auto it = renderAPIStr.find(value); it != renderAPIStr.end())
preferredAPI = it->second;
else
NazaraError("unknown render API \"" + std::string(value) + "\"");
}
if (parameters.GetParameter("render-api-validation", &value))
{
constexpr auto validationStr = frozen::make_unordered_map<frozen::string, RenderAPIValidationLevel>({
{ "debug", RenderAPIValidationLevel::Debug },
{ "errors", RenderAPIValidationLevel::Errors },
{ "none", RenderAPIValidationLevel::None },
{ "verbose", RenderAPIValidationLevel::Verbose },
{ "warnings", RenderAPIValidationLevel::Warnings }
});
if (auto it = validationStr.find(value); it != validationStr.end())
validationLevel = it->second;
else
NazaraError("unknown validation level \"" + std::string(value) + "\"");
}
}
}

View File

@ -176,6 +176,7 @@ local modules = {
Renderer = {
Option = "renderer",
Deps = {"NazaraPlatform"},
Packages = { "frozen" },
PublicPackages = { "nazarautils", "nzsl" },
Custom = function ()
if has_config("embed_rendererbackends") then