Decompiled source of Ridgewarden v0.1.0

plugins/Ridgewarden/Ridgewarden.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
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("Ridgewarden")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("Ridgewarden")]
[assembly: AssemblyTitle("Ridgewarden")]
[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 Ridgewarden
{
	[BepInPlugin("SmeagsMods.Ridgewarden", "Ridgewarden", "0.1.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(ZNetScene), "Awake")]
		private static class ScenePatch
		{
			private static void Postfix(ZNetScene __instance)
			{
				Register(__instance);
			}
		}

		[HarmonyPatch(typeof(ObjectDB), "Awake")]
		private static class DbPatch
		{
			private static void Postfix(ObjectDB __instance)
			{
				AddToHammer(__instance);
			}
		}

		[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
		private static class DbCopyPatch
		{
			private static void Postfix(ObjectDB __instance)
			{
				AddToHammer(__instance);
			}
		}

		public const string Guid = "SmeagsMods.Ridgewarden";

		internal static Plugin Instance;

		private static GameObject storage;

		public static readonly Dictionary<string, GameObject> Prefabs = new Dictionary<string, GameObject>();

		public static readonly string[] Sources = new string[3] { "wood_roof", "darkwood_roof", "piece_grausten_roof_45" };

		private static readonly string[] Labels = new string[3] { "Flat thatch roof", "Flat shingle roof", "Flat grausten roof" };

		private void Awake()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			storage = new GameObject("Ridgewarden Prefab Storage");
			storage.SetActive(false);
			Object.DontDestroyOnLoad((Object)(object)storage);
			new Harmony("SmeagsMods.Ridgewarden").PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Ridgewarden 0.1.0 beta loaded; three 2x2 m flat roof variants.");
		}

		internal static void Register(ZNetScene scene)
		{
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			Dictionary<int, GameObject> dictionary = AccessTools.FieldRefAccess<ZNetScene, Dictionary<int, GameObject>>("m_namedPrefabs").Invoke(scene);
			for (int i = 0; i < Sources.Length; i++)
			{
				string text = "Ridgewarden_" + Sources[i] + "_flat";
				GameObject value = null;
				try
				{
					int stableHashCode = StringExtensionMethods.GetStableHashCode(text);
					if (dictionary.TryGetValue(stableHashCode, out var value2))
					{
						if (!Prefabs.TryGetValue(text, out var value3) || (Object)(object)value3 != (Object)(object)value2)
						{
							throw new InvalidOperationException("Prefab name/hash collision: " + text);
						}
						continue;
					}
					if (!Prefabs.TryGetValue(text, out value))
					{
						GameObject prefab = scene.GetPrefab(Sources[i]);
						if (!Object.op_Implicit((Object)(object)prefab) || !Object.op_Implicit((Object)(object)prefab.GetComponent<Piece>()) || !Object.op_Implicit((Object)(object)prefab.GetComponent<WearNTear>()) || !Object.op_Implicit((Object)(object)prefab.GetComponent<ZNetView>()))
						{
							throw new InvalidOperationException("Missing vanilla roof components: " + Sources[i]);
						}
						value = Object.Instantiate<GameObject>(prefab, storage.transform, false);
						((Object)value).name = text;
						value.transform.localPosition = Vector3.zero;
						value.transform.localRotation = Quaternion.identity;
						value.transform.localScale = Vector3.one;
						Flatten(value);
						Piece component = value.GetComponent<Piece>();
						component.m_name = Labels[i];
						component.m_description = "Ridgewarden flat roof. 2 x 2 m. Matches the original roof's material and recipe.";
						value.SetActive(true);
						Prefabs.Add(text, value);
					}
					scene.m_prefabs.Add(value);
					dictionary.Add(stableHashCode, value);
					((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Registered " + text));
				}
				catch (Exception ex)
				{
					if (Object.op_Implicit((Object)(object)value) && !Prefabs.ContainsKey(text))
					{
						Object.Destroy((Object)(object)value);
					}
					((BaseUnityPlugin)Instance).Logger.LogError((object)("Flat roof not registered: " + text + ": " + ex));
				}
			}
			AddToHammer(ObjectDB.instance);
		}

		internal static void AddToHammer(ObjectDB db)
		{
			if (!Object.op_Implicit((Object)(object)db))
			{
				return;
			}
			GameObject itemPrefab = db.GetItemPrefab("Hammer");
			if (!Object.op_Implicit((Object)(object)itemPrefab))
			{
				return;
			}
			PieceTable val = itemPrefab.GetComponent<ItemDrop>()?.m_itemData.m_shared.m_buildPieces;
			if (!Object.op_Implicit((Object)(object)val))
			{
				return;
			}
			foreach (GameObject value in Prefabs.Values)
			{
				if (!val.m_pieces.Contains(value))
				{
					val.m_pieces.Add(value);
				}
			}
		}

		public static void Flatten(GameObject roof)
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: 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_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_026d: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0303: Unknown result type (might be due to invalid IL or missing references)
			//IL_030f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0314: Unknown result type (might be due to invalid IL or missing references)
			//IL_0319: Unknown result type (might be due to invalid IL or missing references)
			//IL_031d: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_0401: Unknown result type (might be due to invalid IL or missing references)
			//IL_040e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0417: Unknown result type (might be due to invalid IL or missing references)
			//IL_0424: Unknown result type (might be due to invalid IL or missing references)
			//IL_04f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_04fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0508: Unknown result type (might be due to invalid IL or missing references)
			//IL_0515: Unknown result type (might be due to invalid IL or missing references)
			//IL_0517: Unknown result type (might be due to invalid IL or missing references)
			//IL_0499: Unknown result type (might be due to invalid IL or missing references)
			//IL_049e: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_04bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_04cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_033f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0346: Unknown result type (might be due to invalid IL or missing references)
			//IL_0350: Unknown result type (might be due to invalid IL or missing references)
			//IL_035e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0363: Unknown result type (might be due to invalid IL or missing references)
			//IL_0368: Unknown result type (might be due to invalid IL or missing references)
			//IL_036d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0372: Unknown result type (might be due to invalid IL or missing references)
			//IL_037f: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0392: Unknown result type (might be due to invalid IL or missing references)
			//IL_0394: Unknown result type (might be due to invalid IL or missing references)
			Transform transform = roof.transform;
			List<Transform> list = new List<Transform>();
			roof.GetComponent<Piece>().GetSnapPoints(list);
			if (list.Count != 4)
			{
				throw new InvalidOperationException("Expected four vanilla corner snaps");
			}
			Vector3 localPosition = list.OrderBy((Transform t) => t.localPosition.z).First().localPosition;
			Vector3 localPosition2 = list.OrderBy((Transform t) => t.localPosition.z).Last().localPosition;
			float num = (localPosition2.y - localPosition.y) / (localPosition2.z - localPosition.z);
			float num2 = localPosition.y - num * localPosition.z;
			if (Mathf.Abs(localPosition2.z - localPosition.z - 2f) > 0.001f || Mathf.Abs(num) < 0.1f || Mathf.Abs(num) > 1.01f)
			{
				throw new InvalidOperationException("Unexpected roof plane");
			}
			foreach (Transform item in list)
			{
				if (Mathf.Abs(item.localPosition.y - num * item.localPosition.z - num2) > 0.001f || Mathf.Abs(Mathf.Abs(item.localPosition.x) - 1f) > 0.001f)
				{
					throw new InvalidOperationException("Roof snaps do not describe the expected 2x2 m plane");
				}
			}
			if (roof.GetComponentsInChildren<Collider>(true).Any((Collider c) => !(c is BoxCollider)))
			{
				throw new InvalidOperationException("Uninspected collider type; refusing to alter this clone");
			}
			Transform[] array = Enumerable.Range(0, transform.childCount).Select((Func<int, Transform>)transform.GetChild).ToArray();
			float num3 = 0f - num;
			float num4 = 0.5f * Mathf.Atan2(2f * num3, (0f - num3) * num3);
			float num5 = Mathf.Cos(num4);
			float num6 = Mathf.Sin(num4);
			float num7 = Mathf.Sqrt((num5 + num3 * num6) * (num5 + num3 * num6) + num6 * num6);
			float num8 = Mathf.Atan2(num6, num5 + num3 * num6);
			Transform transform2 = new GameObject("Ridgewarden flatten scale").transform;
			transform2.SetParent(transform, false);
			transform2.localPosition = new Vector3(0f, 0f - num2, 0f);
			transform2.localRotation = Quaternion.Euler(num8 * 57.29578f, 0f, 0f);
			transform2.localScale = new Vector3(1f, num7, 1f / num7);
			Transform transform3 = new GameObject("Ridgewarden flatten basis").transform;
			transform3.SetParent(transform2, false);
			transform3.localRotation = Quaternion.Euler((0f - num4) * 57.29578f, 0f, 0f);
			BoxCollider[] componentsInChildren = roof.GetComponentsInChildren<BoxCollider>(true);
			foreach (BoxCollider val in componentsInChildren)
			{
				Matrix4x4 val2 = transform.worldToLocalMatrix * ((Component)val).transform.localToWorldMatrix;
				Bounds val3 = default(Bounds);
				bool flag = true;
				for (int num10 = -1; num10 <= 1; num10 += 2)
				{
					for (int num11 = -1; num11 <= 1; num11 += 2)
					{
						for (int num12 = -1; num12 <= 1; num12 += 2)
						{
							Vector3 val4 = ((Matrix4x4)(ref val2)).MultiplyPoint3x4(val.center + Vector3.Scale(val.size * 0.5f, new Vector3((float)num10, (float)num11, (float)num12)));
							val4.y -= num * val4.z + num2;
							if (flag)
							{
								((Bounds)(ref val3))..ctor(val4, Vector3.zero);
								flag = false;
							}
							else
							{
								((Bounds)(ref val3)).Encapsulate(val4);
							}
						}
					}
				}
				GameObject val5 = new GameObject("Ridgewarden flat collision")
				{
					layer = ((Component)val).gameObject.layer,
					tag = ((Component)val).gameObject.tag
				};
				val5.transform.SetParent(transform, false);
				BoxCollider obj = val5.AddComponent<BoxCollider>();
				obj.center = ((Bounds)(ref val3)).center;
				obj.size = ((Bounds)(ref val3)).size;
				((Collider)obj).sharedMaterial = ((Collider)val).sharedMaterial;
				((Collider)obj).isTrigger = ((Collider)val).isTrigger;
				((Collider)obj).enabled = ((Collider)val).enabled;
				val5.SetActive(((Component)val).gameObject.activeSelf);
				Object.DestroyImmediate((Object)(object)val);
			}
			Transform[] array2 = array;
			foreach (Transform val6 in array2)
			{
				if (list.Contains(val6))
				{
					Vector3 localPosition3 = val6.localPosition;
					val6.localPosition = new Vector3(Mathf.Round(localPosition3.x), 0f, Mathf.Round(localPosition3.z));
					val6.localRotation = Quaternion.identity;
				}
				else
				{
					val6.SetParent(transform3, false);
				}
			}
			WearNTear component = roof.GetComponent<WearNTear>();
			Vector3 comOffset = component.m_comOffset;
			comOffset.y -= num * comOffset.z + num2;
			component.m_comOffset = comOffset;
		}
	}
}