From d281caffbe25170220652352e260bd9a0b805e51 Mon Sep 17 00:00:00 2001 From: Nou Date: Wed, 14 Jan 2015 13:29:52 -0800 Subject: [PATCH] Added ACRE2's hash and hash lists. --- addons/common/XEH_preInit.sqf | 17 +++++++++++ addons/common/functions/fnc_hashCreate.sqf | 5 ++++ addons/common/functions/fnc_hashGet.sqf | 26 ++++++++++++++++ addons/common/functions/fnc_hashHasKey.sqf | 23 ++++++++++++++ .../functions/fnc_hashListCreateHash.sqf | 18 +++++++++++ .../functions/fnc_hashListCreateList.sqf | 7 +++++ addons/common/functions/fnc_hashListPush.sqf | 17 +++++++++++ .../common/functions/fnc_hashListSelect.sqf | 27 +++++++++++++++++ addons/common/functions/fnc_hashListSet.sqf | 24 +++++++++++++++ addons/common/functions/fnc_hashRem.sqf | 30 +++++++++++++++++++ addons/common/functions/fnc_hashSet.sqf | 27 +++++++++++++++++ addons/common/functions/script_component.hpp | 14 ++++++++- addons/main/script_macros.hpp | 13 ++++++++ 13 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 addons/common/functions/fnc_hashCreate.sqf create mode 100644 addons/common/functions/fnc_hashGet.sqf create mode 100644 addons/common/functions/fnc_hashHasKey.sqf create mode 100644 addons/common/functions/fnc_hashListCreateHash.sqf create mode 100644 addons/common/functions/fnc_hashListCreateList.sqf create mode 100644 addons/common/functions/fnc_hashListPush.sqf create mode 100644 addons/common/functions/fnc_hashListSelect.sqf create mode 100644 addons/common/functions/fnc_hashListSet.sqf create mode 100644 addons/common/functions/fnc_hashRem.sqf create mode 100644 addons/common/functions/fnc_hashSet.sqf diff --git a/addons/common/XEH_preInit.sqf b/addons/common/XEH_preInit.sqf index 4e94e554e7..4dd6b5980e 100644 --- a/addons/common/XEH_preInit.sqf +++ b/addons/common/XEH_preInit.sqf @@ -149,6 +149,18 @@ PREP(localEvent); PREP(removeEventHandler); PREP(removeAlLEventHandlers); +// hashes +PREP(hashCreate); +PREP(hashSet); +PREP(hashGet); +PREP(hashHasKey); +PREP(hashRem); +PREP(hashListCreateList); +PREP(hashListCreateHash); +PREP(hashListSelect); +PREP(hashListSet); +PREP(hashListPush); + // Loop to update the ACE_player variable ACE_player = player; @@ -164,3 +176,8 @@ if (hasInterface) then { }; }] call BIS_fnc_addStackedEventHandler; }; + + + + + diff --git a/addons/common/functions/fnc_hashCreate.sqf b/addons/common/functions/fnc_hashCreate.sqf new file mode 100644 index 0000000000..56d9b69922 --- /dev/null +++ b/addons/common/functions/fnc_hashCreate.sqf @@ -0,0 +1,5 @@ +//fnc_hashCreate.sqf +#include "script_component.hpp" + +// diag_log text format["%1 HASH CREATE"]; +[[],[]] diff --git a/addons/common/functions/fnc_hashGet.sqf b/addons/common/functions/fnc_hashGet.sqf new file mode 100644 index 0000000000..6147d06a73 --- /dev/null +++ b/addons/common/functions/fnc_hashGet.sqf @@ -0,0 +1,26 @@ +//fnc_hashGet.sqf +#include "script_component.hpp" + +private ["_hash", "_key", "_val", "_index"]; +// diag_log text format["%1 HASH GET: %2", diag_tickTime, _this]; + +_hash = _this select 0; +_key = _this select 1; +ERRORDATA(2); +_val = nil; +try { + if(VALIDHASH(_hash)) then { + _index = (_hash select 0) find _key; + if(_index != -1) then { + _val = (_hash select 1) select _index; + if(IS_STRING(_val) && {_val == "ACREHASHREMOVEDONOTUSETHISVAL"}) then { + _val = nil; + }; + }; + } else { + ERROR("Input hash is not valid"); + }; +} catch { + HANDLECATCH; +}; +_val diff --git a/addons/common/functions/fnc_hashHasKey.sqf b/addons/common/functions/fnc_hashHasKey.sqf new file mode 100644 index 0000000000..d69ad2f3e8 --- /dev/null +++ b/addons/common/functions/fnc_hashHasKey.sqf @@ -0,0 +1,23 @@ +//fnc_hashHasKey.sqf +#include "script_component.hpp" + +private ["_hash", "_key", "_val", "_index"]; +// diag_log text format["%1 HASH HAS KEY: %2", diag_tickTime, _this]; + +_hash = _this select 0; +_key = _this select 1; +ERRORDATA(2); +_val = false; +try { + if(VALIDHASH(_hash)) then { + _index = (_hash select 0) find _key; + if(_index != -1) then { + _val = true; + }; + } else { + ERROR("Input hash is not valid"); + }; +} catch { + HANDLECATCH; +}; +_val diff --git a/addons/common/functions/fnc_hashListCreateHash.sqf b/addons/common/functions/fnc_hashListCreateHash.sqf new file mode 100644 index 0000000000..fef9c79b85 --- /dev/null +++ b/addons/common/functions/fnc_hashListCreateHash.sqf @@ -0,0 +1,18 @@ +//fnc_hashListCreateHash.sqf +#include "script_component.hpp" + +private ["_hashList", "_hashKeys"]; + +_hashList = _this select 0; +ERRORDATA(1); +_hashKeys = []; +try { + if(VALIDHASH(_hashList)) then { + _hashKeys = (_hashList select 0); + } else { + ERROR("Input hashlist is not valid"); + }; +} catch { + HANDLECATCH; +}; +[_hashKeys, []]; diff --git a/addons/common/functions/fnc_hashListCreateList.sqf b/addons/common/functions/fnc_hashListCreateList.sqf new file mode 100644 index 0000000000..66061c1aa0 --- /dev/null +++ b/addons/common/functions/fnc_hashListCreateList.sqf @@ -0,0 +1,7 @@ +//fnc_hashListCreateList.sqf +#include "script_component.hpp" + +private ["_keys"]; + +_keys = _this select 0; +[_keys,[]]; diff --git a/addons/common/functions/fnc_hashListPush.sqf b/addons/common/functions/fnc_hashListPush.sqf new file mode 100644 index 0000000000..e783f7c324 --- /dev/null +++ b/addons/common/functions/fnc_hashListPush.sqf @@ -0,0 +1,17 @@ +//fnc_hashListPush.sqf +#include "script_component.hpp" + +private ["_hashList", "_value"]; + +_hashList = _this select 0; +_value = _this select 1; +ERRORDATA(2); +try { + if(VALIDHASH(_hashList)) then { + [_hashList, (count (_hashList select 1)), _value] call FUNC(hashListSet); + } else { + ERROR("Input hashlist in push not valid"); + }; +} catch { + HANDLECATCH; +}; diff --git a/addons/common/functions/fnc_hashListSelect.sqf b/addons/common/functions/fnc_hashListSelect.sqf new file mode 100644 index 0000000000..e1ee0aae0f --- /dev/null +++ b/addons/common/functions/fnc_hashListSelect.sqf @@ -0,0 +1,27 @@ +//fnc_hashListSelect.sqf +#include "script_component.hpp" + +private ["_hashList", "_index", "_hash", "_keys", "_hashes", "_values"]; + +_hashList = _this select 0; +_index = _this select 1; +ERRORDATA(2); +_hash = nil; +try { + if(VALIDHASH(_hashList)) then { + _keys = _hashList select 0; + _hashes = _hashList select 1; + if(_index < (count _hashes)) then { + _values = _hashes select _index; + + _hash = [_keys, _values, 1]; + } else { + ERROR("Index of hashlist is out of range"); + }; + } else { + ERROR("Input hashlist is not valid"); + }; +} catch { + HANDLECATCH; +}; +_hash; diff --git a/addons/common/functions/fnc_hashListSet.sqf b/addons/common/functions/fnc_hashListSet.sqf new file mode 100644 index 0000000000..8b7239dffd --- /dev/null +++ b/addons/common/functions/fnc_hashListSet.sqf @@ -0,0 +1,24 @@ +//fnc_hashListSet.sqf +#include "script_component.hpp" + +private ["_hashList", "_index", "_value", "_vals"]; + +_hashList = _this select 0; +_index = _this select 1; +_value = _this select 2; +ERRORDATA(3); +try { + if(VALIDHASH(_hashList)) then { + if(VALIDHASH(_value)) then { + _vals = _value select 1; + + (_hashList select 1) set[_index, _vals]; + } else { + ERROR("Set hash in hashlist is not valid"); + }; + } else { + ERROR("Input hashlist is not valid"); + }; +} catch { + HANDLECATCH; +}; diff --git a/addons/common/functions/fnc_hashRem.sqf b/addons/common/functions/fnc_hashRem.sqf new file mode 100644 index 0000000000..86898fb3df --- /dev/null +++ b/addons/common/functions/fnc_hashRem.sqf @@ -0,0 +1,30 @@ +//fnc_hashRem.sqf +#include "script_component.hpp" + +private ["_hash", "_key", "_val", "_index"]; + +_hash = _this select 0; +_key = _this select 1; +ERRORDATA(2); +_val = nil; +try { + if(VALIDHASH(_hash)) then { + _index = (_hash select 0) find _key; + if(_index != -1) then { + (_hash select 1) set[_index, "ACREHASHREMOVEDONOTUSETHISVAL"]; + // is this hash is not part of a hash list? + // if it is we need to leave the keys intact. + if((count _hash) == 2) then { + // if this is a standalone hash then we can clean it up + (_hash select 0) set[_index, "ACREHASHREMOVEDONOTUSETHISVAL"]; + _hash set[0, ((_hash select 0) - ["ACREHASHREMOVEDONOTUSETHISVAL"])]; + _hash set[1, ((_hash select 1) - ["ACREHASHREMOVEDONOTUSETHISVAL"])]; + }; + }; + } else { + ERROR("Input hash is not valid"); + }; +} catch { + HANDLECATCH; +}; +true diff --git a/addons/common/functions/fnc_hashSet.sqf b/addons/common/functions/fnc_hashSet.sqf new file mode 100644 index 0000000000..23438eaf83 --- /dev/null +++ b/addons/common/functions/fnc_hashSet.sqf @@ -0,0 +1,27 @@ +//fnc_hashSet.sqf +#include "script_component.hpp" + +private ["_hash", "_key", "_val", "_index"]; +// diag_log text format["%1 HASH SET: %2", diag_tickTime, _this]; + +_hash = _this select 0; +_key = _this select 1; +_val = _this select 2; +ERRORDATA(3); +try { + if(VALIDHASH(_hash)) then { + _index = (_hash select 0) find _key; + if(_index == -1) then { + _index = (_hash select 0) find "ACREHASHREMOVEDONOTUSETHISVAL"; + if(_index == -1) then { + _index = (count (_hash select 0)); + }; + (_hash select 0) set[_index, _key]; + }; + (_hash select 1) set[_index, _val]; + } else { + ERROR("Input hash is not valid"); + }; +} catch { + HANDLECATCH; +}; diff --git a/addons/common/functions/script_component.hpp b/addons/common/functions/script_component.hpp index 23da62b05c..d1032476b6 100644 --- a/addons/common/functions/script_component.hpp +++ b/addons/common/functions/script_component.hpp @@ -1 +1,13 @@ -#include "\z\ace\addons\common\script_component.hpp" \ No newline at end of file +#include "\z\ace\addons\common\script_component.hpp" + +#define VALIDHASH(hash) (IS_ARRAY(hash) && {(count hash) >= 2} && {IS_ARRAY(hash select 0)} && {IS_ARRAY(hash select 1)}) +#define ERROR(msg) throw msg + format[" @ %1:%2", _callFrom, _lineNo] +#define HANDLECATCH diag_log text _exception; assert(exception=="") + +#define ERRORDATA(c) private ["_callFrom", "_lineNo"];\ + _callFrom = "";\ + _lineNo = -1;\ + if((count _this) > c) then {\ + _callFrom = _this select c;\ + _lineNo = _this select c+1;\ + }; \ No newline at end of file diff --git a/addons/main/script_macros.hpp b/addons/main/script_macros.hpp index 3b8dd1ff52..36f95d8938 100644 --- a/addons/main/script_macros.hpp +++ b/addons/main/script_macros.hpp @@ -225,3 +225,16 @@ #define EFUNC(var1,var2) TRIPLES(DOUBLES(PREFIX,var1),fnc,var2) #endif + + +#define HASH_CREATE ([] call EFUNC(common,hashCreate)) +#define HASH_SET(hash, key, val) ([hash, key, val, __FILE__, __LINE__] call EFUNC(common,hashSet)) +#define HASH_GET(hash, key) ([hash, key, __FILE__, __LINE__] call EFUNC(common,hashGet)) +#define HASH_REM(hash, key) ([hash, key, __FILE__, __LINE__] call EFUNC(common,hashRem)) +#define HASH_HASKEY(hash, key) ([hash, key, __FILE__, __LINE__] call EFUNC(common,hashHasKey)) + +#define HASHLIST_CREATELIST(keys) ([keys] call EFUNC(common,hashListCreateList)) +#define HASHLIST_CREATEHASH(hashList) ([hashList] call EFUNC(common,hashListCreateHash)) +#define HASHLIST_SELECT(hashList, index) ([hashList, index, __FILE__, __LINE__] call EFUNC(common,hashListSelect)) +#define HASHLIST_SET(hashList, index, value) ([hashList, index, value, __FILE__, __LINE__] call EFUNC(common,hashListSet)) +#define HASHLIST_PUSH(hashList, value) ([hashList, value, __FILE__, __LINE__] call EFUNC(common,hashListPush))