Skip to content

Commit

Permalink
Merge branch 'cacheConfigCompileEH' of https://github.com/dedmen/CBA_A3
Browse files Browse the repository at this point in the history
… into dedmen-cacheConfigCompileEH
  • Loading branch information
commy2 committed Feb 16, 2019
2 parents d6cbc0f + f36e4ba commit 565352d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 75 deletions.
138 changes: 70 additions & 68 deletions addons/xeh/fnc_compileEventHandlers.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,76 @@ params [["_baseConfig", configNull, [configNull]]];

private _result = [];
private _resultNames = [];
private _allowRecompile = (_baseConfig isEqualTo configFile);
if (_allowRecompile) then {
if ((["compile"] call CBA_fnc_isRecompileEnabled) || {isFilePatchingEnabled}) then {
XEH_LOG("XEH: init function preProcessing disabled [recompile or filepatching enabled]");
_allowRecompile = false;
};
};

// note: format is never used for config parsing here, because of it's 8192 character limitation.

{
private _eventName = _x;

{
private _eventFunc = "";
private _funcAll = "";
private _funcClient = "";
private _funcServer = "";
private _customName = configName _x;

if (isClass _x) then {
// events on clients and server
private _entry = _x >> "init";

if (isText _entry) then {
_eventFunc = _eventFunc + getText _entry + ";";
_funcAll = getText _entry;
};

// client only events
if (!isDedicated) then {
_entry = _x >> "clientInit";

if (isText _entry) then {
_eventFunc = _eventFunc + getText _entry + ";";
};
_entry = _x >> "clientInit";
if (isText _entry) then {
_funcClient = getText _entry;
};

// server only events
if (isServer) then {
_entry = _x >> "serverInit";

if (isText _entry) then {
_eventFunc = _eventFunc + getText _entry + ";";
};
_entry = _x >> "serverInit";
if (isText _entry) then {
_funcServer = getText _entry;
};
} else {
// global events
if (isText _x) then {
_eventFunc = getText _x + ";";
_funcAll = getText _x;
};
};

if !(_eventFunc isEqualTo "") then {

//Optimize "QUOTE(call COMPILE_FILE(XEH_preInit));" down to just the content of the EH script
if (!(["compile"] call CBA_fnc_isRecompileEnabled) && { //Users might expect their preInit script to be reloaded everytime when debugging
(toLower (_eventFunc select [0,40])) isEqualTo "call compile preprocessfilelinenumbers '" && {
(_eventFunc select [count _eventFunc -2]) isEqualTo "';"
}}) then {
private _funcPath = _eventFunc select [40, count _eventFunc - 42];
//If there is a quote mark in the path, then something went wrong and we got multiple paths, just skip optimization
//Example cause: "call COMPILE_FILE(XEH_preInit);call COMPILE_FILE(XEH_preClientInit)"
if (_funcPath find "'" == -1) then {
_eventFunc = preprocessFileLineNumbers _funcPath;

TRACE_2("eventfunction redirected",_customName,_funcPath);
private _eventFuncs = [_funcAll, _funcClient, _funcServer] apply {
if (_x == "") then {
TRACE_2("does NOT do something",_customName,_eventName);
{} // apply return
} else {
TRACE_2("does something",_customName,_eventName);
private _eventFunc = _x;

//Optimize "QUOTE(call COMPILE_FILE(XEH_preInit));" down to just the content of the EH script
if (_allowRecompile) then {
if (((toLower (_eventFunc select [0,40])) isEqualTo "call compile preprocessfilelinenumbers '")&& {(_eventFunc select [count _eventFunc - 1]) isEqualTo "'"}) then {
private _funcPath = _eventFunc select [40, count _eventFunc - 41];
//If there is a quote mark in the path, then something went wrong and we got multiple paths, just skip optimization
//Example cause: "call COMPILE_FILE(XEH_preInit);call COMPILE_FILE(XEH_preClientInit)"
if (_funcPath find "'" == -1) then {
_eventFunc = preprocessFileLineNumbers _funcPath;
TRACE_2("eventfunction redirected",_customName,_funcPath);
};
};
// if (_eventFunc isEqualTo _x) then { diag_log text format ["XEH: Could not recompile [%1-%2]: %3", _eventName, _customName, _eventFunc]; };
};
compile _eventFunc // apply return
};

_eventFunc = compile _eventFunc;
TRACE_2("does something",_customName,_eventName);
} else {
_eventFunc = {};
TRACE_2("does NOT do something",_customName,_eventName);
};

_result pushBack ["", _eventName, _eventFunc];
_result pushBack ["", _eventName, _eventFuncs];
_resultNames pushBack _customName;
} forEach configProperties [_baseConfig >> XEH_FORMAT_CONFIG_NAME(_eventName)];
} forEach ["preInit", "postInit"];
Expand All @@ -119,10 +122,12 @@ private _resultNames = [];
};

{
private _eventFunc = _eventFuncBase;
private _customName = configName _x;
private _allowInheritance = true;
private _excludedClasses = [];
private _funcAll = "";
private _funcClient = "";
private _funcServer = "";

if (isClass _x) then {
// allow inheritance of this event?
Expand All @@ -132,6 +137,9 @@ private _resultNames = [];
_allowInheritance = getNumber _scope != 0;
};

// init event handlers that should run on respawn again, onRespawn = 1
private _onRespawn = toLower _eventName in ["init", "initpost"] && {getNumber (_x >> "onRespawn") == 1};

// classes excluded from this event
private _exclude = _x >> "exclude";

Expand All @@ -145,37 +153,28 @@ private _resultNames = [];

// events on clients and server
private _entry = _x >> _entryName;

if (isText _entry) then {
_eventFunc = _eventFunc + getText _entry + ";";
_funcAll = _eventFuncBase + getText _entry + ";";
if (_onRespawn) then { _funcAll = _funcAll + "(_this select 0) addEventHandler ['Respawn', " + str _funcAll + "];"; };
};

// client only events
if (!isDedicated) then {
_entry = _x >> format ["client%1", _entryName];

if (isText _entry) then {
_eventFunc = _eventFunc + getText _entry + ";";
};
_entry = _x >> format ["client%1", _entryName];
if (isText _entry) then {
_funcClient = _eventFuncBase + getText _entry + ";";
if (_onRespawn) then { _funcClient = _funcClient + "(_this select 0) addEventHandler ['Respawn', " + str _funcClient + "];"; };
};

// server only events
if (isServer) then {
_entry = _x >> format ["server%1", _entryName];

if (isText _entry) then {
_eventFunc = _eventFunc + getText _entry + ";";
};
};

// init event handlers that should run on respawn again, onRespawn = 1
if (toLower _eventName in ["init", "initpost"] && {getNumber (_x >> "onRespawn") == 1}) then {
_eventFunc = _eventFunc + "(_this select 0) addEventHandler ['Respawn', " + str _eventFunc + "];";
_entry = _x >> format ["server%1", _entryName];
if (isText _entry) then {
_funcServer = _eventFuncBase + getText _entry + ";";
if (_onRespawn) then { _funcServer = _funcServer + "(_this select 0) addEventHandler ['Respawn', " + str _funcServer + "];"; };
};
} else {
// global events
if (isText _x) then {
_eventFunc = _eventFunc + getText _x + ";";
_funcAll = _eventFuncBase + getText _x + ";";
};
};

Expand All @@ -197,19 +196,22 @@ private _resultNames = [];
};
} forEach _resultNames;

// only add event on machines where it exists
if !(_eventFunc isEqualTo _eventFuncBase) then {
_eventFunc = compile _eventFunc;
TRACE_3("does something",_customName,_className,_eventName);
} else {
_eventFunc = {};
TRACE_3("does NOT do something",_customName,_className,_eventName);
private _eventFuncs = [_funcAll, _funcClient, _funcServer] apply {
// only add event on machines where it exists
if (_x == "") then {
TRACE_3("does NOT do something",_customName,_className,_eventName);
{}
} else {
TRACE_3("does something",_customName,_className,_eventName);
compile _x
};
};

_result pushBack [_className, _eventName, _eventFunc, _allowInheritance, _excludedClasses];
_result pushBack [_className, _eventName, _eventFuncs, _allowInheritance, _excludedClasses];
_resultNames pushBack _customName;
} forEach configProperties [_x];
} forEach configProperties [_baseConfig >> XEH_FORMAT_CONFIG_NAME(_eventName), "isClass _x"];
} forEach [XEH_EVENTS];

_result select {!((_x select 2) isEqualTo {})}
TRACE_2("compiled",_baseConfig,count _result);

_result select {!((_x select 2) isEqualTo [{},{},{}])}
11 changes: 10 additions & 1 deletion addons/xeh/fnc_postInit_unscheduled.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ if (CBA_missionTime == -1) then {
// call PostInit events
{
if (_x select 1 == "postInit") then {
[] call (_x select 2);
(_x select 2) params ["_funcAll", "_funcClient", "_funcServer"];
if (!(_funcAll isEqualTo {})) then {
[] call _funcAll;
};
if ((!isDedicated) && {!(_funcClient isEqualTo {})}) then {
[] call _funcClient;
};
if ((isServer) && {!(_funcServer isEqualTo {})}) then {
[] call _funcServer;
};
};
} forEach GVAR(allEventHandlers);

Expand Down
32 changes: 26 additions & 6 deletions addons/xeh/fnc_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ private _cfgVehicles = configFile >> "CfgVehicles";
XEH_LOG("XEH: Compiling XEH START");
#endif

GVAR(allEventHandlers) = [];
//Get configFile eventhandlers from cache that was generated at preStart
GVAR(allEventHandlers) = call (uiNamespace getVariable [QGVAR(configFileEventHandlers), {[]}]);

{
GVAR(allEventHandlers) append (_x call CBA_fnc_compileEventHandlers);
} forEach XEH_MAIN_CONFIGS;
} forEach (XEH_MAIN_CONFIGS - [configFile]);

#ifdef DEBUG_MODE_FULL
XEH_LOG("XEH: Compiling XEH END");
Expand All @@ -106,7 +107,16 @@ GVAR(fallbackRunning) = false;
{
if (_x select 0 == "") then {
if (_x select 1 == "preInit") then {
[] call (_x select 2);
(_x select 2) params ["_funcAll", "_funcClient", "_funcServer"];
if (!(_funcAll isEqualTo {})) then {
[] call _funcAll;
};
if ((!isDedicated) && {!(_funcClient isEqualTo {})}) then {
[] call _funcClient;
};
if ((isServer) && {!(_funcServer isEqualTo {})}) then {
[] call _funcServer;
};
};
} else {
_x params ["_className", "_eventName", "_eventFunc", "_allowInheritance", "_excludedClasses"];
Expand All @@ -115,9 +125,19 @@ GVAR(fallbackRunning) = false;
if (_eventName == "firedBis") then {
_eventName = "fired";
};

private _success = [_className, _eventName, _eventFunc, _allowInheritance, _excludedClasses] call CBA_fnc_addClassEventHandler;
TRACE_3("addClassEventHandler",_className,_eventName,_success);
_eventFunc params ["_funcAll", "_funcClient", "_funcServer"];
if (!(_funcAll isEqualTo {})) then {
private _success = [_className, _eventName, _funcAll, _allowInheritance, _excludedClasses] call CBA_fnc_addClassEventHandler;
TRACE_3("addClassEventHandler",_className,_eventName,_success);
};
if ((!isDedicated) && {!(_funcClient isEqualTo {})}) then {
private _success = [_className, _eventName, _funcClient, _allowInheritance, _excludedClasses] call CBA_fnc_addClassEventHandler;
TRACE_3("addClassEventHandler",_className,_eventName,_success);
};
if ((isServer) && {!(_funcServer isEqualTo {})}) then {
private _success = [_className, _eventName, _funcServer, _allowInheritance, _excludedClasses] call CBA_fnc_addClassEventHandler;
TRACE_3("addClassEventHandler",_className,_eventName,_success);
};
};
} forEach GVAR(allEventHandlers);

Expand Down
3 changes: 3 additions & 0 deletions addons/xeh/fnc_preStart.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,8 @@ with uiNamespace do {

// cache incompatible classes that are needed in preInit
GVAR(incompatibleClasses) = compileFinal str ([false, true] call CBA_fnc_supportMonitor);

//compile and cache configFile eventhandlers as they won't change from here on
GVAR(configFileEventHandlers) = compileFinal str (configFile call CBA_fnc_compileEventHandlers);
nil // needs return value [a3\functions_f\initfunctions.sqf Line 499]
};

0 comments on commit 565352d

Please sign in to comment.