Decompiled source of Broken Ruunes Invisipacks v1.0.4

plugins/Invisipacks.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using SideLoader;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("brokenruune.invisipacks", "Broken Ruune's Invisipacks", "1.0.4")]
public sealed class Invisipacks : BaseUnityPlugin
{
	private const string PluginId = "brokenruune.invisipacks";

	private const int AdventurerId = 5300000;

	private const float LanternMountHeight = 0.579f;

	private const string BlacksmithId = "3Rx_R0XDLUmYaNWm66SVCQ";

	private const string TokugaNormalPriceId = "EYhBqM653UGhDd5kcdMFXg";

	private const string HowardBrockId = "Zdg-qTDRa0-qOzzNSbtvzg";

	private const string SalDumasId = "QyJwLKySB0epxpEG3EBICQ";

	private static readonly Dictionary<int, int> Prices = new Dictionary<int, int>
	{
		{ -847481, 500 },
		{ -847482, 1500 },
		{ -847483, 2500 }
	};

	private static ManualLogSource log;

	private static ConfigEntry<string> seeded;

	private static HashSet<string> seededShops;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Expected O, but got Unknown
		log = ((BaseUnityPlugin)this).Logger;
		seeded = ((BaseUnityPlugin)this).Config.Bind<string>("Blacksmiths", "SeededStock", "", "Saves and shops that received initial Invisipack stock.");
		seededShops = new HashSet<string>(seeded.Value.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries), StringComparer.Ordinal);
		harmony = new Harmony("brokenruune.invisipacks");
		harmony.Patch((MethodBase)AccessTools.Method(typeof(SL_Bag), "ApplyToItem", new Type[1] { typeof(Item) }, (Type[])null), (HarmonyMethod)null, Patch("RefreshBagCapacity"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(AccessTools.TypeByName("SideLoader.CustomItemVisuals"), "CloneAndSetVisuals", (Type[])null, (Type[])null), (HarmonyMethod)null, Patch("AfterClone"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(Item), "LinkVisuals", new Type[2]
		{
			typeof(ItemVisual),
			typeof(bool)
		}, (Type[])null), (HarmonyMethod)null, Patch("AfterLink"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(Item), "SetDisplayedOnBag", new Type[2]
		{
			typeof(bool),
			typeof(Transform)
		}, (Type[])null), (HarmonyMethod)null, Patch("AfterDisplayOnBag"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(ItemVisual), "SetLinkedItem", new Type[1] { typeof(Item) }, (Type[])null), (HarmonyMethod)null, Patch("AfterVisualLinked"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(VisualSlot), "PositionVisuals", new Type[1] { typeof(Item) }, (Type[])null), (HarmonyMethod)null, Patch("AfterEquip"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(ResourcesPrefabManager), "GetItemIcon", new Type[1] { typeof(Item) }, (Type[])null), Patch("UseAdventurerIcon"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(Item), "GetBuyValue", new Type[3]
		{
			typeof(Character),
			typeof(Merchant),
			typeof(bool)
		}, (Type[])null), (HarmonyMethod)null, Patch("SetPrice"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(MerchantPouch), "RefreshInventory", new Type[1] { typeof(Dropable) }, (Type[])null), Patch("BeforeRestock"), Patch("AfterRestock"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(Merchant), "OnReceiveConfirmStartShopping", new Type[2]
		{
			typeof(bool),
			typeof(Character)
		}, (Type[])null), Patch("SeedShop"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		log.LogInfo((object)"Invisipacks enabled.");
	}

	private static HarmonyMethod Patch(string method)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		HarmonyMethod val = new HarmonyMethod(typeof(Invisipacks), method, (Type[])null);
		val.priority = 0;
		return val;
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}

	private static void RefreshBagCapacity(Item __0)
	{
		Bag val = (Bag)(object)((__0 is Bag) ? __0 : null);
		if (Object.op_Implicit((Object)(object)val) && Prices.ContainsKey(((Item)val).ItemID))
		{
			((Item)val).InitCachedInfos();
			log.LogInfo((object)(((Item)val).Name + " capacity " + val.BagCapacity + "."));
		}
	}

	private static bool UseAdventurerIcon(ResourcesPrefabManager __instance, Item __0, ref Sprite __result)
	{
		if (!Object.op_Implicit((Object)(object)__0) || !Prices.ContainsKey(__0.ItemID))
		{
			return true;
		}
		__result = __instance.GetItemIcon(5300000);
		return !Object.op_Implicit((Object)(object)__result);
	}

	private static void AfterClone(Item __0, GameObject __1, GameObject __result)
	{
		if (Object.op_Implicit((Object)(object)__0) && Prices.ContainsKey(__0.ItemID) && Object.op_Implicit((Object)(object)__result) && !((Object)(object)__result == (Object)(object)__1))
		{
			((Object)__result).name = __0.ItemID + "_Invisipack" + (((Object)__result).name.Contains("_e_v") ? "_e_v" : "_v");
			ConfigurePackVisual(__result.GetComponent<ItemVisual>());
		}
	}

	private static void AfterEquip(VisualSlot __instance, Item __0)
	{
		if (Object.op_Implicit((Object)(object)__0) && Prices.ContainsKey(__0.ItemID) && Object.op_Implicit((Object)(object)__instance.CurrentVisual) && __instance.CurrentVisual.GetItemIDFromName() == __0.ItemID)
		{
			ConfigurePackVisual(__instance.CurrentVisual);
		}
	}

	private static void ConfigurePackVisual(ItemVisual visual)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)visual))
		{
			return;
		}
		Transform val = ((Component)visual).transform.Find("LanternSlotAnchor");
		if (Object.op_Implicit((Object)(object)val))
		{
			Vector3 localPosition = val.localPosition;
			localPosition.x = 0f;
			localPosition.y = 0.579f;
			val.localPosition = localPosition;
		}
		Transform val2 = ((Component)visual).transform.Find("mdl_itm_packAdventurer");
		if (Object.op_Implicit((Object)(object)val2))
		{
			Renderer[] componentsInChildren = ((Component)val2).GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val3 in componentsInChildren)
			{
				val3.forceRenderingOff = true;
			}
		}
	}

	private static void AfterLink(Item __instance, Transform ___m_bagSlotVisualHolder)
	{
		if (Prices.ContainsKey(__instance.ItemID))
		{
			ConfigurePackVisual(__instance.CurrentVisual);
		}
		ApplyLanternVisibility(__instance, __instance.DisplayedOnBag ? ___m_bagSlotVisualHolder : null);
	}

	private static void AfterDisplayOnBag(Item __instance, bool __0, Transform __1)
	{
		ApplyLanternVisibility(__instance, __0 ? __1 : null);
	}

	private static void AfterVisualLinked(ItemVisual __instance, Item __0)
	{
		if (!Object.op_Implicit((Object)(object)__0))
		{
			InvisipackLanternState component = ((Component)__instance).GetComponent<InvisipackLanternState>();
			if (Object.op_Implicit((Object)(object)component))
			{
				component.Restore();
			}
		}
	}

	private static void ApplyLanternVisibility(Item item, Transform holder)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Invalid comparison between Unknown and I4
		if ((int)item.BagCategorySlot != 3 || !Object.op_Implicit((Object)(object)item.CurrentVisual))
		{
			return;
		}
		bool flag = false;
		Transform val = holder;
		while (Object.op_Implicit((Object)(object)val))
		{
			ItemVisual component = ((Component)val).GetComponent<ItemVisual>();
			if (Object.op_Implicit((Object)(object)component))
			{
				flag = Prices.ContainsKey(component.GetItemIDFromName());
				break;
			}
			val = val.parent;
		}
		InvisipackLanternState invisipackLanternState = ((Component)item.CurrentVisual).GetComponent<InvisipackLanternState>();
		if (flag)
		{
			if (!Object.op_Implicit((Object)(object)invisipackLanternState))
			{
				invisipackLanternState = ((Component)item.CurrentVisual).gameObject.AddComponent<InvisipackLanternState>();
			}
			invisipackLanternState.Hide(item);
		}
		else if (Object.op_Implicit((Object)(object)invisipackLanternState))
		{
			invisipackLanternState.Restore();
		}
	}

	private static string Blacksmith(Merchant merchant)
	{
		//IL_000b: 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_0021: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)merchant))
		{
			return null;
		}
		string text = UID.op_Implicit(merchant.HolderUID);
		Scene scene = ((Component)merchant).gameObject.scene;
		switch (((Scene)(ref scene)).name)
		{
		case "CierzoNewTerrain":
			if (!(text == "3Rx_R0XDLUmYaNWm66SVCQ"))
			{
				return null;
			}
			return "Loud-Hammer";
		case "Berg":
			if (!(text == "3Rx_R0XDLUmYaNWm66SVCQ"))
			{
				return null;
			}
			return "Quikiza";
		case "Levant":
			if (!(text == "3Rx_R0XDLUmYaNWm66SVCQ") && !(text == "EYhBqM653UGhDd5kcdMFXg"))
			{
				return null;
			}
			return "Tokuga";
		case "Monsoon":
			if (!(text == "3Rx_R0XDLUmYaNWm66SVCQ"))
			{
				return null;
			}
			return "Vyzyrinthrix";
		case "Harmattan":
			if (!(text == "Zdg-qTDRa0-qOzzNSbtvzg"))
			{
				return null;
			}
			return "Howard Brock";
		case "NewSirocco":
		{
			Transform val = ((Component)merchant).transform;
			while (Object.op_Implicit((Object)(object)val))
			{
				if (((Object)val).name == "BlacksmithMerchant_Dialog")
				{
					return "Sal Dumas";
				}
				val = val.parent;
			}
			if (!(text == "QyJwLKySB0epxpEG3EBICQ"))
			{
				return null;
			}
			return "Sal Dumas";
		}
		default:
			return null;
		}
	}

	private static void SetPrice(Item __instance, Character __0, Merchant __1, bool __2, ref int __result)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		if (Prices.TryGetValue(__instance.ItemID, out var value) && Blacksmith(__1) != null && (__2 || !Object.op_Implicit((Object)(object)__0) || !(EnvironmentConditions.GameTime < __instance.RebuyLimitTime) || !(__instance.PreviousOwnerUID == UID.op_Implicit(__0.UID))))
		{
			__result = value;
		}
	}

	private static void BeforeRestock(MerchantPouch __instance, double ___m_nextRefreshTime, out string __state)
	{
		__state = ((!PhotonNetwork.isNonMasterClientInRoom && EnvironmentConditions.GameTime >= ___m_nextRefreshTime) ? Blacksmith(__instance.Merchant) : null);
	}

	private static void AfterRestock(MerchantPouch __instance, string __state)
	{
		if (__state != null && EnsureStock(__instance))
		{
			log.LogInfo((object)(__state + " stocked all three Invisipacks."));
		}
	}

	private static bool EnsureStock(MerchantPouch pouch)
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Expected O, but got Unknown
		bool flag = true;
		foreach (int key in Prices.Keys)
		{
			if (!HasStock(pouch, key) && Object.op_Implicit((Object)(object)ResourcesPrefabManager.Instance.GetItemPrefab(key)))
			{
				SL_ItemDrop val = new SL_ItemDrop();
				val.DroppedItemID = key;
				val.MinQty = 1;
				val.MaxQty = 1;
				val.GenerateDrop(((Component)pouch).transform);
			}
			flag &= HasStock(pouch, key);
		}
		return flag;
	}

	private static bool HasStock(MerchantPouch pouch, int id)
	{
		return ((ItemContainer)pouch).GetItemsFromID(id).Exists((Item item) => Object.op_Implicit((Object)(object)item) && !item.IsPendingDestroy);
	}

	private static void SeedShop(Merchant __instance, bool __0, Character __1)
	{
		//IL_004d: 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_0082: Unknown result type (might be due to invalid IL or missing references)
		if (!__0 || !Object.op_Implicit((Object)(object)__1) || PhotonNetwork.isNonMasterClientInRoom || Blacksmith(__instance) == null)
		{
			return;
		}
		ItemContainer merchantPouch = __instance.MerchantPouch;
		MerchantPouch val = (MerchantPouch)(object)((merchantPouch is MerchantPouch) ? merchantPouch : null);
		Character worldHostCharacter = CharacterManager.Instance.GetWorldHostCharacter();
		if (Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)worldHostCharacter))
		{
			string[] array = new string[5]
			{
				UID.op_Implicit(worldHostCharacter.UID),
				":",
				null,
				null,
				null
			};
			Scene scene = ((Component)__instance).gameObject.scene;
			array[2] = ((Scene)(ref scene)).name;
			array[3] = ":";
			array[4] = UID.op_Implicit(__instance.HolderUID);
			string item = string.Concat(array);
			if (!seededShops.Contains(item) && EnsureStock(val))
			{
				seededShops.Add(item);
				seeded.Value = string.Join(",", seededShops);
				log.LogInfo((object)(Blacksmith(__instance) + " received all three Invisipacks."));
			}
		}
	}
}
[DefaultExecutionOrder(1000)]
public sealed class InvisipackLanternState : MonoBehaviour
{
	private readonly HashSet<Renderer> hidden = new HashSet<Renderer>();

	private readonly Dictionary<Transform, Vector3> lightOrigins = new Dictionary<Transform, Vector3>();

	private Item lantern;

	private bool moved;

	internal void Hide(Item item)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		lantern = item;
		Light[] componentsInChildren = ((Component)this).GetComponentsInChildren<Light>(true);
		foreach (Light val in componentsInChildren)
		{
			if (!lightOrigins.ContainsKey(((Component)val).transform))
			{
				lightOrigins.Add(((Component)val).transform, ((Component)val).transform.localPosition);
			}
		}
		Renderer[] componentsInChildren2 = ((Component)this).GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val2 in componentsInChildren2)
		{
			if (!val2.forceRenderingOff)
			{
				hidden.Add(val2);
				val2.forceRenderingOff = true;
			}
		}
	}

	private void LateUpdate()
	{
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: 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_008d: 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_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: 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)
		if (!Object.op_Implicit((Object)(object)lantern) || !lantern.DisplayedOnBag || !Object.op_Implicit((Object)(object)lantern.ParentContainer))
		{
			RestoreLightPositions();
			return;
		}
		Bag refBag = lantern.ParentContainer.RefBag;
		Character val = ((Object.op_Implicit((Object)(object)refBag) && ((Item)refBag).IsEquipped) ? ((EffectSynchronizer)refBag).OwnerCharacter : null);
		if (!Object.op_Implicit((Object)(object)val))
		{
			RestoreLightPositions();
			return;
		}
		Vector3 position = val.CenterPosition + Vector3.up * 0.3f + ((Component)val).transform.forward * 0.55f;
		foreach (Transform key in lightOrigins.Keys)
		{
			if (Object.op_Implicit((Object)(object)key))
			{
				key.position = position;
			}
		}
		moved = true;
	}

	private void RestoreLightPositions()
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		if (!moved)
		{
			return;
		}
		foreach (KeyValuePair<Transform, Vector3> lightOrigin in lightOrigins)
		{
			if (Object.op_Implicit((Object)(object)lightOrigin.Key))
			{
				lightOrigin.Key.localPosition = lightOrigin.Value;
			}
		}
		moved = false;
	}

	private void OnDisable()
	{
		RestoreLightPositions();
	}

	internal void Restore()
	{
		foreach (Renderer item in hidden)
		{
			if (Object.op_Implicit((Object)(object)item))
			{
				item.forceRenderingOff = false;
			}
		}
		hidden.Clear();
		RestoreLightPositions();
		lightOrigins.Clear();
		lantern = null;
	}
}