Decompiled source of AutomaticCheatBypass v1.0.0

plugins/TythorZeth.AutomaticCheatBypass.dll

Decompiled 3 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 BepInEx;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("AutomaticCheatBypass")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AutomaticCheatBypass")]
[assembly: AssemblyTitle("AutomaticCheatBypass")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace AutomaticCheatBypass;

[BepInPlugin("local.valheim.automaticcheatbypass", "Automatic Cheat Bypass", "1.0.0")]
[BepInIncompatibility("mtsukn.CheaterBypass")]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch]
	private static class ReturnTrue
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			yield return typeof(PlayerProfile).GetProperty("s_bypassCheatChecks").GetMethod;
			yield return typeof(Achievements).GetMethod("CanGetAchievements");
		}

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

	[HarmonyPatch]
	private static class ReturnFalse
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			yield return typeof(Achievements).GetMethod("IsCheatedAtAll");
			yield return typeof(Achievements).GetMethod("IsWorldCheated");
		}

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

	[HarmonyPatch]
	private static class NewItems
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			yield return typeof(ItemDrop).GetMethod("OnCreateNew", new Type[2]
			{
				typeof(GameObject),
				typeof(bool)
			});
			yield return typeof(ItemDrop).GetMethod("OnCreateNew", new Type[2]
			{
				typeof(ItemDrop),
				typeof(bool)
			});
			yield return typeof(CharacterDrop).GetMethod("DropItems");
			MethodInfo[] methods = typeof(Inventory).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo methodInfo in methods)
			{
				if (methodInfo.Name == "AddItem" && methodInfo.GetParameters().Any((ParameterInfo parameter) => parameter.Name == "cheated" && parameter.ParameterType == typeof(bool)))
				{
					yield return methodInfo;
				}
			}
		}

		private static void Prefix(ref bool cheated)
		{
			cheated = false;
		}
	}

	[HarmonyPatch]
	private static class ExistingItems
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			return from method in typeof(Inventory).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
				where method.Name == "AddItem" && method.GetParameters().Any((ParameterInfo parameter) => parameter.Name == "item" && parameter.ParameterType == typeof(ItemData))
				select method;
		}

		private static void Prefix(ItemData item)
		{
			if (item != null)
			{
				item.m_cheated = false;
			}
		}
	}

	[HarmonyPatch(typeof(ItemDrop), "EnsureValid")]
	private static class ValidateItem
	{
		private static void Postfix(ItemDrop __instance)
		{
			__instance.m_itemData.m_cheated = false;
		}
	}

	[HarmonyPatch(typeof(Inventory), "Changed")]
	private static class PickupWarning
	{
		private static void Prefix(ref bool cheatedStateChanged)
		{
			cheatedStateChanged = false;
		}
	}

	[HarmonyPatch(typeof(PlayerProfile), "LoadPlayerFromDisk")]
	private static class LoadCharacter
	{
		private static void Postfix(PlayerProfile __instance)
		{
			__instance.m_usedCheats = false;
		}
	}

	[HarmonyPatch(typeof(PlayerProfile), "SavePlayerToDisk")]
	private static class SaveCharacter
	{
		private static void Prefix(PlayerProfile __instance)
		{
			__instance.m_usedCheats = false;
		}
	}

	[HarmonyPatch(typeof(ConsoleCommand), "RunAction")]
	private static class RunConsoleCommand
	{
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			FieldInfo field = typeof(ConsoleCommand).GetField("IsCheat");
			MethodInfo method = typeof(RunConsoleCommand).GetMethod("IgnoreCheat", BindingFlags.Static | BindingFlags.NonPublic);
			List<CodeInstruction> list = instructions.ToList();
			int num = 0;
			foreach (CodeInstruction item in list)
			{
				if (item.opcode == OpCodes.Ldfld && object.Equals(item.operand, field))
				{
					item.opcode = OpCodes.Call;
					item.operand = method;
					num++;
				}
			}
			if (num != 2)
			{
				throw new InvalidOperationException($"Expected two RunAction cheat checks, found {num}.");
			}
			return list;
		}

		private static bool IgnoreCheat(ConsoleCommand command)
		{
			return false;
		}
	}

	private const string PluginId = "local.valheim.automaticcheatbypass";

	private const BindingFlags InventoryMembers = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		harmony = new Harmony("local.valheim.automaticcheatbypass");
		try
		{
			harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Automatic cheat bypass active.");
		}
		catch (Exception)
		{
			harmony.UnpatchSelf();
			throw;
		}
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}
}