public string Write(KONNode node, int currentDepth = 0) { //It feels wrong to do it like this but this way it's nice and neatly indented string indent = ""; for (int i = 0; i < currentDepth; i++) { indent += " "; } string indent2 = indent + " "; string output = $"{indent}{GetCase(node.Name, NodeNameWriteMode)}\n{indent}{{"; foreach (KeyValuePair <string, string> pair in node.Values) { output += $"\n{indent2}{GetCase(pair.Key, KeyWriteMode)} = {GetCase(FormatValue(pair.Value), ValueWriteMode)}"; } for (int i = 0; i < node.Children.Count; i++) { output += $"\n{Write(node.Children[i], currentDepth + 1)}"; } for (int i = 0; i < node.Arrays.Count; i++) { KONArray currentArray = node.Arrays[i]; output += $"\n{WriteArray(currentArray, currentDepth + 1)}"; } output += $"\n{indent}}}"; return(output); }
public string WriteArray(KONArray array, int currentDepth = 0) { string indent = ""; for (int i = 0; i < currentDepth; i++) { indent += " "; } string indent2 = indent + " "; string output = $"{indent}{GetCase(array.Name, NodeNameWriteMode)}\n{indent}["; foreach (string str in array.Items) { output += $"\n{indent2}{GetCase(FormatValue(str), ValueWriteMode)}"; } output += $"\n{indent}]"; return(output); }
public bool TryParse(string contents, out KONNode output) { try { string[] lines = contents.Split('\n'); string previousLine = ""; string line = lines[0]; int currentIndex = 0; //Skip any preceding comments before using a line as the name while (line.StartsWith("//")) { currentIndex++; line = lines[currentIndex]; } //Create the KONNode object and find its name based on the current line output = new KONNode(GetCase(line.Trim(), NodeNameReadMode)); KONNode currentNode = output; bool arrayReadMode = false; KONArray currentArray = null; for (int i = currentIndex; i < lines.Length; i++) { previousLine = line; line = lines[i].Trim(); //Ignore any line that starts with // if (line.StartsWith("//")) { line = previousLine; continue; } if (line.Contains("=") && !arrayReadMode) { currentNode.Values.Add(GetCase(line.Split('=')[0].Trim(), KeyReadMode), GetCase(FormatValue(line.Split('=')[1].Trim()), ValueReadMode)); } if (line.Contains("{") && !previousLine.Contains("{") && previousLine != output.Name && !arrayReadMode) { KONNode newNode = new KONNode(GetCase(Regex.Replace(previousLine, @"[^\w\-]", "", RegexOptions.None, TimeSpan.FromSeconds(1)), NodeNameReadMode), currentNode); currentNode.AddChild(newNode); currentNode = newNode; } if (line.Contains("[") && !previousLine.Contains("[") && !arrayReadMode) { currentArray = new KONArray(GetCase(Regex.Replace(previousLine, @"[^\w\-]", "", RegexOptions.None, TimeSpan.FromSeconds(1)), NodeNameReadMode), currentNode); currentNode.AddArray(currentArray); arrayReadMode = true; } if (arrayReadMode && !line.Contains("]") && !line.Contains("[")) { currentArray.Items.Add(GetCase(FormatValue(line), ValueReadMode)); } if (line.Contains("]") && arrayReadMode) { arrayReadMode = false; } if (line.Contains("}")) { currentNode = currentNode.Parent; } } return(true); } catch { output = null; return(false); } }
/// <summary> /// Adds the given array to this node's arrays. /// </summary> /// <param name="array"></param> public void AddArray(KONArray array) { array.Parent = this; Arrays.Add(array); }