Skip to content

Commit

Permalink
Revert "feat: Workbench Organization (SubnauticaModding#486)"
Browse files Browse the repository at this point in the history
This reverts commit e992abe.
  • Loading branch information
Metious committed Sep 3, 2024
1 parent d32aae4 commit 369cc8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 88 deletions.
9 changes: 0 additions & 9 deletions Nautilus/Handlers/CraftTreeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static void AddCraftingNode(CraftTree.Type craftTree, TechType craftingIt

nodes.Add(new CraftingNode(stepsToTab, craftTree, craftingItem));
CraftTreePatcher.CraftingNodes[craftTree] = nodes;
CraftTreePatcher.CachedTrees.Remove(craftTree);
}

/// <summary>
Expand All @@ -47,7 +46,6 @@ public static void AddCraftingNode(CraftTree.Type craftTree, TechType craftingIt

nodes.Add(new CraftingNode(new string[0], craftTree, craftingItem));
CraftTreePatcher.CraftingNodes[craftTree] = nodes;
CraftTreePatcher.CachedTrees.Remove(craftTree);
}

#if SUBNAUTICA
Expand All @@ -67,7 +65,6 @@ public static void AddTabNode(CraftTree.Type craftTree, string name, string disp

craftTreeTabNodes.Add(new TabNode(new string[0], craftTree, sprite, name, displayName));
CraftTreePatcher.TabNodes[craftTree] = craftTreeTabNodes;
CraftTreePatcher.CachedTrees.Remove(craftTree);
}

/// <summary>
Expand All @@ -87,7 +84,6 @@ public static void AddTabNode(CraftTree.Type craftTree, string name, string disp

craftTreeTabNodes.Add(new TabNode(new string[0], craftTree, new Atlas.Sprite(sprite), name, displayName));
CraftTreePatcher.TabNodes[craftTree] = craftTreeTabNodes;
CraftTreePatcher.CachedTrees.Remove(craftTree);
}

/// <summary>
Expand All @@ -112,7 +108,6 @@ public static void AddTabNode(CraftTree.Type craftTree, string name, string disp

craftTreeTabNodes.Add(new TabNode(stepsToTab, craftTree, sprite, name, displayName));
CraftTreePatcher.TabNodes[craftTree] = craftTreeTabNodes;
CraftTreePatcher.CachedTrees.Remove(craftTree);
}

/// <summary>
Expand All @@ -137,7 +132,6 @@ public static void AddTabNode(CraftTree.Type craftTree, string name, string disp

craftTreeTabNodes.Add(new TabNode(stepsToTab, craftTree, new Atlas.Sprite(sprite), name, displayName));
CraftTreePatcher.TabNodes[craftTree] = craftTreeTabNodes;
CraftTreePatcher.CachedTrees.Remove(craftTree);
}

#elif BELOWZERO
Expand All @@ -157,7 +151,6 @@ public static void AddTabNode(CraftTree.Type craftTree, string name, string disp

craftTreeTabNodes.Add(new TabNode(new string[0], craftTree, sprite, name, displayName));
CraftTreePatcher.TabNodes[craftTree] = craftTreeTabNodes;
CraftTreePatcher.CachedTrees.Remove(craftTree);
}

/// <summary>
Expand All @@ -182,7 +175,6 @@ public static void AddTabNode(CraftTree.Type craftTree, string name, string disp

craftTreeTabNodes.Add(new TabNode(stepsToTab, craftTree, sprite, name, displayName));
CraftTreePatcher.TabNodes[craftTree] = craftTreeTabNodes;
CraftTreePatcher.CachedTrees.Remove(craftTree);
}

#endif
Expand All @@ -209,7 +201,6 @@ public static void RemoveNode(CraftTree.Type craftTree, params string[] stepsToN

nodesToRemove.Add(new Node(stepsToNode, craftTree));
CraftTreePatcher.NodesToRemove[craftTree] = nodesToRemove;
CraftTreePatcher.CachedTrees.Remove(craftTree);
}

/// <summary>
Expand Down
88 changes: 9 additions & 79 deletions Nautilus/Patchers/CraftTreePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,84 +18,22 @@ internal class CraftTreePatcher
internal static Dictionary<CraftTree.Type, List<Node>> NodesToRemove = new();
internal static Dictionary<CraftTree.Type, List<CraftingNode>> CraftingNodes = new();
internal static Dictionary<CraftTree.Type, List<TabNode>> TabNodes = new();
internal static Dictionary<CraftTree.Type, CraftTree> CachedTrees = new();
private const string FallbackTabNode = "Modded";
private const string VanillaRoot = "Vanilla";

#endregion

#region Patches

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

private static void CreateFallbackNodes()
{
// Workbench
CreateVanillaTabNode(CraftTree.Type.Workbench, "Modification Station", TechType.Workbench, CraftTree.WorkbenchScheme().root);
CraftTreeHandler.AddTabNode(CraftTree.Type.Workbench, FallbackTabNode + CraftTree.Type.Workbench, "Mod Items", SpriteManager.Get(TechType.Workbench));

// Fabricator
CraftTreeHandler.AddTabNode(CraftTree.Type.Fabricator, FallbackTabNode + CraftTree.Type.Fabricator, "Mod Items", SpriteManager.Get(TechType.Fabricator));

// Constructor
CraftTreeHandler.AddTabNode(CraftTree.Type.Constructor, FallbackTabNode + CraftTree.Type.Constructor, "Mod Items", SpriteManager.Get(TechType.Constructor));

// Seamoth Upgrades
CraftTreeHandler.AddTabNode(CraftTree.Type.SeamothUpgrades, FallbackTabNode + CraftTree.Type.SeamothUpgrades, "Mod Items", SpriteManager.Get(TechType.BaseUpgradeConsole));

// Map Room
CreateVanillaTabNode(CraftTree.Type.MapRoom, "Scanner Upgrades", TechType.BaseMapRoom, CraftTree.MapRoomSheme().root);
CraftTreeHandler.AddTabNode(CraftTree.Type.MapRoom, FallbackTabNode + CraftTree.Type.MapRoom, "Mod Items", SpriteManager.Get(TechType.BaseMapRoom));
#if SUBNAUTICA
// Cyclops Fabricator
CreateVanillaTabNode(CraftTree.Type.CyclopsFabricator, "Cyclops Fabricator", TechType.Cyclops, CraftTree.CyclopsFabricatorScheme().root);
CraftTreeHandler.AddTabNode(CraftTree.Type.CyclopsFabricator, FallbackTabNode + CraftTree.Type.CyclopsFabricator, "Mod Items", SpriteManager.Get(TechType.Cyclops));
#elif BELOWZERO
// SeaTruck Fabricator
CraftTreeHandler.AddTabNode(CraftTree.Type.SeaTruckFabricator, FallbackTabNode+CraftTree.Type.SeaTruckFabricator, "Mod Items", SpriteManager.Get(TechType.SeaTruckFabricator));
#endif
}

private static void CreateVanillaTabNode(CraftTree.Type treeType, string DisplayName, TechType spriteTechType, TreeNode root)
{
var removedNodes = new List<CraftNode>();
foreach (var node in root.nodes)
{
if (node is not CraftNode craftNode || craftNode.action == TreeAction.Expand)
continue;

CraftTreeHandler.RemoveNode(treeType, new[] { node.id });
removedNodes.Add(craftNode);
}

if (removedNodes.Count == 0)
return;

var vanillaTab = VanillaRoot + treeType;
CraftTreeHandler.AddTabNode(treeType, vanillaTab, DisplayName, SpriteManager.Get(spriteTechType));
foreach (var node in removedNodes)
{
InternalLogger.Debug($"Moved {node.techType0} from {treeType} root into new {vanillaTab} tab.");
CraftTreeHandler.AddCraftingNode(treeType, node.techType0, new[] { vanillaTab });
}
InternalLogger.Info($"Reorganized {removedNodes.Count} {treeType} nodes into new {vanillaTab} tab.");
}


[HarmonyPostfix]
[HarmonyPatch(typeof(CraftTree), nameof(CraftTree.GetTree))]
private static void GetTreePreFix(CraftTree.Type treeType, ref CraftTree __result)
{
if (CachedTrees.TryGetValue(treeType, out var cachedTree))
{
__result = cachedTree;
return;
}

__result ??= !CustomTrees.TryGetValue(treeType, out var customRoot) ? __result : customRoot.CustomCraftingTree;

if (__result == null)
Expand All @@ -107,10 +45,14 @@ private static void GetTreePreFix(CraftTree.Type treeType, ref CraftTree __resul
return;
}

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

#endregion
Expand Down Expand Up @@ -140,11 +82,11 @@ private static void AddCustomTabs(ref CraftTree tree, CraftTree.Type type)
continue;
}

if (currentNode.nodes.Any(node => node is CraftNode craftNode && craftNode.action == TreeAction.Craft))
/*if (currentNode.nodes.Any(node => node is CraftNode craftNode && craftNode.action == TreeAction.Craft))
{
InternalLogger.Error($"Cannot add tab: {customNode.Name} to {customNode.Scheme} at {string.Join("/", customNode.Path)} as it is being added to a parent node that contains crafting nodes. {string.Join(", ", currentNode.nodes.Where(node => node is CraftNode craftNode && craftNode.action == TreeAction.Craft).Select(x => x.id))} ");
continue;
}
}*/

// Add the new tab node.
currentNode.AddNode(new TreeNode[]
Expand All @@ -166,18 +108,6 @@ private static void PatchNodes(ref CraftTree tree, CraftTree.Type type)
continue;
}

if (currentNode.nodes.Any(x => x is CraftNode craftNode && craftNode.action == TreeAction.Expand))
{
InternalLogger.Warn($"Cannot add Crafting node: {customNode.TechType.AsString()} as it is being added to {currentNode.id} that contains Tab nodes. {string.Join(", ", currentNode.nodes.Where(node => node is CraftNode craftNode && craftNode.action == TreeAction.Expand).Select(x => x.id))}");
InternalLogger.Warn($"Adding to Fallback {FallbackTabNode} node in tree root.");

if (!TraverseTree(tree.nodes, new[] { FallbackTabNode + customNode.Scheme }, out currentNode))
{
InternalLogger.Error($"Cannot add Crafting node: {customNode.TechType.AsString()} to {customNode.Scheme} at {string.Join("/", customNode.Path)} as the fallback node could not be found.");
continue;
}
}

// Add the node.
currentNode.AddNode(new TreeNode[]
{
Expand Down

0 comments on commit 369cc8a

Please sign in to comment.