Decompiled source of AnarchyBox v2.0.0

AnarchyBox.dll

Decompiled 15 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using AnarchyBox;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("AnarchyBox")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]
[assembly: AssemblyProduct("AnarchyBox")]
[assembly: AssemblyTitle("AnarchyBox")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.0.0.0")]
[module: UnverifiableCode]
[HarmonyPatch(typeof(ValuableDirector), "SetupHost")]
public class ResetPatch
{
	private static void Prefix()
	{
		Plugin.ResetRoll();
	}
}
namespace AnarchyBox
{
	[BepInPlugin("anarchy.box", "AnarchyBox", "2.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private static int forcedCount;

		private Harmony harmony;

		public static ConfigEntry<int> maxAttemptsConfig;

		public static ConfigEntry<int> forcedSuccessConfig;

		public static ConfigEntry<int> commonWeightConfig;

		public static ConfigEntry<int> uncommonWeightConfig;

		public static ConfigEntry<int> rareWeightConfig;

		public static ConfigEntry<int> ultraRareWeightConfig;

		public static ConfigEntry<bool> preventStackingConfig;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Expected O, but got Unknown
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Expected O, but got Unknown
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Expected O, but got Unknown
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Expected O, but got Unknown
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Expected O, but got Unknown
			//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_020a: Expected O, but got Unknown
			//IL_0230: Unknown result type (might be due to invalid IL or missing references)
			//IL_023c: Expected O, but got Unknown
			//IL_0270: Unknown result type (might be due to invalid IL or missing references)
			//IL_027d: Expected O, but got Unknown
			//IL_02b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02be: Expected O, but got Unknown
			//IL_02f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ff: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			harmony = new Harmony("anarchy.box");
			maxAttemptsConfig = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Max Attempts", 25, new ConfigDescription("The maximum attempts for cosmetic loops.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>()));
			forcedSuccessConfig = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Forced Successes", 5, new ConfigDescription("How many box successes to force.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
			preventStackingConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Prevent Stacking", true, new ConfigDescription("If enabled, boxes will not spawn on occupied points if all unique locations are taken. Highly recommended for multiplayer.", (AcceptableValueBase)null, Array.Empty<object>()));
			commonWeightConfig = ((BaseUnityPlugin)this).Config.Bind<int>("Rarity Weights", "Common Weight", 60, new ConfigDescription("Weight of Common boxes.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
			uncommonWeightConfig = ((BaseUnityPlugin)this).Config.Bind<int>("Rarity Weights", "Uncommon Weight", 25, new ConfigDescription("Weight of Uncommon boxes.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
			rareWeightConfig = ((BaseUnityPlugin)this).Config.Bind<int>("Rarity Weights", "Rare Weight", 12, new ConfigDescription("Weight of Rare boxes.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
			ultraRareWeightConfig = ((BaseUnityPlugin)this).Config.Bind<int>("Rarity Weights", "Ultra Rare Weight", 3, new ConfigDescription("Weight of Ultra Rare boxes.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
			((BaseUnityPlugin)this).Config.Save();
			Type type = typeof(ValuableDirector).GetNestedTypes(BindingFlags.NonPublic).FirstOrDefault((Type t) => t.Name.Contains("SetupHost"));
			if (type == null)
			{
				Log.LogError((object)"Could not find the compiler-generated enumerator type for SetupHost. Transpiler patch aborted!");
				return;
			}
			harmony.Patch((MethodBase)AccessTools.Method(typeof(ValuableDirector), "SetupHost", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Plugin), "ResetRoll", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(type, "MoveNext", (Type[])null, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "SetupHostTranspiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(ValuableDirector), "CosmeticWorldObjectLevelLoopsClampedGet", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Plugin), "MaxAttemptsPostfix", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(ValuableDirector), "CosmeticWorldObjectLevelLoopsGet", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Plugin), "MaxAttemptsPostfix", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(ValuableDirector), "SpawnCosmeticWorldObject", (Type[])null, (Type[])null), new HarmonyMethod(AccessTools.Method(typeof(Plugin), "SpawnResetPrefix", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)"ANARCHYBOX v2.0.0 LOADED!");
		}

		public static int GuaranteedRoll(int min, int max)
		{
			if (forcedCount < forcedSuccessConfig.Value)
			{
				forcedCount++;
				Log.LogInfo((object)$"Forcing box success #{forcedCount}");
				return 0;
			}
			return Random.Range(min, max);
		}

		private static IEnumerable<CodeInstruction> SetupHostTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			MethodInfo methodInfo = AccessTools.Method(typeof(Random), "Range", new Type[2]
			{
				typeof(int),
				typeof(int)
			}, (Type[])null);
			MethodInfo operand = AccessTools.Method(typeof(Plugin), "GuaranteedRoll", (Type[])null, (Type[])null);
			bool flag = false;
			for (int i = 0; i < list.Count - 2; i++)
			{
				if (list[i].opcode == OpCodes.Ldc_I4_0 && (list[i + 1].opcode == OpCodes.Ldc_I4_S || list[i + 1].opcode == OpCodes.Ldc_I4) && Convert.ToInt32(list[i + 1].operand) == 100 && CodeInstructionExtensions.Calls(list[i + 2], methodInfo))
				{
					list[i + 2].operand = operand;
					flag = true;
					break;
				}
			}
			if (flag)
			{
				Log.LogInfo((object)"SetupHostTranspiler: Patched Random.Range.");
			}
			return list;
		}

		public static void ResetRoll()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			if (SemiFunc.IsMasterClientOrSingleplayer() && SemiFunc.RunIsLevel())
			{
				forcedCount = 0;
				Log.LogInfo((object)"Reset anarchy counter on Host.");
				if (PhotonNetwork.InRoom && PhotonNetwork.CurrentRoom != null)
				{
					Hashtable val = new Hashtable();
					val[(object)"Anarchy_MaxAttempts"] = maxAttemptsConfig.Value;
					PhotonNetwork.CurrentRoom.SetCustomProperties(val, (Hashtable)null, (WebFlags)null);
					Log.LogInfo((object)$"Synced host Max Attempts ({maxAttemptsConfig.Value}) to Photon Room Properties.");
				}
			}
		}

		public static void MaxAttemptsPostfix(ref int __result)
		{
			if (PhotonNetwork.InRoom && PhotonNetwork.CurrentRoom != null && ((Dictionary<object, object>)(object)((RoomInfo)PhotonNetwork.CurrentRoom).CustomProperties).TryGetValue((object)"Anarchy_MaxAttempts", out object value))
			{
				__result = Convert.ToInt32(value);
			}
			else
			{
				__result = maxAttemptsConfig.Value;
			}
		}

		public static Rarity PickRarity()
		{
			int num = commonWeightConfig.Value + uncommonWeightConfig.Value + rareWeightConfig.Value + ultraRareWeightConfig.Value;
			if (num > 0)
			{
				int num2 = Random.Range(0, num);
				if (num2 >= commonWeightConfig.Value)
				{
					num2 -= commonWeightConfig.Value;
					if (num2 >= uncommonWeightConfig.Value)
					{
						num2 -= uncommonWeightConfig.Value;
						if (num2 >= rareWeightConfig.Value)
						{
							return (Rarity)3;
						}
						return (Rarity)2;
					}
					return (Rarity)1;
				}
				return (Rarity)0;
			}
			return (Rarity)0;
		}

		public static bool SpawnResetPrefix(int[] _volumeIndex, List<ValuableVolume>[] _volumes, ref Rarity _cosmeticRarity, ValuableDirector __instance)
		{
			//IL_0012: 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)
			//IL_001a: Expected I4, but got Unknown
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected I4, but got Unknown
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			if (!SemiFunc.IsMasterClientOrSingleplayer() || !SemiFunc.RunIsLevel())
			{
				return true;
			}
			Rarity val = _cosmeticRarity;
			_cosmeticRarity = (Rarity)(int)PickRarity();
			int num = (int)__instance.cosmeticWorldObjectSetups[(int)_cosmeticRarity].volume;
			if (_volumes == null || num >= _volumes.Length || _volumes[num] == null || _volumes[num].Count == 0)
			{
				Log.LogWarning((object)$"Skipped spawning box of rarity {_cosmeticRarity} (No spawn volumes found).");
				return false;
			}
			if (_volumeIndex[num] >= _volumes[num].Count)
			{
				if (preventStackingConfig.Value)
				{
					Log.LogInfo((object)$"Prevented stacking: all {_volumes[num].Count} spawn points of volume {num} are occupied. Skipping spawn.");
					return false;
				}
				_volumeIndex[num] = 0;
				Log.LogWarning((object)"Stacking box on old points!");
			}
			Log.LogInfo((object)$"Box spawn approved on Host: {val} -> {_cosmeticRarity} at volume index {_volumeIndex[num]}.");
			return true;
		}
	}
	public enum SpawnMode
	{
		Volume_Offline,
		All_Offline,
		AutoLevel_Online
	}
}
public enum SpawnMode
{
	Volume_Offline,
	All_Offline,
	AutoLevel_Online
}
namespace AnarchyBox
{
	public enum SpawnMode__0
	{
		Volume_Offline,
		All_Offline,
		AutoLevel_Online
	}
}