Decompiled source of CatosTents v0.1.0

plugins\CatosTents\CatosTents.dll

Decompiled a month 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 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("CatosTents")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+4e27167a6fc4506199a6e6a91948fb07904ea2ed")]
[assembly: AssemblyProduct("CatosTents")]
[assembly: AssemblyTitle("CatosTents")]
[assembly: AssemblyVersion("0.1.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 CatosTents
{
	[BepInPlugin("com.catosvalheim.catostents", "CatosTents", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string GUID = "com.catosvalheim.catostents";

		public const string NAME = "CatosTents";

		public const string VERSION = "0.1.0";

		internal static ManualLogSource Log;

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

		private void OnDestroy()
		{
			PrefabManager.OnVanillaPrefabsAvailable -= TentPiece.Register;
		}
	}
	internal static class TentPiece
	{
		private sealed class TentDefinition
		{
			public string PieceId { get; }

			public string VisualPrefabName { get; }

			public string DisplayName { get; }

			public TentDefinition(string pieceId, string visualPrefabName, string displayName)
			{
				PieceId = pieceId;
				VisualPrefabName = visualPrefabName;
				DisplayName = displayName;
			}
		}

		private const string BundleResourceSuffix = ".Assets.AssetBundles.catostents";

		private static bool _registered;

		private static AssetBundle _bundle;

		private static readonly TentDefinition[] Tents = new TentDefinition[4]
		{
			new TentDefinition("catostent_market_01", "CatosTent_Market_01_Visual", "Market Tent 01"),
			new TentDefinition("catostent_market_02", "CatosTent_Market_02_Visual", "Market Tent 02"),
			new TentDefinition("catostent_market_props_01", "CatosTent_Market_Props_01_Visual", "Market Tent Props 01"),
			new TentDefinition("catostent_market_props_02", "CatosTent_Market_Props_02_Visual", "Market Tent Props 02")
		};

		public static void Register()
		{
			if (_registered)
			{
				return;
			}
			_registered = true;
			GameObject val = FindBuildPiecePrefab();
			if (!((Object)(object)val == (Object)null) && LoadBundle())
			{
				TentDefinition[] tents = Tents;
				foreach (TentDefinition tent in tents)
				{
					RegisterTent(val, tent);
				}
			}
		}

		private static void RegisterTent(GameObject buildSource, TentDefinition tent)
		{
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Expected O, but got Unknown
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Expected O, but got Unknown
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Expected O, but got Unknown
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Expected O, but got Unknown
			GameObject val = _bundle.LoadAsset<GameObject>(tent.VisualPrefabName);
			Sprite val2 = _bundle.LoadAsset<Sprite>(tent.VisualPrefabName + "_Icon");
			if ((Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null)
			{
				Plugin.Log.LogError((object)("CatosTents: missing visual or icon for " + tent.PieceId + "."));
				return;
			}
			GameObject obj = Object.Instantiate<GameObject>(buildSource);
			((Object)obj).name = tent.PieceId;
			AttachVisual(obj, val);
			PieceConfig val3 = new PieceConfig();
			val3.PieceTable = "Hammer";
			val3.Category = "CatosTents";
			val3.Name = tent.DisplayName;
			val3.Description = "A sturdy placeable tent for camps and market scenes.";
			val3.Icon = val2;
			val3.Requirements = (RequirementConfig[])(object)new RequirementConfig[3]
			{
				new RequirementConfig
				{
					Item = "Wood",
					Amount = 20
				},
				new RequirementConfig
				{
					Item = "DeerHide",
					Amount = 6
				},
				new RequirementConfig
				{
					Item = "FineWood",
					Amount = 4
				}
			};
			CustomPiece val4 = new CustomPiece(obj, true, val3);
			val4.PiecePrefab.GetComponent<Piece>().m_canBeRemoved = true;
			PieceManager.Instance.AddPiece(val4);
			Plugin.Log.LogInfo((object)("Registered functional tent bed piece '" + tent.PieceId + "'"));
		}

		private static GameObject FindBuildPiecePrefab()
		{
			string[] array = new string[4] { "piece_wood_floor", "wood_floor", "piece_stone_floor", "stone_floor" };
			GameObject val = ((IEnumerable<string>)array).Select((Func<string, GameObject>)Cache.GetPrefab<GameObject>).FirstOrDefault((Func<GameObject, bool>)((GameObject prefab) => (Object)(object)prefab != (Object)null));
			if ((Object)(object)val == (Object)null)
			{
				Plugin.Log.LogError((object)("CatosTents: none of the neutral build-piece candidates were found: " + string.Join(", ", array)));
			}
			else
			{
				Plugin.Log.LogInfo((object)("CatosTents: using neutral vanilla build-piece prefab '" + ((Object)val).name + "'"));
			}
			return val;
		}

		private static void AttachVisual(GameObject functionalPrefab, GameObject source)
		{
			GameObject val = Object.Instantiate<GameObject>(source);
			((Object)val).name = ((Object)source).name;
			val.transform.SetParent(functionalPrefab.transform, false);
			Renderer[] componentsInChildren = functionalPrefab.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val2 in componentsInChildren)
			{
				if (!((Component)val2).transform.IsChildOf(val.transform))
				{
					val2.enabled = false;
				}
			}
		}

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