Skip to content

Commit

Permalink
PluginLoader: Call initialize inside game thread for safety
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Mar 21, 2024
1 parent a60f06c commit 87b82c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/mods/PluginLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,7 @@ void PluginLoader::early_init() try {
}

spdlog::info("[PluginLoader] Loading plugins...");

LoadLibraryA("ijwhost.dll");

// Load all dlls in the plugins directory.
for (auto&& entry : fs::directory_iterator{plugin_path}) {
auto&& path = entry.path();
Expand All @@ -645,7 +644,13 @@ void PluginLoader::early_init() try {
spdlog::error("[PluginLoader] Unknown exception during early init");
}

std::optional<std::string> PluginLoader::on_initialize() {
void PluginLoader::on_frame() {
if (m_plugins_loaded) {
return;
}

m_plugins_loaded = true;

std::scoped_lock _{m_mux};

// Call reframework_plugin_required_version on any dlls that export it.
Expand All @@ -663,9 +668,6 @@ std::optional<std::string> PluginLoader::on_initialize() {
reframework::g_renderer_data.device = d3d12->get_device();
reframework::g_renderer_data.swapchain = d3d12->get_swap_chain();
reframework::g_renderer_data.command_queue = d3d12->get_command_queue();
} else {
spdlog::error("[PluginLoader] Unsupported renderer type {}", reframework::g_renderer_data.renderer_type);
return "PluginLoader: Unsupported renderer type detected";
}

verify_sdk_pointers();
Expand Down Expand Up @@ -759,8 +761,6 @@ std::optional<std::string> PluginLoader::on_initialize() {

++it;
}

return std::nullopt;
}

void PluginLoader::on_draw_ui() {
Expand Down
4 changes: 3 additions & 1 deletion src/mods/PluginLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class PluginLoader : public Mod {
void early_init();

std::string_view get_name() const override { return "PluginLoader"; }
std::optional<std::string> on_initialize() override;
void on_frame() override;
void on_draw_ui() override;

private:
bool m_plugins_loaded{false};

std::mutex m_mux{};
std::map<std::string, HMODULE> m_plugins{};
std::map<std::string, std::string> m_plugin_load_errors{};
Expand Down

0 comments on commit 87b82c4

Please sign in to comment.