Decompiled source of Alexguitar9211 Homeward Stone v1.0.0

Homeward StoneV1/HomewardStone.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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 UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace HomewardStone;

[BepInPlugin("com.yourname.homewardstone", "Homeward Stone", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class HomewardStonePlugin : BaseUnityPlugin
{
	private class Home
	{
		public string Name;

		public Vector3 Pos;
	}

	public const string PluginGUID = "com.yourname.homewardstone";

	public const string PluginName = "Homeward Stone";

	public const string PluginVersion = "1.0.0";

	public const string ItemPrefabName = "HomewardStone";

	private const string SaveKey = "HomewardStone_Homes";

	private const float MinHomeDistance = 40f;

	private static HomewardStonePlugin _instance;

	private ConfigEntry<KeyboardShortcut> _markKey;

	private readonly List<Home> _homes = new List<Home>();

	private bool _menuOpen;

	private ItemData _stoneItem;

	private Rect _window = new Rect(200f, 150f, 340f, 400f);

	private Vector2 _scroll;

	private void Awake()
	{
		//IL_0021: 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)
		_instance = this;
		_markKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Controls", "MarkHomeKey", new KeyboardShortcut((KeyCode)288, Array.Empty<KeyCode>()), "Press this to save your current location as a home base.");
		ItemManager.OnVanillaItemsAvailable += AddStoneItem;
		new Harmony("com.yourname.homewardstone").PatchAll();
	}

	private void AddStoneItem()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Expected O, but got Unknown
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Expected O, but got Unknown
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Expected O, but got Unknown
		ItemManager.OnVanillaItemsAvailable -= AddStoneItem;
		ItemConfig val = new ItemConfig();
		val.Name = "Homeward Stone";
		val.Description = "A rune-etched stone. Use it to teleport to one of your saved home bases. Consumed on use.";
		val.CraftingStation = "piece_workbench";
		val.Icons = (Sprite[])(object)new Sprite[1] { CreateIcon() };
		val.Requirements = (RequirementConfig[])(object)new RequirementConfig[2]
		{
			new RequirementConfig("Resin", 3, 0, true),
			new RequirementConfig("GreydwarfEye", 3, 0, true)
		};
		ItemConfig val2 = val;
		CustomItem val3 = new CustomItem("HomewardStone", "Stone", val2);
		val3.ItemDrop.m_itemData.m_shared.m_maxStackSize = 10;
		ItemManager.Instance.AddItem(val3);
	}

	private static Sprite CreateIcon()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		//IL_0177: Unknown result type (might be due to invalid IL or missing references)
		//IL_0186: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0137: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		Texture2D val = new Texture2D(128, 128, (TextureFormat)4, false);
		Color val2 = default(Color);
		((Color)(ref val2))..ctor(0.45f, 0.92f, 1f, 1f);
		for (int i = 0; i < 128; i++)
		{
			for (int j = 0; j < 128; j++)
			{
				float num = ((float)j - 64f) / 54f;
				float num2 = ((float)i - 62f) / 48f;
				float num3 = num * num + num2 * num2;
				Color val3 = Color.clear;
				if (num3 <= 1f)
				{
					float num4 = Mathf.Lerp(0.3f, 0.58f, (float)i / 128f) - num3 * 0.12f;
					((Color)(ref val3))..ctor(num4, num4, num4 * 1.05f, 1f);
					if (num3 > 0.88f)
					{
						((Color)(ref val3))..ctor(0.12f, 0.12f, 0.14f, 1f);
					}
					bool num5 = i >= 66 && i <= 94 && Mathf.Abs(j - 64) <= 94 - i;
					bool flag = j >= 46 && j <= 82 && i >= 38 && i < 66;
					bool flag2 = j >= 58 && j <= 70 && i >= 38 && i <= 54;
					if ((num5 || flag) && !flag2)
					{
						val3 = val2;
					}
				}
				val.SetPixel(j, i, val3);
			}
		}
		val.Apply();
		return Sprite.Create(val, new Rect(0f, 0f, 128f, 128f), new Vector2(0.5f, 0.5f));
	}

	private void LoadHomes()
	{
		//IL_00ab: 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)
		_homes.Clear();
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null || !localPlayer.m_customData.TryGetValue("HomewardStone_Homes", out var value) || string.IsNullOrEmpty(value))
		{
			return;
		}
		string[] array = value.Split(new char[1] { ';' });
		for (int i = 0; i < array.Length; i++)
		{
			string[] array2 = array[i].Split(new char[1] { '|' });
			if (array2.Length == 4)
			{
				try
				{
					_homes.Add(new Home
					{
						Name = array2[0],
						Pos = new Vector3(float.Parse(array2[1], CultureInfo.InvariantCulture), float.Parse(array2[2], CultureInfo.InvariantCulture), float.Parse(array2[3], CultureInfo.InvariantCulture))
					});
				}
				catch (FormatException)
				{
				}
			}
		}
	}

	private void SaveHomes()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return;
		}
		List<string> list = new List<string>();
		foreach (Home home in _homes)
		{
			list.Add(string.Join("|", home.Name.Replace("|", "").Replace(";", ""), home.Pos.x.ToString(CultureInfo.InvariantCulture), home.Pos.y.ToString(CultureInfo.InvariantCulture), home.Pos.z.ToString(CultureInfo.InvariantCulture)));
		}
		localPlayer.m_customData["HomewardStone_Homes"] = string.Join(";", list.ToArray());
	}

	private void MarkHome()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: 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_00ce: 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)
		Player localPlayer = Player.m_localPlayer;
		if (!Object.op_Implicit((Object)(object)EffectArea.IsPointInsideArea(((Component)localPlayer).transform.position, (Type)4, 0f)))
		{
			((Character)localPlayer).Message((MessageType)2, "You can only save a home inside an established base (near a workbench).", 0, (Sprite)null, false);
			return;
		}
		LoadHomes();
		foreach (Home home in _homes)
		{
			if (Vector3.Distance(home.Pos, ((Component)localPlayer).transform.position) < 40f)
			{
				((Character)localPlayer).Message((MessageType)2, "You already saved a home here.", 0, (Sprite)null, false);
				return;
			}
		}
		_homes.Add(new Home
		{
			Name = "Home " + (_homes.Count + 1),
			Pos = ((Component)localPlayer).transform.position
		});
		SaveHomes();
		((Character)localPlayer).Message((MessageType)2, "Saved as Home " + _homes.Count, 0, (Sprite)null, false);
	}

	public static void OpenMenu(ItemData item)
	{
		_instance._stoneItem = item;
		_instance.LoadHomes();
		_instance._menuOpen = true;
		GUIManager.BlockInput(true);
		if ((Object)(object)InventoryGui.instance != (Object)null)
		{
			InventoryGui.instance.Hide();
		}
	}

	private void CloseMenu()
	{
		_menuOpen = false;
		GUIManager.BlockInput(false);
	}

	private void Update()
	{
		//IL_0060: 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)
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			if (_menuOpen)
			{
				CloseMenu();
			}
		}
		else if (_menuOpen)
		{
			if (Input.GetKeyDown((KeyCode)27) || ((Character)localPlayer).IsDead())
			{
				CloseMenu();
			}
		}
		else if (!((Object)(object)Chat.instance != (Object)null) || !Chat.instance.HasFocus())
		{
			KeyboardShortcut value = _markKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				MarkHome();
			}
		}
	}

	private void OnGUI()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Expected O, but got Unknown
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		if (_menuOpen)
		{
			_window = GUI.Window(94817, _window, new WindowFunction(DrawWindow), "Homeward Stone - choose a home");
		}
	}

	private void DrawWindow(int id)
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: 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)
		GUILayout.Label("Teleporting uses up one stone.", Array.Empty<GUILayoutOption>());
		if (_homes.Count == 0)
		{
			GUILayout.Label("No homes saved yet. Close this and press " + ((object)_markKey.Value/*cast due to .constrained prefix*/).ToString() + " at a base to save it.", Array.Empty<GUILayoutOption>());
		}
		_scroll = GUILayout.BeginScrollView(_scroll, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(280f) });
		for (int i = 0; i < _homes.Count; i++)
		{
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			if (GUILayout.Button("Go: " + _homes[i].Name, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(30f) }))
			{
				TeleportHome(_homes[i]);
				GUILayout.EndHorizontal();
				GUILayout.EndScrollView();
				return;
			}
			if (GUILayout.Button("X", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.Width(30f),
				GUILayout.Height(30f)
			}))
			{
				_homes.RemoveAt(i);
				SaveHomes();
				GUILayout.EndHorizontal();
				GUILayout.EndScrollView();
				return;
			}
			GUILayout.EndHorizontal();
		}
		GUILayout.EndScrollView();
		if (GUILayout.Button("Close", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(30f) }))
		{
			CloseMenu();
		}
		GUI.DragWindow();
	}

	private void TeleportHome(Home home)
	{
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			CloseMenu();
		}
		else if (!((Humanoid)localPlayer).IsTeleportable(false))
		{
			((Character)localPlayer).Message((MessageType)2, "You are carrying something too heavy with magic to teleport!", 0, (Sprite)null, false);
			CloseMenu();
		}
		else
		{
			((Humanoid)localPlayer).GetInventory().RemoveOneItem(_stoneItem);
			((Character)localPlayer).TeleportTo(home.Pos + Vector3.up * 0.5f, ((Component)localPlayer).transform.rotation, true);
			CloseMenu();
		}
	}
}
[HarmonyPatch(typeof(Humanoid), "UseItem")]
internal static class UseItemPatch
{
	private static bool Prefix(Humanoid __instance, ItemData item)
	{
		if (item == null || (Object)(object)__instance != (Object)(object)Player.m_localPlayer)
		{
			return true;
		}
		if ((Object)(object)item.m_dropPrefab == (Object)null || ((Object)item.m_dropPrefab).name != "HomewardStone")
		{
			return true;
		}
		HomewardStonePlugin.OpenMenu(item);
		return false;
	}
}