Decompiled source of DoubleEnergyCrystals v1.0.1

BepInEx\plugins\DoubleEnergyCrystals\DoubleEnergyCrystals.dll

Decompiled 42 minutes 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 Microsoft.CodeAnalysis;
using UnityEngine;

[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("DoubleEnergyCrystals")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("DoubleEnergyCrystals")]
[assembly: AssemblyTitle("DoubleEnergyCrystals")]
[assembly: AssemblyVersion("1.0.1.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 FanTeam.DoubleEnergyCrystals
{
	[BepInPlugin("fanteam.doubleenergycrystals", "Double Energy Crystals", "1.0.0")]
	public sealed class DoubleEnergyCrystalsPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "fanteam.doubleenergycrystals";

		public const string PluginName = "Double Energy Crystals";

		public const string PluginVersion = "1.0.0";

		internal static DoubleEnergyCrystalsPlugin Instance;

		internal static ConfigEntry<float> CrystalOffset;

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			CrystalOffset = ((BaseUnityPlugin)this).Config.Bind<float>("Shop", "DuplicateOffset", 0.32f, "Distance in meters between an original Power Crystal and its additional copy on the same display.");
			try
			{
				GameApi.Initialize();
				_harmony = new Harmony("fanteam.doubleenergycrystals");
				_harmony.PatchAll(typeof(DoubleEnergyCrystalsPlugin).Assembly);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded. Every Power Crystal selected for the shop is duplicated once.");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Initialization failed: " + ex));
			}
		}
	}
	internal static class GameApi
	{
		internal static Type PunManagerType;

		internal static Type ItemType;

		internal static Type ItemVolumeType;

		internal static Type ItemSpawnType;

		internal static FieldInfo ItemDefinitionVolumeField;

		internal static FieldInfo DisplayVolumeKindField;

		internal static FieldInfo ItemPrefabField;

		internal static FieldInfo ItemRotationOffsetField;

		internal static FieldInfo SpawnPrefabField;

		internal static FieldInfo SpawnPositionField;

		internal static FieldInfo SpawnRotationField;

		internal static MethodInfo SpawnShopItemMethod;

		internal static MethodInfo ItemSpawnApplyMethod;

		internal static object PowerCrystalVolume;

		internal static void Initialize()
		{
			PunManagerType = AccessTools.TypeByName("PunManager");
			ItemType = AccessTools.TypeByName("Item");
			ItemVolumeType = AccessTools.TypeByName("ItemVolume");
			ItemSpawnType = AccessTools.TypeByName("ItemSpawn");
			Require(PunManagerType, "PunManager");
			Require(ItemType, "Item");
			Require(ItemVolumeType, "ItemVolume");
			Require(ItemSpawnType, "ItemSpawn");
			ItemDefinitionVolumeField = RequireField(ItemType, "itemVolume");
			DisplayVolumeKindField = RequireField(ItemVolumeType, "itemVolume");
			ItemPrefabField = RequireField(ItemType, "prefab");
			ItemRotationOffsetField = RequireField(ItemType, "spawnRotationOffset");
			SpawnPrefabField = RequireField(ItemSpawnType, "prefab");
			SpawnPositionField = RequireField(ItemSpawnType, "position");
			SpawnRotationField = RequireField(ItemSpawnType, "rotation");
			PowerCrystalVolume = Enum.Parse(ItemDefinitionVolumeField.FieldType, "power_crystal");
			Type type = typeof(List<>).MakeGenericType(ItemType);
			Type type2 = typeof(List<>).MakeGenericType(ItemSpawnType);
			Type[] types = new Type[5]
			{
				ItemVolumeType,
				type,
				typeof(int).MakeByRefType(),
				typeof(bool),
				type2
			};
			SpawnShopItemMethod = PunManagerType.GetMethod("SpawnShopItem", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, types, null);
			ItemSpawnApplyMethod = PunManagerType.GetMethod("ItemSpawnApply", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[1] { ItemSpawnType }, null);
			if (SpawnShopItemMethod == null)
			{
				throw new MissingMethodException("PunManager.SpawnShopItem(ItemVolume, List<Item>, ref int, bool, List<ItemSpawn>)");
			}
			if (ItemSpawnApplyMethod == null)
			{
				throw new MissingMethodException("PunManager.ItemSpawnApply(ItemSpawn)");
			}
		}

		internal static object CreateSpawn(object item, Component displayVolume)
		{
			//IL_0022: 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_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			object obj = Activator.CreateInstance(ItemSpawnType);
			object value = ItemPrefabField.GetValue(item);
			Quaternion val = (Quaternion)ItemRotationOffsetField.GetValue(item);
			Transform transform = displayVolume.transform;
			SpawnPrefabField.SetValue(obj, value);
			SpawnPositionField.SetValue(obj, transform.position + transform.right * DoubleEnergyCrystalsPlugin.CrystalOffset.Value);
			SpawnRotationField.SetValue(obj, transform.rotation * val);
			return obj;
		}

		private static FieldInfo RequireField(Type type, string name)
		{
			FieldInfo fieldInfo = AccessTools.Field(type, name);
			if (fieldInfo == null)
			{
				throw new MissingFieldException(type.FullName, name);
			}
			return fieldInfo;
		}

		private static void Require(Type type, string name)
		{
			if (type == null)
			{
				throw new TypeLoadException("Could not find game type: " + name);
			}
		}
	}
	[HarmonyPatch]
	internal static class SpawnShopItemPatch
	{
		private static MethodBase TargetMethod()
		{
			return GameApi.SpawnShopItemMethod;
		}

		[HarmonyPrefix]
		private static void Prefix(object[] __args, out object __state)
		{
			__state = null;
			try
			{
				if (__args == null || __args.Length < 2 || !(__args[1] is IList list))
				{
					return;
				}
				object obj = __args[0];
				if (obj == null)
				{
					return;
				}
				object value = GameApi.DisplayVolumeKindField.GetValue(obj);
				for (int num = list.Count - 1; num >= 0; num--)
				{
					object obj2 = list[num];
					if (obj2 != null && object.Equals(GameApi.ItemDefinitionVolumeField.GetValue(obj2), value))
					{
						__state = obj2;
						break;
					}
				}
			}
			catch (Exception ex)
			{
				DoubleEnergyCrystalsPlugin.Log.LogError((object)("Could not inspect shop item selection: " + ex));
			}
		}

		[HarmonyPostfix]
		private static void Postfix(object[] __args, object __instance, object __state, bool __result)
		{
			if (!__result || __state == null || __args == null || __args.Length < 5)
			{
				return;
			}
			try
			{
				if (!object.Equals(GameApi.ItemDefinitionVolumeField.GetValue(__state), GameApi.PowerCrystalVolume))
				{
					return;
				}
				object obj = __args[0];
				Component val = (Component)((obj is Component) ? obj : null);
				if (val != null)
				{
					object obj2 = GameApi.CreateSpawn(__state, val);
					if (__args[4] is IList list)
					{
						list.Add(obj2);
					}
					else
					{
						GameApi.ItemSpawnApplyMethod.Invoke(__instance, new object[1] { obj2 });
					}
					DoubleEnergyCrystalsPlugin.Log.LogDebug((object)"Duplicated a shop Power Crystal.");
				}
			}
			catch (Exception ex)
			{
				DoubleEnergyCrystalsPlugin.Log.LogError((object)("Could not create the additional Power Crystal: " + ex));
			}
		}
	}
}