Exemple #1
0
        internal void ReadData(Queue<string> lines)
        {
            bool startedCollection = false;

            while (lines.Count > 0)
            {
                string line = lines.Dequeue();
                line.TrimEnd(' ', '\t');

                if (String.IsNullOrEmpty(line))
                    continue;

                string[] parts = line.Parse(true, '\t', ' ');

                if (parts.Length == 0)
                    continue;

                string key = parts[0];

                string unformattedString = null;
                int firstIndex = line.IndexOf('\"');
                int lastIndex = line.LastIndexOf('\"');
                if (lastIndex != -1 && firstIndex != -1 && lastIndex != firstIndex)
                {
                    unformattedString = line.Substring(firstIndex + 1, lastIndex - firstIndex - 1);
                }

                //comment
                if (parts[0][0] == '#')
                    continue;

                //end collection
                if (key == "}")
                {
                    if (startedCollection)
                        return;
                    else
                        throw new Exception("Non-matching collection end symbols (\"}\")");
                }

                //start new collection
                else if (key == "{")
                {
                    if (startedCollection)
                        throw new Exception("Non-matching collection end symbols (\"}\")");
                    else
                        startedCollection = true;
                    continue;
                }

                //add new list
                else if (parts.Length == 1 && key.Substring(key.Length - 2, 2) == "[]")
                {
                    key = key.Substring(0, key.Length - 2);

                    if (ContainsKey(key))
                        throw new Exception("Cannot encode same key twice");

                    AddList(key);
                    List<DataValue> values = lists[key];

                    bool startedList = false;
                    bool endedList = false;
                    while (endedList == false)
                    {
                        line = lines.Dequeue();
                        if (String.IsNullOrEmpty(line))
                            continue;
                        unformattedString = null;
                        firstIndex = line.IndexOf('\"');
                        lastIndex = line.LastIndexOf('\"');
                        if (lastIndex != -1 && firstIndex != -1 && lastIndex != firstIndex)
                        {
                            unformattedString = line.Substring(firstIndex + 1, lastIndex - firstIndex - 1);
                        }
                        parts = line.Parse(true, '\t', ' ');
                        if (parts.Length == 0)
                            continue;
                        key = parts[0];
                        if (parts[0][0] == '#')
                            continue;
                        if (key == "[")
                        {
                            if (!startedList)
                                startedList = true;
                            else
                                throw new Exception("Non-matching list symbols (\"[\"])");
                        }
                        else if (key == "]")
                        {
                            if (startedList)
                                endedList = true;
                            else
                                throw new Exception("Non-matching list symbols (\"[\"])");
                        }
                        else
                        {
                            if (!startedList)
                                throw new Exception("Values for list without list start symbol (\"[\")");

                            var newValue = new DataValue(values.Count.ToString());
                            newValue.ReadData(String.Concat(parts), unformattedString);
                            values.Add(newValue);
                        }
                    }
                }

                //start new collection
                else if (parts.Length == 1)
                {
                    if (ContainsKey(key))
                        throw new Exception("Cannot encode same key twice");
                    var child = AddChild(key);
                    child.ReadData(lines);
                }

                //new value
                else
                {
                    var newParts = parts.ToList();
                    newParts.RemoveAt(0);
                    var value = new DataValue(key);
                    string newString = String.Concat(newParts);
                    value.ReadData(newString, unformattedString);
                    SetValue(value);
                }
            }

            if (startedCollection == true)
                throw new Exception("Non-matching collection end symbols (\"}\")");
        }
Exemple #2
0
 internal void ReadDataBinary(BinaryReader reader)
 {
     int childrenCount = reader.ReadInt32();
     while (childrenCount-- > 0)
     {
         string childName = reader.ReadString();
         if (ContainsKey(childName))
             throw new Exception("Cannot encode same key twice");
         AddChild(childName).ReadDataBinary(reader);
     }
     int valueCount = reader.ReadInt32();
     while (valueCount-- > 0)
     {
         string valueName = reader.ReadString();
         if (ContainsKey(valueName))
             throw new Exception("Cannot encode same key twice");
         var value = new DataValue(valueName);
         value.ReadDataBinary(reader);
         SetValue(value);
     }
     int listCount = reader.ReadInt32();
     while (listCount-- > 0)
     {
         string listName = reader.ReadString();
         if (ContainsKey(listName))
             throw new Exception("Cannot encode same key twice");
         int listLength = reader.ReadInt32();
         AddList(listName);
         for (int i = 0; i < listLength; ++i)
         {
             var value = new DataValue(i.ToString());
             value.ReadDataBinary(reader);
             AddValuesToList(listName, value);
         }
     }
 }
Exemple #3
0
 /// <summary>
 /// Sets a value at the given key to the given Vector2.
 /// </summary>
 public DataValue SetValue(string key, Vector2 value)
 {
     var newValue = new DataValue(key, value);
     RemoveKey(key);
     values[key] = newValue;
     return newValue;
 }
Exemple #4
0
 /// <summary>
 /// Sets a value at the given key to the given value.
 /// </summary>
 public DataValue SetValue(string key, DataValue value)
 {
     RemoveKey(key);
     values[key] = value;
     value.Key = key;
     return value;
 }
Exemple #5
0
 /// <summary>
 /// Sets a value at the given value's key to the given value.
 /// </summary>
 public DataValue SetValue(DataValue value)
 {
     return SetValue(value.Key, value);
 }
Exemple #6
0
 /// <summary>
 /// Removes the given DataValue from the node.
 /// </summary>
 /// <param name="value"></param>
 public void RemoveValue(DataValue value)
 {
     values.Remove(value.Key);
 }
Exemple #7
0
 internal static bool CheckIfNull(DataValue value)
 {
     return value == null;
 }