Decompiled source of Digitalroots GoldBars v1.2.28

plugins/Digitalroot.Valheim.GoldBars.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Digitalroot.Valheim.Common;
using Digitalroot.Valheim.Common.Json;
using JetBrains.Annotations;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using SimpleJson;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Digitalroot.ValheimGoldBars")]
[assembly: AssemblyDescription("Digitalroot GoldBars")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Digitalroot Technologies")]
[assembly: AssemblyProduct("Digitalroot Valheim Mods")]
[assembly: AssemblyCopyright("Copyright © Digitalroot Technologies 2021 - 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3ccc4dc1-5b17-47c1-b996-ca03b8639a61")]
[assembly: AssemblyFileVersion("1.2.28")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.28.0")]
[module: UnverifiableCode]
namespace Digitalroot.Valheim.GoldBars
{
	[BepInPlugin("digitalroot.mods.GoldBars", "Digitalroot GoldBars", "1.2.28")]
	[BepInDependency("com.jotunn.jotunn", "2.10.0")]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class Main : BaseUnityPlugin, ITraceableLogging
	{
		public static Main Instance;

		public static ConfigEntry<int> NexusId;

		public static ConfigEntry<int> CoinPilePieceComfort;

		public static ConfigEntry<int> GoldStackPieceComfort;

		private GameObject _goldBar;

		private GameObject _coinPile;

		private GameObject _coinPilePiece;

		private GameObject _goldStackPiece;

		private AssetBundle _assetBundle;

		public const string Version = "1.2.28";

		public const string Name = "Digitalroot GoldBars";

		public const string Guid = "digitalroot.mods.GoldBars";

		public const string Namespace = "Digitalroot.ValheimGoldBars";

		public string Source => "Digitalroot.ValheimGoldBars";

		public bool EnableTrace { get; }

		public Main()
		{
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Expected O, but got Unknown
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Expected O, but got Unknown
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Expected O, but got Unknown
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Expected O, but got Unknown
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Expected O, but got Unknown
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Expected O, but got Unknown
			Instance = this;
			EnableTrace = false;
			Log.Trace(Instance, "Digitalroot.ValheimGoldBars." + MethodBase.GetCurrentMethod()?.DeclaringType?.Name + "." + MethodBase.GetCurrentMethod()?.Name);
			NexusId = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 1448, new ConfigDescription("Nexus mod ID for updates", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = false,
				ReadOnly = true
			} }));
			CoinPilePieceComfort = ((BaseUnityPlugin)this).Config.Bind<int>("General", "CoinPilePieceComfort", 1, new ConfigDescription("Coin Pile Comfort Level", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true
			} }));
			GoldStackPieceComfort = ((BaseUnityPlugin)this).Config.Bind<int>("General", "GoldStackPieceComfort", 2, new ConfigDescription("Gold Stack Comfort Level", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true
			} }));
		}

		[UsedImplicitly]
		private void Awake()
		{
			try
			{
				Log.Trace(Instance, "Digitalroot.ValheimGoldBars." + MethodBase.GetCurrentMethod()?.DeclaringType?.Name + "." + MethodBase.GetCurrentMethod()?.Name);
				_assetBundle = AssetUtils.LoadAssetBundleFromResources("goldbar", typeof(Main).Assembly);
				LoadPrefabs();
				LoadRecipes();
				LoadConversion();
				LoadPieces();
				_assetBundle.Unload(false);
			}
			catch (Exception e)
			{
				Log.Error(Instance, e);
			}
		}

		private void LoadPrefabs()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Expected O, but got Unknown
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			_goldBar = _assetBundle.LoadAsset<GameObject>("assets/goldingot/goldingot.prefab");
			ItemManager.Instance.AddItem(new CustomItem(_goldBar, false));
			_coinPile = _assetBundle.LoadAsset<GameObject>("assets/goldingot/coinpile.prefab");
			ItemManager instance = ItemManager.Instance;
			GameObject coinPile = _coinPile;
			ItemConfig val = new ItemConfig();
			val.Amount = 1;
			val.Requirements = (RequirementConfig[])(object)new RequirementConfig[1]
			{
				new RequirementConfig
				{
					Item = "Coins",
					Amount = 200
				}
			};
			instance.AddItem(new CustomItem(coinPile, false, val));
		}

		private void LoadRecipes()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Expected O, but got Unknown
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: 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_00b7: Expected O, but got Unknown
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Expected O, but got Unknown
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Expected O, but got Unknown
			RecipeConfig val = new RecipeConfig();
			val.Amount = 200;
			val.Item = "Coins";
			val.Name = "PileToCoins";
			val.Requirements = (RequirementConfig[])(object)new RequirementConfig[1]
			{
				new RequirementConfig
				{
					Item = "CoinPile",
					Amount = 1
				}
			};
			CustomRecipe val2 = new CustomRecipe(val);
			ItemManager.Instance.AddRecipe(val2);
			val = new RecipeConfig();
			val.Amount = 100;
			val.Item = "Coins";
			val.CraftingStation = "forge";
			val.Name = "GoldIngotToCoins";
			val.MinStationLevel = 2;
			val.Requirements = (RequirementConfig[])(object)new RequirementConfig[2]
			{
				new RequirementConfig
				{
					Item = "GoldIngot",
					Amount = 1
				},
				new RequirementConfig
				{
					Item = "Copper",
					Amount = 1
				}
			};
			CustomRecipe val3 = new CustomRecipe(val);
			ItemManager.Instance.AddRecipe(val3);
		}

		private void LoadConversion()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			CustomItemConversion val = new CustomItemConversion((ConversionConfig)new SmelterConversionConfig
			{
				FromItem = "CoinPile",
				ToItem = "GoldIngot"
			});
			ItemManager.Instance.AddItemConversion(val);
		}

		private void LoadPieces()
		{
			AddGoldStack();
			AddCoinPile();
		}

		private void AddCoinPile()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Expected O, but got Unknown
			_coinPilePiece = _assetBundle.LoadAsset<GameObject>("assets/goldingot/piece_coinpile.prefab");
			GameObject coinPilePiece = _coinPilePiece;
			PieceConfig val = new PieceConfig();
			val.PieceTable = "_HammerPieceTable";
			val.CraftingStation = "";
			val.Enabled = true;
			val.Requirements = (RequirementConfig[])(object)new RequirementConfig[1]
			{
				new RequirementConfig
				{
					Item = "Coins",
					Amount = 200,
					Recover = true
				}
			};
			CustomPiece val2 = new CustomPiece(coinPilePiece, false, val);
			val2.Piece.m_comfort = CoinPilePieceComfort.Value;
			CustomPiece val3 = val2;
			PieceManager.Instance.AddPiece(val3);
		}

		private void AddGoldStack()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			//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_008a: Expected O, but got Unknown
			_goldStackPiece = _assetBundle.LoadAsset<GameObject>("assets/goldingot/piece_goldstack.prefab");
			GameObject goldStackPiece = _goldStackPiece;
			PieceConfig val = new PieceConfig();
			val.PieceTable = "_HammerPieceTable";
			val.CraftingStation = "";
			val.Enabled = true;
			val.Requirements = (RequirementConfig[])(object)new RequirementConfig[1]
			{
				new RequirementConfig
				{
					Item = "GoldIngot",
					Amount = 48,
					Recover = true
				}
			};
			CustomPiece val2 = new CustomPiece(goldStackPiece, false, val);
			val2.Piece.m_comfort = GoldStackPieceComfort.Value;
			CustomPiece val3 = val2;
			PieceManager.Instance.AddPiece(val3);
		}
	}
}
namespace Digitalroot.Valheim.Common
{
	internal interface ITraceableLogging
	{
		string Source { get; }

		bool EnableTrace { get; }
	}
	internal sealed class Log
	{
		private static readonly Dictionary<string, TraceLogger> TraceLoggers;

		[UsedImplicitly]
		private static Log Instance { get; }

		static Log()
		{
			TraceLoggers = new Dictionary<string, TraceLogger>();
			Instance = new Log();
		}

		private Log()
		{
			TraceLoggers.Add("Digitalroot", new TraceLogger("Digitalroot", enableTrace: false));
		}

		public static void RegisterSource(ITraceableLogging sender)
		{
			if ((!TraceLoggers.ContainsKey(sender.Source) || TraceLoggers[sender.Source].IsTraceEnabled != sender.EnableTrace) && (!TraceLoggers.ContainsKey(sender.Source) || sender.EnableTrace))
			{
				if (TraceLoggers.ContainsKey(sender.Source) && sender.EnableTrace)
				{
					TraceLoggers[sender.Source].EnableTrace();
				}
				else
				{
					TraceLoggers.Add(sender.Source, new TraceLogger(sender.Source, sender.EnableTrace));
				}
			}
		}

		private static TraceLogger GetTraceLogger(ITraceableLogging sender)
		{
			if (!TraceLoggers.ContainsKey(sender.Source))
			{
				return TraceLoggers["Digitalroot"];
			}
			return TraceLoggers[sender.Source];
		}

		[UsedImplicitly]
		public static void SetEnableTrace(ITraceableLogging sender, bool value)
		{
			if (value)
			{
				GetTraceLogger(sender).EnableTrace();
			}
			else
			{
				GetTraceLogger(sender).DisableTrace();
			}
		}

		[UsedImplicitly]
		public static void SetEnableTraceForAllLoggers(bool value)
		{
			foreach (TraceLogger value2 in TraceLoggers.Values)
			{
				if (value)
				{
					value2.EnableTrace();
				}
				else
				{
					value2.DisableTrace();
				}
			}
		}

		[UsedImplicitly]
		public static void Debug(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogDebug(value);
		}

		[UsedImplicitly]
		public static void Error(ITraceableLogging sender, Exception e, int i = 1)
		{
			Error(sender, "Message: " + e.Message);
			Error(sender, $"TargetSite: {e.TargetSite}");
			Error(sender, "StackTrace: " + e.StackTrace);
			Error(sender, "Source: " + e.Source);
			if (e.Data.Count > 0)
			{
				foreach (object key in e.Data.Keys)
				{
					Error(sender, $"key: {key}, value: {e.Data[key]}");
				}
			}
			if (e.InnerException != null)
			{
				Error(sender, $"--- InnerException [{i}][Start] ---");
				Error(sender, e.InnerException, ++i);
			}
		}

		[UsedImplicitly]
		public static void Error(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogError(value);
		}

		[UsedImplicitly]
		public static void Info(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogInfo(value);
		}

		[UsedImplicitly]
		public static void Fatal(ITraceableLogging sender, Exception e, int i = 1)
		{
			Fatal(sender, "Message: " + e.Message);
			Fatal(sender, $"TargetSite: {e.TargetSite}");
			Fatal(sender, "StackTrace: " + e.StackTrace);
			Fatal(sender, "Source: " + e.Source);
			if (e.Data.Count > 0)
			{
				foreach (object key in e.Data.Keys)
				{
					Fatal(sender, $"key: {key}, value: {e.Data[key]}");
				}
			}
			if (e.InnerException != null)
			{
				Fatal(sender, $"--- InnerException [{i}][Start] ---");
				Fatal(sender, e.InnerException, ++i);
			}
		}

		[UsedImplicitly]
		public static void Fatal(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogFatal(value);
		}

		[UsedImplicitly]
		public static void Message(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogMessage(value);
		}

		[UsedImplicitly]
		public static void Trace(ITraceableLogging sender, object value)
		{
			if (GetTraceLogger(sender).IsTraceEnabled || sender.EnableTrace)
			{
				GetTraceLogger(sender).LoggerRef.Log((LogLevel)63, value);
			}
		}

		[UsedImplicitly]
		public static void Warning(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogWarning(value);
		}
	}
	internal class TraceLogger
	{
		internal readonly ManualLogSource LoggerRef;

		private readonly string _source;

		private readonly FileInfo _traceFileInfo;

		public bool IsTraceEnabled { get; private set; }

		private DirectoryInfo AssemblyDirectory => new FileInfo(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)).Directory;

		public TraceLogger(string source, bool enableTrace)
		{
			_source = source;
			IsTraceEnabled = enableTrace;
			LoggerRef = Logger.CreateLogSource(_source);
			_traceFileInfo = new FileInfo(Path.Combine(Paths.BepInExRootPath ?? AssemblyDirectory.FullName, "logs", _source + ".Trace.log"));
			if (_traceFileInfo.DirectoryName != null)
			{
				Directory.CreateDirectory(_traceFileInfo.DirectoryName);
			}
			if (_traceFileInfo.Exists)
			{
				_traceFileInfo.Delete();
				_traceFileInfo.Refresh();
			}
			LoggerRef.LogEvent += OnLogEvent;
		}

		public void EnableTrace()
		{
			IsTraceEnabled = true;
		}

		public void DisableTrace()
		{
			IsTraceEnabled = false;
		}

		[UsedImplicitly]
		public void StopTrace()
		{
			LoggerRef.LogEvent -= OnLogEvent;
		}

		private void OnLogEvent(object sender, LogEventArgs e)
		{
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			if (e.Source.SourceName != _source || !IsTraceEnabled)
			{
				return;
			}
			using Mutex mutex = new Mutex(initiallyOwned: false, "Digitalroot.Valheim.Common.TraceLogger." + _source);
			mutex.WaitOne();
			try
			{
				if (e.Data is string)
				{
					string contents = $"[{e.Level,-7}:{e.Source.SourceName,10}] {e.Data}{Environment.NewLine}";
					File.AppendAllText(_traceFileInfo.FullName, contents, Encoding.UTF8);
				}
				else
				{
					string contents2 = $"[{e.Level,-7}:{e.Source.SourceName,10}] {JsonSerializationProvider.Serialize(e.Data)}{Environment.NewLine}";
					File.AppendAllText(_traceFileInfo.FullName, contents2, Encoding.UTF8);
				}
			}
			finally
			{
				mutex.ReleaseMutex();
			}
		}
	}
}
namespace Digitalroot.Valheim.Common.Json
{
	[UsedImplicitly]
	internal static class JsonSerializationProvider
	{
		[Obsolete("Use Deserialize<T>()")]
		public static T FromJson<T>(string json)
		{
			return Deserialize<T>(json);
		}

		public static T Deserialize<T>(string json)
		{
			return SimpleJson.DeserializeObject<T>(json, (IJsonSerializerStrategy)(object)new DigitalrootJsonSerializerStrategy());
		}

		[Obsolete("Use Serialize()")]
		public static string ToJson(object obj, bool pretty = false)
		{
			return Serialize(obj);
		}

		public static string Serialize(object obj)
		{
			return SimpleJson.SerializeObject(obj, (IJsonSerializerStrategy)(object)new DigitalrootJsonSerializerStrategy());
		}
	}
	internal class DigitalrootJsonSerializerStrategy : PocoJsonSerializerStrategy
	{
		public override bool TrySerializeNonPrimitiveObject(object input, out object output)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			if (!(input is Vector3 val))
			{
				if (input is Quaternion val2)
				{
					output = new float[4] { val2.x, val2.y, val2.z, val2.w };
					return true;
				}
				return ((PocoJsonSerializerStrategy)this).TrySerializeNonPrimitiveObject(input, ref output);
			}
			output = new float[3] { val.x, val.y, val.z };
			return true;
		}

		public override object DeserializeObject(object value, Type type)
		{
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			if (type == null)
			{
				throw new ArgumentNullException("type");
			}
			if (value == null)
			{
				throw new ArgumentNullException("value");
			}
			if (value is string value2)
			{
				if (string.IsNullOrWhiteSpace(value2))
				{
					throw new ArgumentNullException("value");
				}
				if (type == typeof(Vector3))
				{
					if (!(((PocoJsonSerializerStrategy)this).DeserializeObject(value, typeof(float[])) is float[] array) || (array != null && array.Length != 3))
					{
						throw new ArgumentException(string.Format("The value '{0}' can be converted to a {1}.", value, "Vector3"), "value");
					}
					return (object)new Vector3(array[0], array[1], array[2]);
				}
				if (type == typeof(Quaternion))
				{
					if (!(((PocoJsonSerializerStrategy)this).DeserializeObject(value, typeof(float[])) is float[] array2) || (array2 != null && array2.Length != 4))
					{
						throw new ArgumentException(string.Format("The value '{0}' can be converted to a {1}.", value, "Quaternion"), "value");
					}
					return (object)new Quaternion(array2[0], array2[1], array2[2], array2[3]);
				}
				return ((PocoJsonSerializerStrategy)this).DeserializeObject(value, type);
			}
			throw new ArgumentException($"The value '{value}' can be converted to a {type.Name}.", "value");
		}
	}
}
internal class DigitalrootValheimGoldBars_ProcessedByFody
{
	internal const string FodyVersion = "6.6.0.0";

	internal const string ILMerge = "1.22.0.0";
}