warvulture
This commit is contained in:
parent
1c4b20bb06
commit
d79f78e9f0
10 changed files with 901 additions and 90 deletions
174
JotunnModStub/ZNetSceneOrphanPatch.cs
Normal file
174
JotunnModStub/ZNetSceneOrphanPatch.cs
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using HarmonyLib;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// The "teleport bug", and why it is a crash about teleporting at all.
|
||||
///
|
||||
/// WHAT THE REPORT LOOKS LIKE. Two NullReferenceExceptions out of ZNetScene.Update, the first
|
||||
/// of them through UnityEngine.Component.get_gameObject:
|
||||
///
|
||||
/// ZNetScene.RemoveObjects -> ZNetScene.CreateDestroyObjects -> ZNetScene.Update
|
||||
///
|
||||
/// WHAT IT ACTUALLY IS. ZNetScene keeps a table of every networked object it has built,
|
||||
/// m_instances, mapping ZDO to ZNetView, and RemoveObjects is the pass that unloads whatever is
|
||||
/// no longer near the player. Read out of the assembly rather than assumed, it does exactly
|
||||
/// this:
|
||||
///
|
||||
/// foreach (ZNetView value in m_instances.Values)
|
||||
/// if (value.GetZDO().TempRemoveEarmark != b) m_tempRemoved.Add(value); // (1)
|
||||
/// ...
|
||||
/// UnityEngine.Object.Destroy(zNetView.gameObject); // (2)
|
||||
///
|
||||
/// Neither line can survive an entry whose object is already gone: (1) throws a plain NRE if the
|
||||
/// ZDO has been reset out from under it, and (2) throws through get_gameObject - Unity's "this
|
||||
/// object was destroyed and you are still using it" - if the ZNetView itself is dead. That is
|
||||
/// the exact pair of stack traces in the report.
|
||||
///
|
||||
/// AND THE ENTRY IS NEVER CLEANED UP BY UNITY, which is the part worth writing down, because it
|
||||
/// is the trap every despawn in this mod has to avoid. ZNetView.OnDestroy, in full:
|
||||
///
|
||||
/// private void OnDestroy() { _ = (bool)ZNetScene.instance; if (m_heldReferences != null) ... }
|
||||
///
|
||||
/// It releases asset references and NOTHING else - it does not take itself out of m_instances.
|
||||
/// So any object that is destroyed the Unity way instead of the ZNetScene way (Object.Destroy on
|
||||
/// a networked creature or piece, a ZNetView stripped off a clone that had already woken, an
|
||||
/// ancestor destroyed with a networked child underneath it) leaves a dead entry in the table,
|
||||
/// and it sits there harmlessly until something walks the whole table.
|
||||
///
|
||||
/// WHICH IS WHY IT IS ALWAYS A TELEPORT. Standing still, the dead entry is earmarked as "near"
|
||||
/// every frame and line (2) is never reached for it. A teleport moves the reference position a
|
||||
/// world away, so on that frame everything that was around the player becomes removable, the
|
||||
/// dead entry is finally reached, and RemoveObjects throws. The throw aborts
|
||||
/// CreateDestroyObjects, which aborts ZNetScene.Update - and because the offending entry is
|
||||
/// removed on the line AFTER the one that threw, it is still there next frame. From that moment
|
||||
/// the scene manager is dead for the rest of the session: nothing new loads, nothing unloads,
|
||||
/// the player stands in an empty world.
|
||||
///
|
||||
/// WHAT THIS PATCH DOES. It is a finalizer, so it costs nothing on the millions of calls that
|
||||
/// work. When RemoveObjects does throw, it sweeps m_instances for entries whose ZNetView is dead
|
||||
/// or whose ZDO has been reset, takes them out, and swallows the exception so the next frame
|
||||
/// runs clean. Vanilla's own rule is used for the ZDO behind each one - a temporary ZDO this
|
||||
/// client owns is destroyed with its object, a persistent one is world data and is left exactly
|
||||
/// where it is, because "the view is broken" is not evidence that the player's building should
|
||||
/// be deleted.
|
||||
///
|
||||
/// If the sweep finds nothing, the exception is handed back untouched: this patch is for one
|
||||
/// specific failure and must not hide anybody else's.
|
||||
///
|
||||
/// AND IT NAMES THE CULPRIT. Every purge logs the PREFAB NAME behind each dead entry, which is
|
||||
/// the one thing the stack trace cannot tell us. Whatever is producing these - this mod, another
|
||||
/// mod, or a plain vanilla race condition - the next log from a crashed teleport says so by
|
||||
/// name, and the fix can then go where it belongs: at the despawn that used Object.Destroy
|
||||
/// instead of ZNetScene.Destroy.
|
||||
/// </summary>
|
||||
[HarmonyPatch(typeof(ZNetScene), "RemoveObjects")]
|
||||
public static class ZNetScene_RemoveObjects_OrphanPatch
|
||||
{
|
||||
private static readonly FieldInfo InstancesField = AccessTools.Field(typeof(ZNetScene), "m_instances");
|
||||
|
||||
// Reused rather than allocated per sweep: a sweep runs from inside a failing frame, and the
|
||||
// one thing that must not happen there is extra pressure on the frame that is already sick.
|
||||
private static readonly List<ZDO> Broken = new List<ZDO>();
|
||||
private static readonly Dictionary<string, int> CountByPrefab = new Dictionary<string, int>();
|
||||
|
||||
private static int totalPurged;
|
||||
|
||||
public static Exception Finalizer(Exception __exception, ZNetScene __instance)
|
||||
{
|
||||
if (__exception == null) return null;
|
||||
|
||||
int purged = Purge(__instance);
|
||||
|
||||
// Nothing broken in the table means this throw was not ours to catch.
|
||||
return purged > 0 ? null : __exception;
|
||||
}
|
||||
|
||||
private static int Purge(ZNetScene scene)
|
||||
{
|
||||
if (scene == null || InstancesField == null) return 0;
|
||||
|
||||
Dictionary<ZDO, ZNetView> instances = InstancesField.GetValue(scene) as Dictionary<ZDO, ZNetView>;
|
||||
if (instances == null) return 0;
|
||||
|
||||
Broken.Clear();
|
||||
CountByPrefab.Clear();
|
||||
|
||||
foreach (KeyValuePair<ZDO, ZNetView> instance in instances)
|
||||
{
|
||||
// Unity's own == is what makes the first half of this work: a component whose
|
||||
// GameObject has been destroyed is still a live managed object, and only this
|
||||
// comparison knows it is dead. The second half is the reset-ZDO case from line (1).
|
||||
if (instance.Value != null && instance.Value.GetZDO() != null) continue;
|
||||
|
||||
Broken.Add(instance.Key);
|
||||
}
|
||||
|
||||
if (Broken.Count == 0) return 0;
|
||||
|
||||
int purged = Broken.Count;
|
||||
int persistentLeftAlone = 0;
|
||||
|
||||
foreach (ZDO zdo in Broken)
|
||||
{
|
||||
string prefab = PrefabNameOf(zdo);
|
||||
int seen;
|
||||
CountByPrefab.TryGetValue(prefab, out seen);
|
||||
CountByPrefab[prefab] = seen + 1;
|
||||
|
||||
instances.Remove(zdo);
|
||||
|
||||
if (!zdo.Persistent && zdo.IsOwner() && ZDOMan.instance != null)
|
||||
{
|
||||
ZDOMan.instance.DestroyZDO(zdo);
|
||||
}
|
||||
else if (zdo.Persistent)
|
||||
{
|
||||
persistentLeftAlone++;
|
||||
}
|
||||
}
|
||||
|
||||
totalPurged += purged;
|
||||
|
||||
List<string> named = new List<string>(CountByPrefab.Count);
|
||||
foreach (KeyValuePair<string, int> entry in CountByPrefab)
|
||||
{
|
||||
named.Add(entry.Value == 1 ? entry.Key : entry.Key + " x" + entry.Value);
|
||||
}
|
||||
|
||||
Jotunn.Logger.LogWarning(
|
||||
$"[RacesOfValheim] ZNetScene.RemoveObjects threw on {purged} dead scene " +
|
||||
(purged == 1 ? "entry" : "entries") +
|
||||
$" - the teleport crash. Purged: {string.Join(", ", named)}. " +
|
||||
(persistentLeftAlone > 0
|
||||
? $"{persistentLeftAlone} of them had a persistent ZDO, which is world data and was left alone. "
|
||||
: "") +
|
||||
$"The world keeps loading. ({totalPurged} purged this session.) " +
|
||||
"Whoever destroyed those objects used Object.Destroy where ZNetScene.Destroy was needed.");
|
||||
|
||||
Broken.Clear();
|
||||
CountByPrefab.Clear();
|
||||
|
||||
return purged;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The name behind a dead entry, which is the whole diagnostic value of this patch. A hash
|
||||
/// with no prefab behind it is still worth printing - it means the object came from a mod
|
||||
/// that is no longer loaded, which is an answer too.
|
||||
/// </summary>
|
||||
private static string PrefabNameOf(ZDO zdo)
|
||||
{
|
||||
if (zdo == null) return "no ZDO";
|
||||
|
||||
int hash = zdo.GetPrefab();
|
||||
GameObject prefab = hash != 0 && ZNetScene.instance != null ? ZNetScene.instance.GetPrefab(hash) : null;
|
||||
|
||||
return prefab != null ? prefab.name : $"unknown prefab (hash {hash})";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue