Skip to content

Commit

Permalink
gh-47: remove event management and depending systems / code for futur…
Browse files Browse the repository at this point in the history
…e refactoring
  • Loading branch information
EgorOrachyov committed Aug 6, 2024
1 parent c1fa453 commit d0cf94d
Show file tree
Hide file tree
Showing 69 changed files with 390 additions and 3,587 deletions.
1 change: 0 additions & 1 deletion engine/runtime/asset/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "core/string_id.hpp"
#include "core/uuid.hpp"
#include "core/weak_ref.hpp"
#include "event/event.hpp"
#include "rtti/traits.hpp"

#include <cstddef>
Expand Down
39 changes: 14 additions & 25 deletions engine/runtime/asset/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

#include "asset/asset_pak_fs.hpp"
#include "core/timer.hpp"
#include "event/event_asset.hpp"
#include "event/event_manager.hpp"
#include "platform/file_system.hpp"
#include "profiler/profiler.hpp"
#include "rtti/type_storage.hpp"
Expand All @@ -41,22 +39,24 @@
namespace wmoge {

AssetManager::AssetManager() {
m_file_system = IocContainer::iresolve_v<FileSystem>();
m_type_storage = IocContainer::iresolve_v<RttiTypeStorage>();
m_event_manager = IocContainer::iresolve_v<EventManager>();
m_file_system = IocContainer::iresolve_v<FileSystem>();
m_type_storage = IocContainer::iresolve_v<RttiTypeStorage>();

m_callback = std::make_shared<typename Asset::ReleaseCallback>([this](Asset* asset) {
std::lock_guard guard(m_mutex);

auto* rtti = asset->get_class();
auto unloader = find_unloader(rtti);
auto& id = asset->get_id();
auto& id = asset->get_id();
auto& rtti = asset->get_class_name();

auto unloader = find_unloader(rtti);
if (unloader) {
(*unloader)->unload(asset);
}

m_assets.erase(id);
auto entry = m_assets.find(id);
if (entry != m_assets.end()) {
m_assets.erase(entry);
}
});
}

Expand Down Expand Up @@ -120,12 +120,6 @@ namespace wmoge {
asset->set_name(name);
}

auto event = make_event<EventAsset>();
event->asset_id = name;
event->asset_ref = asset;
event->notification = AssetNotification::Loaded;
m_event_manager->dispatch(event);

std::lock_guard guard(m_mutex);
asset->set_release_callback(m_callback);
m_assets[name] = WeakRef<Asset>(asset);
Expand All @@ -143,11 +137,6 @@ namespace wmoge {
std::lock_guard guard(m_mutex);

if (status == AsyncStatus::Failed) {
auto event = make_event<EventAsset>();
event->asset_id = name;
event->notification = AssetNotification::FailedLoad;
m_event_manager->dispatch(event);

async_op->set_failed();
WG_LOG_ERROR("failed load asset " << name);
}
Expand Down Expand Up @@ -197,23 +186,23 @@ namespace wmoge {

void AssetManager::add_unloader(Ref<AssetUnloader> unloader) {
std::lock_guard guard(m_mutex);
m_unloaders[unloader->get_asset_type()] = std::move(unloader);
m_unloaders[unloader->get_asset_type()->get_name()] = std::move(unloader);
}

void AssetManager::add_pak(std::shared_ptr<AssetPak> pak) {
std::lock_guard guard(m_mutex);
m_paks.push_back(std::move(pak));
}

std::optional<AssetLoader*> AssetManager::find_loader(const Strid& loader) {
std::optional<AssetLoader*> AssetManager::find_loader(const Strid& loader_rtti) {
std::lock_guard guard(m_mutex);
auto query = m_loaders.find(loader);
auto query = m_loaders.find(loader_rtti);
return query != m_loaders.end() ? std::make_optional(query->second.get()) : std::nullopt;
}

std::optional<AssetUnloader*> AssetManager::find_unloader(RttiClass* rtti) {
std::optional<AssetUnloader*> AssetManager::find_unloader(const Strid& asset_rtti) {
std::lock_guard guard(m_mutex);
auto query = m_unloaders.find(rtti);
auto query = m_unloaders.find(asset_rtti);
return query != m_unloaders.end() ? std::make_optional(query->second.get()) : std::nullopt;
}

Expand Down
100 changes: 14 additions & 86 deletions engine/runtime/asset/asset_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,88 +74,17 @@ namespace wmoge {
AssetManager();
~AssetManager() = default;

/**
* @brief Async load of engine asset using provided asset name
*
* Allows to load game asset by its name. Assets are stored inside game
* asset pak file. Asset manager automatically resolves asset path by its name.
*
* @note Each asset must have a asset meta info file (in .xml) format. This meta info
* file allows to get asset reflection data, required to load asset at runtime.
*
* @note If asset already loaded and cached in the engine,
* the reference to loaded instance is returned.
*
* @note If asset is already queued to be loaded, reference to loaded asset is returned.
*
* @note Pass callback function to be notified when asset loading is finished.
* If asset already cached, this function will be called immediately before function return.
*
* @param name Unique name of the asset to load
* @param callback Callback to call on main thread when asset is loaded.
*
* @return Asset reference
*/
AsyncResult<Ref<Asset>> load_async(const AssetId& name, AssetCallback callback = AssetCallback());

/**
* @brief Sync load of the engine asset using provided asset name
*
* Allows to load game asset by its name. Assets are stored inside game
* asset pak file. Asset manager automatically resolves asset path by its name.
*
* @note Each asset must have a asset meta info file (in .xml) format. This meta info
* file allows to get asset reflection data, required to load asset at runtime.
*
* @note If asset already loaded and cached in the engine,
* the reference to loaded instance is returned.
*
* @note Sync asset loading is a simple and straightforward approach to work with
* assets. But blocks current thread and takes extra processing time in case of
* sync load of a large amount of assets. Consider using `load_async` method.
*
* @param name Unique name of the asset to load
*
* @return Asset reference
*/
Ref<Asset> load(const AssetId& name);

/** @brief Find a asset by name if it is already cached */
Ref<Asset> find(const AssetId& name);

/** @brief Add specific format asset loader */
void add_loader(Ref<AssetLoader> loader);

/** @brief Add specific format asset loader */
void add_unloader(Ref<AssetUnloader> unloader);

/** @brief Add additional pak for assets loading */
void add_pak(std::shared_ptr<AssetPak> pak);

/** @brief Find asset loader by loader name */
std::optional<AssetLoader*> find_loader(const Strid& loader);

/** @brief Find asset unloader by asset type */
std::optional<AssetUnloader*> find_unloader(RttiClass* rtti);

/** @brief Find asset meta by asset name */
std::optional<AssetMeta> find_meta(const AssetId& asset);

/**
* @brief Evicts all loaded assets from a cache
*
* Clear entirely cache of loaded assets. Any new asset loading
* operation will require asset loading from a disk.
*
* @note Clearing cache does not free memory of currently used assets
* in the engine due to strong memory references.
*/
void clear();

/**
* @brief Loads loaders into manager from global rtti info
*/
void load_loaders();
AsyncResult<Ref<Asset>> load_async(const AssetId& name, AssetCallback callback = AssetCallback());
Ref<Asset> load(const AssetId& name);
Ref<Asset> find(const AssetId& name);
void add_loader(Ref<AssetLoader> loader);
void add_unloader(Ref<AssetUnloader> unloader);
void add_pak(std::shared_ptr<AssetPak> pak);
std::optional<AssetLoader*> find_loader(const Strid& loader_rtti);
std::optional<AssetUnloader*> find_unloader(const Strid& asset_rtti);
std::optional<AssetMeta> find_meta(const AssetId& asset);
void clear();
void load_loaders();

private:
/**
Expand All @@ -174,12 +103,11 @@ namespace wmoge {
flat_map<AssetId, WeakRef<Asset>> m_assets;
flat_map<AssetId, LoadState> m_loading;
flat_map<Strid, Ref<AssetLoader>> m_loaders;
flat_map<RttiClass*, Ref<AssetUnloader>> m_unloaders;
flat_map<Strid, Ref<AssetUnloader>> m_unloaders;
std::shared_ptr<std::function<void(Asset*)>> m_callback;

class FileSystem* m_file_system = nullptr;
class RttiTypeStorage* m_type_storage = nullptr;
class EventManager* m_event_manager = nullptr;
class FileSystem* m_file_system = nullptr;
class RttiTypeStorage* m_type_storage = nullptr;

mutable std::recursive_mutex m_mutex;
};
Expand Down
91 changes: 2 additions & 89 deletions engine/runtime/debug/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,13 @@
#include "asset/asset_manager.hpp"
#include "core/log.hpp"
#include "core/string_utils.hpp"
#include "event/event.hpp"
#include "event/event_action.hpp"
#include "event/event_input.hpp"
#include "event/event_manager.hpp"
#include "gameplay/action_manager.hpp"
#include "math/math_utils.hpp"
#include "platform/time.hpp"
#include "platform/window.hpp"
#include "platform/window_manager.hpp"
#include "profiler/profiler.hpp"
#include "render/canvas.hpp"
#include "system/config.hpp"
#include "system/engine.hpp"
#include "system/ioc_container.hpp"

#include <algorithm>
Expand Down Expand Up @@ -146,93 +140,12 @@ namespace wmoge {
void Console::init() {
register_commands();
load_settings();

auto* event_manager = Engine::instance()->event_manager();
auto* action_manager = Engine::instance()->action_manager();

m_actions_listener = event_manager->subscribe<EventAction>([this, action_manager](const EventAction& event) {
// Opening closing
{
std::lock_guard guard(m_mutex);

if (event.name == SID("cn_trigger")) {
if (m_state == ConsoleState::Closed || m_state == ConsoleState::Closing) {
m_current_speed = m_speed_open;
m_state = ConsoleState::Opening;

action_manager->activate_all_except(SID("console"), false);

return true;
}
if (m_state == ConsoleState::Open || m_state == ConsoleState::Opening) {
m_current_speed = -m_speed_open;
m_state = ConsoleState::Closing;

action_manager->activate_all(true);

return true;
}
}
}

std::string line_to_process;

// Control
{
std::lock_guard guard(m_mutex);

if (m_state == ConsoleState::Open) {
if (event.name == SID("cn_delete") && !m_line.empty()) {
m_line.pop_back();
m_cursor_offset = m_console_font->get_string_size(m_line, m_text_size).x();
return true;
} else if (event.name == SID("cn_submit") && !m_line.empty()) {
m_cursor_offset = 0;
m_scroll_messages = 0;
std::swap(line_to_process, m_line);
} else if (event.name == SID("cn_scroll_up")) {
m_scroll_messages = Math::max(0, Math::min(m_scroll_messages + 1, static_cast<int>(m_messages.size()) - m_max_to_display));
return true;
} else if (event.name == SID("cn_scroll_down")) {
m_scroll_messages = Math::max(m_scroll_messages - 1, 0);
return true;
}
}
}

if (!line_to_process.empty()) {
process(line_to_process);
return true;
}

return false;
});

m_keyboard_listener = event_manager->subscribe<EventKeyboard>([this](const EventKeyboard& event) {
// Input
{
std::lock_guard guard(m_mutex);

if (m_state == ConsoleState::Open) {
if (event.action == InputAction::Text && !event.text.empty()) {
m_line += event.text;
m_cursor_offset = m_console_font->get_string_size(m_line, m_text_size).x();
return true;
}
}
}

return false;
});
}
void Console::shutdown() {
auto* event_manager = Engine::instance()->event_manager();

event_manager->unsubscribe(m_actions_listener);
event_manager->unsubscribe(m_keyboard_listener);

void Console::shutdown() {
m_console_font.reset();
}

void Console::update() {
WG_AUTO_PROFILE_DEBUG("Console::update");

Expand Down
4 changes: 0 additions & 4 deletions engine/runtime/debug/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "core/flat_map.hpp"
#include "core/string_id.hpp"
#include "core/var.hpp"
#include "event/event_listener.hpp"
#include "grc/font.hpp"
#include "math/color.hpp"

Expand Down Expand Up @@ -177,9 +176,6 @@ namespace wmoge {
std::vector<std::string> m_to_process;
std::recursive_mutex m_mutex;

EventListenerHnd m_actions_listener;
EventListenerHnd m_keyboard_listener;

Ref<Font> m_console_font;
Color4f m_color_back = Color::from_hex4(0x000000ee);
Color4f m_color_line = Color::from_hex4(0x060606be);
Expand Down
4 changes: 2 additions & 2 deletions engine/runtime/debug/debug_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace wmoge {
WG_AUTO_PROFILE_DEBUG("DebugLayer::on_start_frame");

auto engine = Engine::instance();
auto window = engine->window_manager()->primary_window();
auto window = engine->window_manager()->get_primary_window();
auto canvas_debug = engine->canvas_debug();
auto aux_draw_manager = engine->aux_draw_manager();
}
Expand All @@ -56,7 +56,7 @@ namespace wmoge {
auto canvas_debug = engine->canvas_debug();
auto aux_draw_manager = engine->aux_draw_manager();
auto console = engine->console();
auto window = engine->window_manager()->primary_window();
auto window = engine->window_manager()->get_primary_window();

console->update();
console->render();
Expand Down
6 changes: 5 additions & 1 deletion engine/runtime/ecs/ecs_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace wmoge {
struct EcsQuery {
using Bitset = std::bitset<EcsLimits::MAX_COMPONENTS>;

Bitset referenced;
Bitset read_only;
Bitset read_write;
Bitset required;
Expand All @@ -66,9 +67,12 @@ namespace wmoge {

template<typename Component>
EcsQuery& add(EcsComponentPresence presence = EcsComponentPresence::Required, EcsComponentAccess access = EcsComponentAccess::ReadOnly) {
assert(!referenced.test(Component::IDX));
referenced.set(Component::IDX);

if (presence == EcsComponentPresence::Exclude) {
exclude.set(Component::IDX);
return;
return *this;
}
if (presence == EcsComponentPresence::Required) {
required.set(Component::IDX);
Expand Down
Loading

0 comments on commit d0cf94d

Please sign in to comment.