示例#1
0
 public void Unserialize(SValue value)
 {
     string[] raw = value.String.Split('+');
     files.Clear();
     for (int i = 0; i < raw.Length; i++)
     {
         string file = raw[i];
         if (!string.IsNullOrEmpty(file))
         {
             files.Add(file);
         }
     }
     if (files.Count > maxCount)
     {
         files.RemoveRange(files.Count, files.Count - maxCount);
     }
 }
 public SValue Serialize()
 {
     Normalize();
     return(SValue.NewList(list));
 }
示例#3
0
        private static void SerializePrimitive(
            Dictionary <SList, int> listSet, Dictionary <SHash, int> hashSet, Dictionary <string, int> textSet,
            BinaryWriter writer, SValue value)
        {
            writer.Write((byte)value.type);
            switch (value.type)
            {
            case TypeList:
                writer.Write(listSet[(SList)value.value]);
                break;

            case TypeHash:
                writer.Write(hashSet[(SHash)value.value]);
                break;

            case TypeBool:
                writer.Write((bool)value.value);
                break;

            case TypeDouble:
                writer.Write((double)value.value);
                break;

            case TypeInt:
                writer.Write((int)value.value);
                break;

            case TypeString:
                string text = (string)value.value ?? "";
                int    textIndexI;
                if (IsStoredText(text) && textSet.TryGetValue(text, out textIndexI) && textIndexI != -1)
                {
                    writer.Write(true);
                    writer.Write(textIndexI);
                }
                else
                {
                    writer.Write(false);
                    writer.Write(text);
                }
                break;

            case TypeFloat:
                writer.Write((float)value.value);
                break;

            case TypeLong:
                writer.Write((long)value.value);
                break;

            case TypeBytes:
                byte[] bytes = (byte[])value.value;
                if (bytes != null)
                {
                    writer.Write(bytes.Length);
                    writer.Write(bytes);
                }
                else
                {
                    writer.Write(-1);
                }
                break;
            }
        }
示例#4
0
        private static void FillComplexValues(
            Dictionary <SList, int> listSet, Dictionary <SHash, int> hashSet, Dictionary <string, int> textSet,
            SValue value,
            ref int listIndex, ref int hashIndex, ref int textIndex)
        {
            switch (value.type)
            {
            case TypeList:
                SList list = value.value as SList;
                if (listSet.ContainsKey(list))
                {
                    return;
                }
                listSet[list] = listIndex++;

                for (int i = 0, count = list.Count; i < count; i++)
                {
                    FillComplexValues(listSet, hashSet, textSet, list[i], ref listIndex, ref hashIndex, ref textIndex);
                }
                break;

            case TypeHash:
                SHash hash = value.value as SHash;
                if (hashSet.ContainsKey(hash))
                {
                    return;
                }
                hashSet[hash] = hashIndex++;

                foreach (KeyValuePair <string, SValue> pair in hash)
                {
                    string key = pair.Key;
                    if (IsStoredText(key))
                    {
                        int textsCount;
                        if (!textSet.TryGetValue(key, out textsCount))
                        {
                            textSet[key] = -1;
                        }
                        else if (textsCount == -1)
                        {
                            textSet[key] = textIndex++;
                        }
                    }

                    FillComplexValues(listSet, hashSet, textSet, pair.Value, ref listIndex, ref hashIndex, ref textIndex);
                }
                break;

            case TypeString:
            {
                string key = (value.value as string) ?? "";
                int    textsCount;
                if (!textSet.TryGetValue(key, out textsCount))
                {
                    textSet[key] = -1;
                }
                else if (textsCount == -1)
                {
                    textSet[key] = textIndex++;
                }
                break;
            }
            }
        }
示例#5
0
 public static byte[] Serialize(SValue value)
 {
     using (MemoryStream stream = new MemoryStream())
         using (BinaryWriter writer = new BinaryWriter(stream))
         {
             Dictionary <SList, int>  listSet = new Dictionary <SList, int>();
             Dictionary <SHash, int>  hashSet = new Dictionary <SHash, int>();
             Dictionary <string, int> textSet = new Dictionary <string, int>();
             int listIndex = 0;
             int hashIndex = 0;
             int textIndex = 0;
             FillComplexValues(listSet, hashSet, textSet, value, ref listIndex, ref hashIndex, ref textIndex);
             SList[]  lists  = new SList[listIndex];
             SHash[]  hashes = new SHash[hashIndex];
             string[] texts  = new string[textIndex];
             foreach (KeyValuePair <SList, int> pair in listSet)
             {
                 lists[pair.Value] = pair.Key;
             }
             foreach (KeyValuePair <SHash, int> pair in hashSet)
             {
                 hashes[pair.Value] = pair.Key;
             }
             foreach (KeyValuePair <string, int> pair in textSet)
             {
                 if (pair.Value != -1)
                 {
                     texts[pair.Value] = pair.Key;
                 }
             }
             writer.Write((byte)'D');
             writer.Write((byte)'S');
             writer.Write((byte)'V');
             writer.Write(texts.Length);
             writer.Write(lists.Length);
             writer.Write(hashes.Length);
             for (int i = 0; i < texts.Length; i++)
             {
                 writer.Write(texts[i]);
             }
             for (int i = 0; i < lists.Length; i++)
             {
                 SList list = lists[i];
                 writer.Write(list.Count);
                 for (int j = 0, count = list.Count; j < count; j++)
                 {
                     SerializePrimitive(listSet, hashSet, textSet, writer, list[j]);
                 }
             }
             for (int i = 0; i < hashes.Length; i++)
             {
                 SHash hash = hashes[i];
                 writer.Write(hash.Count);
                 foreach (KeyValuePair <string, SValue> pair in hash)
                 {
                     int textIndexI;
                     if (IsStoredText(pair.Key) && textSet.TryGetValue(pair.Key, out textIndexI) && textIndexI != -1)
                     {
                         writer.Write(true);
                         writer.Write(textIndexI);
                     }
                     else
                     {
                         writer.Write(false);
                         writer.Write(pair.Key);
                     }
                     SerializePrimitive(listSet, hashSet, textSet, writer, pair.Value);
                 }
             }
             SerializePrimitive(listSet, hashSet, textSet, writer, value);
             return(stream.ToArray());
         }
 }
示例#6
0
 public SValue With(int index, SValue value)
 {
     this[index] = value;
     return(this);
 }
示例#7
0
 public SValue With(string key, SValue value)
 {
     this[key] = value;
     return(this);
 }