Skip to content

Commit

Permalink
Common - Optimize CBA_fnc_compatibleMagazines (#1585)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnb432 committed Sep 7, 2023
1 parent 91e5af1 commit 9c9db2a
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions addons/common/fnc_compatibleMagazines.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ Description:
Retrieves a list of magazines that are compatible with a weapon.
Parameters:
_weapon - Weapon configName or config
_allMuzzles - Get magazines for all muzzles on this weapon (default: false)
_weapon - Weapon class name or config <STRING, CONFIG>
_allMuzzles - Get magazines for all muzzles on this weapon (optional, default: false) <BOOL>
Example:
Returns:
Array of magazine classnames in config capitalization <ARRAY>
Examples:
(begin example)
_mags = ["arifle_MX_SW_F"] call CBA_fnc_compatibleMagazines
_mags = [configFile >> "CfgWeapons" >> _rifle >> _glMuzzle] call CBA_fnc_compatibleMagazines
_mags = ["arifle_MX_SW_F"] call CBA_fnc_compatibleMagazines;
_mags = [configFile >> "CfgWeapons" >> _rifle >> _glMuzzle] call CBA_fnc_compatibleMagazines;
(end)
Returns:
Array of magazine classnames in config capitalization <ARRAY>
Author:
PabstMirror, based on code from Dedmen
---------------------------------------------------------------------------- */
Expand All @@ -29,37 +29,48 @@ if (_weapon isEqualType "") then {
_weapon = configFile >> "CfgWeapons" >> _weapon;
};

private _cacheKey = format ["%1#%2",_weapon,_allMuzzles];
if (isNil QGVAR(magNamespace)) then { GVAR(magNamespace) = call CBA_fnc_createNamespace; };
if (isNil QGVAR(magNamespace)) then {
GVAR(magNamespace) = createHashMap;
};

private _returnMags = GVAR(magNamespace) getVariable _cacheKey;
private _cacheKey = format ["%1#%2", _weapon, _allMuzzles];
private _returnMags = GVAR(magNamespace) get _cacheKey;

if (isNil "_returnMags") then {
if (_allMuzzles) then {
_returnMags = []; // get all mags from all muzzles
// Get all mags from all muzzles
_returnMags = [];

{
if (_x == "this") then {
_returnMags append (_weapon call CBA_fnc_compatibleMagazines);
} else {
_returnMags append ((_weapon >> _x) call CBA_fnc_compatibleMagazines);
};
} forEach getArray (_weapon >> "muzzles");

_returnMags = _returnMags arrayIntersect _returnMags;
} else {
_returnMags = getArray (_weapon >> "magazines"); // get mags just for a specific muzzle
// Get mags just for a specific muzzle
private _cfgMagazines = configFile >> "CfgMagazines";
private _cfgMagazineWells = configFile >> "CfgMagazineWells";

_returnMags = getArray (_weapon >> "magazines");

{
private _wellConfig = configFile >> "CfgMagazineWells" >> _x;
private _wellConfig = _cfgMagazineWells >> _x;

{
_returnMags append getArray _x;
} forEach configProperties [_wellConfig, "isArray _x", false];
} forEach (getArray (_weapon >> "magazineWell"));

private _cfgMagazines = configFile >> "CfgMagazines";
_returnMags = _returnMags select {isClass (_cfgMagazines >> _x)};
_returnMags = _returnMags apply {configName (_cfgMagazines >> _x)};
_returnMags = _returnMags arrayIntersect _returnMags;
};
GVAR(magNamespace) setVariable [_cacheKey, _returnMags];

GVAR(magNamespace) set [_cacheKey, _returnMags];
};

+_returnMags

0 comments on commit 9c9db2a

Please sign in to comment.