Decompiled source of KeepAchievements v1.2.1

KeepAchievements.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("KeepAchievements")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: AssemblyInformationalVersion("1.2.1")]
[assembly: AssemblyProduct("KeepAchievements")]
[assembly: AssemblyTitle("KeepAchievements")]
[assembly: AssemblyVersion("1.2.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 KeepAchievements
{
	[BepInPlugin("com.bouns.keepachievements", "Keep Achievements & No Cheat Flags", "1.2.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "com.bouns.keepachievements";

		public const string NAME = "Keep Achievements & No Cheat Flags";

		public const string VERSION = "1.2.1";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> HideCheatStats;

		private static readonly FieldInfo F_used = AccessTools.Field(typeof(PlayerProfile), "m_usedCheats");

		private static readonly FieldInfo F_itemChe = AccessTools.Field(typeof(ItemData), "m_cheated");

		private static readonly FieldInfo F_commands = AccessTools.Field(typeof(Terminal), "commands");

		private float _timer;

		private void Awake()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			HideCheatStats = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HideCheatStats", false, "Keeps the character's Cheats stat at 0 so it no longer shows in the stats page. Also removes cheat commands (and devcommands) from the Used commands list. Other commands are kept.");
			try
			{
				new Harmony("com.bouns.keepachievements").PatchAll();
				Enforce();
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Keep Achievements & No Cheat Flags v1.2.1 loaded: achievements stay enabled, cheat flags neutralised.");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Keep Achievements & No Cheat Flags v1.2.1 FAILED to apply its patches (Valheim version mismatch?). Achievements may still be blocked. Details: " + ex));
			}
		}

		private void Update()
		{
			_timer += Time.unscaledDeltaTime;
			if (!(_timer < 2f))
			{
				_timer = 0f;
				Enforce();
			}
		}

		internal static void Enforce()
		{
			Game instance = Game.instance;
			if ((Object)(object)instance != (Object)null)
			{
				PlayerProfile playerProfile = instance.GetPlayerProfile();
				if (playerProfile != null)
				{
					F_used?.SetValue(playerProfile, false);
					if (HideCheatStats.Value)
					{
						ClearCheatStats(playerProfile);
					}
				}
			}
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer != (Object)null) || !(F_itemChe != null))
			{
				return;
			}
			Inventory inventory = ((Humanoid)localPlayer).GetInventory();
			if (inventory == null)
			{
				return;
			}
			List<ItemData> allItems = inventory.GetAllItems();
			for (int i = 0; i < allItems.Count; i++)
			{
				ItemData val = allItems[i];
				if (val != null && (bool)F_itemChe.GetValue(val))
				{
					F_itemChe.SetValue(val, false);
				}
			}
		}

		private static void ClearCheatStats(PlayerProfile prof)
		{
			PlayerStats[] playerStats = prof.m_playerStats;
			if (playerStats == null)
			{
				return;
			}
			Dictionary<string, ConsoleCommand> dictionary = F_commands?.GetValue(null) as Dictionary<string, ConsoleCommand>;
			List<string> list = null;
			PlayerStats[] array = playerStats;
			foreach (PlayerStats val in array)
			{
				if (val == null)
				{
					continue;
				}
				if (val.m_stats != null)
				{
					val.m_stats[(PlayerStatType)4] = 0f;
				}
				Dictionary<string, float> knownCommands = val.m_knownCommands;
				if (knownCommands == null || knownCommands.Count == 0)
				{
					continue;
				}
				foreach (string key in knownCommands.Keys)
				{
					if (key == "devcommands" || (dictionary != null && dictionary.TryGetValue(key, out var value) && value != null && value.IsCheat))
					{
						(list ?? (list = new List<string>())).Add(key);
					}
				}
				if (list == null)
				{
					continue;
				}
				foreach (string item in list)
				{
					knownCommands.Remove(item);
				}
				list.Clear();
			}
		}
	}
	[HarmonyPatch(typeof(PlayerProfile), "IncrementStat")]
	internal static class Patch_IncrementStat
	{
		private static bool Prefix(PlayerStatType stat)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Invalid comparison between Unknown and I4
			if (Plugin.HideCheatStats.Value)
			{
				return (int)stat != 4;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Achievements), "CanGetAchievements")]
	internal static class Patch_CanGetAchievements
	{
		private static void Postfix(ref bool __result)
		{
			__result = true;
		}
	}
	[HarmonyPatch(typeof(PlayerProfile), "get_s_bypassCheatChecks")]
	internal static class Patch_BypassCheatChecks
	{
		private static void Postfix(ref bool __result)
		{
			__result = true;
		}
	}
	[HarmonyPatch(typeof(Achievements), "IsCheatedAtAll")]
	internal static class Patch_IsCheatedAtAll
	{
		private static bool Prefix(ref bool __result)
		{
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(Achievements), "IsWorldCheated")]
	internal static class Patch_IsWorldCheated
	{
		private static bool Prefix(ref bool __result)
		{
			__result = false;
			return false;
		}
	}
	[HarmonyPatch(typeof(Inventory), "AnyCheatedItem")]
	internal static class Patch_AnyCheatedItem
	{
		private static void Postfix(ref bool __result)
		{
			__result = false;
		}
	}
	[HarmonyPatch(typeof(Inventory), "CheatedDamagingItemEquipped")]
	internal static class Patch_CheatedDamagingItemEquipped
	{
		private static void Postfix(ref bool __result)
		{
			__result = false;
		}
	}
}