Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 public KONNode(string name, KONNode parent)
 {
     Name     = name;
     Parent   = parent;
     Values   = new Dictionary <string, string>();
     Children = new List <KONNode>();
     Arrays   = new List <KONArray>();
 }
Exemplo n.º 3
0
 public KONArray(string name, KONNode parent)
 {
     Name   = name;
     Parent = parent;
     Items  = new List <string>();
 }
Exemplo n.º 4
0
 public KONArray(string name)
 {
     Name   = name;
     Items  = new List <string>();
     Parent = null;
 }
Exemplo n.º 5
0
 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);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Adds the given node to this node's children.
 /// </summary>
 /// <param name="node"></param>
 public void AddChild(KONNode node)
 {
     node.Parent = this;
     node.Depth  = Depth + 1;
     Children.Add(node);
 }