Skip to content

Commit

Permalink
experimental: Add addon weapon components support.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomGrobbe committed Aug 29, 2020
1 parent 50fd0c3 commit 3a65b8c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
43 changes: 38 additions & 5 deletions vMenu/data/ValidWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public struct ValidWeapon
public float Damage { get { Game.WeaponHudStats stats = new Game.WeaponHudStats(); Game.GetWeaponHudStats(Hash, ref stats); return stats.hudDamage; } }
public float Range { get { Game.WeaponHudStats stats = new Game.WeaponHudStats(); Game.GetWeaponHudStats(Hash, ref stats); return stats.hudRange; } }
public float Speed { get { Game.WeaponHudStats stats = new Game.WeaponHudStats(); Game.GetWeaponHudStats(Hash, ref stats); return stats.hudSpeed; } }

}

public static class ValidWeapons
Expand All @@ -47,8 +46,42 @@ public static List<ValidWeapon> WeaponList
}
}


private static Dictionary<string, string> _components = new Dictionary<string, string>();
public static Dictionary<string, string> GetWeaponComponents()
{
if (_components.Count == 0)
{
string addons = LoadResourceFile(GetCurrentResourceName(), "config/addons.json") ?? "{}";
_components = weaponComponentNames;
try
{
var addonsFile = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(addons);
if (addonsFile.ContainsKey("weapon_components"))
{
foreach (var item in addonsFile["weapon_components"])
{
string name = item;
string displayName = GetLabelText(name) ?? name;
int unused = 0;
if (GetWeaponComponentHudStats((uint)GetHashKey(name), ref unused))
{
_components.Add(name, displayName);
}
}
}
}
catch
{
Log("[WARNING] The addons.json contains invalid JSON.");
}
}
return _components;
}

private static void CreateWeaponsList()
{
_weaponsList.Clear();
foreach (var weapon in weaponNames)
{
string realName = weapon.Key;
Expand All @@ -57,13 +90,13 @@ private static void CreateWeaponsList()
{
uint hash = (uint)GetHashKey(weapon.Key);
Dictionary<string, uint> componentHashes = new Dictionary<string, uint>();
foreach (var comp in weaponComponentNames.Keys)
foreach (var comp in GetWeaponComponents().Keys)
{
if (DoesWeaponTakeWeaponComponent(hash, (uint)GetHashKey(comp)))
{
if (!componentHashes.ContainsKey(weaponComponentNames[comp]))
if (!componentHashes.ContainsKey(GetWeaponComponents()[comp]))
{
componentHashes.Add(weaponComponentNames[comp], (uint)GetHashKey(comp));
componentHashes.Add(GetWeaponComponents()[comp], (uint)GetHashKey(comp));
}
}
}
Expand Down Expand Up @@ -374,7 +407,7 @@ private static void CreateWeaponsList()
#endregion

#region weapon component names
public static readonly Dictionary<string, string> weaponComponentNames = new Dictionary<string, string>()
private static readonly Dictionary<string, string> weaponComponentNames = new Dictionary<string, string>()
{
["COMPONENT_ADVANCEDRIFLE_CLIP_01"] = GetLabelText("WCT_CLIP1"),
["COMPONENT_ADVANCEDRIFLE_CLIP_02"] = GetLabelText("WCT_CLIP2"),
Expand Down
4 changes: 4 additions & 0 deletions vMenuServer/config/addons.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"weapons": [
"addonweaponname1",
"addonweaponname2"
],
"weapon_components": [
"weapon_component_name_1",
"weapon_component_name_2"
]
}

0 comments on commit 3a65b8c

Please sign in to comment.