Decompiled source of Coin Purse v1.0.1

CoinPurse/CoinPurse.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Managers;
using Jotunn.Utils;
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(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("CoinPurse")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Keep coins and valuables out of your inventory")]
[assembly: AssemblyTitle("CoinPurse")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace CoinPurse
{
	internal class CoinPurse
	{
		private static readonly string SaveFile = Utils.GetSaveDataPath((FileSource)3) + "\\_coinpurse.fch";

		internal static GameObject Prefab;

		internal static GameObject Instance;

		internal static TextMeshProUGUI TextComponent;

		public static Dictionary<long, int> Currencies = new Dictionary<long, int>();

		public static int Currency
		{
			get
			{
				long playerID = Player.m_localPlayer.GetPlayerID();
				if (playerID == 0)
				{
					return 0;
				}
				if (!Currencies.ContainsKey(playerID))
				{
					Currencies[playerID] = 0;
				}
				return Currencies[playerID];
			}
			set
			{
				long playerID = Player.m_localPlayer.GetPlayerID();
				if (playerID != 0)
				{
					Currencies[playerID] = value;
					Text = value.ToString();
				}
			}
		}

		public static string Text
		{
			get
			{
				return ((TMP_Text)TextComponent).text;
			}
			set
			{
				((TMP_Text)TextComponent).text = value;
			}
		}

		public static Image Background { get; private set; }

		public static Image Icon { get; private set; }

		public static void CreatePrefab(string modPath)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			Plugin.Logger.LogInfo((object)"CoinPurse.CreatePrefab called");
			Prefab = new GameObject("CoinPurse", new Type[2]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer)
			});
			if ((Object)(object)Prefab == (Object)null)
			{
				Plugin.Logger.LogFatal((object)"Failed to create CoinPurse");
			}
			Prefab.transform.localPosition = new Vector3(602f, -196.5f, 0f);
			Prefab.layer = 5;
			Prefab.GetComponent<CanvasRenderer>().cullTransparentMesh = false;
			RectTransform component = Prefab.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(1f, 0.5f);
			component.anchorMax = new Vector2(1f, 0.5f);
			component.anchoredPosition = new Vector2(32f, 77.5f);
			component.sizeDelta = new Vector2(80f, 64f);
			CreateBackground(modPath);
			CreateIcon(modPath);
			CreateText(modPath);
			PrefabManager.Instance.AddPrefab(Prefab);
		}

		private static void CreateBackground(string modPath)
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			Sprite sprite = GUIManager.Instance.GetSprite("woodpanel_flik");
			GameObject val = new GameObject("bkg", new Type[3]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer),
				typeof(Image)
			})
			{
				layer = 5
			};
			val.transform.SetParent(Prefab.transform);
			Image component = val.GetComponent<Image>();
			component.sprite = sprite;
			((Graphic)component).color = Color.white;
			val.GetComponent<CanvasRenderer>().cullTransparentMesh = false;
			RectTransform component2 = val.GetComponent<RectTransform>();
			component2.anchorMin = Vector2.zero;
			component2.anchorMax = Vector2.one;
			component2.anchoredPosition = Vector2.zero;
			component2.sizeDelta = Vector2.zero;
			((Transform)component2).localScale = Vector3.one;
		}

		private static void CreateIcon(string modPath)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("cp_icon", new Type[3]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer),
				typeof(Image)
			})
			{
				layer = 5
			};
			val.transform.SetParent(Prefab.transform);
			Texture2D val2 = AssetUtils.LoadTexture(Path.Combine(modPath, "Assets\\trader.png"), true);
			Sprite sprite = Sprite.Create(val2, new Rect(0f, 0f, (float)((Texture)val2).width, (float)((Texture)val2).height), Vector2.zero);
			Image component = val.GetComponent<Image>();
			component.sprite = sprite;
			((Graphic)component).color = Color.white;
			RectTransform component2 = val.GetComponent<RectTransform>();
			component2.anchorMin = new Vector2(0.5f, 0.5f);
			component2.anchorMax = new Vector2(0.5f, 0.5f);
			component2.anchoredPosition = new Vector2(2f, 30f);
			component2.sizeDelta = new Vector2(43f, 43f);
		}

		private static void CreateText(string modPath)
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			Color32 val = default(Color32);
			((Color32)(ref val))..ctor(byte.MaxValue, (byte)216, (byte)0, byte.MaxValue);
			Color32 outlineColor = default(Color32);
			((Color32)(ref outlineColor))..ctor((byte)0, (byte)0, (byte)0, byte.MaxValue);
			TMP_FontAsset prefab = Cache.GetPrefab<TMP_FontAsset>("Valheim-AveriaSansLibre");
			Material prefab2 = Cache.GetPrefab<Material>("Valheim-AveriaSansLibre - Outline");
			GameObject val2 = new GameObject("cp_text", new Type[3]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer),
				typeof(TextMeshProUGUI)
			})
			{
				layer = 5
			};
			val2.transform.SetParent(Prefab.transform);
			RectTransform component = val2.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0.5f, 0.5f);
			component.anchorMax = new Vector2(0.5f, 0.5f);
			component.anchoredPosition = new Vector2(4f, 0f);
			component.sizeDelta = new Vector2(67f, 22f);
			TextMeshProUGUI component2 = val2.GetComponent<TextMeshProUGUI>();
			((TMP_Text)component2).font = prefab;
			((TMP_Text)component2).fontSize = 16f;
			((TMP_Text)component2).fontSharedMaterial = prefab2;
			((TMP_Text)component2).fontMaterial = prefab2;
			((Graphic)component2).color = Color32.op_Implicit(val);
			((TMP_Text)component2).outlineColor = outlineColor;
			((TMP_Text)component2).fontSizeMin = 1f;
			((TMP_Text)component2).fontSizeMax = 16f;
			((TMP_Text)component2).horizontalAlignment = (HorizontalAlignmentOptions)2;
			((TMP_Text)component2).verticalAlignment = (VerticalAlignmentOptions)512;
			((TMP_Text)component2).outlineWidth = 0.175f;
			((TMP_Text)component2).fontFeatures.Add((OTL_FeatureTag)1801810542);
			((TMP_Text)component2).lineSpacing = 1f;
		}

		public static void Instantiate()
		{
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			Plugin.Logger.LogInfo((object)"CoinPurse.Instantiate called");
			if (!Object.op_Implicit((Object)(object)InventoryGui.m_instance?.m_player))
			{
				return;
			}
			Instance = Object.Instantiate<GameObject>(Prefab, ((Component)InventoryGui.m_instance.m_player).transform);
			TextComponent = Instance.GetComponentInChildren<TextMeshProUGUI>();
			Background = ((Component)Instance.transform.Find("bkg")).GetComponent<Image>();
			Icon = ((Component)Instance.transform.Find("cp_icon")).GetComponent<Image>();
			Instance.transform.SetSiblingIndex(3);
			Transform val = ((Transform)InventoryGui.m_instance.m_player).Find("Weight");
			if ((Object)(object)val == (Object)null)
			{
				Plugin.Logger.LogError((object)"Failed to find weight inventory element");
				return;
			}
			Transform val2 = val.Find("bkg");
			if ((Object)(object)val2 == (Object)null)
			{
				Plugin.Logger.LogError((object)"Failed to find bkg inventory element");
				return;
			}
			Image component = ((Component)val2).GetComponent<Image>();
			((Graphic)Background).material = ((Graphic)component).material;
			((Graphic)Background).color = ((Graphic)component).color;
			((Graphic)Icon).material = ((Graphic)component).material;
		}

		public static void SaveToDisk()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Expected O, but got Unknown
			ZPackage val = new ZPackage();
			foreach (KeyValuePair<long, int> currency in Currencies)
			{
				val.Write(currency.Key);
				val.Write(currency.Value);
			}
			byte[] array = val.GetArray();
			try
			{
				FileWriter val2 = new FileWriter(SaveFile, (FileHelperType)0, (FileSource)1);
				val2.m_binary.Write(array.Length);
				val2.m_binary.Write(array);
				val2.Finish();
			}
			catch (Exception ex)
			{
				Plugin.Logger.LogWarning((object)("Error saving CoinPurse data: Path: " + SaveFile + ", Error: " + ex.Message));
			}
		}

		public static void LoadFromDisk()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			if (!File.Exists(SaveFile))
			{
				File.Create(SaveFile).Close();
				return;
			}
			FileReader val;
			try
			{
				val = new FileReader(SaveFile, (FileSource)1, (FileHelperType)0);
			}
			catch (Exception ex)
			{
				Plugin.Logger.LogWarning((object)("Failed to create FileReader for CoinPurse data (" + ex.Message + ")"));
				return;
			}
			byte[] array;
			try
			{
				BinaryReader binary = val.m_binary;
				array = binary.ReadBytes(binary.ReadInt32());
			}
			catch (Exception ex2)
			{
				Plugin.Logger.LogWarning((object)("Error loading CoinPurse data: Path: " + SaveFile + ", Error: " + ex2.Message));
				return;
			}
			finally
			{
				val.Dispose();
			}
			ZPackage val2 = new ZPackage(array);
			while (val2.GetPos() < val2.Size())
			{
				long key = val2.ReadLong();
				int value = val2.ReadInt();
				Currencies[key] = value;
			}
		}
	}
	[HarmonyPatch(typeof(Inventory), "AddItem", new Type[] { typeof(ItemData) })]
	public class Inventory_AddItem_Patch
	{
		public static bool Prefix(ItemData item, ref bool __result)
		{
			if (item.m_shared.m_value <= 0)
			{
				return true;
			}
			CoinPurse.Currency += item.m_stack * item.m_shared.m_value;
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(Inventory), "CanAddItem", new Type[]
	{
		typeof(ItemData),
		typeof(int)
	})]
	public class Inventory_CanAddItem_Patch
	{
		public static void Postfix(ItemData item, ref bool __result)
		{
			__result = __result || item.m_shared.m_value > 0;
		}
	}
	[HarmonyPatch(typeof(Inventory), "RemoveItem", new Type[]
	{
		typeof(string),
		typeof(int),
		typeof(int),
		typeof(bool)
	})]
	public class Inventory_RemoveItem_Patch
	{
		public static void Postfix(string name, int amount)
		{
			if (StoreGui.instance?.m_coinPrefab.m_itemData.m_shared.m_name == name)
			{
				CoinPurse.Currency -= amount;
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "Show")]
	public class InventoryGui_Show_Patch
	{
		public static void Prefix()
		{
			CoinPurse.Text = CoinPurse.Currency.ToString();
		}
	}
	[HarmonyPatch(typeof(Piece), "OnPlaced")]
	public class Piece_OnPlaced_Patch
	{
		public static void Postfix(Piece __instance)
		{
			if (!(__instance.m_name != "$piece_chesttreasure"))
			{
			}
		}
	}
	[HarmonyPatch(typeof(Player))]
	public class PlayerPatches
	{
		[HarmonyPatch("Load")]
		[HarmonyPrefix]
		public static void Load()
		{
			CoinPurse.LoadFromDisk();
		}

		[HarmonyPatch("Save")]
		[HarmonyPostfix]
		public static void Save()
		{
			CoinPurse.SaveToDisk();
		}
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.github.beemerwt.coinpurse", "CoinPurse", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string CoinPurseGuid = "com.github.beemerwt.coinpurse";

		public const string CoinPurseName = "CoinPurse";

		public const string CoinPurseVersion = "1.0.0";

		public static ManualLogSource Logger;

		private static readonly Harmony Harmony = new Harmony("mod.coinpurse");

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin com.github.beemerwt.coinpurse is loaded!");
			Harmony.PatchAll();
			PrefabManager.OnVanillaPrefabsAvailable += AddPrefabItems;
			GUIManager.OnCustomGUIAvailable += CoinPurse.Instantiate;
		}

		private void AddPrefabItems()
		{
			CoinPurse.CreatePrefab(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location));
			PrefabManager.OnVanillaPrefabsAvailable -= AddPrefabItems;
			ReplaceRecipes();
		}

		private void ReplaceRecipes()
		{
			GameObject prefab = PrefabManager.Instance.GetPrefab("piece_chest_treasure");
			Piece component = prefab.GetComponent<Piece>();
			List<Requirement> list = new List<Requirement>(component.m_resources);
			list.First((Requirement r) => r.m_resItem.m_itemData.m_shared.m_name == "$item_coins").m_amount = 259;
			Requirement item = list.First((Requirement r) => r.m_resItem.m_itemData.m_shared.m_name == "$item_ruby");
			list.Remove(item);
			Requirement item2 = list.First((Requirement r) => r.m_resItem.m_itemData.m_shared.m_name == "$item_silvernecklace");
			list.Remove(item2);
			component.m_resources = list.ToArray();
		}
	}
	[HarmonyPatch(typeof(StoreGui), "GetPlayerCoins")]
	public class StoreGui_GetPlayerCoins_Patch
	{
		public static void Postfix(ref int __result)
		{
			__result = CoinPurse.Currency;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "CoinPurse";

		public const string PLUGIN_NAME = "Keep coins and valuables out of your inventory";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}