Skip to content

Commit

Permalink
Merge pull request #268 from CBATeam/cleanupJrCache
Browse files Browse the repository at this point in the history
Add Caching to CBA_fnc_compatibleItems (Cleanup)
  • Loading branch information
Killswitch00 committed Mar 6, 2016
2 parents b6fa7ef + 0af30e5 commit 5e2aff7
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 23 deletions.
59 changes: 37 additions & 22 deletions addons/jr/fnc_compatibleItems.sqf
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
Author: Karel Moricky
Enhanced by Robalo
Caching added 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)
Expand All @@ -15,32 +16,46 @@
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 {[]};

private _cfgWeapon = configfile >> "cfgweapons" >> _weapon;

if (isClass _cfgWeapon) then {
private _compatibleItems = [];
{
private _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 QGVAR(namespace)) then {
GVAR(namespace) = call CBA_fnc_createNamespace;
};
private _compatibleItems = GVAR(namespace) getVariable _weapon;

if (isNil "_compatibleItems") then {
_compatibleItems = [];
private _cfgWeapon = configFile >> "CfgWeapons" >> _weapon;
if (isClass _cfgWeapon) then {
{
private _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"];
_compatibleItems pushBackUnique _x;
nil
} count (getArray _cfgCompatibleItems);
} else {
if (isClass _cfgCompatibleItems) then {
{
if ((getNumber _x > 0)) then {_compatibleItems pushBackUnique (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"];

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 {
["'%1' not found in CfgWeapons",_weapon] call bis_fnc_error;
[]
_compatibleItems select {_typefilter == getNumber(configFile>>"CfgWeapons">>_x>>"itemInfo">>"type")};
};
20 changes: 20 additions & 0 deletions addons/jr/test.sqf
Original file line number Diff line number Diff line change
@@ -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;
43 changes: 43 additions & 0 deletions addons/jr/test_compatibleItems.sqf
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion addons/main/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 5e2aff7

Please sign in to comment.