2018-09-17 19:19:29 +00:00
|
|
|
#include "script_component.hpp"
|
2015-03-11 02:20:05 +00:00
|
|
|
/*
|
2015-09-18 11:09:40 +00:00
|
|
|
* Author: esteldunedain, Jaynus
|
2015-10-21 20:52:21 +00:00
|
|
|
* Returns the result of the function and caches it up to a given time or event
|
2015-03-11 02:20:05 +00:00
|
|
|
*
|
|
|
|
* Arguments:
|
|
|
|
* 0: Parameters <ARRAY>
|
|
|
|
* 1: Function <CODE>
|
|
|
|
* 2: Namespace to store the cache on <NAMESPACE>
|
|
|
|
* 3: Cache uid <STRING>
|
|
|
|
* 4: Max duration of the cache <NUMBER>
|
2015-09-18 11:09:40 +00:00
|
|
|
* 5: Event that clears the cache (default: nil) <STRING>
|
2015-03-11 02:20:05 +00:00
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* Result of the function <ANY>
|
|
|
|
*
|
2017-06-08 13:31:51 +00:00
|
|
|
* Example:
|
|
|
|
* [[array]], {dothings}, NAMESPACE, "UID", 5, "clear"] call ace_common_fnc_cachedCall
|
|
|
|
*
|
2015-03-11 02:20:05 +00:00
|
|
|
* Public: No
|
|
|
|
*/
|
|
|
|
|
2015-09-18 11:09:40 +00:00
|
|
|
params ["_params", "_function", "_namespace", "_uid", "_duration", "_event"];
|
2015-03-11 02:20:05 +00:00
|
|
|
|
2016-03-02 10:01:39 +00:00
|
|
|
if ((_namespace getVariable [_uid, [-99999]]) select 0 < diag_tickTime) then {
|
|
|
|
_namespace setVariable [_uid, [diag_tickTime + _duration, _params call _function]];
|
2015-03-21 20:50:35 +00:00
|
|
|
|
2018-05-17 01:00:51 +00:00
|
|
|
// Does the cache need to be cleared on an event?
|
2015-09-18 11:09:40 +00:00
|
|
|
if (!isNil "_event") then {
|
2015-12-12 15:48:54 +00:00
|
|
|
private _varName = format [QGVAR(clearCache_%1), _event];
|
|
|
|
private _cacheList = missionNamespace getVariable _varName;
|
2015-03-21 20:50:35 +00:00
|
|
|
|
|
|
|
// If there was no EH to clear these caches, add one
|
2015-09-18 11:09:40 +00:00
|
|
|
if (isNil "_cacheList") then {
|
2015-03-21 20:50:35 +00:00
|
|
|
_cacheList = [];
|
|
|
|
missionNamespace setVariable [_varName, _cacheList];
|
|
|
|
|
|
|
|
[_event, {
|
2015-03-21 21:09:42 +00:00
|
|
|
#ifdef DEBUG_MODE_FULL
|
2016-10-02 10:55:31 +00:00
|
|
|
INFO_1("Clear cached variables on event: %1",_eventName);
|
2015-03-21 21:09:42 +00:00
|
|
|
#endif
|
2015-03-21 20:50:35 +00:00
|
|
|
// Get the list of caches to clear
|
2017-05-31 21:09:36 +00:00
|
|
|
//IGNORE_PRIVATE_WARNING ["_eventName"];
|
|
|
|
// _eventName is defined on the function that calls the event
|
2015-12-12 15:48:54 +00:00
|
|
|
private _varName = format [QGVAR(clearCache_%1), _eventName];
|
|
|
|
private _cacheList = missionNamespace getVariable [_varName, []];
|
2015-03-21 20:50:35 +00:00
|
|
|
// Erase all the cached results
|
|
|
|
{
|
|
|
|
_x call FUNC(eraseCache);
|
|
|
|
} forEach _cacheList;
|
|
|
|
// Empty the list
|
|
|
|
missionNamespace setVariable [_varName, []];
|
2016-05-22 15:29:05 +00:00
|
|
|
}] call CBA_fnc_addEventHandler;
|
2015-03-21 20:50:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add this cache to the list of the event
|
|
|
|
_cacheList pushBack [_namespace, _uid];
|
|
|
|
};
|
2015-09-18 11:09:40 +00:00
|
|
|
|
2015-03-11 02:20:05 +00:00
|
|
|
#ifdef DEBUG_MODE_FULL
|
2016-10-02 10:55:31 +00:00
|
|
|
INFO_2("Calculated result: %1 %2",_namespace,_uid);
|
2015-03-11 02:20:05 +00:00
|
|
|
} else {
|
2016-10-02 10:55:31 +00:00
|
|
|
INFO_2("Cached result: %1 %2",_namespace,_uid);
|
2015-03-11 02:20:05 +00:00
|
|
|
#endif
|
2015-09-18 11:09:40 +00:00
|
|
|
|
2015-03-11 02:20:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
(_namespace getVariable _uid) select 1
|