Пример #1
0
        /// <inheritdoc cref="Set(string,string,int,bool)"/>
        public void Set(string prefix, string key, string value, bool persist = false)
        {
            string fullKey = prefix + "!" + key;
            ObjectStorageValue <string> data = new ObjectStorageValue <string> {
                Value = value, Persist = persist
            };

            stringMap[fullKey] = data;
        }
Пример #2
0
        internal void Deserialize(string serialized, bool isPersistentData = true)
        {
            if (string.IsNullOrEmpty(serialized))
            {
                return;
            }

            using StringReader stringReader = new StringReader(serialized);

            int next;

            while ((next = stringReader.Read()) != -1)
            {
                char c = (char)next;
                if (c != '[')
                {
                    throw new InvalidOperationException($"Expected a storage open character '[', but read character '{c}'");
                }

                string storageName = stringReader.ReadUntilChar(':');
                if (ReadAndAssertEndOfData(stringReader) != ':')
                {
                    throw new InvalidOperationException($"Unknown storage type '{storageName}'");
                }

                string entryCountString = stringReader.ReadUntilChar(']');
                int    entryCount       = int.Parse(entryCountString);
                if (ReadAndAssertEndOfData(stringReader) != ']')
                {
                    throw new InvalidOperationException($"Unknown storage type '{storageName}'");
                }

                switch (storageName)
                {
                case IntStorageKey:
                    ReadStorage(intMap, entryCount, false, int.Parse);
                    break;

                case FloatStorageKey:
                    ReadStorage(floatMap, entryCount, false, float.Parse);
                    break;

                case StringStorageKey:
                    ReadStorage(stringMap, entryCount, true, value => value);
                    break;

                default:
                    throw new InvalidOperationException($"Unknown storage type '{storageName}'");
                }
            }

            void ReadStorage <T>(Dictionary <string, ObjectStorageValue <T> > store, int entryCount, bool valueHasLength, Func <string, T> parseValue)
            {
                for (int i = 0; i < entryCount; i++)
                {
                    int    keyLength = ReadLength(stringReader);
                    string key       = stringReader.ReadBlock(keyLength);
                    stringReader.Skip(" = ".Length);

                    string value;
                    if (valueHasLength)
                    {
                        int valueLength = ReadLength(stringReader);
                        value = stringReader.ReadBlock(valueLength);
                    }
                    else
                    {
                        value = stringReader.ReadUntilChar(';');
                    }

                    char endOfValue = ReadAndAssertEndOfData(stringReader);
                    if (endOfValue != ';')
                    {
                        throw new InvalidOperationException($"Expected end of value token ';' but read character {endOfValue}");
                    }

                    store[key] = new ObjectStorageValue <T>
                    {
                        Persist = isPersistentData,
                        Value   = parseValue(value),
                    };
                }
            }
        }