Decompiled source of CrystalAngles v1.8.3

CrystalAngles.dll

Decompiled 9 hours ago
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: AssemblyTitle("CrystalAngles")]
[assembly: AssemblyDescription("Crystal wall floor and roof pieces that save as the vanilla crystal wall.")]
[assembly: AssemblyCompany("Gizmorphium")]
[assembly: AssemblyProduct("CrystalAngles")]
[assembly: AssemblyCopyright("Gizmorphium")]
[assembly: AssemblyFileVersion("1.8.3.0")]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("1.8.3.0")]
namespace CrystalAngles;

[BepInPlugin("gizmorphium.crystalangles", "CrystalAngles", "1.8.3")]
public class CrystalAnglesPlugin : BaseUnityPlugin
{
	internal struct RoofDef
	{
		public float Angle;

		public string Tag;

		public int[][] Runs;

		public string Flavor;
	}

	internal class Mapping
	{
		public Piece Vanilla;

		public Vector3[] LocalPos;

		public Quaternion LocalRot;
	}

	public const string Guid = "gizmorphium.crystalangles";

	public const string Name = "CrystalAngles";

	public const string Version = "1.8.3";

	internal const string VanillaPrefabName = "crystal_wall_1x1";

	internal static readonly RoofDef[] Roofs = new RoofDef[3]
	{
		new RoofDef
		{
			Angle = 26.565f,
			Tag = "26",
			Runs = new int[2][]
			{
				new int[2] { 5, 2 },
				new int[2] { 3, 1 }
			},
			Flavor = "A gentle crystal roof that keeps the rain out and lets the sun in."
		},
		new RoofDef
		{
			Angle = 45f,
			Tag = "45",
			Runs = new int[1][] { new int[2] { 3, 1 } },
			Flavor = "A classic crystal roof, neither steep nor shallow."
		},
		new RoofDef
		{
			Angle = 63.435f,
			Tag = "67",
			Runs = new int[1][] { new int[2] { 5, 1 } },
			Flavor = "A steep crystal roof that sheds snow and lets the light pour in."
		}
	};

	internal static ConfigEntry<bool> HelperSnaps;

	internal static ConfigEntry<bool> GroupPieces;

	internal static ManualLogSource Log;

	internal static readonly Dictionary<string, Mapping> Mappings = new Dictionary<string, Mapping>();

	internal static readonly List<GameObject> CustomPrefabs = new List<GameObject>();

	private static GameObject _holder;

	private static readonly string[] Words = new string[11]
	{
		"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
		"ten"
	};

	private void Awake()
	{
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		HelperSnaps = ((BaseUnityPlugin)this).Config.Bind<bool>("Pieces", "HelperSnapPoints", true, "Adds extra snap points to the roof panes so a run of panes fits a run of beams exactly, ending flush. 26°: 5 panes over two beams, or 3 over one. 45°: 3 over one. 67°: 5 over one. Off = vanilla snap points only. Restart the game after changing.");
		GroupPieces = ((BaseUnityPlugin)this).Config.Bind<bool>("Pieces", "GroupPieces", true, "Adds roof pieces that place a whole run of panes in one go, sized to fit the beams: 26° over two beams or one, 45° over one, 67° over one. Costs one crystal wall per pane. Restart the game after changing.");
		RemoveOldSetting();
		new Harmony("gizmorphium.crystalangles").PatchAll(Assembly.GetExecutingAssembly());
		Log.LogInfo((object)"CrystalAngles 1.8.3 loaded.");
	}

	internal static void Register(ObjectDB db)
	{
		if ((Object)(object)db == (Object)null || db.m_items == null || db.m_items.Count == 0)
		{
			return;
		}
		PieceTable val = null;
		foreach (GameObject item in db.m_items)
		{
			if ((Object)(object)item == (Object)null || ((Object)item).name != "Hammer")
			{
				continue;
			}
			ItemDrop component = item.GetComponent<ItemDrop>();
			if ((Object)(object)component != (Object)null)
			{
				val = component.m_itemData.m_shared.m_buildPieces;
			}
			break;
		}
		if ((Object)(object)val == (Object)null)
		{
			Log.LogWarning((object)"Hammer piece table not found.");
			return;
		}
		int num = val.m_pieces.FindIndex((GameObject p) => (Object)(object)p != (Object)null && ((Object)p).name == "crystal_wall_1x1");
		if (num < 0)
		{
			Log.LogWarning((object)"crystal_wall_1x1 not found in hammer table.");
			return;
		}
		GameObject vanilla = val.m_pieces[num];
		if (CustomPrefabs.Count == 0)
		{
			BuildPrefabs(vanilla);
		}
		int num2 = num + 1;
		foreach (GameObject customPrefab in CustomPrefabs)
		{
			if (val.m_pieces.Contains(customPrefab))
			{
				num2 = val.m_pieces.IndexOf(customPrefab) + 1;
			}
			else
			{
				val.m_pieces.Insert(num2++, customPrefab);
			}
		}
	}

	private static void BuildPrefabs(GameObject vanilla)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Expected O, but got Unknown
		_holder = new GameObject("CrystalAngles_Prefabs");
		_holder.SetActive(false);
		Object.DontDestroyOnLoad((Object)(object)_holder);
		Piece component = vanilla.GetComponent<Piece>();
		Add(BuildOne(vanilla, component, 0f, string.Empty, 0, 0, null, "A crystal wall laid flat. Try not to look down."));
		RoofDef[] roofs = Roofs;
		for (int i = 0; i < roofs.Length; i++)
		{
			RoofDef roofDef = roofs[i];
			List<int[]> snapSets = ((!HelperSnaps.Value) ? null : new List<int[]>(roofDef.Runs));
			Add(BuildOne(vanilla, component, roofDef.Angle, roofDef.Tag, 0, 0, snapSets, roofDef.Flavor));
			if (GroupPieces.Value)
			{
				int[][] runs = roofDef.Runs;
				foreach (int[] array in runs)
				{
					Add(BuildOne(vanilla, component, roofDef.Angle, roofDef.Tag, array[0], array[1], null, null));
				}
			}
		}
		Log.LogInfo((object)("Created " + CustomPrefabs.Count + " crystal angle pieces."));
	}

	private static string Word(int n, bool capital)
	{
		string text = ((n < 0 || n >= Words.Length) ? n.ToString() : Words[n]);
		return (!capital || text.Length <= 0) ? text : (char.ToUpper(text[0]) + text.Substring(1));
	}

	private static void Add(GameObject prefab)
	{
		if ((Object)(object)prefab != (Object)null)
		{
			CustomPrefabs.Add(prefab);
		}
	}

	private void RemoveOldSetting()
	{
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Expected O, but got Unknown
		try
		{
			PropertyInfo property = typeof(ConfigFile).GetProperty("OrphanedEntries", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			Dictionary<ConfigDefinition, string> dictionary = ((!(property != null)) ? null : (property.GetValue(((BaseUnityPlugin)this).Config, null) as Dictionary<ConfigDefinition, string>));
			if (dictionary != null && dictionary.Remove(new ConfigDefinition("General", "Slopes")))
			{
				((BaseUnityPlugin)this).Config.Save();
			}
		}
		catch
		{
		}
	}

	private static GameObject BuildOne(GameObject vanilla, Piece vanillaPiece, float slope, string tag, int panes, int beams, List<int[]> snapSets, string flavor)
	{
		//IL_0df8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0dfa: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Expected O, but got Unknown
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		//IL_018f: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_019f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01be: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01da: Unknown result type (might be due to invalid IL or missing references)
		//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_020a: Unknown result type (might be due to invalid IL or missing references)
		//IL_022e: Unknown result type (might be due to invalid IL or missing references)
		//IL_026e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0275: Expected O, but got Unknown
		//IL_028a: Unknown result type (might be due to invalid IL or missing references)
		//IL_028f: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0a11: Unknown result type (might be due to invalid IL or missing references)
		//IL_0a18: Expected O, but got Unknown
		//IL_057b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0582: Expected O, but got Unknown
		//IL_0325: Unknown result type (might be due to invalid IL or missing references)
		//IL_0327: Unknown result type (might be due to invalid IL or missing references)
		//IL_032c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0331: Unknown result type (might be due to invalid IL or missing references)
		//IL_036a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0378: Unknown result type (might be due to invalid IL or missing references)
		//IL_037a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0382: Unknown result type (might be due to invalid IL or missing references)
		//IL_0387: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_03bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_05ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_05f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0606: Unknown result type (might be due to invalid IL or missing references)
		//IL_060b: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ea: 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_03f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_040d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0a97: Unknown result type (might be due to invalid IL or missing references)
		//IL_0a9c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0ab0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0ab5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0c3c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0c43: Expected O, but got Unknown
		//IL_0afd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b02: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b1e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b23: Unknown result type (might be due to invalid IL or missing references)
		//IL_0726: Unknown result type (might be due to invalid IL or missing references)
		//IL_072b: Unknown result type (might be due to invalid IL or missing references)
		//IL_07c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_07cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_07f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0804: Unknown result type (might be due to invalid IL or missing references)
		bool flag = slope < 0.01f;
		bool flag2 = panes > 1 && beams > 0;
		string sub = ((!flag2) ? string.Empty : (panes + "/" + beams));
		string text = ((!flag) ? ("Roof " + tag + "°" + ((!flag2) ? string.Empty : (" (" + beams + ((beams != 1) ? " beams)" : " beam)")))) : "Floor");
		string text2 = "crystalangles_" + ((!flag) ? ("roof" + tag.Replace('.', '_').Replace(' ', '_')) : "floor") + ((!flag2) ? string.Empty : ("_" + panes + "x" + beams));
		GameObject val = new GameObject(text2);
		val.transform.SetParent(_holder.transform, false);
		val.layer = vanilla.layer;
		GameObject val2 = Object.Instantiate<GameObject>(vanilla, val.transform, false);
		((Object)val2).name = "model";
		if (!LocalMeshBounds(val2.transform, out var bounds))
		{
			Log.LogWarning((object)"No mesh found on crystal_wall_1x1");
			Object.Destroy((Object)(object)val);
			return null;
		}
		Vector3 val3 = ((!(((Bounds)(ref bounds)).size.z <= ((Bounds)(ref bounds)).size.x)) ? Vector3.right : Vector3.forward);
		Vector3 val4 = Vector3.Cross(Vector3.up, val3);
		Quaternion val5 = Quaternion.AngleAxis(90f - slope, val4);
		Vector3 val6 = val5 * ((Bounds)(ref bounds)).center;
		Vector3 val7 = -val6;
		val2.transform.localRotation = val5;
		val2.transform.localPosition = val7;
		List<GameObject> list = new List<GameObject>();
		list.Add(val2);
		List<GameObject> list2 = list;
		List<Vector3> list3 = new List<Vector3>();
		list3.Add(val7);
		List<Vector3> list4 = list3;
		string text3 = string.Empty;
		if (flag2)
		{
			float num = float.MaxValue;
			float num2 = float.MinValue;
			foreach (Transform item in val2.transform)
			{
				Transform val8 = item;
				if (((Component)val8).CompareTag("snappoint"))
				{
					num = Mathf.Min(num, val8.localPosition.y);
					num2 = Mathf.Max(num2, val8.localPosition.y);
				}
			}
			float num3 = ((!(num2 > num + 0.1f)) ? 1f : (num2 - num));
			float num4 = (float)beams * 2f / Mathf.Cos(slope * ((float)Math.PI / 180f));
			float num5 = (num4 - num3) / (float)(panes - 1);
			Vector3 val9 = val5 * Vector3.up;
			for (int i = 1; i < panes; i++)
			{
				GameObject val10 = Object.Instantiate<GameObject>(vanilla, val.transform, false);
				((Object)val10).name = "model" + i;
				val10.transform.localRotation = val5;
				val10.transform.localPosition = val7 + val9 * (num5 * (float)i);
				list2.Add(val10);
				list4.Add(val10.transform.localPosition);
			}
			Vector3 val11 = val9 * (num5 * (float)(panes - 1) * 0.5f);
			for (int j = 0; j < list2.Count; j++)
			{
				Transform transform = list2[j].transform;
				transform.localPosition -= val11;
				list4[j] = list2[j].transform.localPosition;
			}
			text3 = Word(panes, capital: true) + " crystal panes in one go, sized to cover " + Word(beams, capital: false) + " " + tag + "° " + ((beams != 1) ? "beams." : "beam.");
			Log.LogInfo((object)(text2 + ": " + panes + " panes over " + num4.ToString("0.###", CultureInfo.InvariantCulture) + " m, pane " + num3.ToString("0.###", CultureInfo.InvariantCulture) + " m, pitch " + num5.ToString("0.####", CultureInfo.InvariantCulture) + " m, overlap " + ((num3 - num5) * 100f).ToString("0.#", CultureInfo.InvariantCulture) + " cm"));
		}
		string text4 = string.Empty;
		if (!flag2 && snapSets != null && snapSets.Count > 0)
		{
			List<Transform> list5 = new List<Transform>();
			foreach (Transform item2 in val2.transform)
			{
				Transform val12 = item2;
				if (((Component)val12).CompareTag("snappoint"))
				{
					list5.Add(val12);
				}
			}
			float num6 = float.MaxValue;
			float num7 = float.MinValue;
			foreach (Transform item3 in list5)
			{
				num6 = Mathf.Min(num6, item3.localPosition.y);
				num7 = Mathf.Max(num7, item3.localPosition.y);
			}
			float num8 = ((!(num7 > num6 + 0.1f)) ? 1f : (num7 - num6));
			float num9 = 2f / Mathf.Cos(slope * ((float)Math.PI / 180f));
			List<string> list6 = new List<string>();
			foreach (int[] snapSet in snapSets)
			{
				int num10 = snapSet[0];
				int num11 = snapSet[1];
				float num12 = ((float)num10 * num8 - (float)num11 * num9) / (float)(num10 - 1);
				if (num12 <= 0f || num12 >= num8)
				{
					Log.LogWarning((object)(text2 + ": " + num10 + "/" + num11 + " gives no overlap, skipped."));
					continue;
				}
				foreach (Transform item4 in list5)
				{
					float y = item4.localPosition.y;
					bool flag3 = Mathf.Abs(y - num6) < 0.05f;
					bool flag4 = Mathf.Abs(y - num7) < 0.05f;
					if (flag3 || flag4)
					{
						GameObject val13 = Object.Instantiate<GameObject>(((Component)item4).gameObject, val2.transform, false);
						((Object)val13).name = ((Object)item4).name + "_" + num10 + "x" + num11;
						Vector3 localPosition = item4.localPosition;
						localPosition.y += ((!flag3) ? (0f - num12) : num12);
						val13.transform.localPosition = localPosition;
						val13.transform.localRotation = item4.localRotation;
					}
				}
				string text5 = (num12 * 100f).ToString("0.#", CultureInfo.InvariantCulture);
				list6.Add(num10 + ((list6.Count != 0) ? string.Empty : " panes") + " over " + Word(num11, capital: false) + ((snapSets.Count != 1 && num11 <= 1) ? string.Empty : ((num11 != 1) ? " beams" : " beam")));
				Log.LogInfo((object)(text2 + ": snap set " + num10 + "/" + num11 + ", overlap " + text5 + " cm"));
			}
			if (list6.Count == 1)
			{
				text4 = " Extra snap points fit " + list6[0] + ".";
			}
			else if (list6.Count > 1)
			{
				string text6 = list6[list6.Count - 1];
				list6.RemoveAt(list6.Count - 1);
				text4 = " Extra snap points fit " + string.Join(", ", list6.ToArray()) + ", or " + text6 + ".";
			}
		}
		List<Transform> list7 = new List<Transform>();
		for (int k = 0; k < list2.Count; k++)
		{
			List<Transform> list8 = new List<Transform>();
			foreach (Transform item5 in list2[k].transform)
			{
				Transform val14 = item5;
				if (((Component)val14).CompareTag("snappoint"))
				{
					list8.Add(val14);
				}
			}
			if (!flag2)
			{
				list7.AddRange(list8);
				continue;
			}
			float num13 = float.MaxValue;
			float num14 = float.MinValue;
			foreach (Transform item6 in list8)
			{
				num13 = Mathf.Min(num13, item6.localPosition.y);
				num14 = Mathf.Max(num14, item6.localPosition.y);
			}
			foreach (Transform item7 in list8)
			{
				bool flag5 = Mathf.Abs(item7.localPosition.y - num13) < 0.05f;
				bool flag6 = Mathf.Abs(item7.localPosition.y - num14) < 0.05f;
				if ((k == 0 && flag5) || (k == list2.Count - 1 && flag6))
				{
					list7.Add(item7);
				}
				else
				{
					Object.DestroyImmediate((Object)(object)((Component)item7).gameObject);
				}
			}
		}
		foreach (Transform item8 in list7)
		{
			item8.SetParent(val.transform, true);
		}
		Piece val15 = val.AddComponent<Piece>();
		CopyFields(vanillaPiece, val15);
		if (flag2 && vanillaPiece.m_resources != null)
		{
			Requirement[] array = (Requirement[])(object)new Requirement[vanillaPiece.m_resources.Length];
			for (int l = 0; l < array.Length; l++)
			{
				Requirement val16 = vanillaPiece.m_resources[l];
				int num15 = l;
				Requirement val17 = new Requirement();
				val17.m_resItem = val16.m_resItem;
				val17.m_amount = val16.m_amount * panes;
				val17.m_extraAmountOnlyOneIngredient = val16.m_extraAmountOnlyOneIngredient;
				val17.m_amountPerLevel = val16.m_amountPerLevel;
				val17.m_recover = val16.m_recover;
				array[num15] = val17;
			}
			val15.m_resources = array;
		}
		val15.m_name = "Crystal " + text;
		val15.m_description = ((!flag2) ? ((flavor ?? string.Empty) + text4) : text3);
		try
		{
			string text7 = (flag ? "floor" : ((!flag2) ? tag : ("g" + tag + "_" + panes + "x" + beams)));
			Sprite val18 = BakedIcons.Load(text7);
			if ((Object)(object)val18 == (Object)null)
			{
				Log.LogWarning((object)("No baked icon '" + text7 + "', drawing a fallback."));
				val18 = IconMaker.Make(vanillaPiece.m_icon, slope, (!flag) ? tag : string.Empty, sub);
			}
			if ((Object)(object)val18 != (Object)null)
			{
				val15.m_icon = val18;
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("Icon for " + text2 + " failed, using vanilla icon: " + ex.Message));
		}
		Mappings[text2] = new Mapping
		{
			Vanilla = vanillaPiece,
			LocalPos = list4.ToArray(),
			LocalRot = val5
		};
		return val;
	}

	private static bool LocalMeshBounds(Transform modelRoot, out Bounds bounds)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: 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_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: 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_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: 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_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//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_00db: 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)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		bounds = default(Bounds);
		bool flag = false;
		Matrix4x4 worldToLocalMatrix = modelRoot.worldToLocalMatrix;
		MeshFilter[] componentsInChildren = ((Component)modelRoot).GetComponentsInChildren<MeshFilter>(true);
		foreach (MeshFilter val in componentsInChildren)
		{
			if ((Object)(object)val.sharedMesh == (Object)null)
			{
				continue;
			}
			Bounds bounds2 = val.sharedMesh.bounds;
			Matrix4x4 val2 = worldToLocalMatrix * ((Component)val).transform.localToWorldMatrix;
			for (int j = 0; j < 8; j++)
			{
				Vector3 val3 = ((Bounds)(ref bounds2)).center + Vector3.Scale(((Bounds)(ref bounds2)).extents, new Vector3((float)(((j & 1) != 0) ? 1 : (-1)), (float)(((j & 2) != 0) ? 1 : (-1)), (float)(((j & 4) != 0) ? 1 : (-1))));
				val3 = ((Matrix4x4)(ref val2)).MultiplyPoint3x4(val3);
				if (!flag)
				{
					((Bounds)(ref bounds))..ctor(val3, Vector3.zero);
					flag = true;
				}
				else
				{
					((Bounds)(ref bounds)).Encapsulate(val3);
				}
			}
		}
		return flag;
	}

	private static void CopyFields(Piece from, Piece to)
	{
		Type type = typeof(Piece);
		while (type != null && type != typeof(MonoBehaviour))
		{
			FieldInfo[] fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (FieldInfo fieldInfo in fields)
			{
				if (!(fieldInfo.Name == "m_nview") && !(fieldInfo.Name == "m_myListIndex"))
				{
					fieldInfo.SetValue(to, fieldInfo.GetValue(from));
				}
			}
			type = type.BaseType;
		}
	}
}
[HarmonyPatch(typeof(ObjectDB), "Awake")]
internal static class ObjectDB_Awake_Patch
{
	[HarmonyPriority(200)]
	private static void Postfix(ObjectDB __instance)
	{
		CrystalAnglesPlugin.Register(__instance);
	}
}
[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
internal static class ObjectDB_CopyOtherDB_Patch
{
	[HarmonyPriority(200)]
	private static void Postfix(ObjectDB __instance)
	{
		CrystalAnglesPlugin.Register(__instance);
	}
}
[HarmonyPatch(typeof(Player), "PlacePiece")]
internal static class Player_PlacePiece_Patch
{
	internal class Extra
	{
		public Piece Vanilla;

		public List<Vector3> Pos = new List<Vector3>();

		public Quaternion Rot;
	}

	private static bool _placingExtras;

	[HarmonyPriority(800)]
	private static void Prefix(ref Piece piece, ref Vector3 pos, ref Quaternion rot, out Extra __state)
	{
		//IL_004b: 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_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: 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_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: 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_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		__state = null;
		if ((Object)(object)piece == (Object)null || _placingExtras || !CrystalAnglesPlugin.Mappings.TryGetValue(((Object)((Component)piece).gameObject).name, out var value) || (Object)(object)value.Vanilla == (Object)null)
		{
			return;
		}
		Vector3 val = pos;
		Quaternion val2 = rot;
		pos = val + val2 * value.LocalPos[0];
		rot = val2 * value.LocalRot;
		piece = value.Vanilla;
		if (value.LocalPos.Length > 1)
		{
			__state = new Extra
			{
				Vanilla = value.Vanilla,
				Rot = rot
			};
			for (int i = 1; i < value.LocalPos.Length; i++)
			{
				__state.Pos.Add(val + val2 * value.LocalPos[i]);
			}
		}
	}

	private static void Postfix(Player __instance, Extra __state, bool cheated)
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		if (__state == null)
		{
			return;
		}
		_placingExtras = true;
		try
		{
			foreach (Vector3 po in __state.Pos)
			{
				__instance.PlacePiece(__state.Vanilla, po, __state.Rot, false, cheated);
			}
		}
		finally
		{
			_placingExtras = false;
		}
	}
}
internal static class IconMaker
{
	private static readonly Color Outline = new Color(0.06f, 0.1f, 0.16f, 1f);

	private static readonly Color Glass = new Color(0.55f, 0.82f, 1f, 1f);

	private static readonly Color Guide = new Color(0.85f, 0.75f, 0.55f, 1f);

	private static readonly int[][] Digits = new int[11][]
	{
		new int[5] { 7, 5, 5, 5, 7 },
		new int[5] { 2, 6, 2, 2, 7 },
		new int[5] { 7, 1, 7, 4, 7 },
		new int[5] { 7, 1, 7, 1, 7 },
		new int[5] { 5, 5, 7, 1, 1 },
		new int[5] { 7, 4, 7, 1, 7 },
		new int[5] { 7, 4, 7, 5, 7 },
		new int[5] { 7, 1, 1, 1, 1 },
		new int[5] { 7, 5, 7, 5, 7 },
		new int[5] { 7, 5, 7, 1, 7 },
		new int[5] { 1, 1, 2, 4, 4 }
	};

	public static Sprite Make(Sprite vanilla, float slope, string label, string sub)
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: 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_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: 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_03ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c1: Expected O, but got Unknown
		//IL_03e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_032d: Unknown result type (might be due to invalid IL or missing references)
		//IL_032f: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0310: Unknown result type (might be due to invalid IL or missing references)
		//IL_0201: Unknown result type (might be due to invalid IL or missing references)
		int num = 64;
		if ((Object)(object)vanilla != (Object)null)
		{
			Rect rect = vanilla.rect;
			float width = ((Rect)(ref rect)).width;
			Rect rect2 = vanilla.rect;
			num = Mathf.Max(32, Mathf.RoundToInt(Mathf.Min(width, ((Rect)(ref rect2)).height)));
		}
		float num2 = (float)num / 64f;
		Color[] array = (Color[])(object)new Color[num * num];
		for (int i = 0; i < array.Length; i++)
		{
			ref Color reference = ref array[i];
			reference = new Color(0f, 0f, 0f, 0f);
		}
		float num3 = slope * ((float)Math.PI / 180f);
		float num4 = 40f * num2;
		float num5 = 4.5f * num2;
		float num6 = Mathf.Cos(num3);
		float num7 = Mathf.Sin(num3);
		float num8 = (float)num / 2f + 2f * num2;
		float num9 = (float)num / 2f - 2f * num2;
		float num10 = num8 - num6 * num4 / 2f;
		float num11 = num9 - num7 * num4 / 2f;
		bool flag = slope > 0.5f;
		for (int j = 0; j < num; j++)
		{
			for (int k = 0; k < num; k++)
			{
				Color dst = array[j * num + k];
				float num12 = (float)k + 0.5f;
				float num13 = (float)j + 0.5f;
				if (flag)
				{
					float num14 = Mathf.Abs(num13 - num11);
					if (num12 >= num10 - 1f && num12 <= num10 + 22f * num2)
					{
						Over(ref dst, Guide, Mathf.Clamp01(1.2f * num2 - num14 + 0.5f) * 0.9f);
					}
					float num15 = Mathf.Sqrt((num12 - num10) * (num12 - num10) + (num13 - num11) * (num13 - num11));
					float num16 = Mathf.Atan2(num13 - num11, num12 - num10);
					if (num16 >= 0f && num16 <= num3 && Mathf.Abs(num15 - 14f * num2) < 1.2f * num2)
					{
						Over(ref dst, Guide, 0.9f);
					}
				}
				float num17 = (num12 - num8) * num6 + (num13 - num9) * num7;
				float num18 = (0f - (num12 - num8)) * num7 + (num13 - num9) * num6;
				float num19 = Mathf.Abs(num17) - num4 / 2f;
				float num20 = Mathf.Abs(num18) - num5;
				float num21 = Mathf.Sqrt(Mathf.Max(num19, 0f) * Mathf.Max(num19, 0f) + Mathf.Max(num20, 0f) * Mathf.Max(num20, 0f)) + Mathf.Min(Mathf.Max(num19, num20), 0f);
				Over(ref dst, Outline, Mathf.Clamp01(1.6f * num2 - num21 + 0.5f));
				Over(ref dst, Glass, Mathf.Clamp01(0f - num21 + 0.5f) * 0.95f);
				if (num21 < 0f && num18 > num5 * 0.25f && num18 < num5 * 0.75f)
				{
					Over(ref dst, Color.white, 0.75f);
				}
				array[j * num + k] = dst;
			}
		}
		if (!string.IsNullOrEmpty(label))
		{
			Stamp(array, num, label, Mathf.Max(1, Mathf.RoundToInt(2f * num2)), Mathf.RoundToInt(4f * num2), -1);
		}
		if (!string.IsNullOrEmpty(sub))
		{
			Stamp(array, num, sub, Mathf.Max(1, Mathf.RoundToInt(1.5f * num2)), -1, Mathf.RoundToInt(3f * num2));
		}
		Texture2D val = new Texture2D(num, num, (TextureFormat)4, false);
		val.SetPixels(array);
		val.Apply();
		return Sprite.Create(val, new Rect(0f, 0f, (float)num, (float)num), new Vector2(0.5f, 0.5f), 100f);
	}

	private static void Over(ref Color dst, Color c, float a)
	{
		if (!(a <= 0f))
		{
			float a2 = dst.a;
			float num = a + a2 * (1f - a);
			if (!(num <= 0f))
			{
				dst.r = (c.r * a + dst.r * a2 * (1f - a)) / num;
				dst.g = (c.g * a + dst.g * a2 * (1f - a)) / num;
				dst.b = (c.b * a + dst.b * a2 * (1f - a)) / num;
				dst.a = num;
			}
		}
	}

	private static void Stamp(Color[] px, int n, string text, int ps, int topLeftX, int bottomRightInset)
	{
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		int num = 4 * ps;
		int num2 = text.Length * num - ps;
		int num3;
		int num4;
		if (topLeftX >= 0)
		{
			num3 = topLeftX;
			num4 = n - topLeftX - 5 * ps;
		}
		else
		{
			num3 = n - bottomRightInset - num2;
			num4 = bottomRightInset;
		}
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < text.Length; j++)
			{
				int num5 = ((text[j] != '/') ? (text[j] - 48) : 10);
				if (num5 < 0 || num5 > 10)
				{
					continue;
				}
				for (int k = 0; k < 5; k++)
				{
					for (int l = 0; l < 3; l++)
					{
						if ((Digits[num5][k] & (4 >> l)) != 0)
						{
							int num6 = num3 + j * num + l * ps;
							int num7 = num4 + (4 - k) * ps;
							if (i == 0)
							{
								Fill(px, n, num6 - ps, num7 - ps, 3 * ps, 3 * ps, Outline);
							}
							else
							{
								Fill(px, n, num6, num7, ps, ps, Color.white);
							}
						}
					}
				}
			}
		}
	}

	private static void Fill(Color[] px, int n, int x, int y, int fw, int fh, Color col)
	{
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		for (int i = y; i < y + fh; i++)
		{
			for (int j = x; j < x + fw; j++)
			{
				if (j >= 0 && i >= 0 && j < n && i < n)
				{
					px[i * n + j] = col;
				}
			}
		}
	}
}
internal static class BakedIcons
{
	private const int Size = 64;

	public static Sprite Load(string key)
	{
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Expected O, but got Unknown
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		Assembly assembly = typeof(BakedIcons).Assembly;
		using Stream stream = assembly.GetManifestResourceStream("CrystalAngles.Icons." + key + ".rgba");
		if (stream == null)
		{
			return null;
		}
		byte[] array = new byte[16384];
		int num;
		for (int i = 0; i < array.Length; i += num)
		{
			num = stream.Read(array, i, array.Length - i);
			if (num <= 0)
			{
				return null;
			}
		}
		Texture2D val = new Texture2D(64, 64, (TextureFormat)4, false);
		val.LoadRawTextureData(array);
		((Texture)val).filterMode = (FilterMode)1;
		((Texture)val).wrapMode = (TextureWrapMode)1;
		val.Apply(false, true);
		((Object)val).name = "CrystalAngles_" + key;
		Sprite val2 = Sprite.Create(val, new Rect(0f, 0f, 64f, 64f), new Vector2(0.5f, 0.5f), 100f);
		((Object)val2).name = ((Object)val).name;
		return val2;
	}
}