Decompiled source of FurnitureScrapMultiplier v1.0.0

Tomatobird.FurnitureScrapMultiplier.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FurnitureScrapMultiplier.Extensions;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Tomatobird.FurnitureScrapMultiplier")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+180c4947b952d361db186951b2b0b150d08f6ff4")]
[assembly: AssemblyProduct("FurnitureScrapMultiplier")]
[assembly: AssemblyTitle("Tomatobird.FurnitureScrapMultiplier")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace FurnitureScrapMultiplier
{
	[BepInPlugin("Tomatobird.FurnitureScrapMultiplier", "FurnitureScrapMultiplier", "1.0.0")]
	public class FurnitureScrapMultiplier : BaseUnityPlugin
	{
		internal static ConfigEntry<float> luckMultiplier;

		internal static ConfigEntry<float> randomnessLowerBound;

		internal static ConfigEntry<bool> multiplierRandomness;

		internal static ConfigEntry<float> minimumMultiplier;

		internal static float originalMultiplier;

		public static FurnitureScrapMultiplier Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			multiplierRandomness = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "MultiplierRandomness", true, "Should the luck multiplier be randomized?");
			randomnessLowerBound = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RandomnessLowerBound", 0.5f, new ConfigDescription("If MultiplierRandomness is enabled, what should the luck value be multiplied with for the lower bound to choose the scrap multiplier from? (0: select multiplier randomly from range of no multiplier to maximum with current furniture luck. 1: always maximum)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			luckMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "LuckMultiplier", 4f, "How much should luck values be multiplied by for use in scrap amount multiplication?");
			minimumMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MinimumMultiplier", 0.2f, new ConfigDescription("If lots of luck decreasing furniture is installed or LuckMultiplier is very high with only luck decreasing furniture out, the scrap amount will have a decreasing multiplier. Set the lower limit for the scrap amount multiplier. Setting it to 1 will disallow scrap amount decreasing multipliers.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			Patch();
			Logger.LogInfo((object)"Tomatobird.FurnitureScrapMultiplier v1.0.0 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("Tomatobird.FurnitureScrapMultiplier");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Tomatobird.FurnitureScrapMultiplier";

		public const string PLUGIN_NAME = "FurnitureScrapMultiplier";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace FurnitureScrapMultiplier.Patches
{
	[HarmonyPatch(typeof(RoundManager))]
	internal class SpawnScrapInLevelPatcher
	{
		[HarmonyPatch("SpawnScrapInLevel")]
		[HarmonyPrefix]
		internal static void SpawnScrapInLevel_Prefix(RoundManager __instance)
		{
			if (((NetworkBehaviour)__instance).IsServer)
			{
				FurnitureScrapMultiplier.originalMultiplier = __instance.scrapAmountMultiplier;
				FurnitureScrapMultiplier.Logger.LogDebug((object)$"ScrapAmountMultiplier before luck adjustment: {__instance.scrapAmountMultiplier}");
				float num = CalculateLuckValue() * FurnitureScrapMultiplier.luckMultiplier.Value + 1f;
				if (FurnitureScrapMultiplier.multiplierRandomness.Value)
				{
					Random random = new Random(StartOfRound.Instance.randomMapSeed + 1337);
					float num2 = random.NextFloat(CalculateLowerBound(num), num);
					FurnitureScrapMultiplier.Logger.LogDebug((object)$"Calculated luck value: {num}, Lower bound multiplier: {FurnitureScrapMultiplier.randomnessLowerBound.Value}");
					__instance.scrapAmountMultiplier *= Mathf.Max(num2, FurnitureScrapMultiplier.minimumMultiplier.Value);
				}
				else
				{
					__instance.scrapAmountMultiplier *= Mathf.Max(num, FurnitureScrapMultiplier.minimumMultiplier.Value);
				}
				FurnitureScrapMultiplier.Logger.LogDebug((object)$"ScrapAmountMultiplier after luck adjustment: {__instance.scrapAmountMultiplier}");
			}
		}

		internal static float CalculateLuckValue()
		{
			float num = 0f;
			List<int> list = new List<int>();
			AutoParentToShip[] array = Object.FindObjectsByType<AutoParentToShip>((FindObjectsSortMode)0);
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i].unlockableID != -1 && StartOfRound.Instance.unlockablesList.unlockables[array[i].unlockableID].spawnPrefab)
				{
					list.Add(array[i].unlockableID);
				}
			}
			for (int j = 0; j < list.Count; j++)
			{
				if (list[j] > StartOfRound.Instance.unlockablesList.unlockables.Count)
				{
					FurnitureScrapMultiplier.Logger.LogWarning((object)$"'Lucky' furniture with id {list[j]} exceeded the unlockables list size; skipping");
				}
				num = Mathf.Clamp(num + StartOfRound.Instance.unlockablesList.unlockables[list[j]].luckValue, -0.5f, 1f);
			}
			FurnitureScrapMultiplier.Logger.LogDebug((object)$"Luck calculated: {num}");
			return num;
		}

		internal static float CalculateLowerBound(float value)
		{
			float num = value - 1f;
			num *= FurnitureScrapMultiplier.randomnessLowerBound.Value;
			return num + 1f;
		}

		[HarmonyPatch("SpawnScrapInLevel")]
		[HarmonyPostfix]
		internal static void SpawnScrapInLevel_Postfix(RoundManager __instance)
		{
			if (((NetworkBehaviour)__instance).IsServer)
			{
				FurnitureScrapMultiplier.Logger.LogDebug((object)$"SpawnScrapInLevel is done. ScrapAmountMultiplier before reset: {__instance.scrapAmountMultiplier}");
				__instance.scrapAmountMultiplier = FurnitureScrapMultiplier.originalMultiplier;
				FurnitureScrapMultiplier.Logger.LogDebug((object)$"ScrapAmountMultiplier after reset: {__instance.scrapAmountMultiplier}");
			}
		}
	}
}
namespace FurnitureScrapMultiplier.Extensions
{
	public static class RandomExtensions
	{
		public static T NextEnum<T>(this Random random) where T : struct, Enum
		{
			Array values = Enum.GetValues(typeof(T));
			return (T)values.GetValue(random.Next(values.Length));
		}

		public static T NextItem<T>(this Random random, List<T> collection)
		{
			int index = random.Next(collection.Count);
			return collection[index];
		}

		public static T NextItem<T>(this Random random, T[] collection)
		{
			int num = random.Next(collection.Length);
			return collection[num];
		}

		public static double NextDouble(this Random random, double min, double max)
		{
			return random.NextDouble() * (max - min) + min;
		}

		public static float NextFloat(this Random random, float min, float max)
		{
			return (float)random.NextDouble(min, max);
		}

		public static bool NextBool(this Random random)
		{
			return random.Next(2) == 0;
		}

		public static int NextSign(this Random random)
		{
			return random.NextBool() ? 1 : (-1);
		}

		public static Quaternion NextQuaternion(this Random random)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			return Quaternion.Euler(random.NextFloat(0f, 360f), random.NextFloat(0f, 360f), random.NextFloat(0f, 360f));
		}
	}
}