Decompiled source of BussolaPolar v1.0.0

BepInEx\plugins\BussolaMod\BussolaMod.dll

Decompiled 3 weeks 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("BussolaMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BussolaMod")]
[assembly: AssemblyTitle("BussolaMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 BussolaMod
{
	public class CompassUI : MonoBehaviour
	{
		public static CompassUI Instance;

		private Canvas _canvas;

		private Image _bgImage;

		private RectTransform _needleRect;

		private Image _needleImage;

		private Text _debugText;

		private Texture2D _bgTex;

		private Texture2D _needleTex;

		public static void Create(Plugin plugin)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			if (!((Object)(object)Instance != (Object)null))
			{
				GameObject val = new GameObject("BussolaCanvas");
				Object.DontDestroyOnLoad((Object)val);
				Instance = val.AddComponent<CompassUI>();
				Instance.Init(plugin);
				Plugin.Log.LogInfo((object)"CompassUI criada.");
			}
		}

		private void Init(Plugin plugin)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: 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_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Expected O, but got Unknown
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Expected O, but got Unknown
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: 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_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ed: Expected O, but got Unknown
			//IL_0241: Unknown result type (might be due to invalid IL or missing references)
			//IL_025d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0272: Unknown result type (might be due to invalid IL or missing references)
			//IL_0287: Unknown result type (might be due to invalid IL or missing references)
			//IL_029c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("Bussola_Canvas");
			val.transform.SetParent(((Component)this).transform);
			_canvas = val.AddComponent<Canvas>();
			_canvas.renderMode = (RenderMode)0;
			_canvas.sortingOrder = 100;
			val.AddComponent<CanvasScaler>();
			val.AddComponent<GraphicRaycaster>();
			GameObject val2 = new GameObject("CompassContainer");
			val2.transform.SetParent(val.transform, false);
			RectTransform obj = val2.AddComponent<RectTransform>();
			obj.anchorMin = new Vector2(1f, 0f);
			obj.anchorMax = new Vector2(1f, 0f);
			obj.pivot = new Vector2(1f, 0f);
			obj.anchoredPosition = new Vector2(-30f, 30f);
			obj.sizeDelta = new Vector2(128f, 128f);
			GameObject val3 = new GameObject("CompassBG");
			val3.transform.SetParent(val2.transform, false);
			_bgImage = val3.AddComponent<Image>();
			RectTransform component = val3.GetComponent<RectTransform>();
			component.anchorMin = Vector2.zero;
			component.anchorMax = Vector2.one;
			component.offsetMin = Vector2.zero;
			component.offsetMax = Vector2.zero;
			GameObject val4 = new GameObject("CompassNeedle");
			val4.transform.SetParent(val3.transform, false);
			_needleImage = val4.AddComponent<Image>();
			_needleRect = val4.GetComponent<RectTransform>();
			_needleRect.anchorMin = new Vector2(0.5f, 0.5f);
			_needleRect.anchorMax = new Vector2(0.5f, 0.5f);
			_needleRect.pivot = new Vector2(63f / 128f, 0.32421875f);
			_needleRect.sizeDelta = new Vector2(128f, 128f);
			_needleRect.anchoredPosition = Vector2.zero;
			GameObject val5 = new GameObject("CompassText");
			val5.transform.SetParent(val2.transform, false);
			_debugText = val5.AddComponent<Text>();
			_debugText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
			_debugText.alignment = (TextAnchor)4;
			_debugText.fontSize = 12;
			((Graphic)_debugText).color = Color.white;
			RectTransform component2 = val5.GetComponent<RectTransform>();
			component2.anchorMin = new Vector2(0.5f, 0f);
			component2.anchorMax = new Vector2(0.5f, 0f);
			component2.pivot = new Vector2(0.5f, 1f);
			component2.anchoredPosition = new Vector2(0f, -5f);
			component2.sizeDelta = new Vector2(100f, 20f);
			_debugText.text = "";
			LoadTextures(plugin);
		}

		private void LoadTextures(Plugin plugin)
		{
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0220: 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_01b6: Unknown result type (might be due to invalid IL or missing references)
			string pluginPath = plugin.GetPluginPath();
			string[] candidates = new string[4]
			{
				Path.Combine(pluginPath, "Assets", "bussola.png"),
				Path.Combine(pluginPath, "bussola.png"),
				"D:\\valheim opencode\\BussolaMod\\Assets\\bussola.png",
				"D:\\valheim opencode\\imagens\\bussola.png"
			};
			string[] candidates2 = new string[4]
			{
				Path.Combine(pluginPath, "Assets", "ponteira.png"),
				Path.Combine(pluginPath, "ponteira.png"),
				"D:\\valheim opencode\\BussolaMod\\Assets\\ponteira.png",
				"D:\\valheim opencode\\imagens\\ponteira.png"
			};
			_bgTex = LoadTexture(candidates);
			_needleTex = LoadTexture(candidates2);
			if ((Object)(object)_bgTex != (Object)null)
			{
				_bgImage.sprite = Sprite.Create(_bgTex, new Rect(0f, 0f, (float)((Texture)_bgTex).width, (float)((Texture)_bgTex).height), new Vector2(0.5f, 0.5f), 100f);
				_bgImage.preserveAspect = true;
				Plugin.Log.LogInfo((object)$"Bussola BG carregada {((Texture)_bgTex).width}x{((Texture)_bgTex).height}");
			}
			else
			{
				Plugin.Log.LogWarning((object)"Falha ao carregar bussola.png - usando placeholder");
				((Graphic)_bgImage).color = new Color(1f, 1f, 1f, 0.8f);
			}
			if ((Object)(object)_needleTex != (Object)null)
			{
				_needleImage.sprite = Sprite.Create(_needleTex, new Rect(0f, 0f, (float)((Texture)_needleTex).width, (float)((Texture)_needleTex).height), new Vector2(0.5f, 0.5f), 100f);
				_needleImage.preserveAspect = true;
				Plugin.Log.LogInfo((object)$"Ponteiro carregado {((Texture)_needleTex).width}x{((Texture)_needleTex).height}");
			}
			else
			{
				Plugin.Log.LogWarning((object)"Falha ao carregar ponteira.png");
				((Graphic)_needleImage).color = Color.red;
			}
		}

		private Texture2D LoadTexture(string[] candidates)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			foreach (string text in candidates)
			{
				if (!File.Exists(text))
				{
					continue;
				}
				try
				{
					byte[] array = File.ReadAllBytes(text);
					Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
					if (ImageConversion.LoadImage(val, array))
					{
						((Texture)val).filterMode = (FilterMode)1;
						return val;
					}
				}
				catch (Exception ex)
				{
					Plugin.Log.LogWarning((object)("Erro carregando " + text + ": " + ex.Message));
				}
			}
			return null;
		}

		private void Update()
		{
			//IL_008b: 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_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_needleRect == (Object)null)
			{
				return;
			}
			bool flag = (Object)(object)Plugin.Instance != (Object)null && Plugin.Instance.ShouldShowCompass();
			if ((Object)(object)_canvas != (Object)null && ((Component)_canvas).gameObject.activeSelf != flag)
			{
				((Component)_canvas).gameObject.SetActive(flag);
			}
			if (!flag)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null))
			{
				float num = 0f;
				Camera main = Camera.main;
				num = (((Object)(object)main != (Object)null) ? ((Component)main).transform.eulerAngles.y : ((!((Object)(object)GameCamera.instance != (Object)null)) ? ((Component)localPlayer).transform.eulerAngles.y : ((Component)GameCamera.instance).transform.eulerAngles.y));
				((Transform)_needleRect).localEulerAngles = new Vector3(0f, 0f, 0f - num);
				if ((Object)(object)_debugText != (Object)null)
				{
					string cardinal = GetCardinal(num);
					_debugText.text = $"{Mathf.RoundToInt(num)}° {cardinal}";
				}
			}
		}

		private string GetCardinal(float yaw)
		{
			yaw = (yaw % 360f + 360f) % 360f;
			if (yaw < 22.5f || yaw >= 337.5f)
			{
				return "N";
			}
			if (yaw < 67.5f)
			{
				return "NE";
			}
			if (yaw < 112.5f)
			{
				return "E";
			}
			if (yaw < 157.5f)
			{
				return "SE";
			}
			if (yaw < 202.5f)
			{
				return "S";
			}
			if (yaw < 247.5f)
			{
				return "SW";
			}
			if (yaw < 292.5f)
			{
				return "W";
			}
			return "NW";
		}
	}
	[BepInPlugin("yone.bussola", "Bussola Mod", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Humanoid), "ConsumeItem")]
		private class ConsumePatch
		{
			private static void Postfix(Humanoid __instance, Inventory inventory, ItemData item, bool checkWorldLevel, bool __result)
			{
				if (__result && item != null && item.m_shared != null && (item.m_shared.m_name == "$item_bussola" || ((Object)(object)item.m_dropPrefab != (Object)null && ((Object)item.m_dropPrefab).name == "BussolaYone")))
				{
					Log.LogInfo((object)("ConsumePatch detectou Bússola por " + ((Object)__instance).name));
					Player val = (Player)(object)((__instance is Player) ? __instance : null);
					if (val != null)
					{
						UnlockCompass(val);
					}
				}
			}
		}

		[HarmonyPatch(typeof(Player), "ConsumeItem")]
		private class PlayerConsumePatch
		{
			private static void Postfix(Player __instance, Inventory inventory, ItemData item, bool checkWorldLevel, bool __result)
			{
				if (__result && item != null && item.m_shared != null && (item.m_shared.m_name == "$item_bussola" || ((Object)(object)item.m_dropPrefab != (Object)null && ((Object)item.m_dropPrefab).name == "BussolaYone")))
				{
					Log.LogInfo((object)"PlayerConsumePatch detectou Bússola");
					UnlockCompass(__instance);
				}
			}
		}

		public static Plugin Instance;

		public static ManualLogSource Log;

		private ConfigEntry<bool> _alwaysShow;

		private ConfigEntry<bool> _requireItem;

		private const string ItemName = "BussolaYone";

		private void Awake()
		{
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"BussolaMod carregando...");
			_alwaysShow = ((BaseUnityPlugin)this).Config.Bind<bool>("Geral", "SempreMostrar", false, "Se true, mostra bússola mesmo sem item (para debug)");
			_requireItem = ((BaseUnityPlugin)this).Config.Bind<bool>("Geral", "RequerItem", true, "Se true, só mostra se desbloqueou conhecimento");
			PrefabManager.OnVanillaPrefabsAvailable += CreateItem;
			new Harmony("yone.bussola").PatchAll();
			Log.LogInfo((object)"BussolaMod pronto. Aguardando prefabs... Harmony patchado.");
		}

		private void CreateItem()
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Expected O, but got Unknown
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: 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_00c4: Expected O, but got Unknown
			//IL_00c6: 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_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Expected O, but got Unknown
			//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_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Expected O, but got Unknown
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Expected O, but got Unknown
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Expected O, but got Unknown
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Expected O, but got Unknown
			//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Sprite val = LoadIconSprite();
				if ((Object)(object)val != (Object)null)
				{
					Log.LogInfo((object)$"Ícone bússola carregado {((Texture)val.texture).width}x{((Texture)val.texture).height}");
				}
				else
				{
					Log.LogWarning((object)"Ícone bússola não encontrado - usará ícone do Amber");
				}
				ItemConfig val2 = new ItemConfig();
				val2.Name = "$item_bussola";
				val2.Description = "$item_bussola_desc";
				val2.CraftingStation = CraftingStations.Workbench;
				val2.MinStationLevel = 1;
				val2.Requirements = (RequirementConfig[])(object)new RequirementConfig[6]
				{
					new RequirementConfig
					{
						Item = "Wood",
						Amount = 1
					},
					new RequirementConfig
					{
						Item = "Flint",
						Amount = 2
					},
					new RequirementConfig
					{
						Item = "Raspberry",
						Amount = 4
					},
					new RequirementConfig
					{
						Item = "LeatherScraps",
						Amount = 2
					},
					new RequirementConfig
					{
						Item = "Dandelion",
						Amount = 4
					},
					new RequirementConfig
					{
						Item = "Resin",
						Amount = 5
					}
				};
				ItemConfig val3 = val2;
				val3.CraftingStation = "piece_workbench";
				if ((Object)(object)val != (Object)null)
				{
					val3.Icon = val;
				}
				CustomItem val4 = new CustomItem("BussolaYone", "Amber", val3);
				if ((Object)(object)val4.ItemDrop != (Object)null)
				{
					SharedData shared = val4.ItemDrop.m_itemData.m_shared;
					if ((Object)(object)val != (Object)null)
					{
						shared.m_icons = (Sprite[])(object)new Sprite[1] { val };
					}
					shared.m_name = "$item_bussola";
					shared.m_description = "$item_bussola_desc";
					shared.m_itemType = (ItemType)2;
					shared.m_maxStackSize = 10;
					shared.m_maxQuality = 1;
					shared.m_weight = 0.3f;
					shared.m_consumeStatusEffect = null;
				}
				ItemManager.Instance.AddItem(val4);
				Log.LogInfo((object)"Item BussolaYone registrado consumível permanente (clonado de Amber, ícone custom).");
				CustomLocalization val5 = new CustomLocalization();
				string text = "Portuguese";
				val5.AddTranslation(ref text, new Dictionary<string, string>
				{
					["item_bussola"] = "Bússola Polar",
					["item_bussola_desc"] = "Um conhecimento viking antigo que te dá o Norte em sua caminhada. Consuma para aprender para sempre."
				});
				text = "English";
				val5.AddTranslation(ref text, new Dictionary<string, string>
				{
					["item_bussola"] = "Polar Compass",
					["item_bussola_desc"] = "Ancient viking knowledge that gives you North on your journey. Consume to learn permanently."
				});
				LocalizationManager.Instance.AddLocalization(val5);
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Erro ao criar item: {arg}");
			}
			finally
			{
				PrefabManager.OnVanillaPrefabsAvailable -= CreateItem;
			}
		}

		private void Update()
		{
			if ((Object)(object)Player.m_localPlayer != (Object)null && (Object)(object)CompassUI.Instance == (Object)null)
			{
				CompassUI.Create(this);
			}
		}

		public bool ShouldShowCompass()
		{
			if (_alwaysShow.Value)
			{
				return true;
			}
			if (!_requireItem.Value)
			{
				return true;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return false;
			}
			if (localPlayer.m_customData != null && localPlayer.m_customData.TryGetValue("BussolaPolarUnlocked", out var value) && value == "1")
			{
				return true;
			}
			return false;
		}

		public static void UnlockCompass(Player player)
		{
			if (!((Object)(object)player == (Object)null))
			{
				if (player.m_customData == null)
				{
					player.m_customData = new Dictionary<string, string>();
				}
				player.m_customData["BussolaPolarUnlocked"] = "1";
				Log.LogInfo((object)("Bússola Polar desbloqueada para " + player.GetPlayerName()));
				if ((Object)(object)player == (Object)(object)Player.m_localPlayer)
				{
					MessageHud.instance.ShowMessage((MessageType)2, "Conhecimento: Bússola Polar desbloqueada! O Norte agora te guia.", 0, (Sprite)null, false);
				}
			}
		}

		public static bool IsUnlocked(Player player)
		{
			if ((Object)(object)player != (Object)null && player.m_customData != null && player.m_customData.TryGetValue("BussolaPolarUnlocked", out var value))
			{
				return value == "1";
			}
			return false;
		}

		public string GetPluginPath()
		{
			return Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		}

		private Sprite LoadIconSprite()
		{
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Expected O, but got Unknown
			//IL_0097: 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)
			string pluginPath = GetPluginPath();
			string[] array = new string[4]
			{
				Path.Combine(pluginPath, "Assets", "bussola.png"),
				Path.Combine(pluginPath, "bussola.png"),
				"D:\\valheim opencode\\BussolaMod\\Assets\\bussola.png",
				"D:\\valheim opencode\\imagens\\bussola.png"
			};
			foreach (string text in array)
			{
				if (!File.Exists(text))
				{
					continue;
				}
				try
				{
					byte[] array2 = File.ReadAllBytes(text);
					Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
					if (ImageConversion.LoadImage(val, array2))
					{
						((Texture)val).filterMode = (FilterMode)1;
						return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f);
					}
				}
				catch (Exception ex)
				{
					Log.LogWarning((object)("Erro ícone " + text + ": " + ex.Message));
				}
			}
			return null;
		}
	}
}