From 1e66cd406b65fbeb0432d3536f0a6df630698618 Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Sat, 6 Feb 2016 14:18:01 -0600 Subject: [PATCH] Use location hash for ace events --- addons/common/XEH_preInit.sqf | 3 +- .../common/functions/fnc__handleNetEvent.sqf | 9 +-- .../common/functions/fnc_addEventHandler.sqf | 15 +--- addons/common/functions/fnc_localEvent.sqf | 9 +-- .../functions/fnc_removeAllEventHandlers.sqf | 9 +-- .../functions/fnc_removeEventHandler.sqf | 11 +-- addons/common/tests/script_component.hpp | 1 + addons/common/tests/test_eventHandlers.sqf | 78 +++++++++++++++++++ 8 files changed, 96 insertions(+), 39 deletions(-) create mode 100644 addons/common/tests/script_component.hpp create mode 100644 addons/common/tests/test_eventHandlers.sqf diff --git a/addons/common/XEH_preInit.sqf b/addons/common/XEH_preInit.sqf index ea5157c2e6..0ee560420f 100644 --- a/addons/common/XEH_preInit.sqf +++ b/addons/common/XEH_preInit.sqf @@ -256,7 +256,8 @@ PREP(addCuratorUnloadEventhandler); PREP(fixCrateContent); //ACE events global variables -GVAR(events) = [[],[]]; +GVAR(eventsLocation) = createLocation ["ACE_HashLocation", [-10000,-10000,-10000], 0, 0]; +GVAR(eventsLocation) setText QGVAR(eventsLocation); PREP(globalEvent); PREP(_handleNetEvent); diff --git a/addons/common/functions/fnc__handleNetEvent.sqf b/addons/common/functions/fnc__handleNetEvent.sqf index e3b2ac609c..54cef66228 100644 --- a/addons/common/functions/fnc__handleNetEvent.sqf +++ b/addons/common/functions/fnc__handleNetEvent.sqf @@ -16,12 +16,9 @@ params ["_eventType", "_event"]; if (_eventType == "ACEg") then { _event params ["_eventName", "_eventArgs"]; - GVAR(events) params ["_eventNames"]; - private _eventIndex = _eventNames find _eventName; - - if (_eventIndex != -1) then { - private _events = (GVAR(events) select 1) select _eventIndex; + private _eventFunctions = GVAR(eventsLocation) getVariable _eventName; + if (!isNil "_eventFunctions") then { #ifdef DEBUG_EVENTS ACE_LOGINFO_1("* Net Event %1",_eventName); @@ -35,7 +32,7 @@ if (_eventType == "ACEg") then { ACE_LOGINFO_1(" ID: %1",_forEachIndex); #endif }; - } forEach _events; + } forEach _eventFunctions; }; }; diff --git a/addons/common/functions/fnc_addEventHandler.sqf b/addons/common/functions/fnc_addEventHandler.sqf index 128f489701..e7df00f5ff 100644 --- a/addons/common/functions/fnc_addEventHandler.sqf +++ b/addons/common/functions/fnc_addEventHandler.sqf @@ -15,18 +15,11 @@ params ["_eventName", "_eventCode"]; -GVAR(events) params ["_eventNames"]; +private _eventFunctions = GVAR(eventsLocation) getVariable _eventName; -private _eventFunctions = []; -private _eventIndex = _eventNames find _eventName; - -if (_eventIndex != -1) then { - _eventFunctions = (GVAR(events) select 1) select _eventIndex; -} else { - private _eventNameCount = count _eventNames; - - _eventNames set [_eventNameCount, _eventName]; - (GVAR(events) select 1) set [_eventNameCount, _eventFunctions]; +if (isNil "_eventFunctions") then { + _eventFunctions = []; + GVAR(eventsLocation) setVariable [_eventName, _eventFunctions]; }; _eventFunctions pushBack _eventCode // Return event function count diff --git a/addons/common/functions/fnc_localEvent.sqf b/addons/common/functions/fnc_localEvent.sqf index 43f6209d0d..bf17cc0d0e 100644 --- a/addons/common/functions/fnc_localEvent.sqf +++ b/addons/common/functions/fnc_localEvent.sqf @@ -15,12 +15,9 @@ params ["_eventName", "_eventArgs"]; -GVAR(events) params ["_eventNames", "_eventArray"]; +private _eventFunctions = GVAR(eventsLocation) getVariable _eventName; -private _eventIndex = _eventNames find _eventName; - -if (_eventIndex != -1) then { - private _events = _eventArray select _eventIndex; +if (!isNil "_eventFunctions") then { #ifdef DEBUG_EVENTS ACE_LOGINFO_1("* Local Event: %1",_eventName); @@ -35,5 +32,5 @@ if (_eventIndex != -1) then { ACE_LOGINFO_1(" ID: %1",_forEachIndex); #endif }; - } forEach _events; + } forEach _eventFunctions; }; diff --git a/addons/common/functions/fnc_removeAllEventHandlers.sqf b/addons/common/functions/fnc_removeAllEventHandlers.sqf index a309d18d02..ed1cce278c 100644 --- a/addons/common/functions/fnc_removeAllEventHandlers.sqf +++ b/addons/common/functions/fnc_removeAllEventHandlers.sqf @@ -14,11 +14,4 @@ params ["_eventName"]; -GVAR(events) params ["_eventNames", "_events"]; - -private _eventFunctions = []; -private _eventIndex = _eventNames find _eventName; - -if (_eventIndex != -1) then { - _events set [_eventIndex, []]; -}; +GVAR(eventsLocation) setVariable [_eventName, nil]; diff --git a/addons/common/functions/fnc_removeEventHandler.sqf b/addons/common/functions/fnc_removeEventHandler.sqf index 5e307ad896..a0b5ed5333 100644 --- a/addons/common/functions/fnc_removeEventHandler.sqf +++ b/addons/common/functions/fnc_removeEventHandler.sqf @@ -15,12 +15,9 @@ params ["_eventName", "_eventCodeIndex"]; -GVAR(events) params ["_eventNames", "_events"]; +private _eventFunctions = GVAR(eventsLocation) getVariable _eventName; -private _eventFunctions = []; -private _eventIndex = _eventNames find _eventName; +if (isNil "_eventFunctions") exitWith {TRACE_1("eventName not found",_eventName);}; +if ((_eventCodeIndex < 0) || {(count _eventFunctions) <= _eventCodeIndex}) exitWith {TRACE_2("index out of bounds",_eventName,_eventCodeIndex);}; -if (_eventIndex != -1) then { - _eventFunctions = _events select _eventIndex; - _eventFunctions set [_eventCodeIndex, nil]; -}; +_eventFunctions set [_eventCodeIndex, nil]; diff --git a/addons/common/tests/script_component.hpp b/addons/common/tests/script_component.hpp new file mode 100644 index 0000000000..6a1bf9154d --- /dev/null +++ b/addons/common/tests/script_component.hpp @@ -0,0 +1 @@ +#include "\z\ace\addons\common\script_component.hpp" diff --git a/addons/common/tests/test_eventHandlers.sqf b/addons/common/tests/test_eventHandlers.sqf new file mode 100644 index 0000000000..5a75cb0a06 --- /dev/null +++ b/addons/common/tests/test_eventHandlers.sqf @@ -0,0 +1,78 @@ +// ---------------------------------------------------------------------------- +#define DEBUG_MODE_FULL +#include "script_component.hpp" + +#ifndef TEST_DEFINED_AND_OP +if (true) exitWith {}; +#endif + +// ---------------------------------------------------------------------------- + +LOG('Testing EventHandlers'); + +TEST_DEFINED(QFUNC(_handleNetEvent),""); +TEST_DEFINED(QFUNC(addEventHandler),""); +TEST_DEFINED(QFUNC(localEvent),""); +TEST_DEFINED(QFUNC(targetEvent),""); +TEST_DEFINED(QFUNC(globalEvent),""); +TEST_DEFINED(QFUNC(serverEvent),""); +TEST_DEFINED(QFUNC(removeAllEventHandlers),""); +TEST_DEFINED(QFUNC(removeEventHandler),""); + +private _result = ["A", {}] call ace_common_fnc_addEventHandler; +private _expected = 0; +TEST_DEFINED_AND_OP(_result,==,_expected,"Adding first A EH"); + +_result = ["A", {GVAR(test_A2) = _this}] call ace_common_fnc_addEventHandler; +_expected = 1; +TEST_DEFINED_AND_OP(_result,==,_expected,"Adding second A EH"); + +_result = ["A", {GVAR(test_A3) = _this}] call ace_common_fnc_addEventHandler; +_expected = 2; +TEST_DEFINED_AND_OP(_result,==,_expected,"Adding third A EH"); + +GVAR(test_A2) = -1; +["A", 11] call FUNC(localEvent); +_expected = 11; +_result = GVAR(test_A2); +TEST_DEFINED_AND_OP(_result,==,_expected,"Test Local Event"); + +//Remove 2nd EH +["A", 1] call FUNC(removeEventHandler); + +GVAR(test_A2) = -1; +GVAR(test_A3) = -1; +["A", 22] call FUNC(localEvent); +_expected = -1; +_result = GVAR(test_A2); +TEST_DEFINED_AND_OP(_result,==,_expected,"Test 2nd (removed) EH"); +_expected = 22; +_result = GVAR(test_A3); +TEST_DEFINED_AND_OP(_result,==,_expected,"Test 3rd Event"); + +//Remove All EH: +["A"] call FUNC(removeAllEventHandlers); + +GVAR(test_A3) = -1; +["A", 77] call FUNC(localEvent); +_expected = -1; +_result = GVAR(test_A3); +TEST_DEFINED_AND_OP(_result,==,_expected,"Test 3rd is removed after removeAll"); + +//Much harder to test network events +TRACE_2("testing network events",isServer,isDedicated); + +["B", {GVAR(test_B) = _this}] call ace_common_fnc_addEventHandler; + +GVAR(test_B) = -1; +["B", 33] call FUNC(globalEvent); +_expected = 33; +_result = GVAR(test_B); +TEST_DEFINED_AND_OP(_result,==,_expected,"Test globalEvent"); + +GVAR(test_B) = -1; +["B", 44] call FUNC(serverEvent); +_expected = if (isServer) then {44} else {-1}; +_result = GVAR(test_B); +TEST_DEFINED_AND_OP(_result,==,_expected,"Test serverEvent"); +