Decompiled source of AchievementsWithCheats v1.1.3

plugins/AchievementsWithCheats/AchievementsWithCheats.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using UnityEngine;

[assembly: AssemblyCompany("Sfantul")]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("1.1.3.0")]
namespace AchievementsWithCheats;

[BepInDependency("com.normalachievements.valheim", "1.0.2")]
[BepInPlugin("sfantul.achievementswithcheats", "Achievements With Cheats", "1.1.3")]
public sealed class Plugin : BaseUnityPlugin
{
	private Harmony harmony;

	private static bool bypassActive;

	private void Awake()
	{
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Expected O, but got Unknown
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Expected O, but got Unknown
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Expected O, but got Unknown
		//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Expected O, but got Unknown
		//IL_0217: Unknown result type (might be due to invalid IL or missing references)
		//IL_0223: Expected O, but got Unknown
		//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b6: Expected O, but got Unknown
		try
		{
			BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
			MethodInfo method = typeof(Achievements).GetMethod("IsCheatedAtAll", bindingAttr, null, Type.EmptyTypes, null);
			MethodInfo method2 = typeof(PlayerProfile).GetMethod("get_s_bypassCheatChecks", bindingAttr, null, Type.EmptyTypes, null);
			MethodInfo[] array = new MethodInfo[2] { method, method2 };
			foreach (MethodInfo methodInfo in array)
			{
				if (methodInfo == null || methodInfo.ReturnType != typeof(bool) || methodInfo.GetMethodBody() == null)
				{
					throw new InvalidOperationException("Expected cheat gate or native bypass getter is unavailable.");
				}
			}
			harmony = new Harmony("sfantul.achievementswithcheats");
			harmony.Patch((MethodBase)method, new HarmonyMethod(typeof(Plugin), "AllowCheatedProgress", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)method2, new HarmonyMethod(typeof(Plugin), "NativeBypass", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			MethodInfo method3 = typeof(MessageHud).GetMethod("ShowMessage", new Type[6]
			{
				typeof(MessageType),
				typeof(string),
				typeof(int),
				typeof(Sprite),
				typeof(bool),
				typeof(bool)
			});
			if (method3 == null)
			{
				throw new InvalidOperationException("Expected notification method is unavailable.");
			}
			harmony.Patch((MethodBase)method3, new HarmonyMethod(typeof(Plugin), "FilterCheatedItemMessage", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			MethodInfo method4 = typeof(ConsoleCommand).GetMethod("RunAction", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (method4 == null)
			{
				throw new MissingMethodException("ConsoleCommand.RunAction");
			}
			harmony.Patch((MethodBase)method4, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "SkipAchievementConfirmation", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			MethodInfo methodInfo2 = null;
			MethodInfo method5 = typeof(ServerOptionsGUI).GetMethod("SetKeyAndValueIsCheat", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			Type[] nestedTypes = typeof(Terminal).GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic);
			foreach (Type type in nestedTypes)
			{
				array = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (MethodInfo methodInfo in array)
				{
					if (methodInfo.DeclaringType != type || methodInfo.GetMethodBody() == null)
					{
						continue;
					}
					bool flag = false;
					bool flag2 = false;
					foreach (CodeInstruction originalInstruction in PatchProcessor.GetOriginalInstructions((MethodBase)methodInfo, (ILGenerator)null))
					{
						if (CodeInstructionExtensions.Calls(originalInstruction, method5))
						{
							flag = true;
						}
						if (CodeInstructionExtensions.Calls(originalInstruction, method))
						{
							flag2 = true;
						}
					}
					if (flag && flag2)
					{
						if (methodInfo2 != null)
						{
							throw new InvalidOperationException("Ambiguous setkey confirmation handler");
						}
						methodInfo2 = methodInfo;
					}
				}
			}
			if (methodInfo2 == null)
			{
				throw new MissingMethodException("setkey confirmation handler");
			}
			harmony.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "SkipAchievementConfirmation", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			bypassActive = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Achievements With Cheats active: modded and cheat/devcommand achievement restrictions bypassed. No unlock-all or saved-key edits performed.");
		}
		catch (Exception ex)
		{
			bypassActive = false;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			((BaseUnityPlugin)this).Logger.LogError((object)("Refused to enable cheat bypass: " + ex));
		}
	}

	private static IEnumerable<CodeInstruction> SkipAchievementConfirmation(IEnumerable<CodeInstruction> instructions)
	{
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		MethodInfo method = typeof(Achievements).GetMethod("IsCheatedAtAll", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		int num = 0;
		foreach (CodeInstruction item in list)
		{
			if (CodeInstructionExtensions.Calls(item, method))
			{
				num++;
			}
		}
		if (num != 1)
		{
			throw new InvalidOperationException("Expected exactly one command achievement confirmation check; found " + num);
		}
		foreach (CodeInstruction item2 in list)
		{
			if (CodeInstructionExtensions.Calls(item2, method))
			{
				item2.opcode = OpCodes.Ldc_I4_1;
				item2.operand = null;
			}
		}
		return list;
	}

	private static bool AllowCheatedProgress(ref bool __result)
	{
		__result = false;
		return false;
	}

	private static bool NativeBypass(ref bool __result)
	{
		__result = true;
		return false;
	}

	private static bool FilterCheatedItemMessage(string __1)
	{
		return !bypassActive || (__1 != "$achievements_picked_up_cheated_item" && __1 != "$achievements_dropped_cheated_item");
	}

	private void OnDestroy()
	{
		bypassActive = false;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}