Skip to content

Commit

Permalink
cleanup patch logic, look for the zdo bug that prevents initializatio…
Browse files Browse the repository at this point in the history
…n on reload of game
  • Loading branch information
zolantris committed Jun 14, 2024
1 parent e127842 commit 79e6799
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
12 changes: 8 additions & 4 deletions src/ValheimRAFT/ValheimRAFT.Patches/ZNetView_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ private static bool ZNetView_ResetZDO(ZNetView __instance)
[HarmonyPostfix]
private static void ZNetView_Awake(ZNetView __instance)
{
if (__instance.m_zdo != null)
if (__instance.m_zdo == null) return;
BaseVehicleController.InitPiece(__instance);
CultivatableComponent.InitPiece(__instance);

if (ValheimRaftPlugin.Instance.AllowOldV1RaftRecipe.Value)
{
MoveableBaseRootComponent.InitPiece(__instance);
BaseVehicleController.InitPiece(__instance);
CultivatableComponent.InitPiece(__instance);
}
}

Expand All @@ -38,8 +40,10 @@ private static bool ZNetView_OnDestroy(ZNetView __instance)
if ((bool)bv)
{
bv.RemovePiece(__instance);
return true;
}
else

if (ValheimRaftPlugin.Instance.AllowOldV1RaftRecipe.Value)
{
var mbr = __instance.GetComponentInParent<MoveableBaseRootComponent>();
if ((bool)mbr)
Expand Down
16 changes: 7 additions & 9 deletions src/ZdoWatcher/ZdoWatchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class ZdoWatchManager
public static Action<ZDO>? OnReset = null;

public static ZdoWatchManager Instance = new();
private Dictionary<int, ZDO> m_zdoGuidLookup = new();
internal readonly Dictionary<int, ZDO> _mZdoGuidLookup = new();

public void Reset() => m_zdoGuidLookup.Clear();
public void Reset() => _mZdoGuidLookup.Clear();

/// <summary>
/// PersistentIds have migrated to a safer structure so players cannot potentially break their dictionaries with duplicate ZDOIDs
Expand All @@ -33,8 +33,6 @@ public class ZdoWatchManager
// }
public static bool GetPersistentID(ZDO zdo, out int id)
{
id = zdo.GetInt(ZdoVarManager.PersistentUidHash, 0);

id = zdo.GetInt(ZdoVarManager.PersistentUidHash, 0);
return id != 0;
}
Expand All @@ -51,11 +49,11 @@ public int GetOrCreatePersistentID(ZDO? zdo)
id = ZdoIdToId(zdo.m_uid);

// If the ZDO is not unique/exists in the dictionary, this number must be incremented to prevent a collision
while (m_zdoGuidLookup.ContainsKey(id))
while (_mZdoGuidLookup.ContainsKey(id))
++id;
zdo.Set(ZdoVarManager.PersistentUidHash, id, false);

m_zdoGuidLookup[id] = zdo;
_mZdoGuidLookup[id] = zdo;

return id;
}
Expand All @@ -67,15 +65,15 @@ public void HandleRegisterPersistentId(ZDO zdo)
return;
}

m_zdoGuidLookup[id] = zdo;
_mZdoGuidLookup[id] = zdo;
}

private void HandleDeregisterPersistentId(ZDO zdo)
{
if (!GetPersistentID(zdo, out var id))
return;

m_zdoGuidLookup.Remove(id);
_mZdoGuidLookup.Remove(id);
}

public void Deserialize(ZDO zdo)
Expand Down Expand Up @@ -128,7 +126,7 @@ public void Reset(ZDO zdo)
/// <returns>ZDO|null</returns>
public ZDO? GetZdo(int id)
{
return m_zdoGuidLookup.TryGetValue(id, out var zdo) ? zdo : null;
return _mZdoGuidLookup.TryGetValue(id, out var zdo) ? zdo : null;
}

public GameObject? GetGameObject(int id)
Expand Down

0 comments on commit 79e6799

Please sign in to comment.