Decompiled source of NoCheatText v1.1.0

NoCheatText.dll

Decompiled 20 hours 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 BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("NoCheatText")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("NoCheatText")]
[assembly: AssemblyTitle("NoCheatText")]
[assembly: AssemblyVersion("1.1.0.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 NoCheatText
{
	[BepInPlugin("codex.valheim.nocheattext", "No Cheat Text", "1.1.0")]
	[BepInProcess("valheim.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "codex.valheim.nocheattext";

		public const string PluginName = "No Cheat Text";

		public const string PluginVersion = "1.1.0";

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> EnableAchievements;

		internal static ConfigEntry<bool> HideCommandConfirmation;

		private Harmony _harmony;

		private static readonly HashSet<string> HiddenKeys = new HashSet<string>(StringComparer.Ordinal) { "achievements_cheated_item_inventory", "achievements_picked_up_cheated_item", "achievements_temporarily_cheated", "achievements_permanently_cheated_character", "achievements_permanently_cheated_world" };

		private void Awake()
		{
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Expected O, but got Unknown
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Expected O, but got Unknown
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Hide cheat-related labels and notifications without changing cheat flags.");
			EnableAchievements = ((BaseUnityPlugin)this).Config.Bind<bool>("Achievements", "Enabled", true, "Allow achievements for modded, cheated-character, and cheated-world states.");
			HideCommandConfirmation = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HideCommandConfirmation", true, "Also hide the text shown when Valheim asks to confirm use of cheat commands.");
			_harmony = new Harmony("codex.valheim.nocheattext");
			PatchLocalizationOverload(new Type[1] { typeof(string) });
			PatchLocalizationOverload(new Type[2]
			{
				typeof(string),
				typeof(string[])
			});
			PatchAchievementCheck("CanGetAchievements", "AllowAchievements");
			PatchAchievementCheck("IsCheatedAtAll", "ReportNotCheated");
			PatchAchievementCheck("IsWorldCheated", "ReportNotCheated");
			MethodInfo methodInfo = AccessTools.Method("TextsDialog:AddStats", (Type[])null, (Type[])null);
			if (methodInfo != null)
			{
				_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "RemoveRawCheaterLabel", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"No Cheat Text 1.1.0 loaded; cheat state is left unchanged.");
		}

		private void PatchAchievementCheck(string methodName, string postfixName)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.Method("Achievements:" + methodName, (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not find Achievements." + methodName + "; continuing safely."));
			}
			else
			{
				_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), postfixName, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		public static void AllowAchievements(ref bool __result)
		{
			if (EnableAchievements.Value)
			{
				__result = true;
			}
		}

		public static void ReportNotCheated(ref bool __result)
		{
			if (EnableAchievements.Value)
			{
				__result = false;
			}
		}

		private void PatchLocalizationOverload(Type[] signature)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.Method("Localization:Localize", signature, (Type[])null);
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not find a Localization.Localize overload; continuing safely.");
			}
			else
			{
				_harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin), "HideLocalizedMessage", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		private static bool HideLocalizedMessage(ref string __result, string __0)
		{
			if (!Enabled.Value || string.IsNullOrEmpty(__0))
			{
				return true;
			}
			string text = ((__0[0] == '$') ? __0.Substring(1) : __0);
			if (HiddenKeys.Contains(text) || (HideCommandConfirmation.Value && text == "achievements_confirm_cheat"))
			{
				__result = string.Empty;
				return false;
			}
			return true;
		}

		private static IEnumerable<CodeInstruction> RemoveRawCheaterLabel(IEnumerable<CodeInstruction> instructions)
		{
			foreach (CodeInstruction instruction in instructions)
			{
				if (instruction.opcode == OpCodes.Ldstr && instruction.operand is string text && text.IndexOf("Cheater!", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					instruction.opcode = OpCodes.Call;
					instruction.operand = AccessTools.Method(typeof(Plugin), "FilterRawCheaterText", (Type[])null, (Type[])null);
					yield return instruction;
				}
				else
				{
					yield return instruction;
				}
			}
		}

		public static string FilterRawCheaterText()
		{
			if (!Enabled.Value)
			{
				return "Cheater!\n";
			}
			return string.Empty;
		}

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