Decompiled source of Empress Currency v1.0.1

EmpressCurrency.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.TextCore;
using UnityEngine.UI;

[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("EmpressCurrency")]
[assembly: AssemblyTitle("EmpressCurrency")]
[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 RepoOmniCoin
{
	[BepInPlugin("com.repomods.omnicoin", "OmniCoin Currency Overhaul", "1.13.0")]
	public class OmniCoinPlugin : BaseUnityPlugin
	{
		public static string IconTag = "<sprite name=\"OC_Icon\">";

		private void Start()
		{
			LoadCustomIcon();
			Harmony.CreateAndPatchAll(typeof(OmniCoinPatches), (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"OmniCoin System Loaded. 10,000 Dollars = 1 OmniCoin.");
		}

		private void LoadCustomIcon()
		{
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Expected O, but got Unknown
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Expected O, but got Unknown
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Expected O, but got Unknown
			//IL_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Expected O, but got Unknown
			string path = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "empress.dat");
			if (!File.Exists(path))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"empress.dat not found. Using text fallback.");
				IconTag = "OC";
				return;
			}
			try
			{
				byte[] array = File.ReadAllBytes(path);
				for (int i = 0; i < array.Length; i++)
				{
					array[i] = (byte)(array[i] ^ 0x5Au);
				}
				Texture2D val = new Texture2D(2, 2);
				ImageConversion.LoadImage(val, array);
				((Texture)val).filterMode = (FilterMode)1;
				((Texture)val).wrapMode = (TextureWrapMode)1;
				TMP_SpriteAsset val2 = ScriptableObject.CreateInstance<TMP_SpriteAsset>();
				((Object)val2).name = "OmniCoinAsset";
				((TMP_Asset)val2).hashCode = TMP_TextUtilities.GetSimpleHashCode("OmniCoinAsset");
				if (!TrySetField(val2, "m_Version", "1.1.0"))
				{
					TrySetField(val2, "version", "1.1.0");
				}
				Shader val3 = Shader.Find("TextMeshPro/Sprite");
				if (!Object.op_Implicit((Object)(object)val3))
				{
					val3 = Shader.Find("Sprites/Default");
				}
				if (!Object.op_Implicit((Object)(object)val3))
				{
					val3 = Shader.Find("Mobile/Particles/Alpha Blended");
				}
				Material val4 = new Material(val3);
				val4.mainTexture = (Texture)(object)val;
				((TMP_Asset)val2).material = val4;
				val2.spriteSheet = (Texture)(object)val;
				List<TMP_SpriteGlyph> list = new List<TMP_SpriteGlyph>();
				List<TMP_SpriteCharacter> list2 = new List<TMP_SpriteCharacter>();
				TMP_SpriteGlyph val5 = new TMP_SpriteGlyph();
				((Glyph)val5).index = 0u;
				((Glyph)val5).metrics = new GlyphMetrics((float)((Texture)val).width, (float)((Texture)val).height, 0f, (float)((Texture)val).height * 0.8f, (float)((Texture)val).width);
				((Glyph)val5).glyphRect = new GlyphRect(0, 0, ((Texture)val).width, ((Texture)val).height);
				list.Add(val5);
				TMP_SpriteCharacter val6 = new TMP_SpriteCharacter(0u, val5);
				val6.name = "OC_Icon";
				((TMP_TextElement)val6).scale = 1f;
				((TMP_TextElement)val6).unicode = 57344u;
				int simpleHashCode = TMP_TextUtilities.GetSimpleHashCode("OC_Icon");
				SetHashCode((TMP_TextElement)(object)val6, simpleHashCode);
				list2.Add(val6);
				bool flag = TrySetField(val2, "m_SpriteCharacterTable", list2) || TrySetField(val2, "m_spriteCharacterTable", list2);
				bool flag2 = TrySetField(val2, "m_SpriteGlyphTable", list) || TrySetField(val2, "m_spriteGlyphTable", list);
				if (!flag || !flag2)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Failed to inject Sprite Tables! Icon will likely fail.");
				}
				val2.UpdateLookupTables();
				if ((Object)(object)TMP_Settings.defaultSpriteAsset != (Object)null)
				{
					if (TMP_Settings.defaultSpriteAsset.fallbackSpriteAssets == null)
					{
						TMP_Settings.defaultSpriteAsset.fallbackSpriteAssets = new List<TMP_SpriteAsset>();
					}
					TMP_Settings.defaultSpriteAsset.fallbackSpriteAssets.Insert(0, val2);
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Success! empress.dat loaded and registered.");
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"TMP Default Sprite Asset is null. Text fallback active.");
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Icon Load Error: " + ex.Message));
				IconTag = "OC";
			}
		}

		private bool TrySetField(object target, string fieldName, object value)
		{
			FieldInfo fieldInfo = AccessTools.Field(target.GetType(), fieldName);
			if (fieldInfo != null)
			{
				fieldInfo.SetValue(target, value);
				return true;
			}
			return false;
		}

		private void SetHashCode(TMP_TextElement element, int hash)
		{
			if (!TrySetField(element, "m_HashCode", hash) && !TrySetField(element, "m_hashCode", hash) && !TrySetField(element, "<hashCode>k__BackingField", hash))
			{
				PropertyInfo propertyInfo = AccessTools.Property(typeof(TMP_TextElement), "hashCode");
				if (propertyInfo != null && propertyInfo.CanWrite)
				{
					propertyInfo.SetValue(element, hash);
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not find field to set HashCode on TMP_TextElement.");
				}
			}
		}
	}
	[HarmonyPatch]
	public static class OmniCoinPatches
	{
		private const float DIVISOR_THOUSANDS = 10f;

		private const float DIVISOR_RAW = 10000f;

		private static string FormatOC(float value)
		{
			return value.ToString("0.##") + " " + OmniCoinPlugin.IconTag;
		}

		private static string GetOCFromThousands(int value)
		{
			return FormatOC((float)value / 10f);
		}

		private static string GetOCFromRaw(int value)
		{
			return FormatOC((float)value / 10000f);
		}

		private static void EnsureRichText(TextMeshProUGUI ui)
		{
			if (!((TMP_Text)ui).richText)
			{
				((TMP_Text)ui).richText = true;
			}
		}

		private static void EnsureRichText3D(TextMeshPro tm)
		{
			if (!((TMP_Text)tm).richText)
			{
				((TMP_Text)tm).richText = true;
			}
		}

		[HarmonyPatch(typeof(CurrencyUI), "Update")]
		[HarmonyPostfix]
		public static void CurrencyUI_Update_Postfix(CurrencyUI __instance)
		{
			TextMeshProUGUI component = ((Component)__instance).GetComponent<TextMeshProUGUI>();
			if (!((Object)(object)component == (Object)null) && ((Component)component).gameObject.activeInHierarchy)
			{
				EnsureRichText(component);
				if (((TMP_Text)component).text.Contains("$"))
				{
					int value = SemiFunc.StatGetRunCurrency();
					((TMP_Text)component).text = GetOCFromThousands(value);
				}
			}
		}

		[HarmonyPatch(typeof(HaulUI), "Update")]
		[HarmonyPostfix]
		public static void HaulUI_Update_Postfix(HaulUI __instance)
		{
			TextMeshProUGUI component = ((Component)__instance).GetComponent<TextMeshProUGUI>();
			if ((Object)(object)component == (Object)null || !((Component)component).gameObject.activeInHierarchy)
			{
				return;
			}
			EnsureRichText(component);
			if (SemiFunc.RunIsLevel())
			{
				if ((Object)(object)RoundDirector.instance != (Object)null)
				{
					int num = Mathf.Max(0, RoundDirector.instance.currentHaul);
					int extractionHaulGoal = RoundDirector.instance.extractionHaulGoal;
					string iconTag = OmniCoinPlugin.IconTag;
					float num2 = (float)num / 10000f;
					float num3 = (float)extractionHaulGoal / 10000f;
					((TMP_Text)component).text = $"<size=30><color=#558B2F>{iconTag}</color> {num2:0.##}<color=#616161> <size=45>/</size> </color><color=#558B2F>{iconTag}</color> <u>{num3:0.##}";
				}
			}
			else
			{
				int value = SemiFunc.StatGetRunCurrency();
				((TMP_Text)component).text = GetOCFromThousands(value);
			}
		}

		[HarmonyPatch(typeof(ShopCostUI), "Update")]
		[HarmonyPostfix]
		public static void ShopCostUI_Update_Postfix(ShopCostUI __instance)
		{
			TextMeshProUGUI component = ((Component)__instance).GetComponent<TextMeshProUGUI>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			EnsureRichText(component);
			if (SemiFunc.RunIsShop())
			{
				int num = SemiFunc.ShopGetTotalCost();
				if (num > 0)
				{
					((TMP_Text)component).text = "-" + GetOCFromThousands(num);
				}
			}
			else if (SemiFunc.RunIsLevel() && __instance.animatedValue > 0)
			{
				((TMP_Text)component).text = "+" + GetOCFromThousands(__instance.animatedValue);
			}
		}

		[HarmonyPatch(typeof(WorldSpaceUIValue), "Show")]
		[HarmonyPostfix]
		public static void WorldSpaceUIValue_Show_Postfix(WorldSpaceUIValue __instance, int _value, bool _cost)
		{
			TextMeshProUGUI component = ((Component)__instance).GetComponent<TextMeshProUGUI>();
			if (!((Object)(object)component == (Object)null))
			{
				EnsureRichText(component);
				if (_cost)
				{
					string oCFromThousands = GetOCFromThousands(_value);
					((TMP_Text)component).text = "-" + oCFromThousands;
				}
				else
				{
					string oCFromThousands = GetOCFromRaw(_value);
					((TMP_Text)component).text = oCFromThousands;
				}
			}
		}

		[HarmonyPatch(typeof(ExtractionPoint), "SetHaulText")]
		[HarmonyPostfix]
		public static void ExtractionPoint_SetHaulText_Postfix(ExtractionPoint __instance)
		{
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			TextMeshPro haulGoalScreen = __instance.haulGoalScreen;
			if ((Object)(object)haulGoalScreen == (Object)null)
			{
				return;
			}
			EnsureRichText3D(haulGoalScreen);
			int num = (int)AccessTools.Field(typeof(ExtractionPoint), "haulCurrent").GetValue(__instance);
			bool flag = (bool)AccessTools.Field(typeof(ExtractionPoint), "isShop").GetValue(__instance);
			string iconTag = OmniCoinPlugin.IconTag;
			string arg = "<color=#bd4300>";
			if (!flag)
			{
				float num2 = (float)Mathf.Max(0, num) / 10000f;
				((TMP_Text)haulGoalScreen).text = $"{arg}{iconTag}</color> {num2:0.##}";
				return;
			}
			int num3 = (int)AccessTools.Field(typeof(ExtractionPoint), "haulGoal").GetValue(__instance);
			int num4 = num3 - num;
			float num5 = (float)num4 / 10000f;
			if (num4 < 0)
			{
				((Graphic)haulGoalScreen).color = Color.red;
				((TMP_Text)haulGoalScreen).text = $"{iconTag} {num5:0.##}";
			}
			else
			{
				((TMP_Text)haulGoalScreen).text = $"{arg}{iconTag}</color> {num5:0.##}";
			}
		}

		[HarmonyPatch(typeof(PhysGrabCart), "SetHaulText")]
		[HarmonyPostfix]
		public static void PhysGrabCart_SetHaulText_Postfix(PhysGrabCart __instance)
		{
			TextMeshPro displayText = __instance.displayText;
			if (!((Object)(object)displayText == (Object)null))
			{
				EnsureRichText3D(displayText);
				int num = (int)AccessTools.Field(typeof(PhysGrabCart), "haulCurrent").GetValue(__instance);
				string iconTag = OmniCoinPlugin.IconTag;
				string arg = "<color=#bd4300>";
				float num2 = (float)Mathf.Max(0, num) / 10000f;
				((TMP_Text)displayText).text = $"{arg}{iconTag}</color> {num2:0.##}";
			}
		}
	}
}