Decompiled source of NoAll2Handed v0.2.2

NoAll2Handed.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using NoAll2Handed.Patches;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("NoAll2Handed")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+d792b87426802c567847874ac56e09bf4517a6c0")]
[assembly: AssemblyProduct("NoAll2Handed")]
[assembly: AssemblyTitle("NoAll2Handed")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NoAll2Handed
{
	[BepInPlugin("MettaurSp.NoAll2Handed", "No All 2 Handed", "0.1.0")]
	public class NoAll2HandedMod : BaseUnityPlugin
	{
		private const string modGUID = "MettaurSp.NoAll2Handed";

		private const string modName = "No All 2 Handed";

		private const string modVersion = "0.1.0";

		private readonly Harmony harmony = new Harmony("MettaurSp.NoAll2Handed");

		public static NoAll2HandedMod instance;

		internal static ManualLogSource Log;

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = new NoAll2HandedMod();
				Log = Logger.CreateLogSource("MettaurSp.NoAll2Handed");
			}
			harmony.PatchAll(typeof(NoAll2HandedMod));
			harmony.PatchAll(typeof(ItemGenOverridePatch));
			Log.LogInfo((object)"NoAll2Handed mod initialized");
		}
	}
}
namespace NoAll2Handed.Patches
{
	public class NoAll2HandedRandom : Random
	{
		private int OverrideCounter = 0;

		public Random BaseRandom;

		private bool ShouldOverride = true;

		private bool IsRerollingFixedScrap = false;

		public NoAll2HandedRandom(Random baseRandom)
		{
			BaseRandom = baseRandom;
		}

		private static string GetScrapText(SpawnableItemWithRarity scrap)
		{
			return string.Format("{0} handed {1} with rarity {2}", scrap.spawnableItem.twoHanded ? "two" : "one", ((Object)scrap.spawnableItem).name, scrap.rarity);
		}

		public override int Next(int minValue, int maxValue)
		{
			int num = BaseRandom.Next(minValue, maxValue);
			if (IsRerollingFixedScrap && ShouldOverride)
			{
				List<SpawnableItemWithRarity> spawnableScrap = RoundManager.Instance.currentLevel.spawnableScrap;
				ShouldOverride = minValue == 0 && maxValue == spawnableScrap.Count;
				NoAll2HandedMod.Log.LogMessage((object)$"{minValue} - {maxValue}: {num}; {spawnableScrap.Count}; {ShouldOverride}");
				if (ShouldOverride)
				{
					SpawnableItemWithRarity val = spawnableScrap[num];
					string scrapText = GetScrapText(val);
					while (val.spawnableItem.twoHanded)
					{
						NoAll2HandedMod.Log.LogWarning((object)$"Rolled all scrap {scrapText}; rerolled by mod");
						num = BaseRandom.Next(minValue, maxValue);
						val = spawnableScrap[num];
						scrapText = GetScrapText(val);
					}
					bool flag = val.rarity < 5 || val.spawnableItem.twoHanded;
					bool flag2 = flag && OverrideCounter < 2;
					if (flag2)
					{
						NoAll2HandedMod.Log.LogMessage((object)$"Rolled all scrap {scrapText}; rerolled by base game");
						OverrideCounter++;
					}
					if ((!flag || OverrideCounter >= 2) && !flag2)
					{
						NoAll2HandedMod.Log.LogMessage((object)$"Rolled all scrap {scrapText}");
						ShouldOverride = val.rarity < 5;
					}
				}
				if (OverrideCounter >= 2 && minValue == 0 && maxValue == 100)
				{
					ShouldOverride = false;
					NoAll2HandedMod.Log.LogWarning((object)string.Format("Rolled all scrap at rarity < 5; 0 - 100: {0} < 60; {1}", num, (num < 60) ? "switching back to mixed" : "keeping item"));
				}
			}
			bool flag3 = minValue == 0 && maxValue == 500;
			if (flag3 && !IsRerollingFixedScrap)
			{
				NoAll2HandedMod.Log.LogMessage((object)string.Format("Rolling for fixed loot chance of 0 - 500: {0} <= 25; {1}", num, (num <= 25) ? "fixed" : "not fixed"));
			}
			IsRerollingFixedScrap |= flag3;
			ShouldOverride &= !flag3 || num <= 25;
			return num;
		}
	}
	[HarmonyPatch(typeof(RoundManager))]
	internal class ItemGenOverridePatch
	{
		[HarmonyPatch("SpawnScrapInLevel")]
		[HarmonyPrefix]
		private static bool itemGenOverrideStartPatch()
		{
			NoAll2HandedMod.Log.LogInfo((object)"Patching RoundManager.AnomalyRandom in RoundManager.SpawnScrapInLevel");
			RoundManager.Instance.AnomalyRandom = new NoAll2HandedRandom(RoundManager.Instance.AnomalyRandom);
			return true;
		}

		[HarmonyPatch("SpawnScrapInLevel")]
		[HarmonyPostfix]
		private static void itemGenOverrideEndPatch()
		{
			if (RoundManager.Instance.AnomalyRandom is NoAll2HandedRandom noAll2HandedRandom)
			{
				RoundManager.Instance.AnomalyRandom = noAll2HandedRandom.BaseRandom;
				NoAll2HandedMod.Log.LogInfo((object)"Restored RoundManager.AnomalyRandom");
			}
		}
	}
}