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

Common - Optimize CBA_fnc_compatibleMagazines #1585

Merged
merged 5 commits into from
Sep 7, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 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,50 @@ 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 = configNull;
johnb432 marked this conversation as resolved.
Show resolved Hide resolved

{
private _wellConfig = configFile >> "CfgMagazineWells" >> _x;
_wellConfig = _cfgMagazineWells >> _x;
johnb432 marked this conversation as resolved.
Show resolved Hide resolved

{
_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
Loading