Skip to content

Commit

Permalink
fix: Duplicated nodes each time a craft tree is accessed (#559)
Browse files Browse the repository at this point in the history
Fixed nodes getting duplicated everytime
  • Loading branch information
Metious authored Oct 19, 2024
1 parent 288e473 commit 68109eb
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions Nautilus/Patchers/CraftTreePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ internal class CraftTreePatcher

#endregion

private static Dictionary<CraftTree.Type, CraftTree> _originalTrees = new();

#region Patches

internal static void Patch(Harmony harmony)
{
harmony.PatchAll(typeof(CraftTreePatcher));
InternalLogger.Log($"CraftTreePatcher is done.", LogLevel.Debug);
}


[HarmonyPostfix]
[HarmonyPatch(typeof(CraftTree), nameof(CraftTree.GetTree))]
private static void GetTreePreFix(CraftTree.Type treeType, ref CraftTree __result)
{
__result ??= !CustomTrees.TryGetValue(treeType, out var customRoot) ? __result : customRoot.CustomCraftingTree;
var craftTree = !CustomTrees.TryGetValue(treeType, out var customRoot) ? __result : customRoot.CustomCraftingTree;

if (__result == null)
if (craftTree == null)
{
// The game actually has a few CraftTree.Type that are not used...
// None, Unused1, Unused2, etc...
Expand All @@ -45,14 +46,23 @@ private static void GetTreePreFix(CraftTree.Type treeType, ref CraftTree __resul
return;
}

if (!_originalTrees.TryGetValue(treeType, out var originalTree))
{
originalTree = CopyTree(craftTree);
_originalTrees.Add(treeType, originalTree);
}

var treeCopy = CopyTree(originalTree);

#if BELOWZERO
if (treeType is CraftTree.Type.SeaTruckFabricator)
{
PatchCraftTree(ref __result, CraftTree.Type.Fabricator);
PatchCraftTree(ref treeCopy, CraftTree.Type.Fabricator);
}
#endif
PatchCraftTree(ref __result, treeType);
CraftTree.AddToCraftableTech(__result);
PatchCraftTree(ref treeCopy, treeType);
CraftTree.AddToCraftableTech(treeCopy);
__result = treeCopy;
}

#endregion
Expand Down Expand Up @@ -149,6 +159,35 @@ private static void RemoveNodes(ref CraftTree tree, ref List<Node> nodesToRemove
}
}

private static CraftTree CopyTree(CraftTree tree)
{
return new CraftTree(tree.id, (CraftNode)CopyCraftNode(tree.nodes));
}

/// <summary>
/// Copy the specified node and it's inner nodes recursively.
/// </summary>
/// <param name="treeNode">The node to begin this operation on. Can be used on any node.</param>
/// <returns>A complete copy of the passed node.</returns>
private static TreeNode CopyCraftNode(TreeNode treeNode)
{
var copiedNode = treeNode.Copy();
copiedNode.nodes = treeNode.nodes.ToList();

if (copiedNode.nodes.Count == 0)
{
return copiedNode;
}

for (var i = 0; i < copiedNode.nodes.Count; i++)
{
treeNode.nodes[i] = CopyCraftNode(treeNode.nodes[i]);
treeNode.nodes[i].parent = copiedNode;
}

return copiedNode;
}

private static bool TraverseTree(TreeNode nodes, string[] path, out TreeNode currentNode)
{
currentNode = nodes;
Expand Down

0 comments on commit 68109eb

Please sign in to comment.