Decompiled source of AlmanacAchievementRespawnPatcher v1.0.1

AlmanacAchievementFix.dll

Decompiled 5 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 Almanac.Achievements;
using Almanac.UI;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("AlmanacAchievementFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AlmanacAchievementFix")]
[assembly: AssemblyTitle("AlmanacAchievementFix")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace AlmanacAchievementFix;

[BepInPlugin("com.restless.achievementpatch", "Almanac Achievements Fix", "1.0.0")]
public class AlmanacFixPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player), "OnSpawned")]
	public static class Patch_PlayerOnSpawned
	{
		private static void Postfix(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && ClearAllOnLoad.Value)
			{
				_logger.LogInfo((object)"Clearing all saved achievement effects because ClearAllOnLoad = true");
				ClearAllAchievementEffects();
			}
		}
	}

	private static ManualLogSource _logger;

	public static ConfigEntry<bool> ClearAllOnLoad;

	private void Awake()
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Expected O, but got Unknown
		_logger = ((BaseUnityPlugin)this).Logger;
		ClearAllOnLoad = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ClearAllOnLoad", false, "If true, clears all active achievement effects each time you spawn.");
		Harmony val = new Harmony("com.restless.achievementpatch");
		val.PatchAll();
		_logger.LogInfo((object)"Almanac Fix Plugin loaded: removing 'Count > 3' limit & optionally clearing old achievements.");
	}

	[HarmonyPatch(typeof(CreateAlmanac), "OnClickAchievement", new Type[] { })]
	[HarmonyTranspiler]
	public static IEnumerable<CodeInstruction> OnClickAchievement_Transpiler(IEnumerable<CodeInstruction> instructions)
	{
		List<CodeInstruction> codeList = instructions.ToList();
		bool foundSequence = false;
		for (int i = 0; i < codeList.Count; i++)
		{
			int num;
			if (i + 3 < codeList.Count && codeList[i].opcode == OpCodes.Ldsfld)
			{
				object operand = codeList[i].operand;
				if (operand != null && (operand.ToString()?.Contains("SavedAchievementEffectNames")).GetValueOrDefault() && codeList[i + 1].opcode == OpCodes.Callvirt)
				{
					object operand2 = codeList[i + 1].operand;
					if (operand2 != null && (operand2.ToString()?.Contains("get_Count")).GetValueOrDefault() && codeList[i + 2].opcode == OpCodes.Ldc_I4_3)
					{
						num = ((codeList[i + 3].opcode == OpCodes.Bgt_S) ? 1 : 0);
						goto IL_018e;
					}
				}
			}
			num = 0;
			goto IL_018e;
			IL_018e:
			if (num != 0)
			{
				foundSequence = true;
				i += 3;
			}
			else
			{
				yield return codeList[i];
			}
		}
		if (!foundSequence)
		{
			_logger.LogWarning((object)"[AlmanacFix] Could not find 'Count > 3' check in OnClickAchievement().");
		}
	}

	public static void ClearAllAchievementEffects()
	{
		if (!Object.op_Implicit((Object)(object)Player.m_localPlayer))
		{
			_logger.LogWarning((object)"No local player found, cannot remove achievements!");
			return;
		}
		SEMan sEMan = ((Character)Player.m_localPlayer).GetSEMan();
		foreach (StatusEffect item in new List<StatusEffect>(AlmanacEffectManager.ActiveAchievementEffects))
		{
			sEMan.RemoveStatusEffect(item, true);
		}
		AlmanacEffectManager.ActiveAchievementEffects.Clear();
		AlmanacEffectManager.SavedAchievementEffectNames.Clear();
		Player.m_localPlayer.m_customData[AlmanacEffectManager.AchievementKey] = "[]";
	}
}