Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joint Rails - Optimize CBA_fnc_compatibleItems #1558

Merged
merged 10 commits into from
Sep 7, 2023
91 changes: 55 additions & 36 deletions addons/jr/fnc_compatibleItems.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,78 @@ Examples:
(end)

Author:
Original by Karel Moricky, Enhanced by Robalo, jokoho, commy2
Original by Karel Moricky, Enhanced by Robalo, jokoho, commy2, johnb43
---------------------------------------------------------------------------- */
SCRIPT(compatibleItems);

params [["_weapon", "", [""]], ["_typefilter", nil, ["", 0]]];

if (_weapon == "") exitWith {[]};

private _cfgWeapons = configFile >> "CfgWeapons";
private _weaponConfig = _cfgWeapons >> _weapon;

// Check if weapon exists
if !(isClass _weaponConfig) exitWith {
["'%1' not found in CfgWeapons", _weapon] call BIS_fnc_error;

[]
};

// Convert filter into number (if string)
if (_typefilter isEqualType "") then {
_typefilter = [-1, 101, 201, 301, 302] param [["", "muzzle", "optic", "pointer", "bipod"] find _typefilter, -1];
};

private _typeFilterExists = !isNil "_typefilter";

// Check if valid type filter
if (_typeFilterExists && {!(_typefilter in [101, 201, 301, 302])}) exitWith {[]};

if (isNil QGVAR(namespace)) then {
GVAR(namespace) = call CBA_fnc_createNamespace;
GVAR(namespace) = createHashMap;
};

private _compatibleItems = GVAR(namespace) getVariable _weapon;
// Get cached result, if it exists
private _cachekey = format ["%1#%2", _weapon, ["all", _typefilter] select _typeFilterExists];
private _compatibleItems = GVAR(namespace) get _cachekey;

if (!isNil "_compatibleItems") exitWith {
+_compatibleItems
};

if (isNil "_compatibleItems") then {
if (_typeFilterExists) then {
// Get all compatible weapon attachments, then filter
_compatibleItems = _weapon call CBA_fnc_compatibleItems;
_compatibleItems = _compatibleItems select {_typefilter == getNumber (_cfgWeapons >> _x >> "itemInfo" >> "type")};
} else {
_compatibleItems = [];
private _cfgWeapons = configFile >> "CfgWeapons";
private _weaponConfig = _cfgWeapons >> _weapon;

if (isClass _weaponConfig) then {
{
private _cfgCompatibleItems = _x >> "compatibleItems";
{
private _cfgCompatibleItems = _x >> "compatibleItems";

if (isArray _cfgCompatibleItems) then {
if (isArray _cfgCompatibleItems) then {
{
// Ensure item class name is in config case
_compatibleItems pushBackUnique configName (_cfgWeapons >> _x);
} forEach getArray _cfgCompatibleItems;
} else {
if (isClass _cfgCompatibleItems) then {
{
_compatibleItems pushBackUnique _x;
} forEach getArray _cfgCompatibleItems;
} else {
if (isClass _cfgCompatibleItems) then {
{
if (getNumber _x > 0) then {
_compatibleItems pushBackUnique configName _x;
};
} forEach configProperties [_cfgCompatibleItems, "isNumber _x"];
};
if (getNumber _x > 0) then {
// Ensure item class name is in config case
_compatibleItems pushBackUnique configName (_cfgWeapons >> configName _x);
};
} forEach configProperties [_cfgCompatibleItems, "isNumber _x"];
};
} forEach configProperties [_weaponConfig >> "WeaponSlotsInfo", "isClass _x"];
};
} forEach configProperties [_weaponConfig >> "WeaponSlotsInfo", "isClass _x"];

// Ensure item class names are in config case
_compatibleItems = _compatibleItems apply {configName (_cfgWeapons >> _x)};

GVAR(namespace) setVariable [_weapon, _compatibleItems]; // save entry in cache
} else {
["'%1' not found in CfgWeapons", _weapon] call BIS_fnc_error;
};
// Remove non-existent item(s)
_compatibleItems deleteAt (_compatibleItems find "");
};

if (isNil "_typefilter") then { //return
+ _compatibleItems
} else {
if (_typefilter isEqualType "") then {
_typefilter = [-1, 101, 201, 301, 302] param [["", "muzzle", "optic", "pointer", "bipod"] find _typefilter, -1];
};
// Cache result
GVAR(namespace) set [_cachekey, _compatibleItems];

_compatibleItems select {_typefilter == getNumber (configFile >> "CfgWeapons" >> _x >> "itemInfo" >> "type")}
};
+_compatibleItems
Loading