diff --git a/SrcMod/Shell/Modules/Valve/VkvModule.cs b/SrcMod/Shell/Modules/Valve/VkvModule.cs index 9665f6d..0d1ecd7 100644 --- a/SrcMod/Shell/Modules/Valve/VkvModule.cs +++ b/SrcMod/Shell/Modules/Valve/VkvModule.cs @@ -23,11 +23,11 @@ public static class VkvModule if (!File.Exists(path)) throw new($"No file exists at \"{path}\". Did you mean to run \"vkv create\"?"); VkvNode? parentNode; - string parentNodeName = "this doesn't work yet."; + string parentNodeName = string.Empty; try { FileStream fs = new(path, FileMode.Open); - parentNode = SerializeVkv.Deserialize(fs); + parentNode = SerializeVkv.Deserialize(fs, out parentNodeName); if (parentNode is null) throw new("Deserialized VKV node is null."); } @@ -56,20 +56,22 @@ public static class VkvModule VkvModifyPrintAll(ref context, false); // Start modifying the parent node. - int parentSubIndex = 0; - VkvModifyNode(ref parentNode, ref parentNodeName, ref context, true, ref parentSubIndex); + VkvModifyNode(ref parentNode, ref parentNodeName, ref context, true); // Done editing, let's reset the cursor position and exit the command. Console.ResetColor(); Console.SetCursorPosition(0, context.startingCursor + context.displayLines.Count); } - private static void VkvModifyNode(ref VkvNode node, ref string nodeName, ref VkvModifyContext context, - bool isGlobal, ref int subIndex) + private static VkvModifyReturnOption VkvModifyNode(ref VkvNode node, ref string nodeName, + ref VkvModifyContext context, bool isGlobal) { + const string add = " started"; + Console.Title += add; + VkvModifyMode mode = VkvModifyMode.Default; - int doubleSubIndex = -1; // Represents the index of the sub node currently being modified. + int subIndex = -1; // Represents the index of the sub node currently being modified. // If the variable is set to -1, it represents the title. VkvSingleNode? single = node as VkvSingleNode; @@ -95,29 +97,90 @@ public static class VkvModule case ConsoleKey.DownArrow: if (tree is not null) { - doubleSubIndex++; + subIndex++; - if (doubleSubIndex == 0) + if (subIndex == 0) { // We just shifted down from the title to the first sub node. // We need to overlook the next line, '{'. context.lineIndex += 2; + + // Now we also need to start modification of the first sub node. + KeyValuePair? subNode = tree[subIndex]; + if (subNode is not null) + { + string subNodeKey = subNode.Value.Key; + VkvNode subNodeValue = subNode.Value.Value; + VkvModifyReturnOption status = + VkvModifyNode(ref subNodeValue, ref subNodeKey, ref context, false); + + // Update the parent node with our modified sub node. + tree[subIndex] = new(subNodeKey, subNodeValue); + + switch (status) + { + case VkvModifyReturnOption.IncSubIndex: + subIndex++; + context.lineIndex++; + break; + + case VkvModifyReturnOption.DecSubIndex: + subIndex--; + context.lineIndex -= 2; + break; + } + } } - else if (doubleSubIndex == tree.SubNodeCount + 1) + else if (subIndex == tree.SubNodeCount + 1) { // We're outside the maximum sub nodes. Let's increment the parent // sub index and end this method (and increment the line). - subIndex++; context.lineIndex++; - return; + Console.Title = Console.Title[..^add.Length]; + return VkvModifyReturnOption.IncSubIndex; } else { // We're in a valid range. Let's just change the sub node we're // focused on. context.lineIndex++; + + if (subIndex < tree.SubNodeCount) + { + // We are talking about an already existing node, so we also need + // to start modification of the first sub node. + KeyValuePair? subNode = tree[subIndex]; + if (subNode is not null) + { + string subNodeKey = subNode.Value.Key; + VkvNode subNodeValue = subNode.Value.Value; + + VkvModifyReturnOption status = + VkvModifyNode(ref subNodeValue, ref subNodeKey, ref context, false); + + // Update the parent node with our modified sub node. + tree[subIndex] = new(subNodeKey, subNodeValue); + + switch (status) + { + case VkvModifyReturnOption.IncSubIndex: + subIndex++; + context.lineIndex++; + break; + + case VkvModifyReturnOption.DecSubIndex: + subIndex--; + context.lineIndex--; + break; + } + } + } + else + { + // TODO: This is where we can decide to add sub nodes. + } } } else @@ -125,27 +188,27 @@ public static class VkvModule // We aren't in a tree. We just change the parent sub index and // end this method (and increment the line). - subIndex++; context.lineIndex++; - return; + Console.Title = Console.Title[..^add.Length]; + return VkvModifyReturnOption.IncSubIndex; } break; case ConsoleKey.UpArrow: if (tree is not null) { - doubleSubIndex--; + subIndex--; - if (doubleSubIndex == -2) + if (subIndex == -2) { // We're outside the maximum sub nodes. Let's decrement the parent // sub index and end this method (and decrement the line). - subIndex--; context.lineIndex--; - return; + Console.Title = Console.Title[..^add.Length]; + return VkvModifyReturnOption.DecSubIndex; } - else if (doubleSubIndex == -1) + else if (subIndex == -1) { // We just shifted down from the title to the first sub node. // We need to overlook the next line, '{'. @@ -164,9 +227,9 @@ public static class VkvModule // We aren't in a tree. We just change the parent sub index and // end this method (and decrement the line). - subIndex--; context.lineIndex--; - return; + Console.Title = Console.Title[..^add.Length]; + return VkvModifyReturnOption.DecSubIndex; } break; } @@ -229,5 +292,11 @@ public static class VkvModule { Default } + private enum VkvModifyReturnOption + { + Nothing, + IncSubIndex, + DecSubIndex + } #endregion } diff --git a/SrcMod/Valve.NET/Vkv/VkvTreeNode.cs b/SrcMod/Valve.NET/Vkv/VkvTreeNode.cs index c45a39b..df32b38 100644 --- a/SrcMod/Valve.NET/Vkv/VkvTreeNode.cs +++ b/SrcMod/Valve.NET/Vkv/VkvTreeNode.cs @@ -60,23 +60,27 @@ public class VkvTreeNode : VkvNode, IEnumerable> } } } - public VkvNode? this[int index] + public KeyValuePair? this[int index] { get { - if (p_subNodes.Count >= index || index < 0) return null; - return p_subNodes[index]; + if (index >= SubNodeCount || index < 0) return null; + return new(p_subNodeKeys[index], p_subNodes[index]); } set { - if (p_subNodes.Count >= index || index < 0) throw new IndexOutOfRangeException(); + if (index >= SubNodeCount || index < 0) throw new IndexOutOfRangeException(); if (value is null) { p_subNodeKeys.RemoveAt(index); p_subNodes.RemoveAt(index); } - else p_subNodes[index] = value; + else + { + p_subNodeKeys[index] = value.Value.Key; + p_subNodes[index] = value.Value.Value; + } } } public KeyValuePair? this[Func, bool> predicate]