Decompiled source of CosmeticGachaBugFix v0.4.1

CosmeticGachaBugFix.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("REPOJP")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("zabuMod")]
[assembly: AssemblyTitle("zabuMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace REPOJP.CosmeticGachaBugFix
{
	[BepInPlugin("REPOJP.CosmeticGachaBugFix", "CosmeticGachaBugFix", "0.4.1")]
	public class CosmeticGachaBugFixPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "REPOJP.CosmeticGachaBugFix";

		public const string PluginName = "CosmeticGachaBugFix";

		public const string PluginVersion = "0.4.1";

		internal static ManualLogSource Log;

		private Harmony harmony;

		private static FieldInfo interactingRewardField;

		private void Awake()
		{
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Expected O, but got Unknown
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Expected O, but got Unknown
			try
			{
				Log = ((BaseUnityPlugin)this).Logger;
				((Component)this).transform.parent = null;
				((Object)this).hideFlags = (HideFlags)61;
				Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
				Type type = AccessTools.TypeByName("CosmeticShopMachine");
				if (type == null)
				{
					Log.LogWarning((object)"CosmeticShopMachine type was not found. Cosmetic gacha bug fix was skipped.");
					return;
				}
				MethodInfo methodInfo = AccessTools.Method(type, "StateRewardCosmeticIntro", (Type[])null, (Type[])null);
				if (methodInfo == null)
				{
					Log.LogWarning((object)"CosmeticShopMachine.StateRewardCosmeticIntro was not found. Cosmetic gacha bug fix was skipped.");
					return;
				}
				interactingRewardField = AccessTools.Field(type, "interactingReward");
				if (interactingRewardField == null)
				{
					Log.LogWarning((object)"CosmeticShopMachine.interactingReward was not found. Cosmetic gacha bug fix was skipped.");
					return;
				}
				MethodInfo methodInfo2 = AccessTools.Method(typeof(CosmeticGachaBugFixPlugin), "StateRewardCosmeticIntroTranspiler", (Type[])null, (Type[])null);
				if (methodInfo2 == null)
				{
					Log.LogWarning((object)"StateRewardCosmeticIntroTranspiler was not found. Cosmetic gacha bug fix was skipped.");
					return;
				}
				harmony = new Harmony("REPOJP.CosmeticGachaBugFix");
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null);
				Log.LogInfo((object)"CosmeticGachaBugFix 0.4.1 loaded. Cosmetic Index 0 reward validation is fixed.");
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)string.Format("{0} startup failed.\n{1}", "CosmeticGachaBugFix", arg));
			}
		}

		private void OnDestroy()
		{
			try
			{
				if (harmony != null)
				{
					harmony.UnpatchSelf();
					harmony = null;
				}
			}
			catch (Exception arg)
			{
				if (Log != null)
				{
					Log.LogError((object)string.Format("{0} cleanup failed.\n{1}", "CosmeticGachaBugFix", arg));
				}
			}
		}

		private static IEnumerable<CodeInstruction> StateRewardCosmeticIntroTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			bool flag = false;
			for (int i = 2; i < list.Count; i++)
			{
				if (!flag && IsLessOrEqualBranch(list[i]) && IsZeroConstant(list[i - 1]) && IsInteractingRewardFieldLoad(list[i - 2]))
				{
					if (list[i].opcode == OpCodes.Ble_S)
					{
						list[i].opcode = OpCodes.Blt_S;
					}
					else
					{
						list[i].opcode = OpCodes.Blt;
					}
					flag = true;
				}
			}
			if (flag)
			{
				Log.LogInfo((object)"Patched cosmetic reward validation: interactingReward > 0 changed to interactingReward >= 0.");
			}
			else
			{
				Log.LogWarning((object)"Failed to find the cosmetic Index 0 reward validation. The game method may have changed.");
			}
			return list;
		}

		private static bool IsLessOrEqualBranch(CodeInstruction code)
		{
			return code.opcode == OpCodes.Ble || code.opcode == OpCodes.Ble_S;
		}

		private static bool IsZeroConstant(CodeInstruction code)
		{
			if (code.opcode == OpCodes.Ldc_I4_0)
			{
				return true;
			}
			if (code.opcode == OpCodes.Ldc_I4 && code.operand is int && (int)code.operand == 0)
			{
				return true;
			}
			return false;
		}

		private static bool IsInteractingRewardFieldLoad(CodeInstruction code)
		{
			return code.opcode == OpCodes.Ldfld && code.operand == interactingRewardField;
		}
	}
}