From 971f9bc5478fadf87dbdfdf86090122c9bee3621 Mon Sep 17 00:00:00 2001 From: jokoho48 Date: Fri, 12 Feb 2016 00:20:36 +0100 Subject: [PATCH 1/5] add Caching for JR --- addons/jr/fnc_compatibleItems.sqf | 62 +++++++++++++++++++------------ 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/addons/jr/fnc_compatibleItems.sqf b/addons/jr/fnc_compatibleItems.sqf index 9792de94c..24af445da 100644 --- a/addons/jr/fnc_compatibleItems.sqf +++ b/addons/jr/fnc_compatibleItems.sqf @@ -1,10 +1,11 @@ /* Author: Karel Moricky Enhanced by Robalo + adding Caching by joko // Jonas Description: Return all compatible weapon attachments - + Parameter(s): 0: STRING - weapon class 1: STRING - optional, accessory type: number (101 - muzzle, 201 - optic, 301 - pointer, 302 - bipod) @@ -16,34 +17,49 @@ _acclist = ["LMG_Mk200_F"] call CBA_fnc_compatibleItems; _muzzleacclist = ["LMG_Mk200_F", 101] call CBA_fnc_compatibleItems; */ - +#include "script_component.hpp" params [["_weapon", "", [""]], ["_typefilter", 0]]; if (_weapon == "") exitWith {[]}; -private ["_cfgWeapon"]; -_cfgWeapon = configfile >> "cfgweapons" >> _weapon; +if (isNil QGVAR(namespace)) then { + GVAR(namespace) = call CBA_fnc_createNamespace; +}; +private _compatibleItems = GVAR(namespace) getVariable format [QGVAR(cached_%1), _weapon]; -if (isClass _cfgWeapon) then { - private ["_compatibleItems"]; - _compatibleItems = []; - { - private ["_cfgCompatibleItems"]; - _cfgCompatibleItems = _x >> "compatibleItems"; - if (isarray _cfgCompatibleItems) then { - { - if !(_x in _compatibleItems) then {_compatibleItems pushBack _x;}; - } forEach getArray _cfgCompatibleItems; - } else { - if (isclass _cfgCompatibleItems) then { +if (isNil "_compatibleItems") then { + private ["_cfgWeapon"]; + _cfgWeapon = configfile >> "cfgweapons" >> _weapon; + if (isClass _cfgWeapon) then { + private ["_compatibleItems"]; + _compatibleItems = []; + { + private ["_cfgCompatibleItems"]; + _cfgCompatibleItems = _x >> "compatibleItems"; + if (isArray _cfgCompatibleItems) then { { - if (getnumber _x > 0 && {!((configname _x) in _compatibleItems)}) then {_compatibleItems pushBack configname _x}; - } foreach configproperties [_cfgCompatibleItems, "isNumber _x"]; + if !(_x in _compatibleItems) then {_compatibleItems pushBack _x;}; + nil + } count (getArray _cfgCompatibleItems); + } else { + if (isclass _cfgCompatibleItems) then { + { + if (getnumber _x > 0 && {!((configname _x) in _compatibleItems)}) then {_compatibleItems pushBack (configname _x)}; + nil + } count configproperties [_cfgCompatibleItems, "isNumber _x"]; + }; }; - }; - } foreach configproperties [_cfgWeapon >> "WeaponSlotsInfo","isclass _x"]; - if (_typefilter == 0) then {_compatibleItems} else {[_compatibleItems, {_typefilter == getnumber(configfile>>"cfgweapons">>_x>>"itemInfo">>"type")}] call BIS_fnc_conditionalSelect}; + nil + } count configproperties [_cfgWeapon >> "WeaponSlotsInfo","isclass _x"]; + } else { + ["'%1' not found in CfgWeapons",_weapon] call bis_fnc_error; + [] + }; } else { - ["'%1' not found in CfgWeapons",_weapon] call bis_fnc_error; - [] + if (_typefilter == 0) then { + _compatibleItems + } else { + // repace for 1.56 with _compatibleItems select {_typefilter == getNumber(configfile>>"cfgweapons">>_x>>"itemInfo">>"type")}; + [_compatibleItems, {_typefilter == getNumber(configfile>>"cfgweapons">>_x>>"itemInfo">>"type")}] call BIS_fnc_conditionalSelect; + }; }; From d69a0973eb22f49b34a512ddcf212a09f7c065ba Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Thu, 18 Feb 2016 12:57:12 -0600 Subject: [PATCH 2/5] Fix caching and add test case --- addons/jr/fnc_compatibleItems.sqf | 30 +++++++++------------ addons/jr/test.sqf | 20 ++++++++++++++ addons/jr/test_compatibleItems.sqf | 43 ++++++++++++++++++++++++++++++ addons/main/test.sqf | 2 +- 4 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 addons/jr/test.sqf create mode 100644 addons/jr/test_compatibleItems.sqf diff --git a/addons/jr/fnc_compatibleItems.sqf b/addons/jr/fnc_compatibleItems.sqf index 24af445da..4bea56983 100644 --- a/addons/jr/fnc_compatibleItems.sqf +++ b/addons/jr/fnc_compatibleItems.sqf @@ -16,7 +16,7 @@ Examples: _acclist = ["LMG_Mk200_F"] call CBA_fnc_compatibleItems; _muzzleacclist = ["LMG_Mk200_F", 101] call CBA_fnc_compatibleItems; -*/ + */ #include "script_component.hpp" params [["_weapon", "", [""]], ["_typefilter", 0]]; if (_weapon == "") exitWith {[]}; @@ -24,26 +24,23 @@ if (_weapon == "") exitWith {[]}; if (isNil QGVAR(namespace)) then { GVAR(namespace) = call CBA_fnc_createNamespace; }; -private _compatibleItems = GVAR(namespace) getVariable format [QGVAR(cached_%1), _weapon]; +private _compatibleItems = GVAR(namespace) getVariable _weapon; if (isNil "_compatibleItems") then { - private ["_cfgWeapon"]; - _cfgWeapon = configfile >> "cfgweapons" >> _weapon; + _compatibleItems = []; + private _cfgWeapon = configfile >> "cfgweapons" >> _weapon; if (isClass _cfgWeapon) then { - private ["_compatibleItems"]; - _compatibleItems = []; { - private ["_cfgCompatibleItems"]; - _cfgCompatibleItems = _x >> "compatibleItems"; + private _cfgCompatibleItems = _x >> "compatibleItems"; if (isArray _cfgCompatibleItems) then { { if !(_x in _compatibleItems) then {_compatibleItems pushBack _x;}; nil } count (getArray _cfgCompatibleItems); } else { - if (isclass _cfgCompatibleItems) then { + if (isClass _cfgCompatibleItems) then { { - if (getnumber _x > 0 && {!((configname _x) in _compatibleItems)}) then {_compatibleItems pushBack (configname _x)}; + if ((getnumber _x > 0) && {!((configName _x) in _compatibleItems)}) then {_compatibleItems pushBack (configName _x)}; nil } count configproperties [_cfgCompatibleItems, "isNumber _x"]; }; @@ -51,15 +48,14 @@ if (isNil "_compatibleItems") then { nil } count configproperties [_cfgWeapon >> "WeaponSlotsInfo","isclass _x"]; + GVAR(namespace) setVariable [_weapon, _compatibleItems]; //save entry in cache } else { ["'%1' not found in CfgWeapons",_weapon] call bis_fnc_error; - [] }; +}; + +if (_typefilter == 0) then { //return + _compatibleItems } else { - if (_typefilter == 0) then { - _compatibleItems - } else { - // repace for 1.56 with _compatibleItems select {_typefilter == getNumber(configfile>>"cfgweapons">>_x>>"itemInfo">>"type")}; - [_compatibleItems, {_typefilter == getNumber(configfile>>"cfgweapons">>_x>>"itemInfo">>"type")}] call BIS_fnc_conditionalSelect; - }; + _compatibleItems select {_typefilter == getNumber(configfile>>"cfgweapons">>_x>>"itemInfo">>"type")}; }; diff --git a/addons/jr/test.sqf b/addons/jr/test.sqf new file mode 100644 index 000000000..c05e27c19 --- /dev/null +++ b/addons/jr/test.sqf @@ -0,0 +1,20 @@ +// ----------------------------------------------------------------------------- +// Automatically generated by 'functions_config.rb' +// DO NOT MANUALLY EDIT THIS FILE! +// ----------------------------------------------------------------------------- + +#include "script_component.hpp" + +#define TESTS ["compatibleItems"] + +SCRIPT(test-jr); + +// ---------------------------------------------------------------------------- + +LOG("=== Testing JR ==="); + +{ + call compile preprocessFileLineNumbers format ["\x\cba\addons\jr\test_%1.sqf", _x]; +} forEach TESTS; + +nil; diff --git a/addons/jr/test_compatibleItems.sqf b/addons/jr/test_compatibleItems.sqf new file mode 100644 index 000000000..1589e920c --- /dev/null +++ b/addons/jr/test_compatibleItems.sqf @@ -0,0 +1,43 @@ +// ---------------------------------------------------------------------------- +#define DEBUG_MODE_FULL +#include "script_component.hpp" + +SCRIPT(test_compatibleItems); + +// ---------------------------------------------------------------------------- + +LOG('Testing JR Compatible Items'); + +// UNIT TESTS (polar2vect) +private _fn = "CBA_fnc_compatibleItems"; +TEST_DEFINED("CBA_fnc_compatibleItems",""); + +private _result = [] call CBA_fnc_compatibleItems; +private _expected = []; +TEST_DEFINED_AND_OP(_result,isEqualTo,_expected,_fn); + +_result = ["aGunThatDoesNotExist"] call CBA_fnc_compatibleItems; +_expected = []; +TEST_DEFINED_AND_OP(_result,isEqualTo,_expected,_fn); + +_result = [-1] call CBA_fnc_compatibleItems; +_expected = []; +TEST_DEFINED_AND_OP(_result,isEqualTo,_expected,_fn); + +_result = [-1] call CBA_fnc_compatibleItems; +_expected = []; +TEST_DEFINED_AND_OP(_result,isEqualTo,_expected,_fn); + +private _firstResult = ["LMG_Mk200_F"] call CBA_fnc_compatibleItems; +private _cachedResult = ["LMG_Mk200_F"] call CBA_fnc_compatibleItems; +TEST_DEFINED_AND_OP(_firstResult,isEqualTo,_cachedResult,_fn); + +_result = ["LMG_Mk200_F", 99999] call CBA_fnc_compatibleItems; //invalid accesory type +_expected = []; +TEST_DEFINED_AND_OP(_result,isEqualTo,_expected,_fn); + +_result = ["LMG_Mk200_F", 201] call CBA_fnc_compatibleItems; //201 should filter optics +TEST_TRUE("optic_tws_mg" in _result,_fn); +TEST_FALSE("muzzle_snds_h" in _result,_fn); + +nil; diff --git a/addons/main/test.sqf b/addons/main/test.sqf index c00ac2f15..633dca01a 100644 --- a/addons/main/test.sqf +++ b/addons/main/test.sqf @@ -5,7 +5,7 @@ #include "script_component.hpp" -#define CATEGORIES ["arrays", "common", "diagnostic", "hashes", "strings", "vectors"] +#define CATEGORIES ["arrays", "common", "diagnostic", "hashes", "strings", "vectors", "jr"] SCRIPT(test); From 94b0c2d1fb7715cb620a32a815540b142a9aed86 Mon Sep 17 00:00:00 2001 From: jokoho48 Date: Mon, 22 Feb 2016 13:29:16 +0100 Subject: [PATCH 3/5] more use of new commands(pushBackUnique) --- addons/jr/fnc_compatibleItems.sqf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/jr/fnc_compatibleItems.sqf b/addons/jr/fnc_compatibleItems.sqf index 4bea56983..f33aa11b4 100644 --- a/addons/jr/fnc_compatibleItems.sqf +++ b/addons/jr/fnc_compatibleItems.sqf @@ -34,13 +34,13 @@ if (isNil "_compatibleItems") then { private _cfgCompatibleItems = _x >> "compatibleItems"; if (isArray _cfgCompatibleItems) then { { - if !(_x in _compatibleItems) then {_compatibleItems pushBack _x;}; + _compatibleItems pushBackUnique _x; nil } count (getArray _cfgCompatibleItems); } else { if (isClass _cfgCompatibleItems) then { { - if ((getnumber _x > 0) && {!((configName _x) in _compatibleItems)}) then {_compatibleItems pushBack (configName _x)}; + if ((getnumber _x > 0)) then {_compatibleItems pushBackUnique (configName _x)}; nil } count configproperties [_cfgCompatibleItems, "isNumber _x"]; }; @@ -55,7 +55,7 @@ if (isNil "_compatibleItems") then { }; if (_typefilter == 0) then { //return - _compatibleItems + _compatibleItems } else { _compatibleItems select {_typefilter == getNumber(configfile>>"cfgweapons">>_x>>"itemInfo">>"type")}; }; From eaf5c7cb71d79ee4ee4e2df8e63e71b8c3b9c367 Mon Sep 17 00:00:00 2001 From: Joko Date: Mon, 22 Feb 2016 19:19:20 +0100 Subject: [PATCH 4/5] fix spelling mistake --- addons/jr/fnc_compatibleItems.sqf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/jr/fnc_compatibleItems.sqf b/addons/jr/fnc_compatibleItems.sqf index f33aa11b4..11b5f03cd 100644 --- a/addons/jr/fnc_compatibleItems.sqf +++ b/addons/jr/fnc_compatibleItems.sqf @@ -1,7 +1,7 @@ /* Author: Karel Moricky Enhanced by Robalo - adding Caching by joko // Jonas + Caching added by joko // Jonas Description: Return all compatible weapon attachments From 0af30e56ee7ffe37fa0e32d4a49a44143018854c Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Sat, 5 Mar 2016 11:35:48 -0600 Subject: [PATCH 5/5] Camel --- addons/jr/fnc_compatibleItems.sqf | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/addons/jr/fnc_compatibleItems.sqf b/addons/jr/fnc_compatibleItems.sqf index 11b5f03cd..5394a8cb2 100644 --- a/addons/jr/fnc_compatibleItems.sqf +++ b/addons/jr/fnc_compatibleItems.sqf @@ -28,7 +28,7 @@ private _compatibleItems = GVAR(namespace) getVariable _weapon; if (isNil "_compatibleItems") then { _compatibleItems = []; - private _cfgWeapon = configfile >> "cfgweapons" >> _weapon; + private _cfgWeapon = configFile >> "CfgWeapons" >> _weapon; if (isClass _cfgWeapon) then { { private _cfgCompatibleItems = _x >> "compatibleItems"; @@ -40,13 +40,13 @@ if (isNil "_compatibleItems") then { } else { if (isClass _cfgCompatibleItems) then { { - if ((getnumber _x > 0)) then {_compatibleItems pushBackUnique (configName _x)}; + if ((getNumber _x > 0)) then {_compatibleItems pushBackUnique (configName _x)}; nil - } count configproperties [_cfgCompatibleItems, "isNumber _x"]; + } count configProperties [_cfgCompatibleItems, "isNumber _x"]; }; }; nil - } count configproperties [_cfgWeapon >> "WeaponSlotsInfo","isclass _x"]; + } count configProperties [_cfgWeapon >> "WeaponSlotsInfo","isclass _x"]; GVAR(namespace) setVariable [_weapon, _compatibleItems]; //save entry in cache } else { @@ -57,5 +57,5 @@ if (isNil "_compatibleItems") then { if (_typefilter == 0) then { //return _compatibleItems } else { - _compatibleItems select {_typefilter == getNumber(configfile>>"cfgweapons">>_x>>"itemInfo">>"type")}; + _compatibleItems select {_typefilter == getNumber(configFile>>"CfgWeapons">>_x>>"itemInfo">>"type")}; };