Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seatruck Fabricator & Fix tabs and crafting nodes on same level #556

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
124 changes: 18 additions & 106 deletions Nautilus/Patchers/CraftTreePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,85 +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();
internal static Dictionary<CraftTree.Type, TechType> Fallbacks = new()
{
{ CraftTree.Type.Workbench, TechType.Workbench },
{ CraftTree.Type.Fabricator, TechType.Fabricator },
{ CraftTree.Type.Constructor, TechType.Constructor },
{ CraftTree.Type.SeamothUpgrades, TechType.BaseUpgradeConsole },
{ CraftTree.Type.MapRoom, TechType.BaseMapRoom }
};
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()
{
CreateVanillaTabNode(CraftTree.Type.Workbench, "Modification Station", TechType.Workbench, CraftTree.WorkbenchScheme().root);
CreateVanillaTabNode(CraftTree.Type.MapRoom, "Scanner Upgrades", TechType.BaseMapRoom, CraftTree.MapRoomSheme().root);

#if SUBNAUTICA
CreateVanillaTabNode(CraftTree.Type.CyclopsFabricator, "Cyclops Fabricator", TechType.Cyclops, CraftTree.CyclopsFabricatorScheme().root);
Fallbacks.Add(CraftTree.Type.CyclopsFabricator, TechType.Cyclops);
#elif BELOWZERO
Fallbacks.Add(CraftTree.Type.SeaTruckFabricator, TechType.SeaTruckFabricator);
#endif

foreach (var pair in Fallbacks)
CreateFallbackNode(pair.Key, pair.Value);
}

private static void CreateFallbackNode(CraftTree.Type craftTreeType, TechType techTypeForSprite)
{
CraftTreeHandler.AddTabNode(craftTreeType, FallbackTabNode + craftTreeType, "Mod Items", SpriteManager.Get(techTypeForSprite));
}

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 @@ -108,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 All @@ -120,7 +61,7 @@ private static void GetTreePreFix(CraftTree.Type treeType, ref CraftTree __resul

private static void PatchCraftTree(ref CraftTree __result, CraftTree.Type type)
{
List<Node> removals = NodesToRemove.TryGetValue(type, out removals) ? removals : new List<Node>();
List<Node> removals = NodesToRemove.TryGetValue(type, out removals)? removals: new List<Node>();
RemoveNodes(ref __result, ref removals);

AddCustomTabs(ref __result, type);
Expand All @@ -135,26 +76,17 @@ private static void AddCustomTabs(ref CraftTree tree, CraftTree.Type type)
List<TabNode> customTabs = TabNodes.TryGetValue(type, out customTabs) ? customTabs : new List<TabNode>();
foreach (TabNode customNode in customTabs)
{
if (!TraverseTree(tree.nodes, customNode.Path, out var currentNode))
if(!TraverseTree(tree.nodes, customNode.Path, out var currentNode))
{
InternalLogger.Error($"Cannot add tab: {customNode.Name} to {customNode.Scheme} at {string.Join("/", customNode.Path)} as the parent node could not be found.");
continue;
}

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;
}

if (TraverseTree(currentNode, new[] { customNode.Name }, out _))
{
// This node already exists, skip it.
continue;
}

// Add the new tab node.
currentNode.AddNode(new CraftNode(customNode.Name, TreeAction.Expand, TechType.None));
currentNode.AddNode(new TreeNode[]
{
new CraftNode(customNode.Name, TreeAction.Expand, TechType.None)
});
InternalLogger.Debug($"Added tab: {customNode.Name} to {customNode.Scheme} at {string.Join("/", customNode.Path)}");
}
}
Expand All @@ -166,35 +98,15 @@ private static void PatchNodes(ref CraftTree tree, CraftTree.Type type)
{
if (!TraverseTree(tree.nodes, customNode.Path, out var currentNode))
{
InternalLogger.Warn($"Cannot add Crafting node: {customNode.TechType.AsString()} to {customNode.Scheme} at {string.Join("/", customNode.Path)} as the parent node could not be found.");

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;
}
}

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;
}
}

if (TraverseTree(currentNode, new[] { customNode.TechType.AsString(false) }, out _))
{
// This node already exists, skip it.
InternalLogger.Error($"Cannot add Crafting node: {customNode.TechType.AsString()} to {customNode.Scheme} at {string.Join("/", customNode.Path)} as the parent node could not be found.");
continue;
}

// Add the node.
currentNode.AddNode(new CraftNode(customNode.TechType.AsString(false), TreeAction.Craft, customNode.TechType));
currentNode.AddNode(new TreeNode[]
{
new CraftNode(customNode.TechType.AsString(false), TreeAction.Craft, customNode.TechType)
});
InternalLogger.Debug($"Added Crafting node: {customNode.TechType.AsString()} to {customNode.Scheme} at {string.Join("/", customNode.Path)}");
}
}
Expand Down
Loading