diff --git a/addons/arrays/fnc_findMax.sqf b/addons/arrays/fnc_findMax.sqf index 5d7838d2c..c00d1b981 100644 --- a/addons/arrays/fnc_findMax.sqf +++ b/addons/arrays/fnc_findMax.sqf @@ -26,7 +26,7 @@ SCRIPT(findMax); [_this] params [["_array", [], [[]]]]; -if !(_array isEqualTypeAll 0) exitWith {nil}; +if !(_array isEqualTypeAll 0) exitWith { nil }; private _reverse = + _array; reverse _reverse; diff --git a/addons/promise/$PBOPREFIX$ b/addons/promise/$PBOPREFIX$ new file mode 100644 index 000000000..5477a44d8 --- /dev/null +++ b/addons/promise/$PBOPREFIX$ @@ -0,0 +1 @@ +x\cba\addons\promise diff --git a/addons/promise/CfgFunctions.hpp b/addons/promise/CfgFunctions.hpp new file mode 100644 index 000000000..6bebb6192 --- /dev/null +++ b/addons/promise/CfgFunctions.hpp @@ -0,0 +1,11 @@ +class CfgFunctions { + class CBA { + class Network { + PATHTO_FNC(promise_create); + PATHTO_FNC(promise_callback); + PATHTO_FNC(promise_done); + PATHTO_FNC(promise_receiver); + PATHTO_FNC(promise_init); + }; + }; +}; diff --git a/addons/promise/config.cpp b/addons/promise/config.cpp new file mode 100644 index 000000000..82ee0436e --- /dev/null +++ b/addons/promise/config.cpp @@ -0,0 +1,17 @@ +#include "script_component.hpp" + +class CfgPatches { + class ADDON { + author = "$STR_CBA_Author"; + name = CSTRING(component); + url = "$STR_CBA_URL"; + units[] = {}; + weapons[] = {}; + requiredVersion = REQUIRED_VERSION; + requiredAddons[] = {}; + version = VERSION; + authors[] = {"X39"}; + }; +}; + +#include "CfgFunctions.hpp" diff --git a/addons/promise/fnc_callback.sqf b/addons/promise/fnc_callback.sqf new file mode 100644 index 000000000..75aebb644 --- /dev/null +++ b/addons/promise/fnc_callback.sqf @@ -0,0 +1,29 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_promise_callback + +Description: + Function that gets called when the receiver is done. + Will in the end execute the promise-code locally. + + WARNING! Not intended to be called by client-code! + +Parameters: + _id - The Promise-ID of the now finished promise. + _data - The result data of the now finished promise. + +Returns: + Nothing + +Example: + Nothing + +Author: + X39 +---------------------------------------------------------------------------- */ +#include "script_component.hpp" + +params ["_id", "_data"]; +private _request = GVAR(requests) select _id; +GVAR(requests) set [_id, 0]; +[_request select 0, _data] call (_request select 1); +nil diff --git a/addons/promise/fnc_create.sqf b/addons/promise/fnc_create.sqf new file mode 100644 index 000000000..97eb20bcf --- /dev/null +++ b/addons/promise/fnc_create.sqf @@ -0,0 +1,50 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_promise_create + +Description: + Creates and executes a new Promise. + +Parameters: + _receiver - remoteExecCall target. + _function - either string (functionname) or code to execute on target. + _functionArgs - custom arguments to pass to the target. + _args - args to pass to the callback (see next param) + _callback - callback for the promise (executed on current machine). + Will receive an array, with index 0 being what is passed include + _args and index 1 being whatever is returned by the target invokation + [_args, _mthdResult] + +Returns: + Identifier that allows to check if a promise is yet done. See CBA_fnc_promise_done. + +Example: + (begin example) + [ + random_player, "random_function", [], + [_someLocalVariable], { + params ["_args", "_result"]; + _args params ["_someLocalVariable"]; + diag_log _result; + diag_log _someLocalVariable; + } + ] call CBA_fnc_promise_create; + (end) + +Author: + X39 +---------------------------------------------------------------------------- */ +#include "script_component.hpp" +params ["_receiver", "_function", "_functionArgs", "_args", "_callback"]; +private _stamp = diag_tickTime; +private _index = 0; +isNil { + _index = GVAR(requests) find 0; + if (_index == -1) then { + _index = GVAR(requests) pushBack [_args, _callback, _stamp]; + } + else { + GVAR(requests) set [_index, [_args, _callback, _stamp]]; + } +}; +[_index, _function, _functionArgs] remoteExec [QGVAR(requests), _receiver, false]; +[_index, _stamp] diff --git a/addons/promise/fnc_done.sqf b/addons/promise/fnc_done.sqf new file mode 100644 index 000000000..c4550a62f --- /dev/null +++ b/addons/promise/fnc_done.sqf @@ -0,0 +1,24 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_promise_done + +Description: + Checks if a given promise was processed yet. + +Parameters: + _promiseHandle - Handle returned by CBA_fnc_promise_create + +Returns: + Boolean, true if it is done yet, false if it is not. + +Example: + (begin example) + _promiseHandle call CBA_fnc_promise_done; + (end) + +Author: + X39 +---------------------------------------------------------------------------- */ +#include "script_component.hpp" +params ["_index", "_stamp"]; +private _promise = GVAR(requests) select 0; +_promise isEqualTo 0 || {_promise select 2 != _stamp} diff --git a/addons/promise/fnc_init.sqf b/addons/promise/fnc_init.sqf new file mode 100644 index 000000000..855dd5ef0 --- /dev/null +++ b/addons/promise/fnc_init.sqf @@ -0,0 +1,28 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_promise_init + +Description: + Initializes the required variables for the promise system. + +Parameters: + Nothing + +Returns: + Nothing + +Example: + (begin example) + [] call CBA_fnc_promise_init; + (end) + +Author: + X39 +---------------------------------------------------------------------------- */ +#include "script_component.hpp" +isNil { + if isNil QGVAR(requests) then { + GVAR(requests) = []; + }; +}; + +nil diff --git a/addons/promise/fnc_receiver.sqf b/addons/promise/fnc_receiver.sqf new file mode 100644 index 000000000..0228cf41e --- /dev/null +++ b/addons/promise/fnc_receiver.sqf @@ -0,0 +1,32 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_promise_receiver + +Description: + Function that gets called when a sender has a request. + + WARNING! Not intended to be called by client-code! + +Parameters: + _id - the promise ID, local to the sender. + _function - The function to execute, either string (var name) or code. + _args - Arguments to pass to the function. + +Returns: + Nothing + +Example: + Nothing + +Author: + X39 +---------------------------------------------------------------------------- */ +#include "script_component.hpp" + +params ["_id", "_function", "_args"]; +private _res = if (_function isEqualType "") then { + _args call (missionNamespace getVariable _function); +} else { + _args call _function; +}; +[_id, _res] remoteExec [QGVAR(requests), remoteExecutedOwner, false]; +nil diff --git a/addons/promise/license.txt b/addons/promise/license.txt new file mode 100644 index 000000000..740a12b7c --- /dev/null +++ b/addons/promise/license.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Marco Silipo (X39) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/addons/promise/script_component.hpp b/addons/promise/script_component.hpp new file mode 100644 index 000000000..6ef3b1538 --- /dev/null +++ b/addons/promise/script_component.hpp @@ -0,0 +1,4 @@ +#define COMPONENT promise +#include "\x\cba\addons\main\script_mod.hpp" + +#include "\x\cba\addons\main\script_macros.hpp"