Decompiled source of LCGoldScrapConfig v0.1.0

GoldScrapConfig.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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GoldScrapConfig.Patches;
using GoldScrapConfig.Store;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("GoldScrapConfig")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("GoldScrapConfig")]
[assembly: AssemblyTitle("GoldScrapConfig")]
[assembly: AssemblyVersion("0.1.0.0")]
[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 GoldScrapConfig
{
	[BepInPlugin("mine9289.GoldScrapConfig", "GoldScrapConfig", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<float> OreBonusCapMultiplier;

		internal static ConfigEntry<float> CrownPercentMultiplier;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			OreBonusCapMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Ore", "Ore Bonus multiplier", 1f, "Ore bonus cap ×. 1 = vanilla.");
			CrownPercentMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Crown", "Crown multiplier", 1f, "Crown value % ×. 1 = vanilla.");
			StorePricePatch.RegisterPerItemPrices(((BaseUnityPlugin)this).Config);
			harmony = new Harmony("mine9289.GoldScrapConfig");
			harmony.PatchAll(typeof(Plugin).Assembly);
			Log.LogInfo((object)("GoldScrapConfig 0.1.0 ore×" + OreBonusCapMultiplier.Value + " crown×" + CrownPercentMultiplier.Value + " " + ((BaseUnityPlugin)this).Config.ConfigFilePath));
		}
	}
	internal static class PluginInfo
	{
		public const string PLUGIN_GUID = "mine9289.GoldScrapConfig";

		public const string PLUGIN_NAME = "GoldScrapConfig";

		public const string PLUGIN_VERSION = "0.1.0";
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "GoldScrapConfig";

		public const string PLUGIN_NAME = "GoldScrapConfig";

		public const string PLUGIN_VERSION = "0.1.0";
	}
}
namespace GoldScrapConfig.Store
{
	internal static class GoldStoreDefaults
	{
		internal const string NoPriceOverrideFolder = "GoldenTicket";

		internal static readonly Dictionary<string, int> StoreDefaultPriceByFolder = new Dictionary<string, int>(StringComparer.Ordinal)
		{
			["BronzeSuit"] = 2500,
			["CatOGold"] = 777,
			["CreditsCard"] = -1,
			["GoldCrown"] = 5000,
			["GoldfatherClock"] = 612,
			["GoldMedal"] = 3333,
			["GoldNugget"] = 100,
			["GoldOre"] = 500,
			["GoldSuit"] = 10000,
			["GoldenGlove"] = 800,
			["GoldenHourglass"] = 133,
			["GoldenPickaxe"] = 75,
			["GoldenThrone"] = 25000,
			["GoldToilet"] = 450,
			["GoldTrophy"] = 2000,
			["GroovyGold"] = 150,
			["SafeBox"] = 1000,
			["SilverSuit"] = 5000
		};

		internal static int DefaultPriceOrMinusOne(string folderName)
		{
			if (!StoreDefaultPriceByFolder.TryGetValue(folderName, out var value))
			{
				return -1;
			}
			return value;
		}
	}
	internal static class GoldStoreItemIds
	{
		internal static readonly string[] All = new string[19]
		{
			"BronzeSuit", "CatOGold", "CreditsCard", "GoldCrown", "GoldfatherClock", "GoldMedal", "GoldNugget", "GoldOre", "GoldSuit", "GoldenGlove",
			"GoldenHourglass", "GoldenPickaxe", "GoldenThrone", "GoldenTicket", "GoldToilet", "GoldTrophy", "GroovyGold", "SafeBox", "SilverSuit"
		};
	}
	internal static class StorePriceApply
	{
		internal static void ApplyCreditsToItem(object itemData, int credits)
		{
			if (credits < 0 || itemData == null)
			{
				return;
			}
			Traverse obj = Traverse.Create(itemData);
			obj.Field("localStorePrice").SetValue((object)credits);
			object value = obj.Field("itemProperties").GetValue();
			if (value != null && !IsUnityNull(value))
			{
				Traverse.Create(value).Field("creditsWorth").SetValue((object)credits);
			}
			if (!(obj.Field("storeTerminalNodes").GetValue() is Array array))
			{
				return;
			}
			foreach (object item in array)
			{
				if (item != null && !IsUnityNull(item))
				{
					Traverse.Create(item).Field("itemCost").SetValue((object)credits);
				}
			}
		}

		private static bool IsUnityNull(object o)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			return (Object)o == (Object)null;
		}
	}
}
namespace GoldScrapConfig.Patches
{
	[HarmonyPatch]
	internal static class StorePricePatch
	{
		private static readonly Dictionary<string, ConfigEntry<int>> PriceEntries = new Dictionary<string, ConfigEntry<int>>();

		private static MethodBase? TargetMethod()
		{
			return HarmonyTargetHelpers.GoldScrapConfigs_SetCustomGoldStorePrices();
		}

		private static bool Prepare()
		{
			return TargetMethod() != null;
		}

		internal static void RegisterPerItemPrices(ConfigFile cfg)
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			PriceEntries.Clear();
			string[] all = GoldStoreItemIds.All;
			foreach (string text in all)
			{
				if (!string.Equals(text, "GoldenTicket", StringComparison.Ordinal))
				{
					int num = GoldStoreDefaults.DefaultPriceOrMinusOne(text);
					PriceEntries[text] = cfg.Bind<int>("Store.Prices", text, num, new ConfigDescription("-1: LCGold default", (AcceptableValueBase)null, Array.Empty<object>()));
				}
			}
		}

		private static void Postfix()
		{
			if (!(StoreFieldCache.AllGoldStoreItemData?.GetValue(null) is Array array))
			{
				return;
			}
			foreach (object item in array)
			{
				if (item != null)
				{
					string value = Traverse.Create(item).Field<string>("folderName").Value;
					if (!string.IsNullOrEmpty(value) && !string.Equals(value, "GoldenTicket", StringComparison.Ordinal) && PriceEntries.TryGetValue(value, out ConfigEntry<int> value2) && value2.Value >= 0)
					{
						StorePriceApply.ApplyCreditsToItem(item, value2.Value);
					}
				}
			}
		}
	}
	[HarmonyPatch]
	internal static class GoldOreFieldsPatch
	{
		private static MethodBase? TargetMethod()
		{
			return HarmonyTargetHelpers.GoldNuggetScript_Start();
		}

		private static bool Prepare()
		{
			return TargetMethod() != null;
		}

		private static void Prefix(object __instance)
		{
			Traverse val = Traverse.Create(__instance);
			if (HarmonyTargetHelpers.IsGoldOreItem(val.Field("itemType").GetValue()))
			{
				float value = Plugin.OreBonusCapMultiplier.Value;
				if (value > 0f && !Mathf.Approximately(value, 1f))
				{
					int value2 = val.Field<int>("oreBonusPerQuotaMaxIncrease").Value;
					val.Field("oreBonusPerQuotaMaxIncrease").SetValue((object)Mathf.Max(1, Mathf.RoundToInt((float)value2 * value)));
				}
			}
		}
	}
	[HarmonyPatch]
	internal static class CrownFieldsPatch
	{
		private static MethodBase? TargetMethod()
		{
			return HarmonyTargetHelpers.CrownScript_Start();
		}

		private static bool Prepare()
		{
			return TargetMethod() != null;
		}

		private static void Prefix(object __instance)
		{
			Traverse val = Traverse.Create(__instance);
			float value = Plugin.CrownPercentMultiplier.Value;
			if (value > 0f && !Mathf.Approximately(value, 1f))
			{
				int value2 = val.Field<int>("itemValuePercentage").Value;
				val.Field("itemValuePercentage").SetValue((object)Mathf.Max(0, Mathf.RoundToInt((float)value2 * value)));
			}
		}
	}
	internal static class HarmonyTargetHelpers
	{
		private static readonly Lazy<MethodBase?> SetCustomGoldStorePrices = new Lazy<MethodBase>(delegate
		{
			Type type3 = AccessTools.TypeByName("GoldScrapConfigs");
			return (!(type3 == null)) ? AccessTools.Method(type3, "SetCustomGoldStorePrices", new Type[1] { typeof(float) }, (Type[])null) : null;
		});

		private static readonly Lazy<MethodBase?> GoldNuggetStart = new Lazy<MethodBase>(delegate
		{
			Type type2 = AccessTools.TypeByName("GoldNuggetScript");
			return (!(type2 == null)) ? AccessTools.Method(type2, "Start", (Type[])null, (Type[])null) : null;
		});

		private static readonly Lazy<MethodBase?> CrownStart = new Lazy<MethodBase>(delegate
		{
			Type type = AccessTools.TypeByName("CrownScript");
			return (!(type == null)) ? AccessTools.Method(type, "Start", (Type[])null, (Type[])null) : null;
		});

		private static readonly Dictionary<Type, object> GoldOreValueByEnumType = new Dictionary<Type, object>();

		internal static MethodBase? GoldScrapConfigs_SetCustomGoldStorePrices()
		{
			return SetCustomGoldStorePrices.Value;
		}

		internal static MethodBase? GoldNuggetScript_Start()
		{
			return GoldNuggetStart.Value;
		}

		internal static MethodBase? CrownScript_Start()
		{
			return CrownStart.Value;
		}

		internal static bool IsGoldOreItem(object? itemType)
		{
			if (itemType == null)
			{
				return false;
			}
			Type type = itemType.GetType();
			lock (GoldOreValueByEnumType)
			{
				if (!GoldOreValueByEnumType.TryGetValue(type, out object value))
				{
					try
					{
						value = Enum.Parse(type, "GoldOre");
					}
					catch (ArgumentException)
					{
						return false;
					}
					GoldOreValueByEnumType[type] = value;
				}
				return value.Equals(itemType);
			}
		}
	}
	internal static class StoreFieldCache
	{
		private static readonly FieldInfo? AllItems = MakeField();

		internal static FieldInfo? AllGoldStoreItemData => AllItems;

		private static FieldInfo? MakeField()
		{
			Type type = AccessTools.TypeByName("StoreAndTerminal");
			if (!(type == null))
			{
				return AccessTools.Field(type, "allGoldStoreItemData");
			}
			return null;
		}
	}
}