Files
MxValheim/MxValheim/Patch/Doors.cs
2026-02-12 03:15:36 -05:00

70 lines
2.4 KiB
C#

using BepInEx;
using HarmonyLib;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Diagnostics;
using static MxValheimMod;
namespace MxValheim.Patch
{
public class Doors_Patch
{
public class DoorRoot
{
public List<string> ignoreList { get; set; }
}
[HarmonyPatch(typeof(Door), nameof(Door.Interact))]
public static class DoorTracker
{
static void Postfix(Door __instance, Humanoid character, bool hold, bool alt, ZNetView ___m_nview)
{
ZNetView nview = __instance.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
{
// Get the prefab hash and look up the name in the master list
int prefabHash = nview.GetZDO().GetPrefab();
string prefabName = ZNetScene.instance.GetPrefab(prefabHash).name;
DoorRoot iL = JsonConvert.DeserializeObject<DoorRoot>(File.ReadAllText(MxValheimMod.AutoDoorConfigPath));
if (iL.ignoreList.Contains(prefabName)) return;
if (hold || alt || ___m_nview == null || !___m_nview.IsValid()) return;
// Get state: 0 is closed
int state = ___m_nview.GetZDO().GetInt("state");
if (state == 0) return;
lock (_doorQueueNview)
{
Debug.Log($"AutoDoor:Door.Interact Adding \"{prefabName}\" to queue.");
_doorQueueNview.Enqueue(nview);
_doorQueueDoor.Enqueue(__instance);
_doorQueueName.Enqueue(prefabName);
}
}
}
}
public void CloseNextDoor(Door door, ZNetView nview, string doorName)
{
if (door != null && nview != null && nview.IsValid())
{
if (nview.GetZDO().GetInt("state") != 0)
{
Debug.Log($"AutoDoor:CloseNextDoor Closing door \"{doorName}\".");
ZDO zd0 = nview.GetZDO();
zd0.Set("state", 0);
} else
{
Debug.Log($"AutoDoor:CloseNextDoor Door \"{doorName}\" was already closed.");
}
}
}
}
}