diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index 098d8fb292..e0a5e2df45 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -549,6 +549,22 @@ namespace Config writer->WriteInt32("hinting_threshold", model->hinting_threshold); } + static void ReadPlugin(IIniReader* reader) + { + if (reader->ReadSection("plugin")) + { + auto model = &gConfigPlugin; + model->enable_hot_reloading = reader->GetBoolean("enable_hot_reloading", false); + } + } + + static void WritePlugin(IIniWriter* writer) + { + auto model = &gConfigPlugin; + writer->WriteSection("plugin"); + writer->WriteBoolean("enable_hot_reloading", model->enable_hot_reloading); + } + static bool SetDefaults() { try @@ -561,6 +577,7 @@ namespace Config ReadNotifications(reader.get()); ReadTwitch(reader.get()); ReadFont(reader.get()); + ReadPlugin(reader.get()); return true; } catch (const std::exception&) @@ -582,6 +599,7 @@ namespace Config ReadNotifications(reader.get()); ReadTwitch(reader.get()); ReadFont(reader.get()); + ReadPlugin(reader.get()); return true; } catch (const std::exception&) @@ -606,6 +624,7 @@ namespace Config WriteNotifications(writer.get()); WriteTwitch(writer.get()); WriteFont(writer.get()); + WritePlugin(writer.get()); return true; } catch (const std::exception& ex) @@ -721,6 +740,7 @@ TwitchConfiguration gConfigTwitch; NetworkConfiguration gConfigNetwork; NotificationConfiguration gConfigNotifications; FontConfiguration gConfigFonts; +PluginConfiguration gConfigPlugin; void config_set_defaults() { diff --git a/src/openrct2/config/Config.h b/src/openrct2/config/Config.h index a001e44623..feb1c3a1ae 100644 --- a/src/openrct2/config/Config.h +++ b/src/openrct2/config/Config.h @@ -203,6 +203,11 @@ struct FontConfiguration int32_t hinting_threshold; }; +struct PluginConfiguration +{ + bool enable_hot_reloading; +}; + enum SORT { SORT_NAME_ASCENDING, @@ -238,6 +243,7 @@ extern TwitchConfiguration gConfigTwitch; extern NetworkConfiguration gConfigNetwork; extern NotificationConfiguration gConfigNotifications; extern FontConfiguration gConfigFonts; +extern PluginConfiguration gConfigPlugin; bool config_open(const utf8* path); bool config_save(const utf8* path); diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index 8632d94028..9a318c9098 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -10,6 +10,7 @@ #include "ScriptEngine.h" #include "../PlatformEnvironment.h" +#include "../config/Config.h" #include "../core/FileScanner.h" #include "../core/Path.hpp" #include "../interface/InteractiveConsole.h" @@ -83,53 +84,56 @@ void ScriptEngine::Initialise() void ScriptEngine::LoadPlugins() { - auto base = _env.GetDirectoryPath(DIRBASE::USER, DIRID::PLUGIN); - auto pattern = Path::Combine(base, "*.js"); - auto scanner = std::unique_ptr(Path::ScanDirectory(pattern, true)); - while (scanner->Next()) + if (!_initialised) { - auto path = std::string(scanner->GetPath()); - if (ShouldLoadScript(path)) - { - try - { - auto plugin = std::make_shared(_context, path); - ScriptExecutionInfo::PluginScope scope(_execInfo, plugin); - plugin->Load(); - - auto metadata = plugin->GetMetadata(); - if (metadata.MinApiVersion <= OPENRCT2_PLUGIN_API_VERSION) - { - LogPluginInfo(plugin, "Loaded"); - _plugins.push_back(std::move(plugin)); - } - else - { - LogPluginInfo(plugin, "Requires newer API version: v" + std::to_string(metadata.MinApiVersion)); - } - } - catch (const std::exception& e) - { - _console.WriteLineError(e.what()); - } - } + Initialise(); } + auto base = _env.GetDirectoryPath(DIRBASE::USER, DIRID::PLUGIN); + if (Path::DirectoryExists(base)) + { + auto pattern = Path::Combine(base, "*.js"); + auto scanner = std::unique_ptr(Path::ScanDirectory(pattern, true)); + while (scanner->Next()) + { + auto path = std::string(scanner->GetPath()); + if (ShouldLoadScript(path)) + { + LoadPlugin(path); + } + } + + if (gConfigPlugin.enable_hot_reloading) + { + SetupHotReloading(); + } + } + _pluginsLoaded = true; +} + +void ScriptEngine::LoadPlugin(const std::string& path) +{ try { - // Enable hot reloading - _pluginFileWatcher = std::make_unique(base); - _pluginFileWatcher->OnFileChanged = [this](const std::string& path) { - std::lock_guard guard(_changedPluginFilesMutex); - _changedPluginFiles.push_back(path); - }; + auto plugin = std::make_shared(_context, path); + ScriptExecutionInfo::PluginScope scope(_execInfo, plugin); + plugin->Load(); + + auto metadata = plugin->GetMetadata(); + if (metadata.MinApiVersion <= OPENRCT2_PLUGIN_API_VERSION) + { + LogPluginInfo(plugin, "Loaded"); + _plugins.push_back(std::move(plugin)); + } + else + { + LogPluginInfo(plugin, "Requires newer API version: v" + std::to_string(metadata.MinApiVersion)); + } } catch (const std::exception& e) { - std::printf("Unable to enable hot reloading of plugins: %s\n", e.what()); + _console.WriteLineError(e.what()); } - - _pluginsLoaded = true; } bool ScriptEngine::ShouldLoadScript(const std::string& path) @@ -138,6 +142,23 @@ bool ScriptEngine::ShouldLoadScript(const std::string& path) return path.find("/node_modules/") == std::string::npos && path.find("\\node_modules\\") == std::string::npos; } +void ScriptEngine::SetupHotReloading() +{ + try + { + auto base = _env.GetDirectoryPath(DIRBASE::USER, DIRID::PLUGIN); + _pluginFileWatcher = std::make_unique(base); + _pluginFileWatcher->OnFileChanged = [this](const std::string& path) { + std::lock_guard guard(_changedPluginFilesMutex); + _changedPluginFiles.emplace(path); + }; + } + catch (const std::exception& e) + { + std::printf("Unable to enable hot reloading of plugins: %s\n", e.what()); + } +} + void ScriptEngine::AutoReloadPlugins() { if (_changedPluginFiles.size() > 0) diff --git a/src/openrct2/scripting/ScriptEngine.h b/src/openrct2/scripting/ScriptEngine.h index 398e63bf78..70556f436a 100644 --- a/src/openrct2/scripting/ScriptEngine.h +++ b/src/openrct2/scripting/ScriptEngine.h @@ -19,6 +19,7 @@ #include #include #include +#include #include struct duk_hthread; @@ -102,7 +103,7 @@ namespace OpenRCT2::Scripting ScriptExecutionInfo _execInfo; std::unique_ptr _pluginFileWatcher; - std::vector _changedPluginFiles; + std::unordered_set _changedPluginFiles; std::mutex _changedPluginFilesMutex; public: @@ -133,7 +134,9 @@ namespace OpenRCT2::Scripting void Initialise(); void StartPlugins(); void StopPlugins(); + void LoadPlugin(const std::string& path); bool ShouldLoadScript(const std::string& path); + void SetupHotReloading(); void AutoReloadPlugins(); void ProcessREPL(); };