Decompiled source of BigNuke Fix v1.0.0

EmpressBigNukeValuableCompatPatch.dll

Decompiled 7 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
using Photon.Pun;
using REPOLib;
using REPOLib.Modules;
using REPOLib.Objects.Sdk;
using UnityEngine;
using UnityEngine.SceneManagement;
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("Empress")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0")]
[assembly: AssemblyProduct("EmpressBigNukeValuableCompatPatch")]
[assembly: AssemblyTitle("EmpressBigNukeValuableCompatPatch")]
[assembly: AssemblyVersion("1.3.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 EmpressBigNukeValuableCompatPatch
{
	[BepInPlugin("Empress.BigNuke.ValuableCompatPatch", "Empress BigNuke Valuable Compat Patch", "1.3.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class EmpressBigNukeValuableCompatPatchPlugin : BaseUnityPlugin
	{
		internal const string PluginGuid = "Empress.BigNuke.ValuableCompatPatch";

		internal const string PluginName = "Empress BigNuke Valuable Compat Patch";

		internal const string PluginVersion = "1.3.0";

		internal const string DependencyFolderName = "TitanVortex-BigNuke";

		private static readonly string[] TargetTokens = new string[1] { "Valuable Nuke" };

		private static readonly string[] DefaultValuablePresets = new string[0];

		private static EmpressBigNukeValuableCompatPatchPlugin? Instance;

		private static readonly Dictionary<string, MemberInfo?> MemberCache = new Dictionary<string, MemberInfo>(StringComparer.Ordinal);

		private readonly Harmony _harmony = new Harmony("Empress.BigNuke.ValuableCompatPatch");

		private readonly HashSet<int> _loggedBatterySkips = new HashSet<int>();

		private readonly HashSet<string> _registeredValuableNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		private readonly HashSet<int> _preparedRootIds = new HashSet<int>();

		private readonly HashSet<int> _preparedGunIds = new HashSet<int>();

		private readonly HashSet<int> _preparedBatteryIds = new HashSet<int>();

		private readonly Dictionary<int, bool> _targetRootCache = new Dictionary<int, bool>();

		private void Awake()
		{
			TryPatchInstalledDependency();
			HideBepInExManagerObject();
			Instance = this;
			BundleLoader.OnAllBundlesLoaded += OnAllBundlesLoaded;
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Empress BigNuke Valuable Compat Patch is ready for BigNuke.");
		}

		private static void HideBepInExManagerObject()
		{
			if (!((Object)(object)Chainloader.ManagerObject == (Object)null))
			{
				((Object)Chainloader.ManagerObject).hideFlags = (HideFlags)61;
				Object.DontDestroyOnLoad((Object)(object)Chainloader.ManagerObject);
			}
		}

		private void OnDestroy()
		{
			BundleLoader.OnAllBundlesLoaded -= OnAllBundlesLoaded;
			_harmony.UnpatchSelf();
			if (Instance == this)
			{
				Instance = null;
			}
		}

		private void TryPatchInstalledDependency()
		{
			if (string.IsNullOrWhiteSpace("TitanVortex-BigNuke"))
			{
				return;
			}
			string path = Path.Combine(Paths.PluginPath, "TitanVortex-BigNuke");
			if (!Directory.Exists(path))
			{
				return;
			}
			string text = FindRepoLibAssemblyPath();
			if (string.IsNullOrWhiteSpace(text) || !File.Exists(text))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Empress BigNuke Valuable Compat Patch could not find REPOLib.dll while checking TitanVortex-BigNuke.");
				return;
			}
			int num = 0;
			foreach (string item in Directory.EnumerateFiles(path, "*.dll", SearchOption.TopDirectoryOnly))
			{
				string fileName = Path.GetFileName(item);
				if (fileName.Equals("REPOLib.dll", StringComparison.OrdinalIgnoreCase) || fileName.StartsWith("Empress", StringComparison.OrdinalIgnoreCase))
				{
					continue;
				}
				try
				{
					if (TryPatchLegacyRepoLibDll(item, text, out var patchCount))
					{
						num++;
						((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0} patched {1} old REPOLib registration call(s) in {2}\\{3}.", "Empress BigNuke Valuable Compat Patch", patchCount, "TitanVortex-BigNuke", fileName));
					}
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Empress BigNuke Valuable Compat Patch could not patch TitanVortex-BigNuke\\" + fileName + ": " + ex.Message));
				}
			}
			if (num > 0)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Empress BigNuke Valuable Compat Patch finished patching the installed dependency folder TitanVortex-BigNuke.");
			}
		}

		private static string? FindRepoLibAssemblyPath()
		{
			string text = Path.Combine(Paths.PluginPath, "Zehs-REPOLib", "REPOLib.dll");
			if (File.Exists(text))
			{
				return text;
			}
			if (!Directory.Exists(Paths.PluginPath))
			{
				return null;
			}
			return Directory.EnumerateFiles(Paths.PluginPath, "REPOLib.dll", SearchOption.AllDirectories).FirstOrDefault();
		}

		private static bool TryPatchLegacyRepoLibDll(string dllPath, string repoLibAssemblyPath, out int patchCount)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Expected O, but got Unknown
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f8: Expected O, but got Unknown
			//IL_00eb: 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_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			patchCount = 0;
			DefaultAssemblyResolver val = new DefaultAssemblyResolver();
			foreach (string assemblySearchDirectory in GetAssemblySearchDirectories(dllPath, repoLibAssemblyPath))
			{
				((BaseAssemblyResolver)val).AddSearchDirectory(assemblySearchDirectory);
			}
			ReaderParameters val2 = new ReaderParameters
			{
				AssemblyResolver = (IAssemblyResolver)(object)val,
				InMemory = true,
				ReadSymbols = false,
				ReadingMode = (ReadingMode)1
			};
			AssemblyDefinition val3 = AssemblyDefinition.ReadAssembly(dllPath, val2);
			try
			{
				AssemblyDefinition val4 = AssemblyDefinition.ReadAssembly(repoLibAssemblyPath, val2);
				try
				{
					ModuleDefinition mainModule = val3.MainModule;
					ModuleDefinition mainModule2 = val4.MainModule;
					foreach (TypeDefinition item in EnumerateAllTypes((IEnumerable<TypeDefinition>)mainModule.Types))
					{
						Enumerator<MethodDefinition> enumerator3 = item.Methods.GetEnumerator();
						try
						{
							while (enumerator3.MoveNext())
							{
								MethodDefinition current2 = enumerator3.Current;
								if (!current2.HasBody)
								{
									continue;
								}
								Collection<Instruction> instructions = current2.Body.Instructions;
								for (int i = 0; i < instructions.Count; i++)
								{
									Instruction val5 = instructions[i];
									if (val5.OpCode != OpCodes.Call && val5.OpCode != OpCodes.Callvirt)
									{
										continue;
									}
									object operand = val5.Operand;
									MethodReference val6 = (MethodReference)((operand is MethodReference) ? operand : null);
									if (val6 != null && TryImportUpdatedRepoLibMethod(mainModule, mainModule2, val6, out MethodReference replacement))
									{
										val5.Operand = replacement;
										patchCount++;
										if (!NextInstructionIsPop(instructions, i))
										{
											instructions.Insert(i + 1, Instruction.Create(OpCodes.Pop));
											i++;
										}
									}
								}
							}
						}
						finally
						{
							((IDisposable)enumerator3).Dispose();
						}
					}
					if (patchCount == 0)
					{
						return false;
					}
					string text = dllPath + ".empressbackup";
					if (!File.Exists(text))
					{
						File.Copy(dllPath, text, overwrite: false);
					}
					string text2 = dllPath + ".empress.tmp";
					val3.Write(text2, new WriterParameters
					{
						WriteSymbols = false
					});
					File.Copy(text2, dllPath, overwrite: true);
					File.Delete(text2);
					return true;
				}
				finally
				{
					((IDisposable)val4)?.Dispose();
				}
			}
			finally
			{
				((IDisposable)val3)?.Dispose();
			}
		}

		private static IEnumerable<string> GetAssemblySearchDirectories(string dllPath, string repoLibAssemblyPath)
		{
			HashSet<string> seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
			string[] array = new string[4]
			{
				Path.GetDirectoryName(dllPath),
				Path.GetDirectoryName(repoLibAssemblyPath),
				Path.Combine(Paths.BepInExRootPath, "core"),
				Paths.PluginPath
			};
			foreach (string text in array)
			{
				if (!string.IsNullOrWhiteSpace(text) && Directory.Exists(text) && seen.Add(text))
				{
					yield return text;
				}
			}
		}

		private static IEnumerable<TypeDefinition> EnumerateAllTypes(IEnumerable<TypeDefinition> types)
		{
			foreach (TypeDefinition type in types)
			{
				yield return type;
				foreach (TypeDefinition item in EnumerateAllTypes((IEnumerable<TypeDefinition>)type.NestedTypes))
				{
					yield return item;
				}
			}
		}

		private static bool TryImportUpdatedRepoLibMethod(ModuleDefinition targetModule, ModuleDefinition repoLibModule, MethodReference methodReference, out MethodReference replacement)
		{
			MethodReference methodReference2 = methodReference;
			replacement = null;
			if (!IsLegacyRepoLibRegistrationMethod(methodReference2))
			{
				return false;
			}
			TypeDefinition? obj = ((IEnumerable<TypeDefinition>)repoLibModule.Types).FirstOrDefault((Func<TypeDefinition, bool>)((TypeDefinition type) => ((MemberReference)type).FullName == ((MemberReference)((MemberReference)methodReference2).DeclaringType).FullName));
			MethodDefinition val = ((obj != null) ? ((IEnumerable<MethodDefinition>)obj.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition method) => ((MemberReference)method).Name == ((MemberReference)methodReference2).Name && ((MethodReference)method).Parameters.Count == methodReference2.Parameters.Count && ((IEnumerable<ParameterDefinition>)((MethodReference)method).Parameters).Select((ParameterDefinition parameter) => ((MemberReference)((ParameterReference)parameter).ParameterType).FullName).SequenceEqual<string>(((IEnumerable<ParameterDefinition>)methodReference2.Parameters).Select((ParameterDefinition parameter) => ((MemberReference)((ParameterReference)parameter).ParameterType).FullName), StringComparer.Ordinal))) : null);
			if (val == null || ((MemberReference)((MethodReference)val).ReturnType).FullName == ((MemberReference)methodReference2.ReturnType).FullName || ((MemberReference)((MethodReference)val).ReturnType).FullName == "System.Void")
			{
				return false;
			}
			replacement = targetModule.ImportReference((MethodReference)(object)val);
			return true;
		}

		private static bool IsLegacyRepoLibRegistrationMethod(MethodReference methodReference)
		{
			if (!((MemberReference)methodReference.ReturnType).FullName.Equals("System.Void", StringComparison.Ordinal))
			{
				return false;
			}
			string fullName = ((MemberReference)((MemberReference)methodReference).DeclaringType).FullName;
			string name = ((MemberReference)methodReference).Name;
			if (!(fullName == "REPOLib.Modules.Valuables"))
			{
				if (fullName == "REPOLib.Modules.Items" && name == "RegisterItem")
				{
					return true;
				}
			}
			else if (name == "RegisterValuable")
			{
				return true;
			}
			return false;
		}

		private static bool NextInstructionIsPop(Collection<Instruction> instructions, int index)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			if (index + 1 < instructions.Count)
			{
				return instructions[index + 1].OpCode == OpCodes.Pop;
			}
			return false;
		}

		internal static void PrepareRegisteredItem(ItemContent? itemContent)
		{
			if (!((Object)(object)itemContent == (Object)null))
			{
				Instance?.PrepareItemAttributes(itemContent.Prefab);
			}
		}

		internal static void PrepareRegisteredItem(ItemAttributes? itemAttributes)
		{
			Instance?.PrepareItemAttributes(itemAttributes);
		}

		internal static void PrepareBattery(ItemBattery? itemBattery)
		{
			Instance?.PrepareBatteryInternal(itemBattery);
		}

		internal static void FinishBatteryStart(ItemBattery? itemBattery)
		{
			Instance?.FinishBatteryStartInternal(itemBattery);
		}

		internal static void SyncBattery(ItemBattery? itemBattery)
		{
			Instance?.SyncBatteryStateInternal(itemBattery, forceVisualUpdate: true);
		}

		internal static bool CanRunBatteryUpdate(ItemBattery? itemBattery)
		{
			if (!((Object)(object)Instance == (Object)null))
			{
				return Instance.CanRunBatteryUpdateInternal(itemBattery);
			}
			return true;
		}

		internal static void PrepareGun(ItemGun? itemGun)
		{
			Instance?.PrepareGunInternal(itemGun);
		}

		internal static void SyncGunBattery(ItemGun? itemGun)
		{
			Instance?.SyncGunBatteryInternal(itemGun);
		}

		private void PrepareItemAttributes(ItemAttributes? itemAttributes)
		{
			if (!((Object)(object)itemAttributes == (Object)null))
			{
				PreparePrefab(GetRoot(((Component)itemAttributes).gameObject));
			}
		}

		private void PreparePrefab(GameObject? root)
		{
			if (!((Object)(object)root == (Object)null) && IsTargetObject(root) && _preparedRootIds.Add(((Object)root).GetInstanceID()))
			{
				ItemAttributes[] componentsInChildren = root.GetComponentsInChildren<ItemAttributes>(true);
				foreach (ItemAttributes itemAttributes in componentsInChildren)
				{
					EnsureItemAttributes(itemAttributes);
				}
				ItemBattery[] componentsInChildren2 = root.GetComponentsInChildren<ItemBattery>(true);
				foreach (ItemBattery itemBattery in componentsInChildren2)
				{
					PrepareBatteryInternal(itemBattery);
				}
				ItemGun[] componentsInChildren3 = root.GetComponentsInChildren<ItemGun>(true);
				foreach (ItemGun itemGun in componentsInChildren3)
				{
					PrepareGunInternal(itemGun);
				}
			}
		}

		private void EnsureItemAttributes(ItemAttributes? itemAttributes)
		{
			if (!((Object)(object)itemAttributes == (Object)null))
			{
				SetFieldIfNull(itemAttributes, "photonView", ((Component)itemAttributes).GetComponent<PhotonView>() ?? ((Component)itemAttributes).GetComponentInParent<PhotonView>());
				SetFieldIfNull(itemAttributes, "roomVolumeCheck", ((Component)itemAttributes).GetComponent<RoomVolumeCheck>() ?? ((Component)itemAttributes).GetComponentInParent<RoomVolumeCheck>());
				SetFieldIfNull(itemAttributes, "itemEquippable", ((Component)itemAttributes).GetComponent<ItemEquippable>() ?? ((Component)itemAttributes).GetComponentInParent<ItemEquippable>());
				SetFieldIfNull(itemAttributes, "physGrabObject", ((Component)itemAttributes).GetComponent<PhysGrabObject>() ?? ((Component)itemAttributes).GetComponentInChildren<PhysGrabObject>(true) ?? ((Component)itemAttributes).GetComponentInParent<PhysGrabObject>());
				SetFieldIfNull(itemAttributes, "itemToggle", ((Component)itemAttributes).GetComponent<ItemToggle>() ?? ((Component)itemAttributes).GetComponentInParent<ItemToggle>());
			}
		}

		private void PrepareBatteryInternal(ItemBattery? itemBattery)
		{
			if (!((Object)(object)itemBattery == (Object)null) && IsTargetObject(GetRoot(((Component)itemBattery).gameObject)) && (!_preparedBatteryIds.Contains(((Object)itemBattery).GetInstanceID()) || !HasBatteryCompatibility(itemBattery)))
			{
				ItemAttributes value = ((Component)itemBattery).GetComponent<ItemAttributes>() ?? ((Component)itemBattery).GetComponentInParent<ItemAttributes>();
				ItemEquippable value2 = ((Component)itemBattery).GetComponent<ItemEquippable>() ?? ((Component)itemBattery).GetComponentInParent<ItemEquippable>();
				PhysGrabObject value3 = ((Component)itemBattery).GetComponent<PhysGrabObject>() ?? ((Component)itemBattery).GetComponentInChildren<PhysGrabObject>(true) ?? ((Component)itemBattery).GetComponentInParent<PhysGrabObject>();
				PhotonView value4 = ((Component)itemBattery).GetComponent<PhotonView>() ?? ((Component)itemBattery).GetComponentInParent<PhotonView>();
				Transform value5 = GetFieldValue<Transform>(itemBattery, "batteryTransform") ?? FindChildByExactName(((Component)itemBattery).transform, "Item Battery") ?? FindChildByExactName(((Component)itemBattery).transform, "Battery Graphic") ?? FindChildContaining(((Component)itemBattery).transform, "battery") ?? ((Component)itemBattery).transform;
				SetFieldIfNull(itemBattery, "itemAttributes", value);
				SetFieldIfNull(itemBattery, "itemEquippable", value2);
				SetFieldIfNull(itemBattery, "physGrabObject", value3);
				SetFieldIfNull(itemBattery, "photonView", value4);
				SetFieldValue(itemBattery, "batteryTransform", value5);
				int num = GetFieldValue(itemBattery, "batteryBars", 0);
				float fieldValue = GetFieldValue(itemBattery, "batteryLife", 100f);
				ItemGun val = ((Component)itemBattery).GetComponent<ItemGun>() ?? ((Component)itemBattery).GetComponentInParent<ItemGun>();
				float batteryDrain = (((Object)(object)val != (Object)null) ? Mathf.Max(GetFieldValue(val, "batteryDrain", 1f), 0.01f) : 1f);
				int num2 = DeriveBatteryBars(fieldValue, batteryDrain);
				bool flag = ShouldUseDerivedBatteryBars(itemBattery, val, num, fieldValue, num2);
				if (num <= 0 || flag)
				{
					num = num2;
					SetFieldValue(itemBattery, "batteryBars", num);
				}
				int num3 = CalculateBatteryDisplayBars(fieldValue, num);
				if (GetFieldValue(itemBattery, "batteryLifeInt", -1) < 0 || flag)
				{
					SetFieldValue(itemBattery, "batteryLifeInt", num3);
				}
				if (GetFieldValue(itemBattery, "currentBars", -1) < 0 || flag)
				{
					SetFieldValue(itemBattery, "currentBars", num3);
				}
				if (GetFieldValue(itemBattery, "batteryLifeCountBars", -1) < 0 || flag)
				{
					SetFieldValue(itemBattery, "batteryLifeCountBars", num3);
				}
				if (GetFieldValue(itemBattery, "batteryLifeCountBarsPrev", -1) < 0 || flag)
				{
					SetFieldValue(itemBattery, "batteryLifeCountBarsPrev", num3);
				}
				if (GetFieldValue(itemBattery, "batteryLife", -1f) < 0f)
				{
					SetFieldValue(itemBattery, "batteryLife", 100f);
				}
				if (GetFieldValue(itemBattery, "batteryDrainRate", 0f) <= 0f)
				{
					SetFieldValue(itemBattery, "batteryDrainRate", 1f);
				}
				if (GetFieldValue(itemBattery, "chargeRate", 0f) <= 0f)
				{
					SetFieldValue(itemBattery, "chargeRate", 25f);
				}
				if (GetFieldValue<List<GameObject>>(itemBattery, "chargerList") == null)
				{
					SetFieldValue(itemBattery, "chargerList", new List<GameObject>());
				}
				BatteryVisualLogic val2 = GetFieldValue<BatteryVisualLogic>(itemBattery, "batteryVisualLogic") ?? ((Component)itemBattery).GetComponentInChildren<BatteryVisualLogic>(true);
				if ((Object)(object)val2 == (Object)null && IsSceneInstance(((Component)itemBattery).gameObject))
				{
					val2 = CreateBatteryVisualLogic(itemBattery, num);
				}
				if ((Object)(object)val2 != (Object)null)
				{
					EnsureBatteryVisualLogic(itemBattery, val2, num);
					SetFieldValue(itemBattery, "batteryVisualLogic", val2);
				}
				if (HasBatteryCompatibility(itemBattery))
				{
					_preparedBatteryIds.Add(((Object)itemBattery).GetInstanceID());
				}
			}
		}

		private void FinishBatteryStartInternal(ItemBattery? itemBattery)
		{
			if ((Object)(object)itemBattery == (Object)null || !IsTargetObject(GetRoot(((Component)itemBattery).gameObject)))
			{
				return;
			}
			int batteryBars = Mathf.Max(GetFieldValue(itemBattery, "batteryBars", 0), 1);
			BatteryVisualLogic val = GetFieldValue<BatteryVisualLogic>(itemBattery, "batteryVisualLogic") ?? ((Component)itemBattery).GetComponentInChildren<BatteryVisualLogic>(true);
			if ((Object)(object)val == (Object)null)
			{
				val = CreateBatteryVisualLogic(itemBattery, batteryBars);
				if ((Object)(object)val != (Object)null)
				{
					SetFieldValue(itemBattery, "batteryVisualLogic", val);
				}
			}
			if (!((Object)(object)val == (Object)null))
			{
				EnsureBatteryVisualLogic(itemBattery, val, batteryBars);
				TryInvoke(val, "BatteryBarsSet");
				if (!GetFieldValue(itemBattery, "batteryActive", fallback: false) && !GetFieldValue(itemBattery, "isCharging", fallback: false))
				{
					((Component)val).gameObject.SetActive(false);
				}
				SyncBatteryStateInternal(itemBattery, forceVisualUpdate: true);
			}
		}

		private bool CanRunBatteryUpdateInternal(ItemBattery? itemBattery)
		{
			if ((Object)(object)itemBattery == (Object)null || !IsTargetObject(GetRoot(((Component)itemBattery).gameObject)))
			{
				return true;
			}
			if (HasBatteryCompatibility(itemBattery))
			{
				_preparedBatteryIds.Add(((Object)itemBattery).GetInstanceID());
				return true;
			}
			PrepareBatteryInternal(itemBattery);
			if (HasBatteryCompatibility(itemBattery))
			{
				_preparedBatteryIds.Add(((Object)itemBattery).GetInstanceID());
				return true;
			}
			if (_loggedBatterySkips.Add(((Object)itemBattery).GetInstanceID()))
			{
				ManualLogSource logger = ((BaseUnityPlugin)this).Logger;
				GameObject root = GetRoot(((Component)itemBattery).gameObject);
				logger.LogWarning((object)("Empress BigNuke Valuable Compat Patch skipped a broken battery update on " + (((root != null) ? ((Object)root).name : null) ?? ((Object)((Component)itemBattery).gameObject).name) + " until its compatibility data was restored."));
			}
			return false;
		}

		private void PrepareGunInternal(ItemGun? itemGun)
		{
			if (!((Object)(object)itemGun == (Object)null) && IsTargetObject(GetRoot(((Component)itemGun).gameObject)) && !_preparedGunIds.Contains(((Object)itemGun).GetInstanceID()))
			{
				PhysGrabObject value = ((Component)itemGun).GetComponent<PhysGrabObject>() ?? ((Component)itemGun).GetComponentInChildren<PhysGrabObject>(true) ?? ((Component)itemGun).GetComponentInParent<PhysGrabObject>();
				ItemToggle value2 = ((Component)itemGun).GetComponent<ItemToggle>() ?? ((Component)itemGun).GetComponentInParent<ItemToggle>();
				ItemBattery val = ((Component)itemGun).GetComponent<ItemBattery>() ?? ((Component)itemGun).GetComponentInParent<ItemBattery>();
				PhotonView value3 = ((Component)itemGun).GetComponent<PhotonView>() ?? ((Component)itemGun).GetComponentInParent<PhotonView>();
				PhysGrabObjectImpactDetector value4 = ((Component)itemGun).GetComponent<PhysGrabObjectImpactDetector>() ?? ((Component)itemGun).GetComponentInParent<PhysGrabObjectImpactDetector>();
				RoomVolumeCheck value5 = ((Component)itemGun).GetComponent<RoomVolumeCheck>() ?? ((Component)itemGun).GetComponentInParent<RoomVolumeCheck>();
				ItemEquippable value6 = ((Component)itemGun).GetComponent<ItemEquippable>() ?? ((Component)itemGun).GetComponentInParent<ItemEquippable>();
				Transform val2 = GetFieldValue<Transform>(itemGun, "gunMuzzle") ?? FindChildByExactName(((Component)itemGun).transform, "Gun Muzzle") ?? FindChildByExactName(((Component)itemGun).transform, "Muzzle") ?? FindChildContaining(((Component)itemGun).transform, "muzzle");
				GameObject value7 = GetFieldValue<GameObject>(itemGun, "muzzleFlashPrefab") ?? FindGameObjectByExactName(((Component)itemGun).transform, "Muzzle Flash") ?? FindGameObjectByExactName(((Component)itemGun).transform, "MuzzleFlash") ?? FindGameObjectContaining(((Component)itemGun).transform, "muzzle", "flash");
				GameObject val3 = GetFieldValue<GameObject>(itemGun, "bulletPrefab") ?? FindGameObjectByExactName(((Component)itemGun).transform, "Bullet") ?? FindGameObjectContaining(((Component)itemGun).transform, "bullet");
				Transform value8 = GetFieldValue<Transform>(itemGun, "gunTrigger") ?? FindChildByExactName(((Component)itemGun).transform, "Gun Trigger") ?? FindChildByExactName(((Component)itemGun).transform, "Trigger") ?? val2 ?? ((Component)itemGun).transform;
				HurtCollider value9 = GetFieldValue<HurtCollider>(itemGun, "hurtCollider") ?? (((Object)(object)val3 != (Object)null) ? val3.GetComponentInChildren<HurtCollider>(true) : null) ?? ((Component)itemGun).GetComponentInChildren<HurtCollider>(true);
				SetFieldIfNull(itemGun, "physGrabObject", value);
				SetFieldIfNull(itemGun, "itemToggle", value2);
				SetFieldIfNull(itemGun, "itemBattery", val);
				SetFieldIfNull(itemGun, "photonView", value3);
				SetFieldIfNull(itemGun, "impactDetector", value4);
				SetFieldIfNull(itemGun, "roomVolumeCheck", value5);
				SetFieldIfNull(itemGun, "itemEquippable", value6);
				SetFieldIfNull(itemGun, "gunMuzzle", val2);
				SetFieldIfNull(itemGun, "muzzleFlashPrefab", value7);
				SetFieldIfNull(itemGun, "bulletPrefab", val3);
				SetFieldIfNull(itemGun, "gunTrigger", value8);
				SetFieldIfNull(itemGun, "hurtCollider", value9);
				if (GetFieldValue(itemGun, "numberOfBullets", 0) <= 0)
				{
					SetFieldValue(itemGun, "numberOfBullets", 1);
				}
				if (GetFieldValue(itemGun, "shootCooldown", 0f) <= 0f)
				{
					SetFieldValue(itemGun, "shootCooldown", 0.1f);
				}
				if (GetFieldValue(itemGun, "batteryDrain", 0f) <= 0f)
				{
					SetFieldValue(itemGun, "batteryDrain", 1f);
				}
				if (GetFieldValue(itemGun, "batteryDrainFullBar", fallback: false) && GetFieldValue(itemGun, "batteryDrainFullBars", 0) <= 0)
				{
					SetFieldValue(itemGun, "batteryDrainFullBars", Mathf.Max(1, Mathf.RoundToInt(GetFieldValue(itemGun, "batteryDrain", 1f))));
				}
				if ((Object)(object)val != (Object)null)
				{
					PrepareBatteryInternal(val);
				}
				_preparedGunIds.Add(((Object)itemGun).GetInstanceID());
			}
		}

		private void SyncGunBatteryInternal(ItemGun? itemGun)
		{
			if (!((Object)(object)itemGun == (Object)null) && IsTargetObject(GetRoot(((Component)itemGun).gameObject)))
			{
				PrepareGunInternal(itemGun);
				ItemBattery val = ((Component)itemGun).GetComponent<ItemBattery>() ?? ((Component)itemGun).GetComponentInParent<ItemBattery>();
				if (!((Object)(object)val == (Object)null))
				{
					SyncBatteryStateInternal(val, forceVisualUpdate: true);
				}
			}
		}

		private void OnAllBundlesLoaded()
		{
			RegisterTargetValuables();
		}

		private void RegisterTargetValuables()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			if (TargetTokens.Length == 0)
			{
				return;
			}
			ValuableObject[] array = Resources.FindObjectsOfTypeAll<ValuableObject>();
			foreach (ValuableObject val in array)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				GameObject gameObject = ((Component)val).gameObject;
				if ((Object)(object)gameObject == (Object)null)
				{
					continue;
				}
				Scene scene = gameObject.scene;
				if (((Scene)(ref scene)).IsValid() || !IsTargetValuable(val) || IsValuableAlreadyRegistered(gameObject))
				{
					continue;
				}
				try
				{
					Valuables.RegisterValuable(val, ResolveValuablePresets());
					if (_registeredValuableNames.Add(((Object)gameObject).name))
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Empress BigNuke Valuable Compat Patch registered valuable compatibility for " + ((Object)gameObject).name + "."));
					}
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Empress BigNuke Valuable Compat Patch could not register valuable " + ((Object)gameObject).name + ": " + ex.Message));
				}
			}
		}

		private static List<string> ResolveValuablePresets()
		{
			List<string> list = DefaultValuablePresets.Where((string value) => !string.IsNullOrWhiteSpace(value)).Distinct<string>(StringComparer.OrdinalIgnoreCase).ToList();
			if (list.Count <= 0)
			{
				return new List<string> { "Valuables - Generic" };
			}
			return list;
		}

		private static bool IsTargetValuable(ValuableObject valuableObject)
		{
			if ((Object)(object)valuableObject == (Object)null)
			{
				return false;
			}
			foreach (string item in EnumerateCandidateNames(((Component)valuableObject).gameObject))
			{
				if (MatchesTargetToken(item))
				{
					return true;
				}
			}
			return false;
		}

		private static bool IsValuableAlreadyRegistered(GameObject prefab)
		{
			if (ContainsNamedValue(GetFieldValue<IEnumerable>(typeof(Valuables), "_valuablesRegistered"), ((Object)prefab).name))
			{
				return true;
			}
			IDictionary fieldValue = GetFieldValue<IDictionary>(typeof(Valuables), "_valuablesToRegister");
			if (fieldValue != null)
			{
				return ContainsNamedValue(fieldValue.Keys, ((Object)prefab).name);
			}
			return false;
		}

		private static bool ContainsNamedValue(IEnumerable? values, string name)
		{
			if (values == null)
			{
				return false;
			}
			foreach (object value in values)
			{
				if (value != null)
				{
					string fieldValue = GetFieldValue(value, "name", string.Empty);
					if (!string.IsNullOrWhiteSpace(fieldValue) && string.Equals(fieldValue, name, StringComparison.OrdinalIgnoreCase))
					{
						return true;
					}
					GameObject fieldValue2 = GetFieldValue<GameObject>(value, "gameObject");
					if ((Object)(object)fieldValue2 != (Object)null && string.Equals(((Object)fieldValue2).name, name, StringComparison.OrdinalIgnoreCase))
					{
						return true;
					}
				}
			}
			return false;
		}

		private void SyncBatteryStateInternal(ItemBattery? itemBattery, bool forceVisualUpdate)
		{
			if ((Object)(object)itemBattery == (Object)null || !IsTargetObject(GetRoot(((Component)itemBattery).gameObject)))
			{
				return;
			}
			if (!HasBatteryCompatibility(itemBattery))
			{
				PrepareBatteryInternal(itemBattery);
				if (!HasBatteryCompatibility(itemBattery))
				{
					return;
				}
			}
			int num = Mathf.Max(GetFieldValue(itemBattery, "batteryBars", 0), 1);
			int num2 = CalculateBatteryDisplayBars(Mathf.Clamp(GetFieldValue(itemBattery, "batteryLife", 100f), 0f, 100f), num);
			int fieldValue = GetFieldValue(itemBattery, "batteryLifeInt", num2);
			int fieldValue2 = GetFieldValue(itemBattery, "currentBars", num2);
			int fieldValue3 = GetFieldValue(itemBattery, "batteryLifeCountBars", num2);
			int fieldValue4 = GetFieldValue(itemBattery, "batteryLifeCountBarsPrev", num2);
			bool flag = fieldValue != num2 || fieldValue2 != num2 || fieldValue3 != num2 || fieldValue4 != num2;
			SetFieldValue(itemBattery, "batteryLifeInt", num2);
			SetFieldValue(itemBattery, "currentBars", num2);
			SetFieldValue(itemBattery, "batteryLifeCountBars", num2);
			SetFieldValue(itemBattery, "batteryLifeCountBarsPrev", num2);
			BatteryVisualLogic val = GetFieldValue<BatteryVisualLogic>(itemBattery, "batteryVisualLogic") ?? ((Component)itemBattery).GetComponentInChildren<BatteryVisualLogic>(true);
			if ((Object)(object)val != (Object)null)
			{
				EnsureBatteryVisualLogic(itemBattery, val, num);
				SetFieldValue(val, "currentBars", num2);
				if (!((Component)val).gameObject.activeSelf && (num2 < num || GetFieldValue(itemBattery, "batteryActive", fallback: false) || GetFieldValue(itemBattery, "isCharging", fallback: false)))
				{
					((Component)val).gameObject.SetActive(true);
				}
				if (flag || forceVisualUpdate)
				{
					TryInvoke(val, "BatteryBarsSet");
					TryInvoke(val, "BatteryBarsUpdate", num2, true);
				}
				if (num2 <= 0)
				{
					TryInvoke(val, "OverrideBatteryOutWarning", 0.2f);
				}
				else if (num2 <= Mathf.Max(1, num / 2))
				{
					TryInvoke(val, "OverrideChargeNeeded", 0.2f);
				}
				else
				{
					TryInvoke(val, "BatteryColorMainReset");
				}
			}
			SyncLegacyAmmoIndicator(itemBattery, num2, num);
			BatteryUI val2 = FindBatteryUiInstance();
			BatteryVisualLogic val3 = (((Object)(object)val2 != (Object)null) ? (GetFieldValue<BatteryVisualLogic>(val2, "batteryVisualLogic") ?? ((Component)val2).GetComponentInChildren<BatteryVisualLogic>(true)) : null);
			if ((Object)(object)val2 != (Object)null && (Object)(object)val3 != (Object)null && (Object)(object)GetFieldValue<ItemBattery>(val3, "itemBattery") == (Object)(object)itemBattery)
			{
				SetFieldValue(val2, "batteryCurrentBars", num2);
				SetFieldValue(val2, "batteryCurrenyBarsMax", num);
				SetFieldValue(val3, "itemBattery", itemBattery);
				TryInvoke(val3, "BatteryBarsSet");
				TryInvoke(val3, "BatteryBarsUpdate", num2, true);
			}
		}

		private static int DeriveBatteryBars(float legacyBatteryLife, float batteryDrain)
		{
			if (legacyBatteryLife > 0f && batteryDrain > 0f)
			{
				return Mathf.Clamp(Mathf.RoundToInt(legacyBatteryLife / batteryDrain), 1, 200);
			}
			if (legacyBatteryLife > 0f)
			{
				return Mathf.Clamp(Mathf.RoundToInt(legacyBatteryLife / 10f), 1, 200);
			}
			return 10;
		}

		private static bool ShouldUseDerivedBatteryBars(ItemBattery itemBattery, ItemGun? itemGun, int batteryBars, float legacyBatteryLife, int derivedBatteryBars)
		{
			if (derivedBatteryBars <= 0)
			{
				return false;
			}
			if (batteryBars <= 0)
			{
				return true;
			}
			if ((Object)(object)itemGun == (Object)null)
			{
				return false;
			}
			if ((Object)(object)(GetFieldValue<BatteryVisualLogic>(itemBattery, "batteryVisualLogic") ?? ((Component)itemBattery).GetComponentInChildren<BatteryVisualLogic>(true)) != (Object)null)
			{
				return false;
			}
			if (batteryBars != 6)
			{
				return false;
			}
			if (legacyBatteryLife < 99f)
			{
				return false;
			}
			int fieldValue = GetFieldValue(itemBattery, "batteryLifeInt", 6);
			int fieldValue2 = GetFieldValue(itemBattery, "currentBars", 6);
			if (fieldValue == 6)
			{
				return fieldValue2 == 6;
			}
			return false;
		}

		private static int CalculateBatteryDisplayBars(float batteryLife, int batteryBars)
		{
			if (batteryBars <= 0)
			{
				return 0;
			}
			float num = 100f / (float)batteryBars;
			if (num <= 0f)
			{
				return 0;
			}
			return Mathf.Clamp(Mathf.RoundToInt(batteryLife / num), 0, batteryBars);
		}

		private static void SyncLegacyAmmoIndicator(ItemBattery itemBattery, int desiredBars, int batteryBars)
		{
			if (!IsSceneInstance(((Component)itemBattery).gameObject))
			{
				return;
			}
			Transform val = FindLegacyBatteryGraphic(itemBattery);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			MeshRenderer[] componentsInChildren = ((Component)val).GetComponentsInChildren<MeshRenderer>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				((Renderer)componentsInChildren[i]).enabled = false;
			}
			EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator = (((Object)(object)val.parent != (Object)null) ? ((Component)val.parent).GetComponentInChildren<EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator>(true) : null);
			if ((Object)(object)empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator == (Object)null)
			{
				empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator = BuildLegacyAmmoIndicator(val);
			}
			if (!((Object)(object)empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator == (Object)null))
			{
				int filledBars = ((batteryBars > 0) ? Mathf.Clamp(Mathf.CeilToInt((float)desiredBars / (float)batteryBars * (float)empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator.BarCount), 0, empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator.BarCount) : 0);
				EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState state = EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState.Normal;
				if (desiredBars <= 0)
				{
					state = EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState.Empty;
				}
				else if (desiredBars <= Mathf.Max(1, batteryBars / 2))
				{
					state = EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState.Low;
				}
				empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator.SetDisplay(filledBars, state);
			}
		}

		private static Transform? FindLegacyBatteryGraphic(ItemBattery itemBattery)
		{
			return FindChildByExactName(((Component)itemBattery).transform, "Battery Graphic") ?? FindChildContaining(((Component)itemBattery).transform, "battery", "graphic");
		}

		private static bool IsSceneInstance(GameObject gameObject)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			Scene scene = gameObject.scene;
			if (((Scene)(ref scene)).IsValid())
			{
				scene = gameObject.scene;
				return ((Scene)(ref scene)).isLoaded;
			}
			return false;
		}

		private static BatteryUI? FindBatteryUiInstance()
		{
			return ((IEnumerable<BatteryUI>)Resources.FindObjectsOfTypeAll<BatteryUI>()).FirstOrDefault((Func<BatteryUI, bool>)delegate(BatteryUI ui)
			{
				//IL_000f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0014: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)ui != (Object)null)
				{
					Scene scene = ((Component)ui).gameObject.scene;
					return ((Scene)(ref scene)).IsValid();
				}
				return false;
			});
		}

		private static EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator? BuildLegacyAmmoIndicator(Transform legacyGraphic)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: 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_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: 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_0104: 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_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_025a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0281: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)legacyGraphic.parent == (Object)null)
			{
				return null;
			}
			GameObject val = CreateUiObject("Empress Legacy Ammo Indicator", legacyGraphic.parent, new Vector2(88f, 24f));
			val.transform.localPosition = legacyGraphic.localPosition;
			val.transform.localRotation = legacyGraphic.localRotation;
			val.transform.localScale = new Vector3(Mathf.Max(legacyGraphic.localScale.x * 0.004f, 0.00075f), Mathf.Max(legacyGraphic.localScale.y * 0.004f, 0.00075f), Mathf.Max(legacyGraphic.localScale.z * 0.004f, 0.00075f));
			Canvas obj = val.AddComponent<Canvas>();
			obj.renderMode = (RenderMode)2;
			obj.overrideSorting = true;
			obj.sortingOrder = 2600;
			val.AddComponent<CanvasScaler>().dynamicPixelsPerUnit = 12f;
			val.AddComponent<GraphicRaycaster>();
			EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator = val.AddComponent<EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator>();
			CreateImageObject("Frame Shadow", val.transform, new Vector2(0f, 0f), new Vector2(88f, 24f), new Color(0f, 0f, 0f, 0.45f));
			RawImage frame = CreateImageObject("Frame Main", val.transform, new Vector2(0f, 0f), new Vector2(82f, 18f), new Color(0.25f, 1f, 0.15f, 1f));
			RawImage background = CreateImageObject("Background", val.transform, new Vector2(0f, 0f), new Vector2(76f, 12f), new Color(0.03f, 0.07f, 0.03f, 0.92f));
			GameObject val2 = CreateUiObject("Bar Container", val.transform, new Vector2(70f, 10f));
			val2.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
			HorizontalLayoutGroup obj2 = val2.AddComponent<HorizontalLayoutGroup>();
			((HorizontalOrVerticalLayoutGroup)obj2).childControlHeight = false;
			((HorizontalOrVerticalLayoutGroup)obj2).childControlWidth = false;
			((HorizontalOrVerticalLayoutGroup)obj2).childForceExpandHeight = false;
			((HorizontalOrVerticalLayoutGroup)obj2).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)obj2).spacing = 2f;
			List<RawImage> list = new List<RawImage>();
			for (int i = 0; i < 5; i++)
			{
				RawImage val3 = CreateUiObject("Bar " + (i + 1), val2.transform, new Vector2(10f, 10f)).AddComponent<RawImage>();
				((Graphic)val3).color = new Color(0.2f, 1f, 0.15f, 1f);
				list.Add(val3);
			}
			empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator.Initialize(frame, background, list);
			ApplyHideFlags((Object)(object)val);
			return empressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator;
		}

		private static BatteryVisualLogic? CreateBatteryVisualLogic(ItemBattery itemBattery, int batteryBars)
		{
			BatteryVisualLogic val = CloneBatteryVisualLogic(itemBattery);
			if ((Object)(object)val != (Object)null)
			{
				return val;
			}
			return BuildBatteryVisualLogic(itemBattery, batteryBars);
		}

		private static BatteryVisualLogic? CloneBatteryVisualLogic(ItemBattery itemBattery)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			BatteryVisualLogic val = FindTemplateBatteryVisualLogic(itemBattery);
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			GameObject val2 = Object.Instantiate<GameObject>(((Component)val).gameObject, ((Component)itemBattery).transform);
			((Object)val2).name = "Empress Battery Visual Logic";
			val2.transform.localPosition = new Vector3(0f, 0.35f, 0f);
			val2.transform.localRotation = Quaternion.identity;
			if (val2.transform.localScale == Vector3.zero)
			{
				val2.transform.localScale = Vector3.one * 0.0065f;
			}
			ApplyHideFlags((Object)(object)val2);
			return val2.GetComponent<BatteryVisualLogic>();
		}

		private static BatteryVisualLogic? FindTemplateBatteryVisualLogic(ItemBattery itemBattery)
		{
			ItemBattery itemBattery2 = itemBattery;
			return (from visual in Resources.FindObjectsOfTypeAll<BatteryVisualLogic>()
				where (Object)(object)visual != (Object)null && (Object)(object)((Component)visual).gameObject != (Object)(object)((Component)itemBattery2).gameObject
				where (Object)(object)GetFieldValue<GameObject>(visual, "batteryBarPrefab") != (Object)null
				orderby GetFieldValue(visual, "inUI", fallback: false) ? 1 : 0, IsTargetObject(GetRoot(((Component)visual).gameObject)) ? 1 : 0
				select visual).FirstOrDefault();
		}

		private static BatteryVisualLogic BuildBatteryVisualLogic(ItemBattery itemBattery, int batteryBars)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: 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_00c8: 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_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: 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_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			//IL_0223: Unknown result type (might be due to invalid IL or missing references)
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_028a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0299: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_0308: Unknown result type (might be due to invalid IL or missing references)
			//IL_0336: Unknown result type (might be due to invalid IL or missing references)
			//IL_0345: Unknown result type (might be due to invalid IL or missing references)
			//IL_035e: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = CreateUiObject("Empress Battery Visual Logic", ((Component)itemBattery).transform, new Vector2(92f, 26f));
			val.transform.localPosition = new Vector3(0f, 0.35f, 0f);
			val.transform.localRotation = Quaternion.identity;
			val.transform.localScale = Vector3.one * 0.0065f;
			Canvas obj = val.AddComponent<Canvas>();
			obj.renderMode = (RenderMode)2;
			obj.overrideSorting = true;
			obj.sortingOrder = 2500;
			val.AddComponent<CanvasScaler>().dynamicPixelsPerUnit = 12f;
			val.AddComponent<GraphicRaycaster>();
			BatteryVisualLogic obj2 = val.AddComponent<BatteryVisualLogic>();
			RawImage value = CreateImageObject("Border Shadow", val.transform, new Vector2(0f, 0f), new Vector2(90f, 22f), new Color(0f, 0f, 0f, 0.45f));
			RawImage value2 = CreateImageObject("Border Main", val.transform, new Vector2(0f, 0f), new Vector2(86f, 18f), new Color(0.96f, 0.86f, 0.2f, 1f));
			RawImage value3 = CreateImageObject("Background", val.transform, new Vector2(0f, 0f), new Vector2(80f, 12f), new Color(0.08f, 0.08f, 0.08f, 0.88f));
			RawImage val2 = CreateImageObject("Charge", val.transform, new Vector2(0f, 0f), new Vector2(76f, 10f), new Color(0.1f, 1f, 0.2f, 0.6f));
			RawImage val3 = CreateImageObject("Drain", val.transform, new Vector2(0f, 0f), new Vector2(70f, 10f), new Color(1f, 0.35f, 0.1f, 0.6f));
			GameObject val4 = CreateUiObject("Bar Container", val.transform, new Vector2(72f, 10f));
			val4.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
			HorizontalLayoutGroup val5 = val4.AddComponent<HorizontalLayoutGroup>();
			((HorizontalOrVerticalLayoutGroup)val5).childControlHeight = false;
			((HorizontalOrVerticalLayoutGroup)val5).childControlWidth = false;
			((HorizontalOrVerticalLayoutGroup)val5).childForceExpandHeight = false;
			((HorizontalOrVerticalLayoutGroup)val5).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val5).spacing = 2f;
			GameObject gameObject = ((Component)CreateImageObject("Battery Out Visual", val.transform, new Vector2(0f, 0f), new Vector2(88f, 24f), new Color(1f, 0f, 0f, 0.25f))).gameObject;
			gameObject.SetActive(false);
			GameObject gameObject2 = ((Component)CreateImageObject("Battery Charge Needed Visual", val.transform, new Vector2(0f, 0f), new Vector2(88f, 24f), new Color(1f, 0.55f, 0f, 0.2f))).gameObject;
			gameObject2.SetActive(false);
			GameObject gameObject3 = ((Component)CreateImageObject("Bar Prefab", val.transform, new Vector2(-5000f, -5000f), new Vector2(6f, 10f), new Color(1f, 0.92f, 0.3f, 1f))).gameObject;
			GameObject value4 = CreateBarEffectPrefab(val.transform);
			SetFieldValue(obj2, "batteryBarContainerGroup", val5);
			SetFieldValue(obj2, "batteryBarPrefab", gameObject3);
			SetFieldValue(obj2, "batteryBars", batteryBars);
			SetFieldValue(obj2, "batteryBarContainer", val4.transform);
			SetFieldValue(obj2, "inUI", false);
			SetFieldValue(obj2, "cameraTurn", true);
			SetFieldValue(obj2, "batteryBarCharge", ((Component)val2).transform);
			SetFieldValue(obj2, "batteryBarDrain", ((Component)val3).transform);
			SetFieldValue(obj2, "bars", new List<GameObject>());
			SetFieldValue(obj2, "batteryBorderShadow", value);
			SetFieldValue(obj2, "batteryBorderMain", value2);
			SetFieldValue(obj2, "batteryBackground", value3);
			SetFieldValue(obj2, "batteryCharge", val2);
			SetFieldValue(obj2, "batteryDrain", val3);
			SetFieldValue(obj2, "batteryOutVisual", gameObject);
			SetFieldValue(obj2, "batteryChargeNeededVisual", gameObject2);
			SetFieldValue(obj2, "barLossEffectPrefab", value4);
			ApplyHideFlags((Object)(object)val);
			return obj2;
		}

		private static RawImage CreateImageObject(string name, Transform parent, Vector2 anchoredPosition, Vector2 size, Color color)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			GameObject obj = CreateUiObject(name, parent, size);
			obj.GetComponent<RectTransform>().anchoredPosition = anchoredPosition;
			RawImage obj2 = obj.AddComponent<RawImage>();
			((Graphic)obj2).color = color;
			return obj2;
		}

		private static GameObject CreateUiObject(string name, Transform parent, Vector2 size)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: 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)
			//IL_007f: Expected O, but got Unknown
			GameObject val = new GameObject(name, new Type[2]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer)
			});
			val.transform.SetParent(parent, false);
			RectTransform component = val.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0.5f, 0.5f);
			component.anchorMax = new Vector2(0.5f, 0.5f);
			component.pivot = new Vector2(0.5f, 0.5f);
			component.sizeDelta = size;
			return val;
		}

		private static GameObject CreateBarEffectPrefab(Transform parent)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: 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_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: 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_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Expected O, but got Unknown
			GameObject val = CreateUiObject("Bar Effect Prefab", parent, new Vector2(6f, 10f));
			val.GetComponent<RectTransform>().anchoredPosition = new Vector2(-6000f, -6000f);
			((Graphic)val.AddComponent<RawImage>()).color = Color.red;
			BatteryBarEffect val2 = val.AddComponent<BatteryBarEffect>();
			val2.barLossEffect = true;
			val2.barColor = Color.red;
			val2.barAnimationCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
			val2.barFadeOutCurve = new AnimationCurve((Keyframe[])(object)new Keyframe[2]
			{
				new Keyframe(0f, 1f),
				new Keyframe(1f, 0f)
			});
			val2.whiteFlashCurve = new AnimationCurve((Keyframe[])(object)new Keyframe[3]
			{
				new Keyframe(0f, 0f),
				new Keyframe(0.2f, 1f),
				new Keyframe(1f, 0f)
			});
			return val;
		}

		private static void EnsureBatteryVisualLogic(ItemBattery itemBattery, BatteryVisualLogic batteryVisualLogic, int batteryBars)
		{
			SetFieldValue(itemBattery, "batteryVisualLogic", batteryVisualLogic);
			SetFieldValue(itemBattery, "batteryTransform", GetFieldValue<Transform>(itemBattery, "batteryTransform") ?? ((Component)batteryVisualLogic).transform);
			SetFieldValue(itemBattery, "batteryBars", batteryBars);
			SetFieldValue(itemBattery, "batteryLifeInt", Mathf.Clamp(GetFieldValue(itemBattery, "batteryLifeInt", batteryBars), 0, batteryBars));
			SetFieldValue(itemBattery, "currentBars", Mathf.Clamp(GetFieldValue(itemBattery, "currentBars", batteryBars), 0, batteryBars));
			SetFieldValue(itemBattery, "batteryLifeCountBars", Mathf.Clamp(GetFieldValue(itemBattery, "batteryLifeCountBars", batteryBars), 0, batteryBars));
			SetFieldValue(itemBattery, "batteryLifeCountBarsPrev", Mathf.Clamp(GetFieldValue(itemBattery, "batteryLifeCountBarsPrev", batteryBars), 0, batteryBars));
			SetFieldValue(batteryVisualLogic, "itemBattery", itemBattery);
			SetFieldValue(batteryVisualLogic, "batteryBars", batteryBars);
			SetFieldValue(batteryVisualLogic, "currentBars", GetFieldValue(itemBattery, "currentBars", batteryBars));
			if ((Object)(object)GetFieldValue<HorizontalLayoutGroup>(batteryVisualLogic, "batteryBarContainerGroup") == (Object)null)
			{
				SetFieldValue(batteryVisualLogic, "batteryBarContainerGroup", ((Component)batteryVisualLogic).GetComponentInChildren<HorizontalLayoutGroup>(true));
			}
			if ((Object)(object)GetFieldValue<Transform>(batteryVisualLogic, "batteryBarContainer") == (Object)null)
			{
				SetFieldValue(batteryVisualLogic, "batteryBarContainer", FindChildByExactName(((Component)batteryVisualLogic).transform, "Bar Container"));
			}
			if ((Object)(object)GetFieldValue<GameObject>(batteryVisualLogic, "batteryBarPrefab") == (Object)null)
			{
				SetFieldValue(batteryVisualLogic, "batteryBarPrefab", FindGameObjectByExactName(((Component)batteryVisualLogic).transform, "Bar Prefab"));
			}
			if ((Object)(object)GetFieldValue<Transform>(batteryVisualLogic, "batteryBarCharge") == (Object)null)
			{
				SetFieldValue(batteryVisualLogic, "batteryBarCharge", FindChildByExactName(((Component)batteryVisualLogic).transform, "Charge"));
			}
			if ((Object)(object)GetFieldValue<Transform>(batteryVisualLogic, "batteryBarDrain") == (Object)null)
			{
				SetFieldValue(batteryVisualLogic, "batteryBarDrain", FindChildByExactName(((Component)batteryVisualLogic).transform, "Drain"));
			}
			if ((Object)(object)GetFieldValue<RawImage>(batteryVisualLogic, "batteryBorderShadow") == (Object)null)
			{
				Transform? obj = FindChildByExactName(((Component)batteryVisualLogic).transform, "Border Shadow");
				SetFieldValue(batteryVisualLogic, "batteryBorderShadow", (obj != null) ? ((Component)obj).GetComponent<RawImage>() : null);
			}
			if ((Object)(object)GetFieldValue<RawImage>(batteryVisualLogic, "batteryBorderMain") == (Object)null)
			{
				Transform? obj2 = FindChildByExactName(((Component)batteryVisualLogic).transform, "Border Main");
				SetFieldValue(batteryVisualLogic, "batteryBorderMain", (obj2 != null) ? ((Component)obj2).GetComponent<RawImage>() : null);
			}
			if ((Object)(object)GetFieldValue<RawImage>(batteryVisualLogic, "batteryBackground") == (Object)null)
			{
				Transform? obj3 = FindChildByExactName(((Component)batteryVisualLogic).transform, "Background");
				SetFieldValue(batteryVisualLogic, "batteryBackground", (obj3 != null) ? ((Component)obj3).GetComponent<RawImage>() : null);
			}
			if ((Object)(object)GetFieldValue<RawImage>(batteryVisualLogic, "batteryCharge") == (Object)null)
			{
				Transform? obj4 = FindChildByExactName(((Component)batteryVisualLogic).transform, "Charge");
				SetFieldValue(batteryVisualLogic, "batteryCharge", (obj4 != null) ? ((Component)obj4).GetComponent<RawImage>() : null);
			}
			if ((Object)(object)GetFieldValue<RawImage>(batteryVisualLogic, "batteryDrain") == (Object)null)
			{
				Transform? obj5 = FindChildByExactName(((Component)batteryVisualLogic).transform, "Drain");
				SetFieldValue(batteryVisualLogic, "batteryDrain", (obj5 != null) ? ((Component)obj5).GetComponent<RawImage>() : null);
			}
			if ((Object)(object)GetFieldValue<GameObject>(batteryVisualLogic, "batteryOutVisual") == (Object)null)
			{
				SetFieldValue(batteryVisualLogic, "batteryOutVisual", FindGameObjectByExactName(((Component)batteryVisualLogic).transform, "Battery Out Visual"));
			}
			if ((Object)(object)GetFieldValue<GameObject>(batteryVisualLogic, "batteryChargeNeededVisual") == (Object)null)
			{
				SetFieldValue(batteryVisualLogic, "batteryChargeNeededVisual", FindGameObjectByExactName(((Component)batteryVisualLogic).transform, "Battery Charge Needed Visual"));
			}
			if ((Object)(object)GetFieldValue<GameObject>(batteryVisualLogic, "barLossEffectPrefab") == (Object)null)
			{
				SetFieldValue(batteryVisualLogic, "barLossEffectPrefab", FindGameObjectByExactName(((Component)batteryVisualLogic).transform, "Bar Effect Prefab"));
			}
			if (GetFieldValue<List<GameObject>>(batteryVisualLogic, "bars") == null)
			{
				SetFieldValue(batteryVisualLogic, "bars", new List<GameObject>());
			}
		}

		private static bool IsTargetObject(GameObject? gameObject)
		{
			if ((Object)(object)gameObject == (Object)null)
			{
				return false;
			}
			if ((Object)(object)Instance != (Object)null && Instance._targetRootCache.TryGetValue(((Object)(object)gameObject.transform.root != (Object)null) ? ((Object)((Component)gameObject.transform.root).gameObject).GetInstanceID() : ((Object)gameObject).GetInstanceID(), out var value))
			{
				return value;
			}
			GameObject root = GetRoot(gameObject);
			bool flag = false;
			foreach (string item in EnumerateCandidateNames(root))
			{
				if (MatchesTargetToken(item))
				{
					flag = true;
					break;
				}
			}
			if ((Object)(object)Instance != (Object)null)
			{
				Instance._targetRootCache[((Object)root).GetInstanceID()] = flag;
			}
			return flag;
		}

		private static IEnumerable<string> EnumerateCandidateNames(GameObject root)
		{
			yield return ((Object)root).name;
			ItemAttributes[] componentsInChildren = root.GetComponentsInChildren<ItemAttributes>(true);
			foreach (ItemAttributes itemAttributes in componentsInChildren)
			{
				yield return ((Object)itemAttributes).name;
				yield return GetFieldValue(itemAttributes, "itemName", string.Empty);
				yield return GetFieldValue(itemAttributes, "itemAssetName", string.Empty);
				object item = GetFieldValue<object>(itemAttributes, "item");
				if (item != null)
				{
					yield return GetFieldValue(item, "itemName", string.Empty);
					yield return GetFieldValue(item, "itemAssetName", string.Empty);
				}
			}
			ItemGun[] componentsInChildren2 = root.GetComponentsInChildren<ItemGun>(true);
			foreach (ItemGun val in componentsInChildren2)
			{
				yield return ((Object)val).name;
			}
			ItemBattery[] componentsInChildren3 = root.GetComponentsInChildren<ItemBattery>(true);
			foreach (ItemBattery val2 in componentsInChildren3)
			{
				yield return ((Object)val2).name;
			}
			ValuableObject[] componentsInChildren4 = root.GetComponentsInChildren<ValuableObject>(true);
			foreach (ValuableObject valuableObject in componentsInChildren4)
			{
				yield return ((Object)valuableObject).name;
				yield return ((Object)((Component)valuableObject).gameObject).name;
			}
		}

		private static bool MatchesTargetToken(string? value)
		{
			string value2 = value;
			if (string.IsNullOrWhiteSpace(value2))
			{
				return false;
			}
			return TargetTokens.Where((string token) => !string.IsNullOrWhiteSpace(token)).Any((string token) => value2.IndexOf(token, StringComparison.OrdinalIgnoreCase) >= 0);
		}

		private static GameObject GetRoot(GameObject gameObject)
		{
			if (!((Object)(object)gameObject.transform.root != (Object)null))
			{
				return gameObject;
			}
			return ((Component)gameObject.transform.root).gameObject;
		}

		private static Transform? FindChildByExactName(Transform root, string name)
		{
			string name2 = name;
			return ((IEnumerable<Transform>)((Component)root).GetComponentsInChildren<Transform>(true)).FirstOrDefault((Func<Transform, bool>)((Transform child) => string.Equals(((Object)child).name, name2, StringComparison.OrdinalIgnoreCase)));
		}

		private static Transform? FindChildContaining(Transform root, params string[] fragments)
		{
			string[] fragments2 = fragments;
			return ((IEnumerable<Transform>)((Component)root).GetComponentsInChildren<Transform>(true)).FirstOrDefault((Func<Transform, bool>)((Transform child) => fragments2.All((string fragment) => ((Object)child).name.IndexOf(fragment, StringComparison.OrdinalIgnoreCase) >= 0)));
		}

		private static GameObject? FindGameObjectByExactName(Transform root, string name)
		{
			Transform? obj = FindChildByExactName(root, name);
			if (obj == null)
			{
				return null;
			}
			return ((Component)obj).gameObject;
		}

		private static GameObject? FindGameObjectContaining(Transform root, params string[] fragments)
		{
			Transform? obj = FindChildContaining(root, fragments);
			if (obj == null)
			{
				return null;
			}
			return ((Component)obj).gameObject;
		}

		private static T? GetFieldValue<T>(object instance, string fieldName)
		{
			return GetFieldValue(instance, fieldName, default(T));
		}

		private static T GetFieldValue<T>(object instance, string fieldName, T fallback)
		{
			if (instance == null)
			{
				return fallback;
			}
			Type type2 = ((instance is Type type) ? type : instance.GetType());
			object obj = ((instance is Type) ? null : instance);
			MemberInfo memberInfo = ResolveMember(type2, fieldName);
			object obj2 = ((memberInfo is FieldInfo fieldInfo) ? fieldInfo.GetValue(obj) : ((!(memberInfo is PropertyInfo propertyInfo) || !propertyInfo.CanRead) ? null : propertyInfo.GetValue(obj)));
			object obj3 = obj2;
			if (obj3 is T)
			{
				return (T)obj3;
			}
			if (obj3 == null)
			{
				return fallback;
			}
			try
			{
				return (T)Convert.ChangeType(obj3, typeof(T));
			}
			catch
			{
				return fallback;
			}
		}

		private static void SetFieldValue(object instance, string fieldName, object? value)
		{
			if (instance == null)
			{
				return;
			}
			Type type2 = ((instance is Type type) ? type : instance.GetType());
			object obj = ((instance is Type) ? null : instance);
			MemberInfo memberInfo = ResolveMember(type2, fieldName);
			if (!(memberInfo is FieldInfo fieldInfo))
			{
				if (memberInfo is PropertyInfo propertyInfo && propertyInfo.CanWrite)
				{
					propertyInfo.SetValue(obj, value);
				}
			}
			else
			{
				fieldInfo.SetValue(obj, value);
			}
		}

		private static void SetFieldIfNull(object instance, string fieldName, object? value)
		{
			if (value != null)
			{
				Type type2 = ((instance is Type type) ? type : instance.GetType());
				object obj = ((instance is Type) ? null : instance);
				MemberInfo memberInfo = ResolveMember(type2, fieldName);
				object obj2 = ((memberInfo is FieldInfo fieldInfo) ? fieldInfo.GetValue(obj) : ((!(memberInfo is PropertyInfo propertyInfo) || !propertyInfo.CanRead) ? new object() : propertyInfo.GetValue(obj)));
				if (obj2 == null)
				{
					SetFieldValue(instance, fieldName, value);
				}
			}
		}

		private static void TryInvoke(object instance, string methodName, params object[] args)
		{
			AccessTools.Method(instance.GetType(), methodName, (Type[])null, (Type[])null)?.Invoke(instance, args);
		}

		private static void ApplyHideFlags(Object obj)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			obj.hideFlags = (HideFlags)(obj.hideFlags | 0x23);
			GameObject val = (GameObject)(object)((obj is GameObject) ? obj : null);
			if (val == null)
			{
				return;
			}
			Transform[] componentsInChildren = val.GetComponentsInChildren<Transform>(true);
			foreach (Transform obj2 in componentsInChildren)
			{
				((Object)obj2).hideFlags = (HideFlags)(((Object)obj2).hideFlags | 0x23);
				GameObject gameObject = ((Component)obj2).gameObject;
				((Object)gameObject).hideFlags = (HideFlags)(((Object)gameObject).hideFlags | 0x23);
				Component[] components = ((Component)obj2).GetComponents<Component>();
				foreach (Component val2 in components)
				{
					if ((Object)(object)val2 != (Object)null)
					{
						((Object)val2).hideFlags = (HideFlags)(((Object)val2).hideFlags | 0x23);
					}
				}
			}
		}

		private static MemberInfo? ResolveMember(Type type, string memberName)
		{
			string key = type.AssemblyQualifiedName + "|" + memberName;
			if (MemberCache.TryGetValue(key, out MemberInfo value))
			{
				return value;
			}
			Type type2 = type;
			while (type2 != null)
			{
				FieldInfo field = type2.GetField(memberName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null)
				{
					MemberCache[key] = field;
					return field;
				}
				PropertyInfo property = type2.GetProperty(memberName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (property != null)
				{
					MemberCache[key] = property;
					return property;
				}
				type2 = type2.BaseType;
			}
			MemberCache[key] = null;
			return null;
		}

		private static bool HasBatteryCompatibility(ItemBattery itemBattery)
		{
			if ((Object)(object)GetFieldValue<BatteryVisualLogic>(itemBattery, "batteryVisualLogic") != (Object)null && (Object)(object)GetFieldValue<ItemAttributes>(itemBattery, "itemAttributes") != (Object)null && (Object)(object)GetFieldValue<ItemEquippable>(itemBattery, "itemEquippable") != (Object)null)
			{
				return (Object)(object)GetFieldValue<PhysGrabObject>(itemBattery, "physGrabObject") != (Object)null;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(Items), "RegisterItem", new Type[] { typeof(ItemContent) })]
	internal static class EmpressBigNukeValuableCompatPatchPluginRegisterItemContentPatch
	{
		[HarmonyPrefix]
		private static void Prefix(ItemContent itemContent)
		{
			EmpressBigNukeValuableCompatPatchPlugin.PrepareRegisteredItem(itemContent);
		}
	}
	[HarmonyPatch(typeof(Items), "RegisterItem", new Type[] { typeof(ItemAttributes) })]
	internal static class EmpressBigNukeValuableCompatPatchPluginRegisterItemAttributesPatch
	{
		[HarmonyPrefix]
		private static void Prefix(ItemAttributes itemAttributes)
		{
			EmpressBigNukeValuableCompatPatchPlugin.PrepareRegisteredItem(itemAttributes);
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "Start")]
	internal static class EmpressBigNukeValuableCompatPatchPluginItemBatteryStartPatch
	{
		[HarmonyPrefix]
		private static void Prefix(ItemBattery __instance)
		{
			EmpressBigNukeValuableCompatPatchPlugin.PrepareBattery(__instance);
		}

		[HarmonyPostfix]
		private static void Postfix(ItemBattery __instance)
		{
			EmpressBigNukeValuableCompatPatchPlugin.FinishBatteryStart(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemGun), "Start")]
	internal static class EmpressBigNukeValuableCompatPatchPluginItemGunStartPatch
	{
		[HarmonyPrefix]
		private static void Prefix(ItemGun __instance)
		{
			EmpressBigNukeValuableCompatPatchPlugin.PrepareGun(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemGun), "ShootRPC")]
	internal static class EmpressBigNukeValuableCompatPatchPluginItemGunShootRpcPatch
	{
		[HarmonyPostfix]
		private static void Postfix(ItemGun __instance)
		{
			EmpressBigNukeValuableCompatPatchPlugin.SyncGunBattery(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "RemoveFullBar")]
	internal static class EmpressBigNukeValuableCompatPatchPluginItemBatteryRemoveFullBarPatch
	{
		[HarmonyPostfix]
		private static void Postfix(ItemBattery __instance)
		{
			EmpressBigNukeValuableCompatPatchPlugin.SyncBattery(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "SetBatteryLife")]
	internal static class EmpressBigNukeValuableCompatPatchPluginItemBatterySetBatteryLifePatch
	{
		[HarmonyPostfix]
		private static void Postfix(ItemBattery __instance)
		{
			EmpressBigNukeValuableCompatPatchPlugin.SyncBattery(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "BatteryFullPercentChangeLogic")]
	internal static class EmpressBigNukeValuableCompatPatchPluginItemBatteryFullPercentChangeLogicPatch
	{
		[HarmonyPostfix]
		private static void Postfix(ItemBattery __instance)
		{
			EmpressBigNukeValuableCompatPatchPlugin.SyncBattery(__instance);
		}
	}
	internal enum EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState
	{
		Normal,
		Low,
		Empty
	}
	internal sealed class EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicator : MonoBehaviour
	{
		private RawImage _frame;

		private RawImage _background;

		private List<RawImage> _bars;

		internal int BarCount => _bars?.Count ?? 0;

		internal void Initialize(RawImage frame, RawImage background, List<RawImage> bars)
		{
			_frame = frame;
			_background = background;
			_bars = bars;
		}

		internal void SetDisplay(int filledBars, EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState state)
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: 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_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//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_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: 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_0130: 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_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: 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_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			if (_bars != null && _bars.Count != 0)
			{
				Color color = (Color)(state switch
				{
					EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState.Empty => new Color(1f, 0.18f, 0.18f, 1f), 
					EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState.Low => new Color(1f, 0.82f, 0.2f, 1f), 
					_ => new Color(0.25f, 1f, 0.15f, 1f), 
				});
				Color val = (Color)(state switch
				{
					EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState.Empty => new Color(1f, 0.18f, 0.18f, 1f), 
					EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState.Low => new Color(1f, 0.82f, 0.2f, 1f), 
					_ => new Color(0.25f, 1f, 0.15f, 1f), 
				});
				Color val2 = (Color)(state switch
				{
					EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState.Empty => new Color(0.22f, 0.02f, 0.02f, 0.95f), 
					EmpressBigNukeValuableCompatPatchPluginLegacyAmmoIndicatorState.Low => new Color(0.12f, 0.09f, 0.02f, 0.92f), 
					_ => new Color(0.02f, 0.08f, 0.02f, 0.9f), 
				});
				((Graphic)_frame).color = color;
				((Graphic)_background).color = val2;
				for (int i = 0; i < _bars.Count; i++)
				{
					((Graphic)_bars[i]).color = ((i < filledBars) ? val : val2);
				}
			}
		}
	}
}