Decompiled source of Buy Houses From The Start v1.0.0

plugins/BuyHousesFromTheStart.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using NodeCanvas.Framework;
using NodeCanvas.Tasks.Actions;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("BuyHousesFromTheStart")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BuyHousesFromTheStart")]
[assembly: AssemblyCopyright("Copyright © ModderJohn 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c5450fe0-edcf-483f-b9ea-4b1ef9d36da7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = "")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace BuyHousesFromTheStart;

[BepInPlugin("ModderJohn.BuyHousesFromTheStart", "BuyHousesFromTheStart", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(SetObjectActive), "OnExecute")]
	public static class SetObjectActive_OnExecute_Patch
	{
		private static bool Prefix(SetObjectActive __instance)
		{
			try
			{
				Transform agent = ((ActionTask<Transform>)(object)__instance).agent;
				if ((Object)(object)agent == (Object)null)
				{
					return true;
				}
				if (!TryGetForcedStateForObject(((Component)agent).gameObject, out var shouldBeActive))
				{
					return true;
				}
				bool vanillaState = GetVanillaState(__instance, ((Component)agent).gameObject);
				if (vanillaState == shouldBeActive)
				{
					return true;
				}
				ApplyObjectState(((Component)agent).gameObject, shouldBeActive);
				((ActionTask)__instance).EndAction();
				return false;
			}
			catch (Exception ex)
			{
				Log.LogError((object)("[BuyHousesFromTheStart] SetObjectActive prefix exception: " + ex));
				return true;
			}
		}

		private static bool GetVanillaState(SetObjectActive instance, GameObject obj)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Invalid comparison between Unknown and I4
			if ((int)instance.setTo == 2)
			{
				return !obj.activeSelf;
			}
			return (int)instance.setTo == 1;
		}
	}

	private sealed class SellerDefinition
	{
		public readonly string SceneName;

		public readonly string HouseOwnedEventUID;

		public readonly string TransformPath;

		public SellerDefinition(string sceneName, string houseOwnedEventUID, string transformPath)
		{
			SceneName = sceneName;
			HouseOwnedEventUID = houseOwnedEventUID;
			TransformPath = transformPath;
		}

		public bool MatchesObject(GameObject obj)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)obj == (Object)null))
			{
				Scene scene = obj.scene;
				if (IsInScene(((Scene)(ref scene)).name))
				{
					return GetScenePath(obj).Equals(TransformPath, StringComparison.OrdinalIgnoreCase);
				}
			}
			return false;
		}

		public bool IsInScene(string sceneName)
		{
			return sceneName.Equals(SceneName, StringComparison.OrdinalIgnoreCase);
		}
	}

	internal static ManualLogSource Log;

	private const string HarmattanHouseOwnedEventUID = "0r087PIxTUqoj6N7z2HFNw";

	private const string HarmattanHouseEntrancePath = "Environment/Interiors/IntraAreaWarpEntrance_PlayerHouse";

	private static readonly SellerDefinition[] HouseSellers = new SellerDefinition[4]
	{
		new SellerDefinition("Berg", "g403vlCU6EG0s1mI6t_rFA", "_SNPC/_Merchants/HumanSNPC_HouseSeller"),
		new SellerDefinition("Monsoon", "shhCMFa-lUqbIYS9hRcsdg", "_SNPC/CharactersToDesactivate/_Merchants/HumanSNPC_HouseSeller"),
		new SellerDefinition("Levant", "LpVUuoxfhkaWOgh6XLbarA", "_SNPC/DisablingForHMQ4/_UNPC/HumanSNPC_HouseSeller"),
		new SellerDefinition("Harmattan", "0r087PIxTUqoj6N7z2HFNw", "Interactions/NPCs/NPC_Minor/UNPC_DLC_HarmattanHouseSeller")
	};

	private void Awake()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		new Harmony("ModderJohn.BuyHousesFromTheStart").PatchAll();
		SceneManager.sceneLoaded += OnSceneLoaded;
		((MonoBehaviour)this).StartCoroutine(DelayedHouseSellerSweep(SceneManager.GetActiveScene()));
		Log.LogMessage((object)"[BuyHousesFromTheStart] Loaded.");
	}

	private void OnDestroy()
	{
		SceneManager.sceneLoaded -= OnSceneLoaded;
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		((MonoBehaviour)this).StartCoroutine(DelayedHouseSellerSweep(scene));
	}

	private static bool IsHouseOwned(string eventUID)
	{
		if (string.IsNullOrEmpty(eventUID) || (Object)(object)QuestEventManager.Instance == (Object)null)
		{
			return false;
		}
		return QuestEventManager.Instance.GetEventCurrentStack(eventUID) > 0;
	}

	private static bool TryGetForcedStateForObject(GameObject obj, out bool shouldBeActive)
	{
		shouldBeActive = false;
		if ((Object)(object)obj == (Object)null)
		{
			return false;
		}
		SellerDefinition[] houseSellers = HouseSellers;
		foreach (SellerDefinition sellerDefinition in houseSellers)
		{
			if (sellerDefinition.MatchesObject(obj))
			{
				shouldBeActive = !IsHouseOwned(sellerDefinition.HouseOwnedEventUID);
				return true;
			}
		}
		if (IsHarmattanOwnedActiveObject(obj))
		{
			shouldBeActive = IsHouseOwned("0r087PIxTUqoj6N7z2HFNw");
			return true;
		}
		return false;
	}

	private static bool IsHarmattanOwnedActiveObject(GameObject obj)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)obj == (Object)null))
		{
			Scene scene = obj.scene;
			if (((Scene)(ref scene)).name.Equals("Harmattan", StringComparison.OrdinalIgnoreCase))
			{
				return GetScenePath(obj).Equals("Environment/Interiors/IntraAreaWarpEntrance_PlayerHouse", StringComparison.OrdinalIgnoreCase);
			}
		}
		return false;
	}

	private static void ApplyHouseSellerState(GameObject obj, string eventUID)
	{
		ApplyObjectState(obj, !IsHouseOwned(eventUID));
	}

	private static void ApplyObjectState(GameObject obj, bool shouldBeActive)
	{
		if (obj.activeSelf != shouldBeActive)
		{
			obj.SetActive(shouldBeActive);
		}
	}

	private IEnumerator DelayedHouseSellerSweep(Scene scene)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		yield return (object)new WaitForSeconds(2f);
		SweepHouseSellers(scene);
	}

	private static void SweepHouseSellers(Scene scene)
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!((Scene)(ref scene)).IsValid())
			{
				return;
			}
			for (int i = 0; i < HouseSellers.Length; i++)
			{
				SellerDefinition sellerDefinition = HouseSellers[i];
				if (sellerDefinition.IsInScene(((Scene)(ref scene)).name))
				{
					ApplySellerState(scene, sellerDefinition);
				}
			}
		}
		catch (Exception ex)
		{
			Log.LogError((object)("[BuyHousesFromTheStart] SweepHouseSellers exception: " + ex));
		}
	}

	private static void ApplySellerState(Scene scene, SellerDefinition seller)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = FindByScenePath(scene, seller.TransformPath);
		if ((Object)(object)val == (Object)null)
		{
			Log.LogWarning((object)("[BuyHousesFromTheStart] Could not find " + seller.SceneName + " seller at path " + seller.TransformPath));
			return;
		}
		ApplyHouseSellerState(val, seller.HouseOwnedEventUID);
		ApplyOwnedHouseExtras(scene, seller);
	}

	private static void ApplyOwnedHouseExtras(Scene scene, SellerDefinition seller)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		if (seller.IsInScene("Harmattan") && IsHouseOwned(seller.HouseOwnedEventUID))
		{
			GameObject val = FindByScenePath(scene, "Environment/Interiors/IntraAreaWarpEntrance_PlayerHouse");
			if ((Object)(object)val != (Object)null)
			{
				ApplyObjectState(val, shouldBeActive: true);
			}
			else
			{
				Log.LogWarning((object)"[BuyHousesFromTheStart] Could not find Harmattan owned object at path Environment/Interiors/IntraAreaWarpEntrance_PlayerHouse");
			}
		}
	}

	private static GameObject FindByScenePath(Scene scene, string path)
	{
		string[] array = path.Split(new char[1] { '/' }, StringSplitOptions.RemoveEmptyEntries);
		if (array.Length == 0)
		{
			return null;
		}
		GameObject[] rootGameObjects = ((Scene)(ref scene)).GetRootGameObjects();
		for (int i = 0; i < rootGameObjects.Length; i++)
		{
			if (!((Object)rootGameObjects[i]).name.Equals(array[0], StringComparison.OrdinalIgnoreCase))
			{
				continue;
			}
			Transform val = rootGameObjects[i].transform;
			for (int j = 1; j < array.Length; j++)
			{
				val = FindDirectChild(val, array[j]);
				if ((Object)(object)val == (Object)null)
				{
					break;
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				return ((Component)val).gameObject;
			}
		}
		return null;
	}

	private static string GetScenePath(GameObject obj)
	{
		List<string> list = new List<string>();
		Transform val = obj.transform;
		while ((Object)(object)val != (Object)null)
		{
			list.Add(((Object)((Component)val).gameObject).name);
			val = val.parent;
		}
		list.Reverse();
		return string.Join("/", list.ToArray());
	}

	private static Transform FindDirectChild(Transform parent, string childName)
	{
		for (int i = 0; i < parent.childCount; i++)
		{
			Transform child = parent.GetChild(i);
			if (((Object)child).name.Equals(childName, StringComparison.OrdinalIgnoreCase))
			{
				return child;
			}
		}
		return null;
	}
}