Decompiled source of Empress Valuable Editor v1.0.0

EmpressValuableEditor.dll

Decompiled 5 days ago
using System;
using System.Collections;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
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("Omniscye")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EmpressValuableEditor")]
[assembly: AssemblyTitle("EmpressValuableEditor")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
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;
		}
	}
}
namespace Empress.ValuableEditor
{
	[BepInPlugin("Empress.ValuableEditor", "Empress Valuable Editor", "1.2.0")]
	public class ValuableEditorPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "Empress.ValuableEditor";

		public const string PluginName = "Empress Valuable Editor";

		public const string PluginVersion = "1.2.0";

		public static ConfigEntry<float> GlobalMultiplierConfig;

		public static Dictionary<string, ConfigEntry<int>> ValuableConfigEntries = new Dictionary<string, ConfigEntry<int>>();

		public static ValuableEditorPlugin Instance;

		public static ManualLogSource Log;

		private void Awake()
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			GlobalMultiplierConfig = ((BaseUnityPlugin)this).Config.Bind<float>("0. Global Settings", "Global Value Multiplier", 1f, new ConfigDescription("Multiplies all valuable prices. 1 is Normal (100%), 0.5 is 50%, 10 is 1000%.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 10f), Array.Empty<object>()));
			Harmony val = new Harmony("Empress.ValuableEditor");
			val.PatchAll();
			((MonoBehaviour)this).StartCoroutine(GenerateConfigFromRegistry());
			Log.LogInfo((object)"Empress Valuable Editor Loaded");
		}

		private IEnumerator GenerateConfigFromRegistry()
		{
			yield return (object)new WaitForSeconds(1f);
			Type rmType = AccessTools.TypeByName("RunManager");
			if (rmType == null)
			{
				yield break;
			}
			FieldInfo instanceField = AccessTools.Field(rmType, "instance");
			object rmInstance = instanceField.GetValue(null);
			FieldInfo levelsField = AccessTools.Field(rmType, "levels");
			if (!(levelsField.GetValue(rmInstance) is IEnumerable levels))
			{
				yield break;
			}
			Log.LogInfo((object)"Scanning RunManager for Valuables...");
			foreach (object lvl in levels)
			{
				if (lvl == null || !(AccessTools.Field(lvl.GetType(), "ValuablePresets")?.GetValue(lvl) is IEnumerable presets))
				{
					continue;
				}
				foreach (object preset in presets)
				{
					string[] categories = new string[7] { "tiny", "small", "medium", "big", "wide", "tall", "veryTall" };
					string[] array = categories;
					foreach (string catName in array)
					{
						if (!(AccessTools.Field(preset.GetType(), catName)?.GetValue(preset) is IEnumerable prefabsList))
						{
							continue;
						}
						foreach (object prefabObj in prefabsList)
						{
							GameObject actualPrefab = null;
							PropertyInfo prefabProp = AccessTools.Property(prefabObj.GetType(), "Prefab");
							if (prefabProp != null)
							{
								object? value = prefabProp.GetValue(prefabObj, null);
								actualPrefab = (GameObject)((value is GameObject) ? value : null);
							}
							else
							{
								GameObject go = (GameObject)((prefabObj is GameObject) ? prefabObj : null);
								if (go != null)
								{
									actualPrefab = go;
								}
							}
							if ((Object)(object)actualPrefab != (Object)null)
							{
								ValuableObject valuableComp = actualPrefab.GetComponent<ValuableObject>();
								if ((Object)(object)valuableComp != (Object)null)
								{
									GetOrCreateValuableConfig(valuableComp);
								}
							}
						}
					}
				}
			}
			((BaseUnityPlugin)Instance).Config.Save();
			Log.LogInfo((object)$"Populated Config with {ValuableConfigEntries.Count} valuables.");
		}

		public static int GetOrCreateValuableConfig(ValuableObject valuable)
		{
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected O, but got Unknown
			if ((Object)(object)valuable == (Object)null)
			{
				return 100;
			}
			string text = ((Object)((Component)valuable).gameObject).name.Replace("(Clone)", "").Trim();
			if (!ValuableConfigEntries.ContainsKey(text))
			{
				int num = 100;
				if ((Object)(object)valuable.valuePreset != (Object)null)
				{
					num = (int)valuable.valuePreset.valueMax;
				}
				string text2 = $"Set the value for {text} in DOLLARS.\n(Example: 100). Vanilla Max was approx: {num}";
				ConfigDescription val = new ConfigDescription(text2, (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 1000000), Array.Empty<object>());
				ConfigEntry<int> value = ((BaseUnityPlugin)Instance).Config.Bind<int>("Valuables", text, num, val);
				ValuableConfigEntries.Add(text, value);
			}
			return ValuableConfigEntries[text].Value;
		}
	}
	[HarmonyPatch(typeof(ValuableObject), "DollarValueSetLogic")]
	public static class ValuableObjectValuePatch
	{
		private static bool Prefix(ValuableObject __instance)
		{
			if (!SemiFunc.IsMasterClientOrSingleplayer())
			{
				return true;
			}
			if (__instance.dollarValueSet)
			{
				return false;
			}
			int orCreateValuableConfig = ValuableEditorPlugin.GetOrCreateValuableConfig(__instance);
			float value = ValuableEditorPlugin.GlobalMultiplierConfig.Value;
			int num = Mathf.RoundToInt((float)orCreateValuableConfig * value);
			if (__instance.dollarValueOverride != 0)
			{
				__instance.dollarValueOriginal = __instance.dollarValueOverride;
				__instance.dollarValueCurrent = __instance.dollarValueOverride;
			}
			else
			{
				__instance.dollarValueOriginal = num;
				__instance.dollarValueCurrent = num;
			}
			__instance.dollarValueSet = true;
			return false;
		}
	}
}