Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
64 changes: 62 additions & 2 deletions vMenu/data/ValidWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public static List<ValidWeapon> WeaponList
{
get
{
if (_weaponsList.Count == weaponNames.Count - 1)
// Note: We changed the check here to only return if empty,
// because checking against weaponNames.Count is inaccurate now that we have dynamic addons.
if (_weaponsList.Count > 0)
{
return _weaponsList;
}
Expand Down Expand Up @@ -99,10 +101,11 @@ public static Dictionary<string, string> GetWeaponComponents()
}
return _components;
}

private static void CreateWeaponsList()
{
_weaponsList.Clear();

// 1. ADD STANDARD HARDCODED WEAPONS
foreach (var weapon in weaponNames)
{
var realName = weapon.Key;
Expand Down Expand Up @@ -137,6 +140,63 @@ private static void CreateWeaponsList()
_weaponsList.Add(vw);
}
}

// 2. ADD DYNAMIC ADDON WEAPONS
try
{
var addonsContent = LoadResourceFile(GetCurrentResourceName(), "config/addons.json") ?? "{}";
var addonsFile = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(addonsContent);

if (addonsFile != null && addonsFile.TryGetValue("weapons", out var addonWeapons))
{
foreach (var addonSpawnName in addonWeapons)
{
if (string.IsNullOrWhiteSpace(addonSpawnName)) continue;

var hash = (uint)GetHashKey(addonSpawnName);

var componentHashes = new Dictionary<string, uint>();
var weaponComponents = GetWeaponComponents();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be done outside the foreach loop I think, and that would save having to load the components from the resource file each time for every weapon.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Yes I think that could be done. Would save some resources and performance. Do I need to test it or did you already confirm that this works?

Thanks

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have not tested it, so if you can give it a quick test that'd be great

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. Give me like an hour and I will comment again

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No rush. I'm just also looking at this PR as a whole and I wonder why did you add the addon weapons to the valid weapons list? Isn't that going to put those weapons into the main weapons categories as well?

It's been so long that I don't quite remember how it all works exactly, but I have a feeling we're going to have the addon weapons in the nomral categories as well as the addons weapon list now.

I feel like rather than adding it to this list, we should be making a new list for addon weapons that we also look at when saving/restoring the loadout.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I believe should be the fix for this issue that you're facing would be something like this:

#604

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a long time ago for me too haha. I indeed had the problem that the Addon Weapons were in the main categories of vMenu. But I think I fixed it from line 180 to 193, if I am correct.
We could do a new list etc... but I just did a fast temporary fix for me and my friends xD

I will text you until tommorrow then!

var weaponComponentKeys = weaponComponents.Keys;

foreach (var comp in weaponComponentKeys)
{
var componentHash = (uint)GetHashKey(comp);
// Only add the component if the game says this weapon supports it
if (DoesWeaponTakeWeaponComponent(hash, componentHash))
{
var componentName = weaponComponents[comp];
if (!componentHashes.ContainsKey(componentName))
{
componentHashes[componentName] = componentHash;
}
}
}

// Since we don't know the AddTextEntry key, we just use the spawn name as the display name.
string displayName = addonSpawnName;

var vw = new ValidWeapon
{
Hash = hash,
SpawnName = addonSpawnName,
Name = displayName,
Components = componentHashes,
// We use WPUnarmed as a "Safe" fallback permission that usually allows usage. So we don't need to make a new permission for every addon weapon.
Perm = Permission.WPUnarmed
};

if (!_weaponsList.Contains(vw))
{
_weaponsList.Add(vw);
}
}
}
}
catch (JsonException)
{
Log("[WARNING] The addons.json contains invalid JSON (Weapon loading).");
}
}

#region Weapon names, hashes and localized names (+ all components & tints).
Expand Down
5 changes: 3 additions & 2 deletions vMenu/menus/WeaponOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ async void IndexChangedEventHandler(Menu sender, MenuListItem item, int oldIndex
#region Loop through all weapons, create menus for them and add all menu items and handle events.
foreach (var weapon in ValidWeapons.WeaponList)
{
if (!ValidWeapons.weaponNames.ContainsKey(weapon.SpawnName)) continue;

var cat = (uint)GetWeapontypeGroup(weapon.Hash);
if (!string.IsNullOrEmpty(weapon.Name) && IsAllowed(weapon.Perm))
{
Expand Down Expand Up @@ -893,7 +895,6 @@ void OnIndexChange(Menu m, MenuItem i)
addonWeaponsMenu.OnMenuOpen += (sender) => { OnIndexChange(sender, sender.GetCurrentMenuItem()); };
}


#endregion

/// <summary>
Expand All @@ -909,4 +910,4 @@ public Menu GetMenu()
return menu;
}
}
}
}
Loading