Decompiled source of Hourglass v0.1.0

Hourglass.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("Hourglass")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+93b98e56dca869067d6e7543e5e10318e7fd73d9")]
[assembly: AssemblyProduct("Hourglass")]
[assembly: AssemblyTitle("Hourglass")]
[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 Hourglass
{
	[BepInPlugin("games.loxley.hourglass", "Hourglass", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class HourglassPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "games.loxley.hourglass";

		public const string PluginName = "Hourglass";

		public const string PluginVersion = "0.1.0";

		public const string PrefabName = "LoxleyHourglass";

		public const string Key = "loxley_hourglass";

		public static ConfigEntry<float> TimeOfDay;

		public static bool IsHeld
		{
			get
			{
				if (Object.op_Implicit((Object)(object)ZoneSystem.instance))
				{
					return ZoneSystem.instance.GetGlobalKey("loxley_hourglass");
				}
				return false;
			}
		}

		private void Awake()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			TimeOfDay = ((BaseUnityPlugin)this).Config.Bind<float>("General", "TimeOfDay", 0.5f, "Where the sun is held. 0 = midnight, 0.25 = dawn, 0.5 = noon, 0.75 = dusk.");
			PrefabManager.OnVanillaPrefabsAvailable += AddPiece;
			new Harmony("games.loxley.hourglass").PatchAll();
		}

		private void AddPiece()
		{
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Expected O, but got Unknown
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Expected O, but got Unknown
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Expected O, but got Unknown
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Expected O, but got Unknown
			GameObject val = PrefabManager.Instance.CreateClonedPrefab("LoxleyHourglass", "guard_stone");
			PrivateArea[] componentsInChildren = val.GetComponentsInChildren<PrivateArea>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren[i]);
			}
			val.AddComponent<HourglassPiece>();
			HourglassModel.Build(val);
			string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "hourglass_icon.png");
			Sprite icon = (File.Exists(text) ? AssetUtils.LoadSpriteFromFile(text) : null);
			PieceManager instance = PieceManager.Instance;
			PieceConfig val2 = new PieceConfig();
			val2.Name = "Hourglass";
			val2.Icon = icon;
			val2.Description = "Holds the sun at noon for everyone until used again. Good for building.";
			val2.PieceTable = PieceTables.Hammer;
			val2.Category = PieceCategories.Misc;
			val2.CraftingStation = CraftingStations.Workbench;
			val2.Requirements = (RequirementConfig[])(object)new RequirementConfig[3]
			{
				new RequirementConfig("FineWood", 6, 0, true),
				new RequirementConfig("Resin", 4, 0, true),
				new RequirementConfig("GreydwarfEye", 2, 0, true)
			};
			instance.AddPiece(new CustomPiece(val, true, val2));
			PrefabManager.OnVanillaPrefabsAvailable -= AddPiece;
		}
	}
	public static class HourglassModel
	{
		public static void Build(GameObject prefab)
		{
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Expected O, but got Unknown
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
			Renderer[] componentsInChildren = prefab.GetComponentsInChildren<Renderer>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].enabled = false;
			}
			Light[] componentsInChildren2 = prefab.GetComponentsInChildren<Light>(true);
			for (int i = 0; i < componentsInChildren2.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren2[i]);
			}
			ParticleSystem[] componentsInChildren3 = prefab.GetComponentsInChildren<ParticleSystem>(true);
			for (int i = 0; i < componentsInChildren3.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)((Component)componentsInChildren3[i]).gameObject);
			}
			Material val = MaterialOf("wood_pole2") ?? MaterialOf("guard_stone");
			Material mat = MaterialOf("crystal_wall_1x1") ?? val;
			Material mat2 = MaterialOf("piece_beehive") ?? val;
			GameObject val2 = new GameObject("hourglass");
			val2.transform.SetParent(prefab.transform, false);
			val2.layer = prefab.layer;
			Part(val2, (PrimitiveType)2, new Vector3(0f, 0.04f, 0f), new Vector3(0.54f, 0.04f, 0.54f), val);
			Part(val2, (PrimitiveType)2, new Vector3(0f, 0.8f, 0f), new Vector3(0.54f, 0.04f, 0.54f), val);
			Part(val2, (PrimitiveType)2, new Vector3(0f, 0.845f, 0f), new Vector3(0.16f, 0.015f, 0.16f), val);
			for (int j = 0; j < 3; j++)
			{
				float num = (float)j * (float)Math.PI * 2f / 3f;
				Part(val2, (PrimitiveType)2, new Vector3(Mathf.Cos(num) * 0.21f, 0.42f, Mathf.Sin(num) * 0.21f), new Vector3(0.05f, 0.34f, 0.05f), val);
			}
			Part(val2, (PrimitiveType)0, new Vector3(0f, 0.21f, 0f), new Vector3(0.24f, 0.16f, 0.24f), mat2);
			Lathe(val2, "glass", mat, new(float, float)[11]
			{
				(0.06f, 0.1f),
				(0.12f, 0.15f),
				(0.2f, 0.175f),
				(0.3f, 0.14f),
				(0.38f, 0.06f),
				(0.42f, 0.03f),
				(0.46f, 0.06f),
				(0.54f, 0.14f),
				(0.64f, 0.175f),
				(0.72f, 0.15f),
				(0.78f, 0.1f)
			});
		}

		private static string Describe(Material m)
		{
			if (!Object.op_Implicit((Object)(object)m))
			{
				return "null";
			}
			return ((Object)m).name + "/" + ((Object)m.shader).name + "/tex=" + (Object.op_Implicit((Object)(object)m.mainTexture) ? ((Object)m.mainTexture).name : "none");
		}

		private static Material MaterialOf(string prefabName)
		{
			GameObject prefab = PrefabManager.Instance.GetPrefab(prefabName);
			if (!Object.op_Implicit((Object)(object)prefab))
			{
				return null;
			}
			Material val = null;
			MeshRenderer[] componentsInChildren = prefab.GetComponentsInChildren<MeshRenderer>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Material[] sharedMaterials = ((Renderer)componentsInChildren[i]).sharedMaterials;
				foreach (Material val2 in sharedMaterials)
				{
					if (Object.op_Implicit((Object)(object)val2) && !((Object)val2.shader).name.Contains("Snow") && !((Object)val2).name.ToLower().Contains("snow"))
					{
						if (Object.op_Implicit((Object)(object)val2.mainTexture))
						{
							return val2;
						}
						val = (Object.op_Implicit((Object)(object)val) ? val : val2);
					}
				}
			}
			return val;
		}

		private static void Lathe(GameObject parent, string name, Material mat, (float y, float r)[] profile)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: 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_0039: 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_0176: 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_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//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_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: 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_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: 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_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: 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)
			//IL_00d8: 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_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: 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_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: 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_01d8: 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_01f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0377: Unknown result type (might be due to invalid IL or missing references)
			//IL_037c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0385: Expected O, but got Unknown
			//IL_03b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d3: Unknown result type (might be due to invalid IL or missing references)
			List<Vector2> list = new List<Vector2>();
			for (int i = 0; i < profile.Length - 1; i++)
			{
				Vector2 val = P(i - 1);
				Vector2 val2 = P(i);
				Vector2 val3 = P(i + 1);
				Vector2 val4 = P(i + 2);
				for (int j = 0; j < 4; j++)
				{
					float num = (float)j / 4f;
					float num2 = num * num;
					float num3 = num2 * num;
					list.Add(0.5f * (2f * val2 + (-val + val3) * num + (2f * val - 5f * val2 + 4f * val3 - val4) * num2 + (-val + 3f * val2 - 3f * val3 + val4) * num3));
				}
			}
			list.Add(new Vector2(profile[profile.Length - 1].r, profile[profile.Length - 1].y));
			int count = list.Count;
			List<Vector3> list2 = new List<Vector3>();
			List<Vector2> list3 = new List<Vector2>();
			List<int> list4 = new List<int>();
			for (int k = 0; k < count; k++)
			{
				for (int l = 0; l <= 28; l++)
				{
					float num4 = (float)l / 28f * (float)Math.PI * 2f;
					list2.Add(new Vector3(Mathf.Cos(num4) * list[k].x, list[k].y, Mathf.Sin(num4) * list[k].x));
					list3.Add(new Vector2((float)l / 28f, (float)k / (float)(count - 1)));
				}
			}
			int num5 = 29;
			for (int m = 0; m < count - 1; m++)
			{
				for (int n = 0; n < 28; n++)
				{
					int num6 = m * num5 + n;
					int num7 = num6 + 1;
					int num8 = num6 + num5;
					int num9 = num8 + 1;
					list4.AddRange(new int[6] { num6, num8, num7, num7, num8, num9 });
				}
			}
			int[] array = new int[2]
			{
				0,
				count - 1
			};
			foreach (int num11 in array)
			{
				int count2 = list2.Count;
				list2.Add(new Vector3(0f, list[num11].y, 0f));
				list3.Add(new Vector2(0.5f, 0.5f));
				for (int num12 = 0; num12 < 28; num12++)
				{
					int num13 = num11 * num5 + num12;
					int num14 = num13 + 1;
					if (num11 == 0)
					{
						list4.AddRange(new int[3] { count2, num14, num13 });
					}
					else
					{
						list4.AddRange(new int[3] { count2, num13, num14 });
					}
				}
			}
			Mesh val5 = new Mesh
			{
				name = name
			};
			val5.SetVertices(list2);
			val5.SetUVs(0, list3);
			val5.SetTriangles(list4, 0);
			val5.RecalculateNormals();
			val5.RecalculateBounds();
			GameObject val6 = new GameObject(name)
			{
				layer = parent.layer
			};
			val6.transform.SetParent(parent.transform, false);
			val6.AddComponent<MeshFilter>().sharedMesh = val5;
			MeshRenderer val7 = val6.AddComponent<MeshRenderer>();
			if (Object.op_Implicit((Object)(object)mat))
			{
				((Renderer)val7).sharedMaterial = mat;
			}
			Vector2 P(int num15)
			{
				//IL_0035: Unknown result type (might be due to invalid IL or missing references)
				num15 = Mathf.Clamp(num15, 0, profile.Length - 1);
				return new Vector2(profile[num15].r, profile[num15].y);
			}
		}

		private unsafe static void Part(GameObject parent, PrimitiveType type, Vector3 pos, Vector3 scale, Material mat)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = GameObject.CreatePrimitive(type);
			Object.DestroyImmediate((Object)(object)val.GetComponent<Collider>());
			((Object)val).name = ((object)(*(PrimitiveType*)(&type))/*cast due to .constrained prefix*/).ToString();
			val.layer = parent.layer;
			val.transform.SetParent(parent.transform, false);
			val.transform.localPosition = pos;
			val.transform.localScale = scale;
			if (Object.op_Implicit((Object)(object)mat))
			{
				((Renderer)val.GetComponent<MeshRenderer>()).sharedMaterial = mat;
			}
		}
	}
	public class HourglassPiece : MonoBehaviour, Interactable, Hoverable
	{
		private static readonly List<HourglassPiece> All = new List<HourglassPiece>();

		private void Awake()
		{
			All.Add(this);
			WearNTear component = ((Component)this).GetComponent<WearNTear>();
			if (Object.op_Implicit((Object)(object)component))
			{
				component.m_onDestroyed = (Action)Delegate.Combine(component.m_onDestroyed, new Action(OnDestroyed));
			}
		}

		private void OnDestroy()
		{
			All.Remove(this);
		}

		private void OnDestroyed()
		{
			All.Remove(this);
			if (All.Count == 0 && HourglassPlugin.IsHeld)
			{
				ZoneSystem.instance.RemoveGlobalKey("loxley_hourglass");
				Player localPlayer = Player.m_localPlayer;
				if (localPlayer != null)
				{
					((Character)localPlayer).Message((MessageType)2, "Time flows again", 0, (Sprite)null, false);
				}
			}
		}

		public bool Interact(Humanoid user, bool hold, bool alt)
		{
			if (hold)
			{
				return false;
			}
			if (HourglassPlugin.IsHeld)
			{
				ZoneSystem.instance.RemoveGlobalKey("loxley_hourglass");
				((Character)user).Message((MessageType)2, "Time flows again", 0, (Sprite)null, false);
			}
			else
			{
				ZoneSystem.instance.SetGlobalKey("loxley_hourglass");
				((Character)user).Message((MessageType)2, "Time stands still", 0, (Sprite)null, false);
			}
			return true;
		}

		public bool UseItem(Humanoid user, ItemData item)
		{
			return false;
		}

		public string GetHoverName()
		{
			return "Hourglass";
		}

		public float GetHoverOffset()
		{
			return 0f;
		}

		public string GetHoverText()
		{
			string text = (HourglassPlugin.IsHeld ? "Let time flow" : "Stop time");
			return Localization.instance.Localize("Hourglass\n[<color=yellow><b>$KEY_Use</b></color>] " + text);
		}
	}
	[HarmonyPatch(typeof(EnvMan), "FixedUpdate")]
	internal static class HoldTime
	{
		private static bool s_wasHeld;

		private static void Prefix(EnvMan __instance)
		{
			bool isHeld = HourglassPlugin.IsHeld;
			if (isHeld)
			{
				__instance.m_debugTimeOfDay = true;
				__instance.m_debugTime = HourglassPlugin.TimeOfDay.Value;
			}
			else if (s_wasHeld)
			{
				__instance.m_debugTimeOfDay = false;
			}
			s_wasHeld = isHeld;
		}
	}
}