Skip to content

Commit

Permalink
👼 Script: added SCRIPT_(UN)LOADED event types.
Browse files Browse the repository at this point in the history
  • Loading branch information
ohlidalp committed Jul 23, 2023
1 parent a73a252 commit dcf0863
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
9 changes: 8 additions & 1 deletion source/main/gameplay/ScriptEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion source/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,21 @@ int main(int argc, char *argv[])
{
LoadScriptRequest* request = static_cast<LoadScriptRequest*>(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;
}

case MSG_APP_UNLOAD_SCRIPT_REQUESTED:
{
ScriptUnitId_t* id = static_cast<ScriptUnitId_t*>(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;
Expand Down
7 changes: 7 additions & 0 deletions source/main/scripting/bindings/ScriptEventsAngelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
2 changes: 1 addition & 1 deletion source/main/system/ConsoleCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dcf0863

Please sign in to comment.