Decompiled source of GlasVitrineRegal v1.0.0

GlasVitrineRegal.dll

Decompiled a week ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace GlasVitrineRegal;

[BepInPlugin("agentf.GlasVitrineRegal", "GlasVitrineRegal", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "agentf.GlasVitrineRegal";

	public const string NAME = "GlasVitrineRegal";

	public const string VERSION = "1.0.0";

	internal static ManualLogSource Log;

	internal static FieldInfo CustomItemShelfField;

	internal static MethodInfo SpawnMethod;

	internal static MethodInfo SetParentMethod;

	internal static Type ShopManagerPatchType;

	internal static readonly string[] TargetNames = new string[1] { "Drink Shelves (Weird)" };

	private void Awake()
	{
		//IL_019f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Expected O, but got Unknown
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: Expected O, but got Unknown
		Log = base.Logger;
		Log.LogInfo((object)"GlasVitrineRegal 1.0.0 starting...");
		Assembly assembly = null;
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly2 in assemblies)
		{
			if (assembly2.GetName().Name == "MoreShopItems")
			{
				assembly = assembly2;
				break;
			}
		}
		if (assembly == null)
		{
			Log.LogError((object)"MoreShopItems assembly not loaded - aborting.");
			return;
		}
		Type type = assembly.GetType("MoreShopItems.Plugin");
		ShopManagerPatchType = assembly.GetType("MoreShopItems.ShopManagerPatch");
		Type type2 = assembly.GetType("MoreShopItems.ShelfSpawner") ?? assembly.GetType("ShelfSpawner");
		if (type == null || ShopManagerPatchType == null)
		{
			Log.LogError((object)"Required MoreShopItems types not found - aborting.");
			return;
		}
		CustomItemShelfField = type.GetField("CustomItemShelf", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		if (CustomItemShelfField == null)
		{
			Log.LogError((object)"MoreShopItems.Plugin.CustomItemShelf field not found.");
			return;
		}
		if (type2 != null)
		{
			SpawnMethod = type2.GetMethod("Spawn", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			SetParentMethod = type2.GetMethod("SetParent", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		}
		if (SpawnMethod == null)
		{
			Log.LogWarning((object)"ShelfSpawner.Spawn not found - using fallback Instantiate.");
		}
		MethodInfo method = ShopManagerPatchType.GetMethod("SpawnShelf", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		if (method == null)
		{
			Log.LogError((object)"ShopManagerPatch.SpawnShelf not found - aborting.");
			return;
		}
		Harmony val = new Harmony("agentf.GlasVitrineRegal");
		MethodInfo method2 = typeof(VitrinePostfix).GetMethod("AfterSpawnShelf", BindingFlags.Static | BindingFlags.Public);
		MethodInfo methodInfo = method;
		HarmonyMethod val2 = new HarmonyMethod(method2);
		val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null);
		Log.LogInfo((object)"Hooked MoreShopItems.ShopManagerPatch.SpawnShelf - vitrines will be replaced.");
	}

	internal static GameObject GetCustomItemShelf()
	{
		return (GameObject)((!(CustomItemShelfField == null)) ? /*isinst with value type is only supported in some contexts*/: null);
	}
}
public static class VitrinePostfix
{
	public static void AfterSpawnShelf()
	{
		//IL_00a4: 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_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_013a: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: 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_0117: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (RunManager.instance == null || RunManager.instance.levelCurrent == null || !SemiFunc.IsLevelShop(RunManager.instance.levelCurrent))
			{
				return;
			}
			GameObject customItemShelf = Plugin.GetCustomItemShelf();
			if ((Object)(object)customItemShelf == (Object)null)
			{
				Plugin.Log.LogWarning((object)"CustomItemShelf prefab is null - skipping.");
				return;
			}
			int num = 0;
			string[] targetNames = Plugin.TargetNames;
			foreach (string text in targetNames)
			{
				GameObject val = GameObject.Find(text);
				if ((Object)(object)val == (Object)null || !val.activeSelf)
				{
					continue;
				}
				Vector3 position = val.transform.position;
				Quaternion rotation = val.transform.rotation;
				Transform parent = val.transform.parent;
				GameObject val2 = null;
				if (SemiFunc.IsMultiplayer())
				{
					if (!SemiFunc.IsMasterClient())
					{
						val.SetActive(false);
						num++;
						continue;
					}
					if (Plugin.SpawnMethod != null)
					{
						object? obj = Plugin.SpawnMethod.Invoke(null, new object[4] { position, rotation, parent, text });
						val2 = (GameObject)((obj is GameObject) ? obj : null);
					}
				}
				else
				{
					Object obj2 = Object.Instantiate((Object)(object)customItemShelf, position, rotation, parent);
					val2 = (GameObject)(object)((obj2 is GameObject) ? obj2 : null);
				}
				if ((Object)(object)val2 != (Object)null && Plugin.SetParentMethod != null)
				{
					try
					{
						Plugin.SetParentMethod.Invoke(null, new object[2] { parent, val2 });
					}
					catch
					{
					}
				}
				val.SetActive(false);
				num++;
				Plugin.Log.LogInfo((object)("Replaced vitrine '" + text + "' with custom shelf."));
			}
			if (num > 0)
			{
				Plugin.Log.LogInfo((object)("GlasVitrineRegal: replaced " + num + " vitrine(s)."));
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)("GlasVitrineRegal postfix failed: " + ex));
		}
	}
}