Decompiled source of CatosWagon v1.0.0

BepInEx/plugins/CatosWagon.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("CatosWagon")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+441d35bec1043ae9cdb9a9898d2c5ec5a4b683cb")]
[assembly: AssemblyProduct("CatosWagon")]
[assembly: AssemblyTitle("CatosWagon")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 CatosWagon
{
	[BepInPlugin("com.catosaurluna.catoswagon", "CatosWagon", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string GUID = "com.catosaurluna.catoswagon";

		public const string NAME = "CatosWagon";

		public const string VERSION = "1.0.0";

		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			PrefabManager.OnVanillaPrefabsAvailable += WagonPiece.Register;
			Log.LogInfo((object)"CatosWagon 1.0.0 loaded; waiting for vanilla prefabs");
		}

		private void OnDestroy()
		{
			PrefabManager.OnVanillaPrefabsAvailable -= WagonPiece.Register;
		}
	}
	internal static class WagonPiece
	{
		private const string BundleResourceSuffix = ".Assets.Wagon.catoswagon_market_wagons";

		private const string OpenVisualName = "HouseholdMarketWagon_Open_Visual";

		private const string ClosedVisualName = "HouseholdMarketWagon_Closed_Visual";

		private static bool _registered;

		private static AssetBundle _visualBundle;

		private static Sprite _wagonIcon;

		private static MethodInfo _loadImage;

		public static void Register()
		{
			if (_registered)
			{
				return;
			}
			PrefabManager.OnVanillaPrefabsAvailable -= Register;
			string text = FindBuildPiecePrefabName();
			if (text == null || !LoadBundle())
			{
				return;
			}
			Sprite val = LoadWagonIcon();
			if (!((Object)(object)val == (Object)null))
			{
				bool flag = RegisterWagon(text, "piece_catoswagon_open", "HouseholdMarketWagon_Open_Visual", "Catos Wagon (Open)", "A rustic market wagon with its canopy open.", val);
				bool flag2 = RegisterWagon(text, "piece_catoswagon_closed", "HouseholdMarketWagon_Closed_Visual", "Catos Wagon (Closed)", "A rustic market wagon with its canopy closed.", val);
				_registered = flag && flag2;
				if (_registered)
				{
					Plugin.Log.LogInfo((object)"Catos Wagon pieces registered");
				}
			}
		}

		private static bool RegisterWagon(string buildSourceName, string pieceName, string visualName, string displayName, string description, Sprite icon)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Expected O, but got Unknown
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: 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_009e: Expected O, but got Unknown
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Expected O, but got Unknown
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Expected O, but got Unknown
			GameObject val = LoadVisualPrefab(visualName);
			if ((Object)(object)val == (Object)null)
			{
				Plugin.Log.LogError((object)("Catos Wagon: visual prefab '" + visualName + "' was not found in the embedded bundle."));
				return false;
			}
			PieceConfig val2 = new PieceConfig();
			val2.PieceTable = "Hammer";
			val2.Category = "Catos Wagon";
			val2.Name = displayName;
			val2.Description = description;
			val2.Icon = icon;
			val2.Requirements = (RequirementConfig[])(object)new RequirementConfig[3]
			{
				new RequirementConfig
				{
					Item = "Wood",
					Amount = 40
				},
				new RequirementConfig
				{
					Item = "FineWood",
					Amount = 20
				},
				new RequirementConfig
				{
					Item = "DeerHide",
					Amount = 8
				}
			};
			CustomPiece val3 = new CustomPiece(pieceName, buildSourceName, val2);
			AttachVisual(val3.PiecePrefab, val);
			Piece component = val3.PiecePrefab.GetComponent<Piece>();
			if ((Object)(object)component != (Object)null)
			{
				component.m_canBeRemoved = true;
			}
			PieceManager.Instance.AddPiece(val3);
			Plugin.Log.LogInfo((object)("Registered " + pieceName + " using visual " + visualName));
			return true;
		}

		private static string FindBuildPiecePrefabName()
		{
			string[] array = new string[4] { "piece_wood_floor", "wood_floor", "piece_stone_floor", "stone_floor" };
			string? text = array.FirstOrDefault((string candidate) => (Object)(object)Cache.GetPrefab<GameObject>(candidate) != (Object)null);
			if (text == null)
			{
				Plugin.Log.LogError((object)("Catos Wagon: no neutral vanilla build-piece found: " + string.Join(", ", array)));
			}
			return text;
		}

		private static Sprite LoadWagonIcon()
		{
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Expected O, but got Unknown
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_wagonIcon != (Object)null)
			{
				return _wagonIcon;
			}
			Assembly assembly = typeof(WagonPiece).Assembly;
			string text = assembly.GetManifestResourceNames().FirstOrDefault((string name) => name.EndsWith(".Assets.icon.png", StringComparison.OrdinalIgnoreCase));
			if (text == null)
			{
				Plugin.Log.LogError((object)"Catos Wagon: embedded wagon icon was not found.");
				return null;
			}
			using Stream stream = assembly.GetManifestResourceStream(text);
			using MemoryStream memoryStream = new MemoryStream();
			if (stream == null)
			{
				Plugin.Log.LogError((object)("Catos Wagon: embedded icon stream '" + text + "' was empty."));
				return null;
			}
			stream.CopyTo(memoryStream);
			byte[] bytes = memoryStream.ToArray();
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
			if (!TryLoadImage(val, bytes))
			{
				Object.Destroy((Object)(object)val);
				Plugin.Log.LogError((object)"Catos Wagon: embedded icon could not be decoded.");
				return null;
			}
			((Object)val).name = "CatosWagonIconTexture";
			_wagonIcon = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f);
			((Object)_wagonIcon).name = "CatosWagonIcon";
			return _wagonIcon;
		}

		private static bool TryLoadImage(Texture2D texture, byte[] bytes)
		{
			if (_loadImage == null)
			{
				_loadImage = typeof(Texture2D).GetMethod("LoadImage", new Type[1] { typeof(byte[]) });
				if (_loadImage == null)
				{
					Type type = Type.GetType("UnityEngine.ImageConversion, UnityEngine.ImageConversionModule");
					if (type != null)
					{
						_loadImage = type.GetMethod("LoadImage", new Type[2]
						{
							typeof(Texture2D),
							typeof(byte[])
						});
					}
				}
			}
			if (_loadImage == null)
			{
				Plugin.Log.LogError((object)"Catos Wagon: Unity image conversion support was not found.");
				return false;
			}
			object obj = ((_loadImage.GetParameters().Length == 1) ? _loadImage.Invoke(texture, new object[1] { bytes }) : _loadImage.Invoke(null, new object[2] { texture, bytes }));
			bool flag = default(bool);
			int num;
			if (obj is bool)
			{
				flag = (bool)obj;
				num = 1;
			}
			else
			{
				num = 0;
			}
			return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
		}

		private static void AttachVisual(GameObject functionalPrefab, GameObject source)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = Object.Instantiate<GameObject>(source);
			((Object)val).name = ((Object)source).name;
			val.transform.SetParent(functionalPrefab.transform, false);
			val.transform.localPosition = Vector3.zero;
			val.transform.localRotation = Quaternion.identity;
			val.transform.localScale = Vector3.one;
			Renderer[] componentsInChildren = functionalPrefab.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val2 in componentsInChildren)
			{
				if (!((Component)val2).transform.IsChildOf(val.transform))
				{
					val2.enabled = false;
				}
			}
		}

		private static GameObject LoadVisualPrefab(string visualName)
		{
			if (!((Object)(object)_visualBundle == (Object)null))
			{
				return _visualBundle.LoadAsset<GameObject>(visualName);
			}
			return null;
		}

		private static bool LoadBundle()
		{
			if ((Object)(object)_visualBundle != (Object)null)
			{
				return true;
			}
			Assembly assembly = typeof(WagonPiece).Assembly;
			string text = assembly.GetManifestResourceNames().FirstOrDefault((string name) => name.EndsWith(".Assets.Wagon.catoswagon_market_wagons", StringComparison.OrdinalIgnoreCase));
			if (text == null)
			{
				Plugin.Log.LogError((object)"Catos Wagon: embedded bundle ending with '.Assets.Wagon.catoswagon_market_wagons' was not found.");
				return false;
			}
			using (Stream stream = assembly.GetManifestResourceStream(text))
			{
				using MemoryStream memoryStream = new MemoryStream();
				if (stream == null)
				{
					Plugin.Log.LogError((object)("Catos Wagon: embedded bundle stream '" + text + "' was empty."));
					return false;
				}
				stream.CopyTo(memoryStream);
				_visualBundle = AssetBundle.LoadFromMemory(memoryStream.ToArray());
			}
			if ((Object)(object)_visualBundle == (Object)null)
			{
				Plugin.Log.LogError((object)"Catos Wagon: AssetBundle.LoadFromMemory returned null.");
				return false;
			}
			return true;
		}
	}
}