Skip to content

Commit

Permalink
module util
Browse files Browse the repository at this point in the history
  • Loading branch information
Hatrickek committed Feb 22, 2024
1 parent 37faf6d commit 1a9bb27
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
18 changes: 3 additions & 15 deletions Oxylus/src/Core/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#include "ProjectSerializer.h"

#include "Modules/ModuleInterface.h"

#include "Scripting/LuaManager.h"
#include "Modules/ModuleUtil.h"

#include "Utils/Log.h"
#include "Utils/Profiler.h"
Expand All @@ -25,22 +24,11 @@ void Project::load_module() {
return;

const auto module_path = FileSystem::append_paths(get_project_directory(), project_config.module_name);
const auto lib = ModuleRegistry::get()->add_lib(project_config.module_name, module_path);
if (!lib)
return;

auto* state = LuaManager::get()->get_state();
lib->interface->register_components(state, entt::locator<entt::meta_ctx>::handle());
OX_CORE_INFO("Successfully loaded module: {}", project_config.module_name);
ModuleUtil::load_module(project_config.module_name, module_path);
}

void Project::unload_module() const {
const auto lib = ModuleRegistry::get()->get_lib(project_config.module_name);
if (!lib)
return;

auto* state = LuaManager::get()->get_state();
lib->interface->unregister_components(state, entt::locator<entt::meta_ctx>::handle());
ModuleUtil::unload_module(project_config.module_name);
}

Shared<Project> Project::create_new() {
Expand Down
4 changes: 2 additions & 2 deletions Oxylus/src/Modules/ModuleRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Utils/Log.h"

namespace Ox {
Module* ModuleRegistry::add_lib(std::string_view name, std::string_view path) {
Module* ModuleRegistry::add_lib(const std::string& name, std::string_view path) {
try {
auto lib = create_unique<dylib>(path);

Expand All @@ -25,7 +25,7 @@ Module* ModuleRegistry::add_lib(std::string_view name, std::string_view path) {
}
}

Module* ModuleRegistry::get_lib(std::string_view name) {
Module* ModuleRegistry::get_lib(const std::string& name) {
try {
return libs.at(name).get();
}
Expand Down
6 changes: 3 additions & 3 deletions Oxylus/src/Modules/ModuleRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ struct Module {

class ModuleRegistry {
public:
Module* add_lib(std::string_view name, std::string_view path);
Module* get_lib(std::string_view name);
Module* add_lib(const std::string& name, std::string_view path);
Module* get_lib(const std::string& name);
void clear();

static ModuleRegistry* get();

private:
ankerl::unordered_dense::map<std::string_view, Unique<Module>> libs = {};
ankerl::unordered_dense::map<std::string, Unique<Module>> libs = {};
};
}
29 changes: 29 additions & 0 deletions Oxylus/src/Modules/ModuleUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "ModuleUtil.h"

#include "ModuleInterface.h"
#include "ModuleRegistry.h"

#include "Scripting/LuaManager.h"

#include "Utils/Log.h"

namespace Ox {
void ModuleUtil::load_module(const std::string& name, const std::string& path) {
const auto lib = ModuleRegistry::get()->add_lib(name, path);
if (!lib)
return;

auto* state = LuaManager::get()->get_state();
lib->interface->register_components(state, entt::locator<entt::meta_ctx>::handle());
OX_CORE_INFO("Successfully loaded module: {}", name);
}

void ModuleUtil::unload_module(const std::string& module_name) {
const auto lib = ModuleRegistry::get()->get_lib(module_name);
if (!lib)
return;

auto* state = LuaManager::get()->get_state();
lib->interface->unregister_components(state, entt::locator<entt::meta_ctx>::handle());
}
}
12 changes: 12 additions & 0 deletions Oxylus/src/Modules/ModuleUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <string>

namespace Ox {
class ModuleUtil {
public:
static void load_module(const std::string& name, const std::string& path);
static void unload_module(const std::string& module_name);
};
}

0 comments on commit 1a9bb27

Please sign in to comment.