Decompiled source of SpyglassAndExplorationTweaks v1.0.0

SpyglassAndExplorationTweaks.dll

Decompiled 8 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
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;

[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: AssemblyCompany("SpyglassAndExplorationTweaks")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SpyglassAndExplorationTweaks")]
[assembly: AssemblyTitle("SpyglassAndExplorationTweaks")]
[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 SpyglassAndExplorationTweaks
{
	internal static class MapTable
	{
		[HarmonyPatch(typeof(Minimap), "ExploreOthers")]
		private static class Minimap_ExploreOthers_Patch
		{
			private static void Postfix(Minimap __instance, int x, int y, bool __result)
			{
				//IL_003c: 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_0051: Unknown result type (might be due to invalid IL or missing references)
				if (__result && Plugin.SharedAsExplored.Value)
				{
					Texture2D val = Fog.Invoke(__instance);
					Explored.Invoke(__instance)[y * ((Texture)val).width + x] = true;
					Color pixel = val.GetPixel(x, y);
					pixel.r = 0f;
					val.SetPixel(x, y, pixel);
				}
			}
		}

		[HarmonyPatch(typeof(Minimap), "ResetAndExplore", new Type[]
		{
			typeof(BitArray),
			typeof(BitArray)
		})]
		private static class Minimap_ResetAndExplore_Patch
		{
			private static void Prefix(BitArray explored, BitArray exploredOthers)
			{
				if (Plugin.SharedAsExplored.Value && explored != null && exploredOthers != null && explored.Length == exploredOthers.Length)
				{
					explored.Or(exploredOthers);
				}
			}
		}

		private static readonly FieldRef<Minimap, BitArray> Explored = AccessTools.FieldRefAccess<Minimap, BitArray>("m_explored");

		private static readonly FieldRef<Minimap, Texture2D> Fog = AccessTools.FieldRefAccess<Minimap, Texture2D>("m_fogTexture");
	}
	[BepInPlugin("com.spyglassandexplorationtweaks", "SpyglassAndExplorationTweaks", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.spyglassandexplorationtweaks";

		public const string PluginName = "SpyglassAndExplorationTweaks";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<int> RadiusPerLevel;

		internal static ConfigEntry<int> CostPercent;

		internal static ConfigEntry<bool> RequireStationLevel;

		internal static ConfigEntry<bool> SharedAsExplored;

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected O, but got Unknown
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Expected O, but got Unknown
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			ConfigurationManagerAttributes val = new ConfigurationManagerAttributes
			{
				IsAdminOnly = true
			};
			RadiusPerLevel = ((BaseUnityPlugin)this).Config.Bind<int>("Spyglass", "ExtraRadiusPerLevel", 50, new ConfigDescription("How many meters the map reveal grows for each level of the Spyglass. The game's normal reveal radius is 100, so level 3 with the default 50 reveals 250.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 300), new object[1] { val }));
			CostPercent = ((BaseUnityPlugin)this).Config.Bind<int>("Spyglass", "CostPercent", 100, new ConfigDescription("Scales how many materials crafting and upgrading cost. 100 = the default amounts.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(10, 500), new object[1] { val }));
			RequireStationLevel = ((BaseUnityPlugin)this).Config.Bind<bool>("Spyglass", "RequireStationUpgrades", false, new ConfigDescription("Off (default): upgrades only need a forge. On: each upgrade level also needs the forge to have one more upgrade (the game's usual rule).", (AcceptableValueBase)null, new object[1] { val }));
			SharedAsExplored = ((BaseUnityPlugin)this).Config.Bind<bool>("Map", "SharedMapCountsAsExplored", false, new ConfigDescription("Off = vanilla: map data you get from a map table shows slightly shaded, apart from the parts you explored yourself.\nOn = that data shows as fully explored, exactly like areas you walked yourself, and is kept as your own.\nApplies to map data read from now on, and to shared data already saved on your map the next time you load the world.", (AcceptableValueBase)null, new object[1] { val }));
			PrefabManager.OnVanillaPrefabsAvailable += SpyglassItem.Create;
			ItemManager.OnItemsRegisteredFejd += SpyglassItem.ApplyRecipe;
			ItemManager.OnItemsRegistered += SpyglassItem.ApplyRecipe;
			new Harmony("com.spyglassandexplorationtweaks").PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"SpyglassAndExplorationTweaks 1.0.0 loaded");
		}

		private void Update()
		{
			SpyglassItem.TickRadius();
		}
	}
	internal static class SpyglassItem
	{
		private class Cost
		{
			public string[] Names;

			public int[] PerLevel;

			public Cost(int[] perLevel, params string[] names)
			{
				PerLevel = perLevel;
				Names = names;
			}
		}

		[HarmonyPatch(typeof(Requirement), "GetAmount")]
		private static class Requirement_GetAmount_Patch
		{
			private static void Postfix(Requirement __instance, int qualityLevel, ref int __result)
			{
				if (Schedules.TryGetValue(__instance, out var value))
				{
					int num = value[Mathf.Clamp(qualityLevel - 1, 0, value.Length - 1)];
					if (num > 0)
					{
						num = Mathf.Max(1, Mathf.RoundToInt((float)(num * Plugin.CostPercent.Value) / 100f));
					}
					__result = num;
				}
			}
		}

		[HarmonyPatch(typeof(Recipe), "GetRequiredStationLevel")]
		private static class Recipe_GetRequiredStationLevel_Patch
		{
			private static void Postfix(Recipe __instance, ref int __result)
			{
				if (!Plugin.RequireStationLevel.Value && (Object)(object)__instance == (Object)(object)_recipe)
				{
					__result = Mathf.Max(1, __instance.m_minStationLevel);
				}
			}
		}

		private const string ItemName = "Spyglass";

		private const int MaxLevel = 6;

		private static readonly Cost[] Costs = new Cost[12]
		{
			new Cost(new int[6] { 5, 0, 0, 0, 0, 0 }, "Copper"),
			new Cost(new int[6] { 10, 0, 0, 0, 0, 0 }, "Resin"),
			new Cost(new int[6] { 0, 8, 0, 0, 0, 0 }, "Iron"),
			new Cost(new int[6] { 0, 8, 0, 0, 0, 0 }, "Guck"),
			new Cost(new int[6] { 0, 0, 8, 0, 0, 0 }, "Silver"),
			new Cost(new int[6] { 0, 0, 6, 0, 0, 0 }, "Crystal"),
			new Cost(new int[6] { 0, 0, 0, 8, 0, 0 }, "BlackMetal"),
			new Cost(new int[6] { 0, 0, 0, 10, 0, 0 }, "Tar"),
			new Cost(new int[6] { 0, 0, 0, 0, 8, 0 }, "Carapace"),
			new Cost(new int[6] { 0, 0, 0, 0, 10, 0 }, "YggdrasilWood"),
			new Cost(new int[6] { 0, 0, 0, 0, 0, 8 }, "FlametalNew", "Flametal"),
			new Cost(new int[6] { 0, 0, 0, 0, 0, 10 }, "CharredBone")
		};

		private static readonly Dictionary<Requirement, int[]> Schedules = new Dictionary<Requirement, int[]>();

		private static Recipe _recipe;

		private static readonly FieldRef<Humanoid, ItemData> UtilityItem = AccessTools.FieldRefAccess<Humanoid, ItemData>("m_utilityItem");

		private static float _normalRadius = -1f;

		private static float _nextTick;

		private static Sprite MakeIcon()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			//IL_00a1: 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_00a7: 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_00b2: 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_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: 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_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: 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_0190: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(64, 64, (TextureFormat)4, false)
			{
				wrapMode = (TextureWrapMode)1
			};
			Color val2 = default(Color);
			((Color)(ref val2))..ctor(0f, 0f, 0f, 0f);
			Color val3 = default(Color);
			((Color)(ref val3))..ctor(0.85f, 0.65f, 0.25f);
			Color val4 = default(Color);
			((Color)(ref val4))..ctor(0.45f, 0.3f, 0.12f);
			Color val5 = default(Color);
			((Color)(ref val5))..ctor(0.12f, 0.08f, 0.04f);
			Color val6 = default(Color);
			((Color)(ref val6))..ctor(0.55f, 0.8f, 0.9f);
			Vector2 val7 = default(Vector2);
			((Vector2)(ref val7))..ctor(10f, 12f);
			Vector2 val8 = new Vector2(54f, 52f);
			Vector2 val9 = val8 - val7;
			Vector2 normalized = ((Vector2)(ref val9)).normalized;
			val9 = val8 - val7;
			float magnitude = ((Vector2)(ref val9)).magnitude;
			for (int i = 0; i < 64; i++)
			{
				for (int j = 0; j < 64; j++)
				{
					Vector2 val10 = new Vector2((float)j, (float)i) - val7;
					float num = Vector2.Dot(val10, normalized) / magnitude;
					float num2 = Mathf.Abs(val10.x * normalized.y - val10.y * normalized.x);
					Color val11 = val2;
					if (num >= 0f && num <= 1f)
					{
						float num3 = Mathf.Lerp(4f, 8f, num);
						bool flag = (num > 0.3f && num < 0.34f) || (num > 0.64f && num < 0.68f);
						if (num2 <= num3 - 1.5f)
						{
							val11 = (flag ? val4 : ((num > 0.95f) ? val6 : val3));
						}
						else if (num2 <= num3)
						{
							val11 = val5;
						}
					}
					val.SetPixel(j, i, val11);
				}
			}
			val.Apply();
			return Sprite.Create(val, new Rect(0f, 0f, 64f, 64f), new Vector2(0.5f, 0.5f));
		}

		internal static void Create()
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Expected O, but got Unknown
			PrefabManager.OnVanillaPrefabsAvailable -= Create;
			ItemConfig val = new ItemConfig();
			val.Name = "Spyglass";
			val.Description = "Wear it in the utility slot to reveal more of the map around you. Upgrade it with materials from each biome.";
			val.CraftingStation = CraftingStations.Forge;
			val.MinStationLevel = 1;
			val.Icons = (Sprite[])(object)new Sprite[1] { MakeIcon() };
			val.Amount = 1;
			ItemConfig val2 = val;
			val2.AddRequirement("Copper", 5, 0);
			val2.AddRequirement("Resin", 10, 0);
			CustomItem val3 = new CustomItem("Spyglass", "Wishbone", val2);
			SharedData shared = val3.ItemDrop.m_itemData.m_shared;
			shared.m_maxQuality = 6;
			shared.m_equipStatusEffect = null;
			shared.m_useDurability = false;
			ItemManager.Instance.AddItem(val3);
		}

		internal static void ApplyRecipe()
		{
			//IL_011c: 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)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Expected O, but got Unknown
			ObjectDB instance = ObjectDB.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			foreach (Recipe recipe in instance.m_recipes)
			{
				if ((Object)(object)recipe == (Object)null || (Object)(object)recipe.m_item == (Object)null || ((Object)recipe.m_item).name != "Spyglass")
				{
					continue;
				}
				if (recipe.m_resources.Length != 0 && Schedules.ContainsKey(recipe.m_resources[0]))
				{
					_recipe = recipe;
					continue;
				}
				List<Requirement> list = new List<Requirement>();
				Cost[] costs = Costs;
				foreach (Cost cost in costs)
				{
					ItemDrop val = null;
					string[] names = cost.Names;
					foreach (string text in names)
					{
						GameObject itemPrefab = instance.GetItemPrefab(text);
						val = (Object.op_Implicit((Object)(object)itemPrefab) ? itemPrefab.GetComponent<ItemDrop>() : null);
						if ((Object)(object)val != (Object)null)
						{
							break;
						}
					}
					if ((Object)(object)val == (Object)null)
					{
						Plugin.Log.LogWarning((object)("Material not found, skipped: " + cost.Names[0]));
						continue;
					}
					Requirement val2 = new Requirement
					{
						m_resItem = val,
						m_amount = cost.PerLevel[0],
						m_amountPerLevel = 1,
						m_recover = true
					};
					list.Add(val2);
					Schedules[val2] = (int[])cost.PerLevel.Clone();
				}
				recipe.m_resources = list.ToArray();
				_recipe = recipe;
				Plugin.Log.LogInfo((object)$"Spyglass recipe ready: {list.Count} materials across {6} levels");
			}
		}

		internal static void TickRadius()
		{
			if (Time.unscaledTime < _nextTick)
			{
				return;
			}
			_nextTick = Time.unscaledTime + 0.5f;
			Minimap instance = Minimap.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			if (_normalRadius < 0f)
			{
				_normalRadius = instance.m_exploreRadius;
			}
			int num = 0;
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer != (Object)null)
			{
				foreach (ItemData equippedItem in ((Humanoid)localPlayer).GetInventory().GetEquippedItems())
				{
					if ((Object)(object)equippedItem.m_dropPrefab != (Object)null && ((Object)equippedItem.m_dropPrefab).name == "Spyglass")
					{
						num = equippedItem.m_quality;
						break;
					}
				}
			}
			instance.m_exploreRadius = _normalRadius + (float)(num * Plugin.RadiusPerLevel.Value);
		}
	}
}