Decompiled source of FastTokenDraw v1.0.0

FastTokenDraw.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace FastTokenDraw;

[BepInPlugin("com.fasttokendraw.repo", "FastTokenDraw", "1.0.0")]
[BepInProcess("REPO.exe")]
public class FastTokenDrawPlugin : BaseUnityPlugin
{
	public const string Guid = "com.fasttokendraw.repo";

	public const string PluginName = "FastTokenDraw";

	public const string Version = "1.0.0";

	internal static FastTokenDrawPlugin Instance;

	internal static ManualLogSource Log;

	internal ConfigEntry<bool> CfgEnabled;

	internal ConfigEntry<bool> CfgDisableAnimation;

	internal ConfigEntry<int> CfgDrawCount;

	internal ConfigEntry<bool> CfgVerbose;

	internal ConfigEntry<bool> CfgAbort;

	private Harmony harmony;

	internal bool IsActive
	{
		get
		{
			if (CfgEnabled != null)
			{
				return CfgEnabled.Value;
			}
			return false;
		}
	}

	internal bool AnimationDisabled
	{
		get
		{
			if (CfgDisableAnimation != null)
			{
				return CfgDisableAnimation.Value;
			}
			return false;
		}
	}

	internal int RequestedCount
	{
		get
		{
			if (CfgDrawCount == null)
			{
				return 1;
			}
			return Mathf.Max(1, CfgDrawCount.Value);
		}
	}

	internal bool Verbose
	{
		get
		{
			if (CfgVerbose != null)
			{
				return CfgVerbose.Value;
			}
			return false;
		}
	}

	private void Awake()
	{
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Expected O, but got Unknown
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		CfgEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "模组总开关。关闭后完全恢复原版抽取行为。");
		CfgDisableAnimation = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DisableDrawAnimation", false, "关闭抽奖动画:一次把抽奖结果全部结算完毕,不播放机器动画。\nfalse = 保留原版动画(每次抽取都完整播放一遍)。");
		CfgDrawCount = ((BaseUnityPlugin)this).Config.Bind<int>("General", "DrawCount", 1, new ConfigDescription("一次性抽奖数量(最小 1)。\n若该值大于当前持有的服饰币数量,则按实际持有量抽取。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 999), Array.Empty<object>()));
		CfgAbort = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AbortDraw", false, "【重置】把它切换为开启,立刻中止正在播放的抽奖动画和整批连抽;\n尚未抽取的服饰币不会被消耗。执行后本项会自动变回关闭(false)。");
		CfgVerbose = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "VerboseLog", false, "输出每一次抽取的详细日志(稀有度 + 饰品名)。");
		Log.LogInfo((object)string.Format("{0} v{1} 已加载 | Enabled={2} | DisableDrawAnimation={3} | DrawCount={4}", "FastTokenDraw", "1.0.0", CfgEnabled.Value, CfgDisableAnimation.Value, CfgDrawCount.Value));
		if (!DrawCore.Initialize())
		{
			Log.LogError((object)"关键接口反射失败,模组将保持待机状态(原版行为不受影响)。");
			return;
		}
		try
		{
			harmony = new Harmony("com.fasttokendraw.repo");
			InteractPatch.Apply(harmony);
			StateIdlePatch.Apply(harmony);
			Log.LogInfo((object)"Harmony 补丁安装完成。");
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Harmony 补丁安装失败:" + ex));
		}
	}

	private void Update()
	{
		if (CfgAbort != null && CfgAbort.Value)
		{
			CfgAbort.Value = false;
			DrawCore.AbortAll();
		}
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
			harmony = null;
		}
	}
}
internal static class DrawCore
{
	private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

	private static FieldInfo tokensField;

	private static FieldInfo unlocksField;

	internal static MethodInfo interactMethod;

	private static MethodInfo updateStateMethod;

	private static FieldInfo stateCurrentField;

	private static FieldInfo stateTimerField;

	private static FieldInfo stateImpulseField;

	private static FieldInfo infoTextField;

	private static FieldInfo infoActiveField;

	private static FieldInfo extractionPointField;

	private static MethodInfo updateHaulTextShop;

	private static int pendingDraw;

	private static bool suppress;

	internal static volatile bool abortRequested;

	internal static bool Suppressed => suppress;

	internal static bool Initialize()
	{
		try
		{
			tokensField = typeof(MetaManager).GetField("cosmeticTokens", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			unlocksField = typeof(MetaManager).GetField("cosmeticUnlocks", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			interactMethod = typeof(CosmeticShopMachine).GetMethod("Interact", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
			updateStateMethod = typeof(CosmeticShopMachine).GetMethod("UpdateState", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			stateCurrentField = typeof(CosmeticShopMachine).GetField("stateCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			stateTimerField = typeof(CosmeticShopMachine).GetField("stateTimer", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			stateImpulseField = typeof(CosmeticShopMachine).GetField("stateImpulse", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			infoTextField = typeof(CosmeticShopMachine).GetField("infoTextTrigger", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			infoActiveField = typeof(InfoTextTrigger).GetField("active", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			extractionPointField = typeof(RoundDirector).GetField("extractionPointCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			updateHaulTextShop = typeof(ExtractionPoint).GetMethod("UpdateHaulTextShop", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (tokensField == null || interactMethod == null || stateCurrentField == null || stateTimerField == null)
			{
				FastTokenDrawPlugin.Log.LogError((object)"缺少必要反射句柄(服饰币列表或状态机字段)。");
				return false;
			}
			return true;
		}
		catch (Exception ex)
		{
			FastTokenDrawPlugin.Log.LogError((object)("反射初始化异常:" + ex));
			return false;
		}
	}

	internal static List<int> GetTokens(MetaManager meta)
	{
		if ((Object)(object)meta == (Object)null || tokensField == null)
		{
			return null;
		}
		return tokensField.GetValue(meta) as List<int>;
	}

	internal static List<int> GetUnlocks(MetaManager meta)
	{
		if ((Object)(object)meta == (Object)null || unlocksField == null)
		{
			return null;
		}
		return unlocksField.GetValue(meta) as List<int>;
	}

	internal static State GetState(CosmeticShopMachine machine)
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		object value = stateCurrentField.GetValue(machine);
		if (value == null)
		{
			return (State)0;
		}
		return (State)value;
	}

	internal static float GetStateTimer(CosmeticShopMachine machine)
	{
		object value = stateTimerField.GetValue(machine);
		if (!(value is float))
		{
			return 0f;
		}
		return (float)value;
	}

	internal static bool IsPlayerAtMachine(CosmeticShopMachine machine)
	{
		if (infoTextField == null || infoActiveField == null)
		{
			return false;
		}
		object? value = infoTextField.GetValue(machine);
		InfoTextTrigger val = (InfoTextTrigger)((value is InfoTextTrigger) ? value : null);
		if ((Object)(object)val == (Object)null)
		{
			return false;
		}
		object value2 = infoActiveField.GetValue(val);
		if (value2 is bool)
		{
			return (bool)value2;
		}
		return false;
	}

	internal static int CurrencyForRarity(Rarity rarity)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected I4, but got Unknown
		return (int)rarity switch
		{
			0 => 2, 
			1 => 4, 
			2 => 8, 
			3 => 15, 
			_ => 0, 
		};
	}

	internal static void InstantDraw(CosmeticShopMachine machine, int requested)
	{
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01de: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0186: Unknown result type (might be due to invalid IL or missing references)
		MetaManager instance = MetaManager.instance;
		if ((Object)(object)instance == (Object)null || instance.cosmeticAssets == null)
		{
			return;
		}
		List<int> tokens = GetTokens(instance);
		if (tokens == null || tokens.Count == 0)
		{
			return;
		}
		List<int> unlocks = GetUnlocks(instance);
		bool flag = SemiFunc.IsMasterClientOrSingleplayer();
		int num = Mathf.Clamp(requested, 1, tokens.Count);
		bool flag2 = (Object)(object)FastTokenDrawPlugin.Instance != (Object)null && FastTokenDrawPlugin.Instance.Verbose;
		int num2 = 0;
		int num3 = 0;
		int num4 = 0;
		List<string> list = new List<string>();
		bool flag3 = false;
		for (int i = 0; i < num; i++)
		{
			if (tokens.Count == 0)
			{
				break;
			}
			if (abortRequested)
			{
				FastTokenDrawPlugin.Log.LogInfo((object)"批量抽取收到重置请求,停止结算剩余次数。");
				break;
			}
			Rarity val = (Rarity)tokens[tokens.Count - 1];
			CosmeticAsset val2;
			try
			{
				val2 = instance.CosmeticLockedGet(val);
			}
			catch (Exception ex)
			{
				FastTokenDrawPlugin.Log.LogError((object)("抽取判定异常,已中止本次批量抽取:" + ex.Message));
				break;
			}
			if ((Object)(object)val2 != (Object)null)
			{
				bool flag4 = false;
				if (unlocks != null)
				{
					int num5 = instance.cosmeticAssets.IndexOf(val2);
					if (num5 >= 0 && !unlocks.Contains(num5))
					{
						unlocks.Add(num5);
						flag4 = true;
					}
				}
				else
				{
					flag4 = instance.CosmeticUnlock(val2);
				}
				list.Add(string.Format("{0} [{1}]{2}", val2.assetName, val, flag4 ? "" : "(已在收藏中)"));
				num3++;
				if (flag2)
				{
					FastTokenDrawPlugin.Log.LogInfo((object)string.Format("  #{0} 稀有度={1} 饰品={2}{3}", i + 1, val, val2.assetName, flag4 ? "" : "(已解锁)"));
				}
			}
			else
			{
				if (!flag)
				{
					flag3 = true;
					break;
				}
				num4 += CurrencyForRarity(val);
				list.Add($"金币 +{CurrencyForRarity(val)} [{val}]");
			}
			num2++;
		}
		if (num2 <= 0)
		{
			if (flag3)
			{
				FastTokenDrawPlugin.Log.LogWarning((object)"该稀有度饰品已全部解锁,需要折算金币,但当前不是主机,无法结算——本次未消耗任何服饰币。");
			}
			return;
		}
		tokens.RemoveRange(tokens.Count - num2, num2);
		try
		{
			instance.Save();
		}
		catch (Exception ex2)
		{
			FastTokenDrawPlugin.Log.LogError((object)("存档写入失败:" + ex2.Message));
		}
		if (num4 > 0)
		{
			SemiFunc.StatSetRunCurrency(SemiFunc.StatGetRunCurrency() + num4);
			RefreshCurrencyUi();
			if ((Object)(object)ShopIncreaseUI.instance != (Object)null)
			{
				ShopIncreaseUI.instance.ShowIncrease(num4, 3f);
			}
		}
		if ((Object)(object)CosmeticTokenUI.instance != (Object)null)
		{
			CosmeticTokenUI.instance.Setup();
		}
		LogSummary(requested, num2, num3, list, num4, flag3);
	}

	private static void LogSummary(int requested, int consumed, int unlocked, List<string> rewards, int currency, bool stoppedByCurrency)
	{
		string text = $"即时抽取完成:请求 {requested} 次 → 实际消耗服饰币 {consumed} 个 → 获得饰品 {unlocked} 件";
		if (currency > 0)
		{
			text += $" + 折算金币 {currency}";
		}
		if (stoppedByCurrency)
		{
			text += "(因非主机无法折算金币而提前停止)";
		}
		FastTokenDrawPlugin.Log.LogInfo((object)text);
		if ((Object)(object)FastTokenDrawPlugin.Instance != (Object)null && FastTokenDrawPlugin.Instance.Verbose)
		{
			for (int i = 0; i < rewards.Count; i++)
			{
				FastTokenDrawPlugin.Log.LogInfo((object)("  → " + rewards[i]));
			}
		}
	}

	internal static void RefreshCurrencyUi()
	{
		try
		{
			if ((Object)(object)CurrencyUI.instance != (Object)null)
			{
				CurrencyUI.instance.FetchCurrency();
			}
			if (!(extractionPointField != null) || !(updateHaulTextShop != null))
			{
				return;
			}
			RoundDirector instance = RoundDirector.instance;
			if ((Object)(object)instance != (Object)null)
			{
				object? value = extractionPointField.GetValue(instance);
				ExtractionPoint val = (ExtractionPoint)((value is ExtractionPoint) ? value : null);
				if ((Object)(object)val != (Object)null)
				{
					updateHaulTextShop.Invoke(val, null);
				}
			}
		}
		catch (Exception ex)
		{
			FastTokenDrawPlugin.Log.LogWarning((object)("刷新货币 UI 失败:" + ex.Message));
		}
	}

	internal static void TickAutoDraw(CosmeticShopMachine machine)
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		if (pendingDraw <= 0)
		{
			return;
		}
		if ((Object)(object)machine == (Object)null)
		{
			pendingDraw = 0;
		}
		else if (abortRequested)
		{
			pendingDraw = 0;
		}
		else
		{
			if ((int)GetState(machine) != 0)
			{
				return;
			}
			List<int> tokens = GetTokens(MetaManager.instance);
			if (tokens == null || tokens.Count == 0)
			{
				pendingDraw = 0;
			}
			else
			{
				if (GetStateTimer(machine) > 0f)
				{
					return;
				}
				if (IsPlayerAtMachine(machine))
				{
					pendingDraw--;
					suppress = true;
					try
					{
						interactMethod.Invoke(machine, null);
						return;
					}
					catch (Exception ex)
					{
						FastTokenDrawPlugin.Log.LogError((object)("自动续抽失败,已停止:" + ex));
						pendingDraw = 0;
						return;
					}
					finally
					{
						suppress = false;
					}
				}
				FastTokenDrawPlugin.Log.LogInfo((object)$"玩家已离开抽奖机,自动连抽中止,剩余 {pendingDraw} 次未执行(服饰币未消耗)。");
				pendingDraw = 0;
			}
		}
	}

	internal static void SetPending(int value)
	{
		pendingDraw = ((value >= 0) ? value : 0);
	}

	internal static void ClearPending()
	{
		pendingDraw = 0;
	}

	internal static void AbortAll()
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		int num = pendingDraw;
		pendingDraw = 0;
		abortRequested = true;
		bool flag = false;
		CosmeticShopMachine instance = CosmeticShopMachine.instance;
		if ((Object)(object)instance != (Object)null)
		{
			try
			{
				if ((int)GetState(instance) != 0)
				{
					flag = ForceIdle(instance);
					if (!flag)
					{
						FastTokenDrawPlugin.Log.LogWarning((object)"重置时未能复位抽奖机状态,动画可能播放到本枚结束(数值不受影响)。");
					}
				}
			}
			catch (Exception ex)
			{
				FastTokenDrawPlugin.Log.LogWarning((object)("重置时访问抽奖机状态失败:" + ex.Message));
			}
		}
		RefreshCurrencyUi();
		abortRequested = false;
		FastTokenDrawPlugin.Log.LogInfo((object)string.Format("已重置:{0},撤销 {1} 次排队连抽,未抽取的服饰币保持原样。", flag ? "抽奖动画已中止并回到待机" : "当前没有进行中的抽奖动画", num));
	}

	private static bool ForceIdle(CosmeticShopMachine machine)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		State val = (State)0;
		if (SemiFunc.IsMasterClientOrSingleplayer() && updateStateMethod != null)
		{
			try
			{
				updateStateMethod.Invoke(machine, new object[1] { val });
				return true;
			}
			catch (Exception ex)
			{
				FastTokenDrawPlugin.Log.LogWarning((object)("调用 UpdateState(Idle) 失败,改用直接写字段:" + ex.Message));
			}
		}
		try
		{
			stateCurrentField.SetValue(machine, val);
			if (stateImpulseField != null)
			{
				stateImpulseField.SetValue(machine, true);
			}
			if (stateTimerField != null)
			{
				stateTimerField.SetValue(machine, 0f);
			}
			return true;
		}
		catch (Exception ex2)
		{
			FastTokenDrawPlugin.Log.LogError((object)("复位抽奖机状态失败:" + ex2.Message));
			return false;
		}
	}
}
internal static class InteractPatch
{
	internal static void Apply(Harmony harmony)
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Expected O, but got Unknown
		MethodInfo interactMethod = DrawCore.interactMethod;
		if (interactMethod == null)
		{
			FastTokenDrawPlugin.Log.LogError((object)"未找到 CosmeticShopMachine.Interact,补丁跳过。");
			return;
		}
		MethodInfo method = typeof(InteractPatch).GetMethod("Prefix", BindingFlags.Static | BindingFlags.NonPublic);
		harmony.Patch((MethodBase)interactMethod, new HarmonyMethod(method), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
	}

	private static bool Prefix(CosmeticShopMachine __instance)
	{
		FastTokenDrawPlugin instance = FastTokenDrawPlugin.Instance;
		if ((Object)(object)instance == (Object)null || !instance.IsActive)
		{
			return true;
		}
		if (DrawCore.Suppressed)
		{
			return true;
		}
		MetaManager instance2 = MetaManager.instance;
		if ((Object)(object)instance2 == (Object)null)
		{
			return true;
		}
		List<int> tokens = DrawCore.GetTokens(instance2);
		if (tokens == null || tokens.Count == 0)
		{
			return true;
		}
		int requestedCount = instance.RequestedCount;
		bool animationDisabled = instance.AnimationDisabled;
		if (requestedCount <= 1 && !animationDisabled)
		{
			return true;
		}
		int num = Mathf.Clamp(requestedCount, 1, tokens.Count);
		if (!animationDisabled)
		{
			DrawCore.SetPending(num - 1);
			if (num > 1)
			{
				FastTokenDrawPlugin.Log.LogInfo((object)$"连抽开始:本次共抽 {num} 次(请求 {requestedCount},持有 {tokens.Count}),保留原版动画。");
			}
			return true;
		}
		DrawCore.ClearPending();
		DrawCore.InstantDraw(__instance, num);
		return false;
	}
}
internal static class StateIdlePatch
{
	internal static void Apply(Harmony harmony)
	{
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		MethodInfo method = typeof(CosmeticShopMachine).GetMethod("StateIdle", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
		if (method == null)
		{
			FastTokenDrawPlugin.Log.LogError((object)"未找到 CosmeticShopMachine.StateIdle,自动续抽不可用。");
			return;
		}
		MethodInfo method2 = typeof(StateIdlePatch).GetMethod("Postfix", BindingFlags.Static | BindingFlags.NonPublic);
		harmony.Patch((MethodBase)method, (HarmonyMethod)null, new HarmonyMethod(method2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
	}

	private static void Postfix(CosmeticShopMachine __instance)
	{
		FastTokenDrawPlugin instance = FastTokenDrawPlugin.Instance;
		if (!((Object)(object)instance == (Object)null) && instance.IsActive)
		{
			DrawCore.TickAutoDraw(__instance);
		}
	}
}