Decompiled source of ChilloutPathRules v1.0.2

plugins/ChilloutPathRules/ChilloutPathRules.dll

Decompiled 7 hours ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using PassiveTree;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace ChilloutPathRules;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("chillout.path.rules", "Chillout Path Rules", "1.0.2")]
public sealed class Plugin : BaseUnityPlugin
{
	public const string PluginGuid = "chillout.path.rules";

	public const string PluginName = "Chillout Path Rules";

	public const string PluginVersion = "1.0.2";

	internal static ManualLogSource PluginLog;

	private Harmony _harmony;

	private void Awake()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		PluginLog = ((BaseUnityPlugin)this).Logger;
		_harmony = new Harmony("chillout.path.rules");
		InstallOilProgression();
		InstallSetupWizardSuppression();
		_harmony.PatchAll(typeof(ResourceCellProfessionUnlockPatch));
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Path rules active: boss-gated oils, quiet setup wizard and profession-gated resource cells.");
	}

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

	private void InstallOilProgression()
	{
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Expected O, but got Unknown
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Expected O, but got Unknown
		Type type = AccessTools.TypeByName("PassiveTree.Oils");
		if (type == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"PassiveTree.Oils was not found; the patch was not applied.");
			return;
		}
		MethodInfo methodInfo = AccessTools.Method(type, "Learn", new Type[1] { typeof(Player) }, (Type[])null);
		if (methodInfo == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"PassiveTree.Oils.Learn(Player) was not found; the patch was not applied.");
			return;
		}
		HarmonyMethod val = new HarmonyMethod(typeof(OilRecipeProgressionPatch), "Prefix", (Type[])null);
		_harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		MethodInfo methodInfo2 = AccessTools.Method(typeof(Player), "UpdateKnownRecipesList", (Type[])null, (Type[])null);
		if (methodInfo2 == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Player.UpdateKnownRecipesList was not found; world-progression gating was not applied.");
			return;
		}
		HarmonyMethod val2 = new HarmonyMethod(typeof(OilRecipeProgressionPatch), "RefreshPostfix", (Type[])null);
		_harmony.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Path oil recipes follow world boss progression.");
	}

	private void InstallSetupWizardSuppression()
	{
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Expected O, but got Unknown
		Type type = AccessTools.TypeByName("PassiveTree.UI.SetupWizard");
		MethodInfo methodInfo = ((type == null) ? null : AccessTools.Method(type, "MaybeShow", (Type[])null, (Type[])null));
		if (methodInfo == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"PassiveTree.UI.SetupWizard.MaybeShow was not found; its automatic popup was not disabled.");
			return;
		}
		HarmonyMethod val = new HarmonyMethod(typeof(SetupWizardSuppressionPatch), "Prefix", (Type[])null);
		_harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Path's automatic setup wizard is disabled.");
	}
}
[HarmonyPatch(typeof(ResourceCells), "IsOpen")]
internal static class ResourceCellProfessionUnlockPatch
{
	[HarmonyPostfix]
	private static void UnlockFromProfessionCapacity(Player p, Box box, ref bool __result)
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Invalid comparison between Unknown and I4
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		if (!__result && !((Object)(object)p == (Object)null) && box != null && (int)box.Cap >= 0)
		{
			Progress val = Progress.Peek(p);
			if (val != null && val.Stats != null)
			{
				__result = val.Stats.Alt(box.Cap, (Form)2) > 0f;
			}
		}
	}
}
internal static class OilRecipeProgressionPatch
{
	private static readonly FieldInfo KnownRecipesField = AccessTools.Field(typeof(Player), "m_knownRecipes");

	private static readonly string[] OilPrefabIds = new string[4] { "PT_OilFire", "PT_OilFrost", "PT_OilLightning", "PT_OilPoison" };

	public static bool Prefix(Player __0)
	{
		try
		{
			if ((Object)(object)__0 == (Object)null || KnownRecipesField == null || (Object)(object)ObjectDB.instance == (Object)null)
			{
				return false;
			}
			if (!(KnownRecipesField.GetValue(__0) is HashSet<string> recipesObject))
			{
				return false;
			}
			int num = RemoveLockedRecipes(recipesObject, respectWorldProgression: true);
			if (num > 0)
			{
				Plugin.PluginLog.LogInfo((object)$"Removed {num} oil recipes still locked by world progression.");
			}
		}
		catch (Exception arg)
		{
			Plugin.PluginLog.LogError((object)$"Oil recipe progression could not be corrected: {arg}");
		}
		return false;
	}

	public static void RefreshPostfix(Player __instance)
	{
		try
		{
			if (!((Object)(object)__instance == (Object)null) && !(KnownRecipesField == null) && !((Object)(object)ObjectDB.instance == (Object)null) && KnownRecipesField.GetValue(__instance) is HashSet<string> recipesObject)
			{
				RemoveLockedRecipes(recipesObject, respectWorldProgression: true);
			}
		}
		catch (Exception arg)
		{
			Plugin.PluginLog.LogError((object)$"Oil world progression could not be enforced: {arg}");
		}
	}

	private static int RemoveLockedRecipes(object recipesObject, bool respectWorldProgression)
	{
		if (!(recipesObject is HashSet<string> hashSet))
		{
			return 0;
		}
		int num = 0;
		string[] oilPrefabIds = OilPrefabIds;
		foreach (string text in oilPrefabIds)
		{
			if (respectWorldProgression && IsWorldKeyActive(GetRequiredWorldKey(text)))
			{
				continue;
			}
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(text);
			if (!((Object)(object)itemPrefab == (Object)null))
			{
				ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
				string text2 = null;
				if ((Object)(object)component != (Object)null && component.m_itemData != null && component.m_itemData.m_shared != null)
				{
					text2 = component.m_itemData.m_shared.m_name;
				}
				if (!string.IsNullOrEmpty(text2) && hashSet.Remove(text2))
				{
					num++;
				}
			}
		}
		return num;
	}

	private static bool IsWorldKeyActive(string key)
	{
		if ((Object)(object)ZoneSystem.instance != (Object)null)
		{
			return ZoneSystem.instance.GetGlobalKey(key);
		}
		return false;
	}

	private static string GetRequiredWorldKey(string prefabId)
	{
		switch (prefabId)
		{
		case "PT_OilFire":
		case "PT_OilLightning":
			return "defeated_eikthyr";
		case "PT_OilPoison":
			return "defeated_gdking";
		case "PT_OilFrost":
			return "defeated_bonemass";
		default:
			return string.Empty;
		}
	}
}
internal static class SetupWizardSuppressionPatch
{
	public static bool Prefix(ref bool __result)
	{
		__result = false;
		return false;
	}
}