Decompiled source of NormalAchievements v1.0.2

plugins/NormalAchievements/NormalAchievements.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;

[assembly: AssemblyCopyright("Copyright © 2026 Sfantul")]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyCompany("Sfantul")]
[assembly: AssemblyTitle("NormalAchievements")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("1.0.2.0")]
namespace NormalAchievements;

[BepInPlugin("com.normalachievements.valheim", "Normal Achievements", "1.0.2")]
public sealed class NormalAchievementsPlugin : BaseUnityPlugin
{
	public const string Id = "com.normalachievements.valheim";

	private Harmony harmony;

	private void Awake()
	{
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Expected O, but got Unknown
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Expected O, but got Unknown
		try
		{
			MethodInfo method = typeof(Achievements).GetMethod("IsCheatedAtAll", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
			if (method == null || method.ReturnType != typeof(bool) || method.GetMethodBody() == null)
			{
				throw new InvalidOperationException("Expected static bool Achievements.IsCheatedAtAll() was not found.");
			}
			harmony = new Harmony("com.normalachievements.valheim");
			harmony.Patch((MethodBase)method, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(ModdedReadPatch), "Transpiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"NormalAchievements by Sfantul active: ignored one Game.isModded read in IsCheatedAtAll. All other checks are unchanged.");
		}
		catch (Exception ex)
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			((BaseUnityPlugin)this).Logger.LogError((object)("NormalAchievements refused to patch; no achievement bypass installed by this plugin. " + ex));
		}
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}
internal static class ModdedReadPatch
{
	internal static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
	{
		return Rewrite(instructions);
	}

	internal static List<CodeInstruction> Rewrite(IEnumerable<CodeInstruction> instructions)
	{
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Expected O, but got Unknown
		FieldInfo field = typeof(Game).GetField("isModded", BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		if (field == null || !field.IsStatic || field.FieldType != typeof(bool))
		{
			throw new InvalidOperationException("Expected static bool Game.isModded was not found.");
		}
		List<CodeInstruction> list = new List<CodeInstruction>();
		int num = -1;
		int num2 = 0;
		foreach (CodeInstruction instruction in instructions)
		{
			list.Add(new CodeInstruction(instruction));
			if (instruction.opcode == OpCodes.Ldsfld && field.Equals(instruction.operand))
			{
				num = list.Count - 1;
				num2++;
			}
		}
		if (num2 != 1)
		{
			throw new InvalidOperationException("Expected exactly one Game.isModded load; found " + num2 + ". Refusing patch.");
		}
		if (num > 0 && list[num - 1].opcode.OpCodeType == OpCodeType.Prefix)
		{
			throw new InvalidOperationException("Unexpected prefix before Game.isModded load. Refusing patch.");
		}
		list[num].opcode = OpCodes.Ldc_I4_0;
		list[num].operand = null;
		return list;
	}
}