From dcf0863077d726abdf0f87627e0afe33e0a31473 Mon Sep 17 00:00:00 2001 From: Petr Ohlidal Date: Sun, 23 Jul 2023 03:06:49 +0200 Subject: [PATCH] :angel: Script: added `SCRIPT_(UN)LOADED` event types. --- source/main/gameplay/ScriptEvents.h | 9 ++++++++- source/main/main.cpp | 9 ++++++++- .../main/scripting/bindings/ScriptEventsAngelscript.cpp | 7 +++++++ source/main/system/ConsoleCmd.cpp | 2 +- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/source/main/gameplay/ScriptEvents.h b/source/main/gameplay/ScriptEvents.h index fc67d95277..c051bdbbc2 100644 --- a/source/main/gameplay/ScriptEvents.h +++ b/source/main/gameplay/ScriptEvents.h @@ -51,7 +51,7 @@ enum scriptEvents SE_TRUCK_TELEPORT = BITMASK(16), //!< triggered when the user teleports the truck, the argument refers to the actor ID of the vehicle SE_TRUCK_MOUSE_GRAB = BITMASK(17), //!< triggered when the user uses the mouse to interact with the actor, the argument refers to the actor ID - SE_ANGELSCRIPT_MANIPULATIONS = BITMASK(18), //!< triggered when the user tries to dynamically use the scripting capabilities (prevent cheating) + SE_ANGELSCRIPT_MANIPULATIONS = BITMASK(18), //!< triggered when the user tries to dynamically use the scripting capabilities (prevent cheating) args: #1 angelScriptManipulationType, #2 ScriptUnitId_t, #3 RoR::ScriptCategory, #4 unused, #5 filename SE_ANGELSCRIPT_MSGCALLBACK = BITMASK(19), //!< The diagnostic info directly from AngelScript engine (see `asSMessageInfo`), args: #1 ScriptUnitID, #2 asEMsgType, #3 row, #4 col, #5 sectionName, #6 message SE_GENERIC_MESSAGEBOX_CLICK = BITMASK(20), //!< triggered when the user clicks on a message box button, the argument refers to the button pressed @@ -60,6 +60,13 @@ enum scriptEvents }; +enum angelScriptManipulationType +{ + MANIP_CONSOLE_SNIPPET_EXECUTED = 0, // Backwards compat + MANIP_SCRIPT_LOADED, + MANIP_SCRIPT_UNLOADED +}; + /// Args for `eventCallbackEx()` queued via `MSG_SIM_SCRIPT_EVENT_TRIGGERED` /// See descriptions at `enum RoR::scriptEvents`. struct ScriptEventArgs diff --git a/source/main/main.cpp b/source/main/main.cpp index 4dbb8711a6..10816a0d32 100644 --- a/source/main/main.cpp +++ b/source/main/main.cpp @@ -398,7 +398,10 @@ int main(int argc, char *argv[]) { LoadScriptRequest* request = static_cast(m.payload); ActorPtr actor = App::GetGameContext()->GetActorManager()->GetActorById(request->lsr_associated_actor); - App::GetScriptEngine()->loadScript(request->lsr_filename, request->lsr_category, actor, request->lsr_buffer); + ScriptUnitId_t nid = App::GetScriptEngine()->loadScript(request->lsr_filename, request->lsr_category, actor, request->lsr_buffer); + // we want to notify any running scripts that we might change something (prevent cheating) + App::GetScriptEngine()->triggerEvent(SE_ANGELSCRIPT_MANIPULATIONS, + MANIP_SCRIPT_LOADED, nid, (int)request->lsr_category, 0, request->lsr_filename); delete request; break; } @@ -406,6 +409,10 @@ int main(int argc, char *argv[]) case MSG_APP_UNLOAD_SCRIPT_REQUESTED: { ScriptUnitId_t* id = static_cast(m.payload); + ScriptUnit& unit = App::GetScriptEngine()->getScriptUnit(*id); + // we want to notify any running scripts that we might change something (prevent cheating) + App::GetScriptEngine()->triggerEvent(SE_ANGELSCRIPT_MANIPULATIONS, + MANIP_SCRIPT_UNLOADED, *id, (int)unit.scriptCategory, 0, unit.scriptName); App::GetScriptEngine()->unloadScript(*id); delete id; break; diff --git a/source/main/scripting/bindings/ScriptEventsAngelscript.cpp b/source/main/scripting/bindings/ScriptEventsAngelscript.cpp index 74f413d8ed..1b827a887e 100644 --- a/source/main/scripting/bindings/ScriptEventsAngelscript.cpp +++ b/source/main/scripting/bindings/ScriptEventsAngelscript.cpp @@ -61,5 +61,12 @@ void RoR::RegisterScriptEvents(asIScriptEngine *engine) result = engine->RegisterEnumValue("scriptEvents", "SE_GENERIC_MESSAGEBOX_CLICK", SE_GENERIC_MESSAGEBOX_CLICK); ROR_ASSERT(result>=0); result = engine->RegisterEnumValue("scriptEvents", "SE_ALL_EVENTS", SE_ALL_EVENTS); ROR_ASSERT(result>=0); + + // enum angelScriptManipulationType + result = engine->RegisterEnum("angelScriptManipulationType"); ROR_ASSERT(result>=0); + + result = engine->RegisterEnumValue("angelScriptManipulationType", "ASMANIP_CONSOLE_SNIPPET_EXECUTED", MANIP_CONSOLE_SNIPPET_EXECUTED); ROR_ASSERT(result >= 0); + result = engine->RegisterEnumValue("angelScriptManipulationType", "ASMANIP_SCRIPT_LOADED", MANIP_SCRIPT_LOADED); ROR_ASSERT(result >= 0); + result = engine->RegisterEnumValue("angelScriptManipulationType", "ASMANIP_SCRIPT_UNLOADED", MANIP_SCRIPT_UNLOADED); ROR_ASSERT(result >= 0); } diff --git a/source/main/system/ConsoleCmd.cpp b/source/main/system/ConsoleCmd.cpp index a77109c3ab..08454d4e10 100644 --- a/source/main/system/ConsoleCmd.cpp +++ b/source/main/system/ConsoleCmd.cpp @@ -331,7 +331,7 @@ class AsCmd: public ConsoleCmd #ifdef USE_ANGELSCRIPT // we want to notify any running scripts that we might change something (prevent cheating) - App::GetScriptEngine()->triggerEvent(SE_ANGELSCRIPT_MANIPULATIONS); + App::GetScriptEngine()->triggerEvent(SE_ANGELSCRIPT_MANIPULATIONS, MANIP_CONSOLE_SNIPPET_EXECUTED); // Re-compose the code snippet Str<1000> code;