Skip to content

Commit

Permalink
gh-83: rework app structure & remove globals entirely, explicit depen…
Browse files Browse the repository at this point in the history
…dency propagation
  • Loading branch information
EgorOrachyov committed Oct 9, 2024
1 parent 8b5455d commit 098da9d
Show file tree
Hide file tree
Showing 133 changed files with 1,075 additions and 3,836 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
/.idea
/.vscode

# vs code setup
!.vscode/launch.json

# python cache files
/**/__pycache__

Expand Down
4 changes: 2 additions & 2 deletions engine/plugins/assimp/assimp_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cassert>
#include <cstring>
Expand Down Expand Up @@ -76,7 +76,7 @@ namespace wmoge {
return StatusCode::Error;
}

MeshManager* mesh_manager = IocContainer::iresolve_v<MeshManager>();
MeshManager* mesh_manager = context.ioc->resolve_value<MeshManager>();

MeshFlags flags;
flags.set(MeshFlag::FromDisk);
Expand Down
3 changes: 2 additions & 1 deletion engine/plugins/freetype/freetype_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -56,7 +57,7 @@ namespace wmoge {
asset = make_ref<Font>();
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);
}

Expand Down
10 changes: 5 additions & 5 deletions engine/plugins/freetype/freetype_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <freetype/freetype.h>

namespace wmoge {

FreetypeFont::FreetypeFont() {
m_gfx_driver = IocContainer::iresolve_v<GfxDriver>();
m_file_system = IocContainer::iresolve_v<FileSystem>();
m_texture_manager = IocContainer::iresolve_v<TextureManager>();
FreetypeFont::FreetypeFont(IocContainer* ioc) {
m_gfx_driver = ioc->resolve_value<GfxDriver>();
m_file_system = ioc->resolve_value<FileSystem>();
m_texture_manager = ioc->resolve_value<TextureManager>();
}

Status FreetypeFont::load(const Ref<Font>& font, array_view<const std::uint8_t> ttf_data, int height, int glyphs_in_row) {
Expand Down
2 changes: 1 addition & 1 deletion engine/plugins/freetype/freetype_font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions engine/plugins/runtime/asset/default_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -55,7 +55,7 @@ namespace wmoge {
Ref<AssetImportData> import_data = context.asset_meta.import_data.cast<AssetImportData>();
assert(import_data);

RttiClass* rtti = IocContainer::iresolve_v<RttiTypeStorage>()->find_class(context.asset_meta.rtti);
RttiClass* rtti = context.ioc->resolve_value<RttiTypeStorage>()->find_class(context.asset_meta.rtti);
if (!rtti) {
WG_LOG_ERROR("no rtti type for " << asset_id);
return StatusCode::InvalidData;
Expand Down
4 changes: 2 additions & 2 deletions engine/plugins/runtime/asset/shader_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<ShaderManager>();
auto* shader_manager = context.ioc->resolve_value<ShaderManager>();

ShaderReflection shader_reflection;
WG_CHECKED(shader_manager->load_shader_reflection(shader_file, shader_reflection));
Expand Down
12 changes: 7 additions & 5 deletions engine/plugins/runtime/asset/texture_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
#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"
#include "grc/texture.hpp"
#include "grc/texture_manager.hpp"
#include "grc/texture_resize.hpp"
#include "profiler/profiler.hpp"
#include "system/ioc_container.hpp"

namespace wmoge {

Expand Down Expand Up @@ -71,8 +71,9 @@ namespace wmoge {
return StatusCode::FailedResize;
}

auto gfx_driver = IocContainer::iresolve_v<GfxDriver>();
auto texture_manager = IocContainer::iresolve_v<TextureManager>();
auto ioc = context.ioc;
auto gfx_driver = ioc->resolve_value<GfxDriver>();
auto texture_manager = ioc->resolve_value<TextureManager>();

TextureFlags flags;
flags.set(TextureFlag::Pooled);
Expand Down Expand Up @@ -172,8 +173,9 @@ namespace wmoge {
}
}

auto gfx_driver = IocContainer::iresolve_v<GfxDriver>();
auto texture_manager = IocContainer::iresolve_v<TextureManager>();
auto ioc = context.ioc;
auto gfx_driver = ioc->resolve_value<GfxDriver>();
auto texture_manager = ioc->resolve_value<TextureManager>();

TextureFlags flags;
flags.set(TextureFlag::Pooled);
Expand Down
2 changes: 1 addition & 1 deletion engine/runtime/asset/asset_library_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
#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"
#include "io/tree_yaml.hpp"
#include "platform/file_system.hpp"
#include "profiler/profiler.hpp"
#include "rtti/type_storage.hpp"
#include "system/ioc_container.hpp"

namespace wmoge {

Expand Down
6 changes: 4 additions & 2 deletions engine/runtime/asset/asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
};

/**
Expand Down
11 changes: 6 additions & 5 deletions engine/runtime/asset/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <chrono>

Expand All @@ -42,6 +42,7 @@ namespace wmoge {
m_ioc_container = ioc;
m_file_system = ioc->resolve_value<FileSystem>();
m_type_storage = ioc->resolve_value<RttiTypeStorage>();
m_task_manager = ioc->resolve_value<TaskManager>();

m_callback = std::make_shared<typename Asset::ReleaseCallback>([this](Asset* asset) {
std::lock_guard guard(m_mutex);
Expand Down Expand Up @@ -100,16 +101,16 @@ namespace wmoge {
load_state->async_op = make_async_op<Ref<Asset>>();
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<IocContainer*>(m_ioc_container);

Task task_fill_requests(name, [=](TaskContext&) -> int {
if (!load_state->loader->fill_request(load_state->context, name, load_state->request)) {
WG_LOG_ERROR("failed to fill request for " << name);
return 1;
}

buffered_vector<Async> file_data_requests;
buffered_vector<Async, 16> file_data_requests;

file_data_requests.reserve(load_state->request.data_files.size());
load_state->data_buffers.reserve(load_state->request.data_files.size());
Expand Down Expand Up @@ -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<int>&) {
task_load.schedule(m_task_manager, Async::join(array_view(file_data_requests))).add_on_completion([=](AsyncStatus status, std::optional<int>&) {
if (status == AsyncStatus::Failed) {
load_state->async_op->set_failed();
}
Expand All @@ -163,7 +164,7 @@ namespace wmoge {
return 0;
});

task_fill_requests.schedule(Async::join(array_view(deps))).add_on_completion([=](AsyncStatus status, std::optional<int>&) {
task_fill_requests.schedule(m_task_manager, Async::join(array_view(deps))).add_on_completion([=](AsyncStatus status, std::optional<int>&) {
if (status == AsyncStatus::Failed) {
load_state->async_op->set_failed();
}
Expand Down
1 change: 1 addition & 0 deletions engine/runtime/asset/asset_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
86 changes: 73 additions & 13 deletions engine/runtime/core/cmd_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,105 @@
#include "cmd_line.hpp"

#include <iostream>
#include <sstream>

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<std::string> CmdLineUtil::to_vector(int argc, const char* const* argv) {
std::vector<std::string> 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<int>()->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<bool>()->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<std::string>()->default_value(value)));
}

bool CmdLine::parse(int argc, const char* const* argv) {
std::optional<CmdLineParseResult> CmdLineOptions::parse(const std::vector<std::string>& args) {
const int argc = static_cast<int>(args.size());
std::vector<const char*> 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<int>();
}
bool CmdLine::get_bool(const std::string& name) {

bool CmdLineParseResult::get_bool(const std::string& name) {
return m_parsed[name].as<bool>();
}
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>();
}

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
Loading

0 comments on commit 098da9d

Please sign in to comment.