Add config option for hot reload and refactor

This commit is contained in:
Ted John committed 2020-04-26 14:35:01 +01:00
1 parent 3ac22476c6
commit 0e8c627324
4 files changed
+89 -39

No files matched your search

+59 -38
View File
@@ -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<IFileScanner>(Path::ScanDirectory(pattern, true));
while (scanner->Next())
if (!_initialised)
{
auto path = std::string(scanner->GetPath());
if (ShouldLoadScript(path))
{
try
{
auto plugin = std::make_shared<Plugin>(_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<IFileScanner>(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<FileWatcher>(base);
_pluginFileWatcher->OnFileChanged = [this](const std::string& path) {
std::lock_guard<std::mutex> guard(_changedPluginFilesMutex);
_changedPluginFiles.push_back(path);
};
auto plugin = std::make_shared<Plugin>(_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<FileWatcher>(base);
_pluginFileWatcher->OnFileChanged = [this](const std::string& path) {
std::lock_guard<std::mutex> 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)