Decompiled source of AllVanity v1.2.0

plugins/AllVanity.dll

Decompiled 5 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using AllVanity.Patches;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using BepInEx.Unity.IL2CPP.Hook;
using DropServer.VanityItems;
using GameData;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppInterop.Runtime.Runtime;
using Il2CppSystem.Collections.Generic;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyFileVersion("1.2.0")]
[assembly: AssemblyInformationalVersion("1.2.0")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("AllVanity")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyProduct("AllVanity")]
[assembly: AssemblyTitle("AllVanity")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.0.0")]
[module: UnverifiableCode]
namespace AllVanity
{
	[BepInPlugin("dev.aurirex.gtfo.allvanity", "All Vanity", "1.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInIncompatibility("com.mccad00.AmongDrip")]
	public class Plugin : BasePlugin
	{
		public const string GUID = "dev.aurirex.gtfo.allvanity";

		public const string NAME = "All Vanity";

		public const string VERSION = "1.2.0";

		public const string DEVIOUSLICK_GUID = "com.mccad00.AmongDrip";

		public const string NOBOOSTERS_GUID = "dev.aurirex.gtfo.noboosters";

		public const string SIMPLEPROGRESSION_GUID = "dev.aurirex.gtfo.simpleprogression";

		private Harmony _harmonyInstance;

		internal static ManualLogSource L;

		internal static bool noboostersLoaded = false;

		internal static bool simpleProgressionLoaded = false;

		internal static string hexColorUnlocked = "faa";

		public override void Load()
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Expected O, but got Unknown
			L = ((BasePlugin)this).Log;
			noboostersLoaded = ((BaseChainloader<BasePlugin>)(object)IL2CPPChainloader.Instance).Plugins.Any((KeyValuePair<string, PluginInfo> kvp) => string.Equals(kvp.Key, "dev.aurirex.gtfo.noboosters", StringComparison.InvariantCultureIgnoreCase));
			simpleProgressionLoaded = ((BaseChainloader<BasePlugin>)(object)IL2CPPChainloader.Instance).Plugins.Any((KeyValuePair<string, PluginInfo> kvp) => string.Equals(kvp.Key, "dev.aurirex.gtfo.simpleprogression", StringComparison.InvariantCultureIgnoreCase));
			if (simpleProgressionLoaded)
			{
				((BasePlugin)this).Log.LogInfo((object)"Simple Progression is installed, doing nothing :)");
				return;
			}
			_harmonyInstance = new Harmony("dev.aurirex.gtfo.allvanity");
			if (noboostersLoaded)
			{
				((BasePlugin)this).Log.LogInfo((object)"NoBoosters is installed, harmony patching ...");
				_harmonyInstance.PatchAll(typeof(Managed.PersistentInventoryManager_CommitPendingTransactions_Patch));
			}
			else
			{
				((BasePlugin)this).Log.LogInfo((object)"NoBoosters is NOT installed, native patching ...");
				Native.ApplyNative();
			}
		}
	}
	public static class Unlock
	{
		private static readonly HashSet<Func<VanityItemsTemplateDataBlock, UnlockState>> _lockStateFuncs = new HashSet<Func<VanityItemsTemplateDataBlock, UnlockState>>();

		public static void RegisterUnlockMethod(Func<VanityItemsTemplateDataBlock, UnlockState> func)
		{
			if (func != null)
			{
				_lockStateFuncs.Add(func);
			}
		}

		public static bool IsAllowedToUnlock(VanityItemsTemplateDataBlock block)
		{
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Expected O, but got Unknown
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Expected O, but got Unknown
			if (block == null)
			{
				return false;
			}
			if (_lockStateFuncs.Count == 0)
			{
				if (((GameDataBlockBase<VanityItemsTemplateDataBlock>)(object)block).name.StartsWith("LOCK_"))
				{
					return false;
				}
				return true;
			}
			bool flag = false;
			bool flag2 = false;
			bool flag3 = false;
			bool flag4 = default(bool);
			foreach (Func<VanityItemsTemplateDataBlock, UnlockState> lockStateFunc in _lockStateFuncs)
			{
				try
				{
					switch (lockStateFunc(block))
					{
					case UnlockState.TryUnlock:
						flag = true;
						break;
					case UnlockState.TryLock:
						flag2 = true;
						break;
					case UnlockState.ForceLock:
						flag3 = true;
						break;
					case UnlockState.ForceUnlock:
						return true;
					case UnlockState.Skip:
						break;
					}
				}
				catch (Exception ex)
				{
					ManualLogSource l = Plugin.L;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(24, 1, ref flag4);
					if (flag4)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("A provided ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("IsAllowedToUnlock");
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" func failed!");
					}
					l.LogWarning(val);
					ManualLogSource l2 = Plugin.L;
					BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(3, 3, ref flag4);
					if (flag4)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.GetType().Name);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(": ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("\n");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.StackTrace);
					}
					l2.LogError(val2);
				}
			}
			if (flag3)
			{
				return false;
			}
			if (flag)
			{
				return true;
			}
			if (((GameDataBlockBase<VanityItemsTemplateDataBlock>)(object)block).name.StartsWith("LOCK_"))
			{
				return false;
			}
			return !flag2;
		}

		public static void ReloadInventory()
		{
			PersistentInventoryManager.SetInventoryDirty(true);
		}
	}
	public enum UnlockState
	{
		Skip,
		TryUnlock,
		TryLock,
		ForceUnlock,
		ForceLock
	}
}
namespace AllVanity.Patches
{
	internal class Managed
	{
		[HarmonyPatch(typeof(PersistentInventoryManager), "CommitPendingTransactions")]
		internal static class PersistentInventoryManager_CommitPendingTransactions_Patch
		{
			[HarmonyPriority(500)]
			public static bool Prefix()
			{
				if (PersistentInventoryManager.m_dirty)
				{
					SetupVanityInventory();
					PersistentInventoryManager.m_dirty = false;
					return false;
				}
				return true;
			}
		}

		public static void SetupVanityInventory()
		{
			Plugin.L.LogWarning((object)"Setting up Vanity Item Inventory!");
			PersistentInventoryManager.Current.m_vanityItemsInventory.UpdateItems(CreateVanityPlayerData());
		}

		internal static VanityItemPlayerData CreateVanityPlayerData()
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Expected O, but got Unknown
			Il2CppArrayBase<VanityItemsTemplateDataBlock> allBlocks = GameDataBlockBase<VanityItemsTemplateDataBlock>.GetAllBlocks();
			List<VanityItemsTemplateDataBlock> list = new List<VanityItemsTemplateDataBlock>();
			foreach (VanityItemsTemplateDataBlock item in allBlocks)
			{
				if (Unlock.IsAllowedToUnlock(item))
				{
					list.Add(item);
				}
			}
			VanityItemPlayerData val = new VanityItemPlayerData(ClassInjector.DerivedConstructorPointer<VanityItemPlayerData>());
			Il2CppReferenceArray<VanityItem> val2 = new Il2CppReferenceArray<VanityItem>((long)list.Count);
			int num = 0;
			foreach (VanityItemsTemplateDataBlock item2 in list)
			{
				VanityItem val3 = new VanityItem(ClassInjector.DerivedConstructorPointer<VanityItem>())
				{
					Flags = (InventoryItemFlags)3,
					ItemId = ((GameDataBlockBase<VanityItemsTemplateDataBlock>)(object)item2).persistentID
				};
				((Il2CppArrayBase<VanityItem>)(object)val2)[num] = val3;
				num++;
			}
			val.Items = val2;
			return val;
		}
	}
	internal class Native
	{
		public unsafe delegate void UpdateItems(IntPtr _this, IntPtr data, Il2CppMethodInfo* methodInfo);

		private static readonly List<INativeDetour> _detours = new List<INativeDetour>();

		private static UpdateItems _originalUpdateItems;

		public unsafe static void* GetIl2CppMethod<T>(string methodName, string returnTypeName, bool isGeneric, params string[] argTypes) where T : Il2CppObjectBase
		{
			void** ptr = (void**)IL2CPP.GetIl2CppMethod(Il2CppClassPointerStore<T>.NativeClassPtr, isGeneric, methodName, returnTypeName, argTypes).ToPointer();
			if (ptr == null)
			{
				return ptr;
			}
			return *ptr;
		}

		internal unsafe static void ApplyNative()
		{
			_detours.Add(INativeDetour.CreateAndApply<UpdateItems>((IntPtr)(nint)Native.GetIl2CppMethod<VanityItemInventory>("UpdateItems", "System.Void", isGeneric: false, new string[1] { "VanityItemPlayerData" }), (UpdateItems)UpdateItemsPatch, ref _originalUpdateItems));
		}

		public unsafe static void UpdateItemsPatch(IntPtr self, IntPtr vanityItemPlayerData, Il2CppMethodInfo* methodInfo)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Expected O, but got Unknown
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			VanityItemInventory val = new VanityItemInventory(self);
			_originalUpdateItems(self, vanityItemPlayerData, methodInfo);
			List<uint> list = new List<uint>();
			if (val.m_backednItems == null)
			{
				val.m_backednItems = new List<VanityItem>(0);
			}
			Enumerator<VanityItem> enumerator = val.m_backednItems.GetEnumerator();
			while (enumerator.MoveNext())
			{
				VanityItem current = enumerator.Current;
				list.Add(current.id);
			}
			foreach (VanityItemsTemplateDataBlock allBlock in GameDataBlockBase<VanityItemsTemplateDataBlock>.GetAllBlocks())
			{
				if (allBlock != null && !list.Contains(((GameDataBlockBase<VanityItemsTemplateDataBlock>)(object)allBlock).persistentID) && Unlock.IsAllowedToUnlock(allBlock))
				{
					VanityItem val2 = new VanityItem(ClassInjector.DerivedConstructorPointer<VanityItem>());
					val2.publicName = $"<#{Plugin.hexColorUnlocked}>{allBlock.publicName}</color>";
					val2.type = allBlock.type;
					val2.prefab = allBlock.prefab;
					val2.flags = (VanityItemFlags)3;
					val2.id = ((GameDataBlockBase<VanityItemsTemplateDataBlock>)(object)allBlock).persistentID;
					val.m_backednItems.Add(val2);
				}
			}
		}
	}
}