Skip to content

Commit cd368b2

Browse files
committed
Experimental mode
1 parent 4aee6c4 commit cd368b2

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

VCMP-LUA/Core.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ PluginInfo* g_Info;
99

1010
sol::state Lua;
1111

12+
static std::vector<std::string> s_ScriptFiles;
13+
14+
void reload_scripts();
15+
1216
int my_exception_handler(lua_State* L, sol::optional<const std::exception&> maybe_exception, sol::string_view description) {
1317
std::cout << "An exception occurred in a function ";
1418
if (maybe_exception) {
@@ -61,6 +65,8 @@ extern "C" EXPORT unsigned int VcmpPluginInit(PluginFuncs * pluginFuncs, PluginC
6165
RegisterClasses(&Lua);
6266
RegisterVCMPCallbacks();
6367

68+
bool experimental_mode = false;
69+
6470
// Load Configuration
6571
{
6672
std::list<CSimpleIniA::Entry> configOptions;
@@ -78,6 +84,9 @@ extern "C" EXPORT unsigned int VcmpPluginInit(PluginFuncs * pluginFuncs, PluginC
7884
else if (strcmp(setting.pItem, "logfile") == 0) {
7985
Logger::setFile(conf.GetValue("config", setting.pItem), 0, 0);
8086
}
87+
else if (strcmp(setting.pItem, "experimental") == 0) {
88+
experimental_mode = static_cast<bool>(conf.GetValue("config", setting.pItem));
89+
}
8190
}
8291
}
8392
else spdlog::warn("No configuration settings supplied, using defaults");
@@ -89,6 +98,7 @@ extern "C" EXPORT unsigned int VcmpPluginInit(PluginFuncs * pluginFuncs, PluginC
8998
if (conf.GetAllValues("scripts", "script", scripts) && scripts.size() > 0) {
9099
for (auto it = scripts.begin(); it != scripts.end(); it++) {
91100
auto result = Lua.safe_script_file(it->pItem, sol::script_pass_on_error);
101+
s_ScriptFiles.emplace_back(it->pItem);
92102
if (!result.valid()) {
93103
sol::error e = result;
94104
spdlog::error("Failed to load script: {}", e.what());
@@ -97,6 +107,34 @@ extern "C" EXPORT unsigned int VcmpPluginInit(PluginFuncs * pluginFuncs, PluginC
97107
}
98108
else spdlog::error("No Lua scripts specified to load");
99109
}
100-
110+
111+
if (experimental_mode) {
112+
spdlog::warn("Experimental features may be really unstable, be very careful when using them.");
113+
Lua["__reload_scripts"] = &reload_scripts;
114+
}
115+
101116
return 1;
117+
}
118+
119+
void reload_scripts()
120+
{
121+
spdlog::warn("Reloading scripts...");
122+
123+
// Re-trigger some crucial events which simulate a 'restart'
124+
EventManager::Trigger("onServerShutdown");
125+
// Reset the event handlers to avoid duplication
126+
EventManager::Reset();
127+
128+
for (const auto& scriptFile : s_ScriptFiles)
129+
{
130+
spdlog::info("Parsing script: {}", scriptFile);
131+
auto result = Lua.safe_script_file(scriptFile, sol::script_pass_on_error);
132+
if (!result.valid()) {
133+
sol::error e = result;
134+
spdlog::error("Failed to load script: {}", e.what());
135+
}
136+
}
137+
138+
// Re-trigger some crucial events which simulate a 'restart'
139+
EventManager::Trigger("onServerInit");
102140
}

VCMP-LUA/vcmpWrap/EventManager/EventManager.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ void EventManager::Init(sol::state* Lua) {
7474
userdata["cancel"] = &EventManager::cancel;
7575
}
7676

77+
void EventManager::Reset() {
78+
for (auto& [eventKey, eventHandlers] : m_Handlers)
79+
eventHandlers.clear();
80+
}
81+
82+
void EventManager::Trigger(const std::string& eventName) {
83+
// This is used by the plugin itself not by Lua
84+
if (eventExists(eventName)) {
85+
auto handlers = GetHandlers(eventName);
86+
if(handlers.size() > 0)
87+
for (auto fn : handlers) {
88+
fn();
89+
if (EventManager::m_bWasEventCancelled) {
90+
EventManager::cancelEvent();
91+
break;
92+
}
93+
}
94+
}
95+
}
96+
7797
const std::vector<sol::function>& EventManager::GetHandlers(std::string eventName) {
7898
return m_Handlers[eventName];
7999
}

VCMP-LUA/vcmpWrap/EventManager/EventManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
class EventManager {
55
public:
66
static void Init(sol::state* Lua);
7+
static void Reset();
8+
static void Trigger(const std::string& eventName);
79
static const std::vector<sol::function>& GetHandlers(std::string eventName);
810
static bool eventExists(const std::string& eventName);
911
static inline void cancelEvent() { m_bWasEventCancelled = false; }

0 commit comments

Comments
 (0)