Decompiled source of StartWithRandomWeapon v1.1.0

StartWithRandomWeapon.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using StartWithRandomWeapon;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("StartWithRandomWeapon")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StartWithRandomWeapon")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("53d41deb-9a66-4104-ad33-67b001526273")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.osmarbriones.startwithrandomweapon", "StartWithRandomWeapon", "1.1.0")]
public class StartWithRandomWeaponPlugin : BaseUnityPlugin
{
	public static StartWithRandomWeaponPlugin Instance { get; set; }

	private void Awake()
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Expected O, but got Unknown
		Instance = this;
		Log("StartWithRandomWeaponPlugin initializing...");
		StartWithRandomWeaponConfig.BindConfig();
		Harmony val = new Harmony("com.osmarbriones.startwithrandomweapon");
		val.PatchAll();
		Log("StartWithRandomWeaponPlugin initialized.");
	}

	public static void Log(string Message)
	{
		((BaseUnityPlugin)Instance).Logger.LogInfo((object)Message);
	}
}
namespace StartWithRandomWeapon;

[HarmonyPatch(typeof(RunManager), "SetRunLevel")]
public class RunManager_SetRunLevel_Patch
{
	private static void Postfix(RunManager __instance)
	{
		if (CanSpawn(__instance))
		{
			StartWithRandomWeaponConfig.ReloadConfig();
			StatsManager instance = StatsManager.instance;
			List<string> list = BuildAvailableList(instance);
			if (list.Count == 0)
			{
				StartWithRandomWeaponPlugin.Log("No configured starting items are valid/present.");
				return;
			}
			int spawnCount = GetSpawnCount(list.Count);
			List<string> list2 = SelectItems(list, spawnCount);
			PurchaseItems(instance, list2);
			StartWithRandomWeaponPlugin.Log(string.Format("Spawned {0} unique starting item(s): {1}", list2.Count, string.Join(", ", list2)));
		}
	}

	private static bool CanSpawn(RunManager runManager)
	{
		if (runManager.levelsCompleted != 0)
		{
			return false;
		}
		return (Object)(object)StatsManager.instance != (Object)null;
	}

	private static List<string> BuildAvailableList(StatsManager stats)
	{
		return (from k in StartWithRandomWeaponConfig.GetEnabledWeaponKeys()
			where stats.itemDictionary.ContainsKey(k)
			select k).Where(delegate(string k)
		{
			Item val = stats.itemDictionary[k];
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			if (!val.physicalItem)
			{
				return false;
			}
			if (val.disabled)
			{
				return false;
			}
			return (!stats.itemsPurchased.ContainsKey(k) || stats.itemsPurchased[k] <= 0) ? true : false;
		}).Distinct().ToList();
	}

	private static int GetSpawnCount(int availableCount)
	{
		if (StartWithRandomWeaponConfig.SpawnOnePerPlayer != null && StartWithRandomWeaponConfig.SpawnOnePerPlayer.Value)
		{
			int num = 1;
			if ((Object)(object)GameDirector.instance != (Object)null && GameDirector.instance.PlayerList != null)
			{
				num = GameDirector.instance.PlayerList.Count;
			}
			if (num < 1)
			{
				num = 1;
			}
			if (num > availableCount)
			{
				num = availableCount;
			}
			return num;
		}
		int num2 = StartWithRandomWeaponConfig.WeaponsToSpawn?.Value ?? 1;
		if (num2 < 1)
		{
			num2 = 1;
		}
		if (num2 > 50)
		{
			num2 = 50;
		}
		if (num2 > availableCount)
		{
			num2 = availableCount;
		}
		return num2;
	}

	private static List<string> SelectItems(List<string> available, int count)
	{
		Shuffle(available);
		return available.Take(count).ToList();
	}

	private static void PurchaseItems(StatsManager statsManager, List<string> selectedItems)
	{
		foreach (string selectedItem in selectedItems)
		{
			statsManager.ItemPurchase(selectedItem);
		}
	}

	private static void Shuffle(List<string> list)
	{
		for (int num = list.Count - 1; num > 0; num--)
		{
			int num2 = Random.Range(0, num + 1);
			if (num2 != num)
			{
				string value = list[num];
				list[num] = list[num2];
				list[num2] = value;
			}
		}
	}
}
internal static class StartWithRandomWeaponConfig
{
	public static ConfigEntry<int> WeaponsToSpawn;

	public static ConfigEntry<bool> SpawnOnePerPlayer;

	public static Dictionary<string, ConfigEntry<bool>> WeaponToggles = new Dictionary<string, ConfigEntry<bool>>();

	public static StartWithRandomWeaponPlugin Plugin => StartWithRandomWeaponPlugin.Instance;

	public static Dictionary<string, bool> WeaponDefaults { get; } = new Dictionary<string, bool>
	{
		{ "Item Rubber Duck", true },
		{ "Item Melee Inflatable Hammer", true },
		{ "Item Melee Frying Pan", true },
		{ "Item Melee Sword", true },
		{ "Item Melee Stun Baton", true },
		{ "Item Melee Baseball Bat", true },
		{ "Item Melee Sledge Hammer", false },
		{ "Item Gun Shockwave", false },
		{ "Item Gun Stun", false },
		{ "Item Gun Handgun", false },
		{ "Item Gun Tranq", false },
		{ "Item Gun Laser", false },
		{ "Item Gun Shotgun", false },
		{ "Item Cart Cannon", false },
		{ "Item Cart Laser", false }
	};


	public static void BindConfig()
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Expected O, but got Unknown
		WeaponsToSpawn = ((BaseUnityPlugin)Plugin).Config.Bind<int>("General", "WeaponsToSpawn", 3, new ConfigDescription("Number of starting weapons to spawn (ignored if SpawnOnePerPlayer is true).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 50), Array.Empty<object>()));
		SpawnOnePerPlayer = ((BaseUnityPlugin)Plugin).Config.Bind<bool>("General", "SpawnOnePerPlayer", true, new ConfigDescription("If true, ignore WeaponsToSpawn and spawn one starting weapon per current player.", (AcceptableValueBase)null, Array.Empty<object>()));
		BindWeaponToggles();
		StartWithRandomWeaponPlugin.Log($"Config loaded: WeaponsToSpawn = {WeaponsToSpawn.Value}, " + $"SpawnOnePerPlayer = {SpawnOnePerPlayer.Value}, " + "WeaponToggles = " + string.Join(", ", WeaponToggles.Select((KeyValuePair<string, ConfigEntry<bool>> kvp) => $"{kvp.Key} = {kvp.Value.Value}")));
	}

	public static void BindWeaponToggles()
	{
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Expected O, but got Unknown
		foreach (KeyValuePair<string, bool> weaponDefault in WeaponDefaults)
		{
			string key = weaponDefault.Key;
			bool value = weaponDefault.Value;
			ConfigEntry<bool> value2 = ((BaseUnityPlugin)Plugin).Config.Bind<bool>("Weapons", key, value, new ConfigDescription("Enable spawning " + key + " weapon at start.", (AcceptableValueBase)null, Array.Empty<object>()));
			WeaponToggles[key] = value2;
		}
	}

	public static void ReloadConfig()
	{
		((BaseUnityPlugin)Plugin).Config.Reload();
	}

	public static List<string> GetEnabledWeaponKeys()
	{
		return (from kvp in WeaponToggles
			where kvp.Value.Value
			select kvp.Key).ToList();
	}
}