From 098da9de929f2148f5aaea62bf3477396cb749cf Mon Sep 17 00:00:00 2001 From: Egor Orachyov Date: Thu, 10 Oct 2024 02:54:41 +0300 Subject: [PATCH] gh-83: rework app structure & remove globals entirely, explicit dependency propagation --- .gitignore | 3 - engine/plugins/assimp/assimp_asset_loader.cpp | 4 +- .../freetype/freetype_asset_loader.cpp | 3 +- engine/plugins/freetype/freetype_font.cpp | 10 +- engine/plugins/freetype/freetype_font.hpp | 2 +- .../runtime/asset/default_asset_loader.cpp | 4 +- .../runtime/asset/shader_asset_loader.cpp | 4 +- .../runtime/asset/texture_asset_loader.cpp | 12 +- engine/runtime/asset/asset_library_fs.cpp | 2 +- engine/runtime/asset/asset_loader.hpp | 6 +- engine/runtime/asset/asset_manager.cpp | 11 +- engine/runtime/asset/asset_manager.hpp | 1 + engine/runtime/core/cmd_line.cpp | 86 ++++++- engine/runtime/core/cmd_line.hpp | 73 +++++- .../{system => core}/ioc_container.cpp | 10 - .../{system => core}/ioc_container.hpp | 25 +- engine/runtime/core/log.cpp | 43 ++-- engine/runtime/core/log.hpp | 8 +- engine/runtime/core/task.cpp | 13 +- engine/runtime/core/task.hpp | 16 +- engine/runtime/core/task_parallel_for.cpp | 13 +- engine/runtime/core/task_parallel_for.hpp | 17 +- engine/runtime/ecs/ecs_core.cpp | 7 +- engine/runtime/ecs/ecs_core.hpp | 7 +- engine/runtime/ecs/ecs_memory.cpp | 14 +- engine/runtime/ecs/ecs_memory.hpp | 2 +- engine/runtime/ecs/ecs_registry.cpp | 8 - engine/runtime/ecs/ecs_registry.hpp | 3 - engine/runtime/ecs/ecs_world.cpp | 13 +- engine/runtime/ecs/ecs_world.hpp | 2 +- engine/runtime/engine.hpp | 12 +- engine/runtime/gfx/vulkan/vk_driver.cpp | 27 +-- engine/runtime/gfx/vulkan/vk_driver.hpp | 2 +- engine/runtime/gfx/vulkan/vk_window.cpp | 5 - engine/runtime/glsl/glsl_shader_compiler.cpp | 16 +- engine/runtime/glsl/glsl_shader_compiler.hpp | 23 +- engine/runtime/grc/image.cpp | 2 +- engine/runtime/grc/pso_cache.cpp | 18 +- engine/runtime/grc/pso_cache.hpp | 2 +- engine/runtime/grc/pso_file.cpp | 22 -- engine/runtime/grc/pso_file.hpp | 10 - engine/runtime/grc/shader_cache.cpp | 19 +- engine/runtime/grc/shader_cache.hpp | 4 +- engine/runtime/grc/shader_library.cpp | 43 ++-- engine/runtime/grc/shader_library.hpp | 13 +- engine/runtime/grc/shader_manager.cpp | 55 ++--- engine/runtime/grc/shader_manager.hpp | 5 +- engine/runtime/grc/shader_pass.cpp | 13 +- engine/runtime/grc/texture.cpp | 2 +- engine/runtime/grc/texture_manager.cpp | 6 +- engine/runtime/grc/texture_manager.hpp | 2 +- engine/runtime/hooks/hook_engine.hpp | 106 --------- engine/runtime/hooks/hook_logs.hpp | 91 ------- engine/runtime/hooks/hook_profiler.hpp | 100 -------- engine/runtime/hooks/hook_root_remap.hpp | 66 ------ engine/runtime/hooks/hook_uuid_gen.hpp | 66 ------ engine/runtime/io/async_file_system.cpp | 5 +- engine/runtime/io/stream_file.cpp | 8 +- engine/runtime/io/stream_file.hpp | 2 +- engine/runtime/io/tree_yaml.cpp | 16 +- engine/runtime/io/tree_yaml.hpp | 4 +- engine/runtime/mesh/mesh_manager.cpp | 6 +- engine/runtime/mesh/mesh_manager.hpp | 2 +- engine/runtime/platform/dll_manager.cpp | 10 +- engine/runtime/platform/dll_manager.hpp | 3 +- engine/runtime/platform/file_system.cpp | 2 +- engine/runtime/profiler/profiler.cpp | 88 ++++--- engine/runtime/profiler/profiler.hpp | 18 +- engine/runtime/rdg/rdg_graph.cpp | 5 +- engine/runtime/rdg/rdg_graph.hpp | 2 +- engine/runtime/rdg/rdg_pool.cpp | 9 +- engine/runtime/rdg/rdg_pool.hpp | 6 +- engine/runtime/render/aux_draw_manager.cpp | 20 +- engine/runtime/render/aux_draw_manager.hpp | 3 - engine/runtime/render/culling.cpp | 4 +- engine/runtime/render/culling.hpp | 2 +- engine/runtime/render/render_engine.cpp | 6 +- engine/runtime/render/render_engine.hpp | 4 +- engine/runtime/rtti/struct.hpp | 1 + engine/runtime/rtti/traits.hpp | 5 +- engine/runtime/rtti/type_storage.cpp | 3 +- engine/runtime/rtti/type_storage.hpp | 3 +- engine/runtime/scene/scene.cpp | 6 +- engine/runtime/scene/scene.hpp | 13 +- engine/runtime/scene/scene_manager.cpp | 15 +- engine/runtime/scene/scene_manager.hpp | 3 +- engine/runtime/scene/scene_packed.cpp | 34 --- engine/runtime/scene/scene_packed.hpp | 3 - engine/runtime/scripting/_rtti.cpp | 38 --- engine/runtime/scripting/_rtti.hpp | 34 --- engine/runtime/scripting/lua/lua_defs.cpp | 28 --- engine/runtime/scripting/lua/lua_defs.hpp | 50 ---- engine/runtime/scripting/lua/lua_script.cpp | 93 -------- engine/runtime/scripting/lua/lua_script.hpp | 76 ------ .../scripting/lua/lua_script_instance.cpp | 108 --------- .../scripting/lua/lua_script_instance.hpp | 75 ------ .../scripting/lua/lua_script_system.cpp | 146 ------------ .../scripting/lua/lua_script_system.hpp | 79 ------ .../runtime/scripting/lua/lua_type_traits.cpp | 112 --------- .../scripting/lua_bindings/lua_bindings.hpp | 86 ------- .../lua_bindings/lua_bindings_asset.cpp | 98 -------- .../lua_bindings/lua_bindings_asset.hpp | 135 ----------- .../lua_bindings/lua_bindings_core.cpp | 163 ------------- .../lua_bindings/lua_bindings_core.hpp | 105 -------- .../lua_bindings/lua_bindings_math.cpp | 188 --------------- .../lua_bindings/lua_bindings_math.hpp | 47 ---- .../lua_bindings/lua_bindings_platform.cpp | 111 --------- .../lua_bindings/lua_bindings_platform.hpp | 90 ------- .../lua_bindings/lua_bindings_scene.cpp | 35 --- .../lua_bindings/lua_bindings_scene.hpp | 42 ---- engine/runtime/scripting/script.cpp | 54 ----- engine/runtime/scripting/script.hpp | 82 ------- engine/runtime/scripting/script_instance.hpp | 84 ------- engine/runtime/scripting/script_native.hpp | 84 ------- engine/runtime/scripting/script_system.hpp | 74 ------ .../{platform => system}/application.cpp | 180 ++++---------- .../{platform => system}/application.hpp | 67 ++++-- engine/runtime/system/config.cpp | 11 +- engine/runtime/system/config.hpp | 5 +- engine/runtime/system/config_file.cpp | 24 +- engine/runtime/system/config_file.hpp | 25 +- engine/runtime/system/console.cpp | 33 +-- engine/runtime/system/console.hpp | 4 +- engine/runtime/system/engine.cpp | 142 +++++------ engine/runtime/system/engine.hpp | 118 +++++---- .../game_application.cpp} | 33 ++- .../game_application.hpp} | 41 ++-- engine/runtime/system/hook.cpp | 50 ---- engine/runtime/system/hook.hpp | 85 ------- engine/runtime/system/hooks.cpp | 224 ++++++++++++++++++ .../lua_type_traits.hpp => system/hooks.hpp} | 32 +-- engine/runtime/system/plugin_manager.cpp | 2 +- template/main.cpp | 55 ++++- 133 files changed, 1075 insertions(+), 3836 deletions(-) rename engine/runtime/{system => core}/ioc_container.cpp (93%) rename engine/runtime/{system => core}/ioc_container.hpp (94%) delete mode 100644 engine/runtime/hooks/hook_engine.hpp delete mode 100644 engine/runtime/hooks/hook_logs.hpp delete mode 100644 engine/runtime/hooks/hook_profiler.hpp delete mode 100644 engine/runtime/hooks/hook_root_remap.hpp delete mode 100644 engine/runtime/hooks/hook_uuid_gen.hpp delete mode 100644 engine/runtime/scripting/_rtti.cpp delete mode 100644 engine/runtime/scripting/_rtti.hpp delete mode 100644 engine/runtime/scripting/lua/lua_defs.cpp delete mode 100644 engine/runtime/scripting/lua/lua_defs.hpp delete mode 100644 engine/runtime/scripting/lua/lua_script.cpp delete mode 100644 engine/runtime/scripting/lua/lua_script.hpp delete mode 100644 engine/runtime/scripting/lua/lua_script_instance.cpp delete mode 100644 engine/runtime/scripting/lua/lua_script_instance.hpp delete mode 100644 engine/runtime/scripting/lua/lua_script_system.cpp delete mode 100644 engine/runtime/scripting/lua/lua_script_system.hpp delete mode 100644 engine/runtime/scripting/lua/lua_type_traits.cpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings.hpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_asset.cpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_asset.hpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_core.cpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_core.hpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_math.cpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_math.hpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_platform.cpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_platform.hpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_scene.cpp delete mode 100644 engine/runtime/scripting/lua_bindings/lua_bindings_scene.hpp delete mode 100644 engine/runtime/scripting/script.cpp delete mode 100644 engine/runtime/scripting/script.hpp delete mode 100644 engine/runtime/scripting/script_instance.hpp delete mode 100644 engine/runtime/scripting/script_native.hpp delete mode 100644 engine/runtime/scripting/script_system.hpp rename engine/runtime/{platform => system}/application.cpp (63%) rename engine/runtime/{platform => system}/application.hpp (71%) rename engine/runtime/{scripting/script_native.cpp => system/game_application.cpp} (78%) rename engine/runtime/{hooks/hook_config.hpp => system/game_application.hpp} (77%) delete mode 100644 engine/runtime/system/hook.cpp delete mode 100644 engine/runtime/system/hook.hpp create mode 100644 engine/runtime/system/hooks.cpp rename engine/runtime/{scripting/lua/lua_type_traits.hpp => system/hooks.hpp} (75%) diff --git a/.gitignore b/.gitignore index 82b2d0844..daf359fe1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,6 @@ /.idea /.vscode -# vs code setup -!.vscode/launch.json - # python cache files /**/__pycache__ diff --git a/engine/plugins/assimp/assimp_asset_loader.cpp b/engine/plugins/assimp/assimp_asset_loader.cpp index 80e8920de..75f9e273b 100644 --- a/engine/plugins/assimp/assimp_asset_loader.cpp +++ b/engine/plugins/assimp/assimp_asset_loader.cpp @@ -30,13 +30,13 @@ #include "assimp_import_data.hpp" #include "assimp_importer.hpp" #include "core/data.hpp" +#include "core/ioc_container.hpp" #include "math/math_utils3d.hpp" #include "mesh/mesh.hpp" #include "mesh/mesh_builder.hpp" #include "mesh/mesh_manager.hpp" #include "platform/file_system.hpp" #include "profiler/profiler.hpp" -#include "system/ioc_container.hpp" #include #include @@ -76,7 +76,7 @@ namespace wmoge { return StatusCode::Error; } - MeshManager* mesh_manager = IocContainer::iresolve_v(); + MeshManager* mesh_manager = context.ioc->resolve_value(); MeshFlags flags; flags.set(MeshFlag::FromDisk); diff --git a/engine/plugins/freetype/freetype_asset_loader.cpp b/engine/plugins/freetype/freetype_asset_loader.cpp index 1564a1092..3811eab5e 100644 --- a/engine/plugins/freetype/freetype_asset_loader.cpp +++ b/engine/plugins/freetype/freetype_asset_loader.cpp @@ -27,6 +27,7 @@ #include "freetype_asset_loader.hpp" +#include "core/ioc_container.hpp" #include "freetype_font.hpp" #include "freetype_import_data.hpp" #include "profiler/profiler.hpp" @@ -56,7 +57,7 @@ namespace wmoge { asset = make_ref(); asset->set_id(asset_id); - FreetypeFont loader; + FreetypeFont loader(context.ioc); return loader.load(asset, result.get_data_file(FILE_TAG), import_data->height, import_data->glyphs_in_row); } diff --git a/engine/plugins/freetype/freetype_font.cpp b/engine/plugins/freetype/freetype_font.cpp index 480a4ae6d..283497436 100644 --- a/engine/plugins/freetype/freetype_font.cpp +++ b/engine/plugins/freetype/freetype_font.cpp @@ -27,21 +27,21 @@ #include "freetype_font.hpp" +#include "core/ioc_container.hpp" #include "gfx/gfx_driver.hpp" #include "grc/image.hpp" #include "grc/texture.hpp" #include "platform/file_system.hpp" #include "profiler/profiler.hpp" -#include "system/ioc_container.hpp" #include namespace wmoge { - FreetypeFont::FreetypeFont() { - m_gfx_driver = IocContainer::iresolve_v(); - m_file_system = IocContainer::iresolve_v(); - m_texture_manager = IocContainer::iresolve_v(); + FreetypeFont::FreetypeFont(IocContainer* ioc) { + m_gfx_driver = ioc->resolve_value(); + m_file_system = ioc->resolve_value(); + m_texture_manager = ioc->resolve_value(); } Status FreetypeFont::load(const Ref& font, array_view ttf_data, int height, int glyphs_in_row) { diff --git a/engine/plugins/freetype/freetype_font.hpp b/engine/plugins/freetype/freetype_font.hpp index 449b3bd7f..f10e07cde 100644 --- a/engine/plugins/freetype/freetype_font.hpp +++ b/engine/plugins/freetype/freetype_font.hpp @@ -43,7 +43,7 @@ namespace wmoge { */ class FreetypeFont { public: - FreetypeFont(); + FreetypeFont(class IocContainer* ioc); /** * @brief Loads font from a .ttf file from file system using specified height in pixels diff --git a/engine/plugins/runtime/asset/default_asset_loader.cpp b/engine/plugins/runtime/asset/default_asset_loader.cpp index 67af1477d..17f44ee2b 100644 --- a/engine/plugins/runtime/asset/default_asset_loader.cpp +++ b/engine/plugins/runtime/asset/default_asset_loader.cpp @@ -28,10 +28,10 @@ #include "default_asset_loader.hpp" +#include "core/ioc_container.hpp" #include "io/tree_yaml.hpp" #include "profiler/profiler.hpp" #include "rtti/type_storage.hpp" -#include "system/ioc_container.hpp" namespace wmoge { @@ -55,7 +55,7 @@ namespace wmoge { Ref import_data = context.asset_meta.import_data.cast(); assert(import_data); - RttiClass* rtti = IocContainer::iresolve_v()->find_class(context.asset_meta.rtti); + RttiClass* rtti = context.ioc->resolve_value()->find_class(context.asset_meta.rtti); if (!rtti) { WG_LOG_ERROR("no rtti type for " << asset_id); return StatusCode::InvalidData; diff --git a/engine/plugins/runtime/asset/shader_asset_loader.cpp b/engine/plugins/runtime/asset/shader_asset_loader.cpp index 03eec6cfd..de9c3e6ae 100644 --- a/engine/plugins/runtime/asset/shader_asset_loader.cpp +++ b/engine/plugins/runtime/asset/shader_asset_loader.cpp @@ -27,12 +27,12 @@ #include "shader_asset_loader.hpp" +#include "core/ioc_container.hpp" #include "grc/shader.hpp" #include "grc/shader_file.hpp" #include "grc/shader_manager.hpp" #include "io/tree_yaml.hpp" #include "profiler/profiler.hpp" -#include "system/ioc_container.hpp" namespace wmoge { @@ -61,7 +61,7 @@ namespace wmoge { WG_CHECKED(tree.parse_data(result.get_data_file(FILE_TAG))); WG_TREE_READ(context.io_context, tree, shader_file); - auto* shader_manager = IocContainer::iresolve_v(); + auto* shader_manager = context.ioc->resolve_value(); ShaderReflection shader_reflection; WG_CHECKED(shader_manager->load_shader_reflection(shader_file, shader_reflection)); diff --git a/engine/plugins/runtime/asset/texture_asset_loader.cpp b/engine/plugins/runtime/asset/texture_asset_loader.cpp index 1f5f27eda..bd4a39f00 100644 --- a/engine/plugins/runtime/asset/texture_asset_loader.cpp +++ b/engine/plugins/runtime/asset/texture_asset_loader.cpp @@ -28,6 +28,7 @@ #include "texture_asset_loader.hpp" #include "asset/texture_import_data.hpp" +#include "core/ioc_container.hpp" #include "gfx/gfx_cmd_list.hpp" #include "gfx/gfx_driver.hpp" #include "grc/image.hpp" @@ -35,7 +36,6 @@ #include "grc/texture_manager.hpp" #include "grc/texture_resize.hpp" #include "profiler/profiler.hpp" -#include "system/ioc_container.hpp" namespace wmoge { @@ -71,8 +71,9 @@ namespace wmoge { return StatusCode::FailedResize; } - auto gfx_driver = IocContainer::iresolve_v(); - auto texture_manager = IocContainer::iresolve_v(); + auto ioc = context.ioc; + auto gfx_driver = ioc->resolve_value(); + auto texture_manager = ioc->resolve_value(); TextureFlags flags; flags.set(TextureFlag::Pooled); @@ -172,8 +173,9 @@ namespace wmoge { } } - auto gfx_driver = IocContainer::iresolve_v(); - auto texture_manager = IocContainer::iresolve_v(); + auto ioc = context.ioc; + auto gfx_driver = ioc->resolve_value(); + auto texture_manager = ioc->resolve_value(); TextureFlags flags; flags.set(TextureFlag::Pooled); diff --git a/engine/runtime/asset/asset_library_fs.cpp b/engine/runtime/asset/asset_library_fs.cpp index ca6c239f5..320339260 100644 --- a/engine/runtime/asset/asset_library_fs.cpp +++ b/engine/runtime/asset/asset_library_fs.cpp @@ -28,6 +28,7 @@ #include "asset_library_fs.hpp" #include "asset/asset_manager.hpp" +#include "core/ioc_container.hpp" #include "core/string_utils.hpp" #include "io/async_file_system.hpp" #include "io/tree.hpp" @@ -35,7 +36,6 @@ #include "platform/file_system.hpp" #include "profiler/profiler.hpp" #include "rtti/type_storage.hpp" -#include "system/ioc_container.hpp" namespace wmoge { diff --git a/engine/runtime/asset/asset_loader.hpp b/engine/runtime/asset/asset_loader.hpp index fe94aae53..5723aae07 100644 --- a/engine/runtime/asset/asset_loader.hpp +++ b/engine/runtime/asset/asset_loader.hpp @@ -32,6 +32,7 @@ #include "asset/asset_meta.hpp" #include "core/array_view.hpp" #include "core/flat_map.hpp" +#include "core/ioc_container.hpp" #include "io/context.hpp" #include "rtti/traits.hpp" @@ -65,8 +66,9 @@ namespace wmoge { * @brief Context passed to the loader */ struct AssetLoadContext { - IoContext io_context; - AssetMeta asset_meta; + IocContainer* ioc; + IoContext io_context; + AssetMeta asset_meta; }; /** diff --git a/engine/runtime/asset/asset_manager.cpp b/engine/runtime/asset/asset_manager.cpp index deba66fbe..dc8c3bff0 100644 --- a/engine/runtime/asset/asset_manager.cpp +++ b/engine/runtime/asset/asset_manager.cpp @@ -28,11 +28,11 @@ #include "asset_manager.hpp" #include "asset/asset_library_fs.hpp" +#include "core/ioc_container.hpp" #include "core/timer.hpp" #include "platform/file_system.hpp" #include "profiler/profiler.hpp" #include "rtti/type_storage.hpp" -#include "system/ioc_container.hpp" #include @@ -42,6 +42,7 @@ namespace wmoge { m_ioc_container = ioc; m_file_system = ioc->resolve_value(); m_type_storage = ioc->resolve_value(); + m_task_manager = ioc->resolve_value(); m_callback = std::make_shared([this](Asset* asset) { std::lock_guard guard(m_mutex); @@ -100,8 +101,8 @@ namespace wmoge { load_state->async_op = make_async_op>(); load_state->library = asset_library.value(); load_state->loader = loader.value(); + load_state->context.ioc = m_ioc_container; load_state->context.asset_meta = std::move(asset_meta); - load_state->context.io_context.add(m_ioc_container); Task task_fill_requests(name, [=](TaskContext&) -> int { if (!load_state->loader->fill_request(load_state->context, name, load_state->request)) { @@ -109,7 +110,7 @@ namespace wmoge { return 1; } - buffered_vector file_data_requests; + buffered_vector file_data_requests; file_data_requests.reserve(load_state->request.data_files.size()); load_state->data_buffers.reserve(load_state->request.data_files.size()); @@ -152,7 +153,7 @@ namespace wmoge { return 0; }); - task_load.schedule(Async::join(array_view(file_data_requests))).add_on_completion([=](AsyncStatus status, std::optional&) { + task_load.schedule(m_task_manager, Async::join(array_view(file_data_requests))).add_on_completion([=](AsyncStatus status, std::optional&) { if (status == AsyncStatus::Failed) { load_state->async_op->set_failed(); } @@ -163,7 +164,7 @@ namespace wmoge { return 0; }); - task_fill_requests.schedule(Async::join(array_view(deps))).add_on_completion([=](AsyncStatus status, std::optional&) { + task_fill_requests.schedule(m_task_manager, Async::join(array_view(deps))).add_on_completion([=](AsyncStatus status, std::optional&) { if (status == AsyncStatus::Failed) { load_state->async_op->set_failed(); } diff --git a/engine/runtime/asset/asset_manager.hpp b/engine/runtime/asset/asset_manager.hpp index 65b3f3981..765018337 100644 --- a/engine/runtime/asset/asset_manager.hpp +++ b/engine/runtime/asset/asset_manager.hpp @@ -98,6 +98,7 @@ namespace wmoge { class FileSystem* m_file_system = nullptr; class RttiTypeStorage* m_type_storage = nullptr; class IocContainer* m_ioc_container = nullptr; + class TaskManager* m_task_manager = nullptr; mutable std::recursive_mutex m_mutex; }; diff --git a/engine/runtime/core/cmd_line.cpp b/engine/runtime/core/cmd_line.cpp index c766ce4e3..2ee9772ca 100644 --- a/engine/runtime/core/cmd_line.cpp +++ b/engine/runtime/core/cmd_line.cpp @@ -28,45 +28,105 @@ #include "cmd_line.hpp" #include +#include namespace wmoge { - CmdLine::CmdLine() : m_options("wmoge", "wmoge engine runtime and tools") { + std::string CmdLineUtil::to_string(int argc, const char* const* argv) { + std::stringstream s; + for (int i = 0; i < argc; i++) { + s << argv[i] << " "; + } + return s.str(); + } + + std::vector CmdLineUtil::to_vector(int argc, const char* const* argv) { + std::vector v; + v.reserve(argc); + for (int i = 0; i < argc; i++) { + v.emplace_back(argv[i]); + } + return std::move(v); } - void CmdLine::add_int(const std::string& name, const std::string& desc, const std::string& value) { + CmdLineOptions::CmdLineOptions(const std::string& name, const std::string& desc) + : m_options(name, desc) { + } + + void CmdLineOptions::add_int(const std::string& name, const std::string& desc, const std::string& value) { m_options.add_option("", cxxopts::Option(name, desc, cxxopts::value()->default_value(value))); } - void CmdLine::add_bool(const std::string& name, const std::string& desc, const std::string& value) { + + void CmdLineOptions::add_bool(const std::string& name, const std::string& desc, const std::string& value) { m_options.add_option("", cxxopts::Option(name, desc, cxxopts::value()->default_value(value))); } - void CmdLine::add_string(const std::string& name, const std::string& desc, const std::string& value) { + + void CmdLineOptions::add_string(const std::string& name, const std::string& desc, const std::string& value) { m_options.add_option("", cxxopts::Option(name, desc, cxxopts::value()->default_value(value))); } - bool CmdLine::parse(int argc, const char* const* argv) { + std::optional CmdLineOptions::parse(const std::vector& args) { + const int argc = static_cast(args.size()); + std::vector argv; + + argv.reserve(argc); + for (auto& arg : args) { + argv.push_back(arg.c_str()); + } + try { - m_parsed = m_options.parse(argc, argv); - return true; + return CmdLineParseResult(m_options.parse(argc, argv.data())); } catch (const std::exception& e) { std::cerr << "failed to parse options due to: " << e.what(); } - return false; + return std::nullopt; } - int CmdLine::get_int(const std::string& name) { + std::string CmdLineOptions::get_help() const { + return m_options.help(); + } + + CmdLineParseResult::CmdLineParseResult(cxxopts::ParseResult parsed) + : m_parsed(std::move(parsed)) { + } + + int CmdLineParseResult::get_int(const std::string& name) { return m_parsed[name].as(); } - bool CmdLine::get_bool(const std::string& name) { + + bool CmdLineParseResult::get_bool(const std::string& name) { return m_parsed[name].as(); } - std::string CmdLine::get_string(const std::string& name) { + + std::string CmdLineParseResult::get_string(const std::string& name) { return m_parsed[name].as(); } - std::string CmdLine::get_help() const { - return m_options.help(); + void CmdLineHookList::add(CmdLineHook hook) { + m_storage.emplace_back(std::move(hook)); + } + + void CmdLineHookList::clear() { + m_storage.clear(); + } + + Status CmdLineHookList::process(CmdLineParseResult& cmd_line) { + for (auto& hook : m_storage) { + const Status status = hook(cmd_line); + const StatusCode code = status.code(); + + if (code == StatusCode::ExitCode0) { + return StatusCode::ExitCode0; + } + if (code == StatusCode::ExitCode1) { + return StatusCode::ExitCode1; + } + if (code != StatusCode::Ok) { + return status; + } + } + return WG_OK; } }// namespace wmoge \ No newline at end of file diff --git a/engine/runtime/core/cmd_line.hpp b/engine/runtime/core/cmd_line.hpp index 11658051e..ec71fac79 100644 --- a/engine/runtime/core/cmd_line.hpp +++ b/engine/runtime/core/cmd_line.hpp @@ -27,33 +27,84 @@ #pragma once +#include "core/status.hpp" + #include +#include +#include +#include +#include +#include + namespace wmoge { /** - * @class CmdLine - * @brief Container for parsed command-line options + * @class CmdLineUtil + * @brief Util for command-line + */ + class CmdLineUtil { + public: + static std::string to_string(int argc, const char* const* argv); + static std::vector to_vector(int argc, const char* const* argv); + }; + + /** + * @class CmdLineParseResult + * @brief Container for command-line parse result */ - class CmdLine { + class CmdLineParseResult { public: - CmdLine(); + CmdLineParseResult(cxxopts::ParseResult parsed); + + [[nodiscard]] int get_int(const std::string& name); + [[nodiscard]] bool get_bool(const std::string& name); + [[nodiscard]] std::string get_string(const std::string& name); + + private: + cxxopts::ParseResult m_parsed; + }; + + using CmdLineArgs = std::vector; + + /** + * @class CmdLineOptions + * @brief Container for command-line options + */ + class CmdLineOptions { + public: + CmdLineOptions(const std::string& name, const std::string& desc); void add_int(const std::string& name, const std::string& desc, const std::string& value = ""); void add_bool(const std::string& name, const std::string& desc, const std::string& value = ""); void add_string(const std::string& name, const std::string& desc, const std::string& value = ""); - bool parse(int argc, const char* const* argv); + [[nodiscard]] std::optional parse(const std::vector& args); + [[nodiscard]] std::string get_help() const; + + private: + cxxopts::Options m_options; + }; + + /** + * @brief Cmd line hook + */ + using CmdLineHook = std::function; - int get_int(const std::string& name); - bool get_bool(const std::string& name); - std::string get_string(const std::string& name); + /** + * @class CmdLineHookList + * @brief Storage for cmd line hooks + */ + class CmdLineHookList { + public: + CmdLineHookList() = default; - std::string get_help() const; + void add(CmdLineHook hook); + void clear(); + Status process(CmdLineParseResult& cmd_line); private: - cxxopts::Options m_options; - cxxopts::ParseResult m_parsed; + std::vector m_storage; }; }// namespace wmoge \ No newline at end of file diff --git a/engine/runtime/system/ioc_container.cpp b/engine/runtime/core/ioc_container.cpp similarity index 93% rename from engine/runtime/system/ioc_container.cpp rename to engine/runtime/core/ioc_container.cpp index 0e10f12be..2d791eae2 100644 --- a/engine/runtime/system/ioc_container.cpp +++ b/engine/runtime/core/ioc_container.cpp @@ -29,8 +29,6 @@ namespace wmoge { - IocContainer* IocContainer::g_ioc_container = nullptr; - void IocContainer::clear() { std::lock_guard guard(m_mutex); @@ -73,12 +71,4 @@ namespace wmoge { return std::nullopt; } - void IocContainer::provide(IocContainer* ioc) { - g_ioc_container = ioc; - } - - IocContainer* IocContainer::instance() { - return g_ioc_container; - } - }// namespace wmoge \ No newline at end of file diff --git a/engine/runtime/system/ioc_container.hpp b/engine/runtime/core/ioc_container.hpp similarity index 94% rename from engine/runtime/system/ioc_container.hpp rename to engine/runtime/core/ioc_container.hpp index 36850e5b1..52510448a 100644 --- a/engine/runtime/system/ioc_container.hpp +++ b/engine/runtime/core/ioc_container.hpp @@ -69,9 +69,7 @@ namespace wmoge { */ class IocContainer { public: - IocContainer() = default; - IocContainer(const IocContainer&) = delete; - IocContainer(IocContainer&&) noexcept = delete; + IocContainer() = default; void clear(); void add(IocEntry&& entry); @@ -84,6 +82,9 @@ namespace wmoge { template void bind_by_instance(std::shared_ptr instance); + template + void bind_by_pointer(S* instance); + template void bind_by_ioc(); @@ -99,17 +100,9 @@ namespace wmoge { template S* resolve_value(); - template - static S* iresolve_v(); - - static void provide(IocContainer* ioc); - static IocContainer* instance(); - private: std::unordered_map m_entries; std::recursive_mutex m_mutex; - - static IocContainer* g_ioc_container; }; template @@ -136,6 +129,11 @@ namespace wmoge { }); } + template + inline void IocContainer::bind_by_pointer(S* instance) { + bind_by_instance(std::shared_ptr(instance, [](auto p) {})); + } + template inline void IocContainer::bind_by_ioc() { std::lock_guard guard(m_mutex); @@ -210,9 +208,4 @@ namespace wmoge { return resolve().value(); } - template - inline S* IocContainer::iresolve_v() { - return instance()->resolve_value(); - } - }// namespace wmoge \ No newline at end of file diff --git a/engine/runtime/core/log.cpp b/engine/runtime/core/log.cpp index 24b57bc87..6c6841fcb 100644 --- a/engine/runtime/core/log.cpp +++ b/engine/runtime/core/log.cpp @@ -30,7 +30,6 @@ #include "platform/file_system.hpp" #include "platform/time.hpp" #include "system/console.hpp" -#include "system/ioc_container.hpp" #include @@ -45,28 +44,32 @@ namespace wmoge { - Log g_null_log; - Log* Log::g_log = &g_null_log; + Log g_log_default; + Log* Log::g_log = &g_log_default; void Log::listen(const std::shared_ptr& listener) { assert(listener); std::lock_guard guard(m_mutex); m_listeners.push_back(listener); } + void Log::remove(const std::shared_ptr& listener) { assert(listener); std::lock_guard guard(m_mutex); m_listeners.erase(std::find(m_listeners.begin(), m_listeners.end(), listener)); } + void Log::log(LogLevel level, std::string message, std::string file, std::string function, std::size_t line) { std::lock_guard guard(m_mutex); LogEntry entry{std::move(message), std::move(file), std::move(function), line, level}; for (auto& listener : m_listeners) listener->on_message(entry); } + Log* Log::instance() { return g_log; } + void Log::provide(Log* log) { g_log = log; } @@ -74,6 +77,7 @@ namespace wmoge { LogListenerStdout::LogListenerStdout(std::string name, LogLevel level) : m_name(std::move(name)), m_level(level) { } + void LogListenerStdout::on_message(const LogEntry& entry) { if (entry.level == LogLevel::Never || entry.level < m_level) return; @@ -90,21 +94,10 @@ namespace wmoge { std::cout << output.str(); } - LogListenerStream::LogListenerStream(std::string name, wmoge::LogLevel level) - : m_name(std::move(name)), m_level(level) { - FileSystem* file_system = IocContainer::iresolve_v(); - Time* engine_time = IocContainer::iresolve_v