Ejemplo n.º 1
0
        private static dynamic ReadObject(ITrwReader reader)
        {
            var result       = new ExpandoObject() as IDictionary <string, object>;
            var propertyName = (string)null;

            while (reader.MoveNext())
            {
                switch (reader.TokenType)
                {
                case TrwTokenType.PropertyName:
                    propertyName = reader.ValueAsString;
                    break;

                case TrwTokenType.EndObject:
                    return(result);

                case TrwTokenType.StartObject:
                case TrwTokenType.StartArray:
                case TrwTokenType.Null:
                case TrwTokenType.Boolean:
                case TrwTokenType.Integer:
                case TrwTokenType.Float:
                case TrwTokenType.String:
                    Debug.Assert(propertyName != null, nameof(propertyName) + " != null");
                    result[propertyName] = ReadValue(reader);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        private static List <dynamic> ReadArray(ITrwReader reader)
        {
            var result = new List <object>();

            while (reader.MoveNext())
            {
                switch (reader.TokenType)
                {
                case TrwTokenType.StartObject:
                case TrwTokenType.StartArray:
                case TrwTokenType.Null:
                case TrwTokenType.Boolean:
                case TrwTokenType.Integer:
                case TrwTokenType.Float:
                case TrwTokenType.String:
                    result.Add(ReadValue(reader));
                    break;

                case TrwTokenType.EndArray:
                    return(result);

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
 private void SetText(string text)
 {
     streamWriter.Write(text);
     streamWriter.Flush();
     stream.Position = 0;
     reader          = new TrwReaderXml(stream);
 }
Ejemplo n.º 4
0
 public static void Check(this ITrwReader reader, TrwTokenType tokenType)
 {
     if (reader.TokenType != tokenType)
     {
         throw new Exception($"'{tokenType}' was expected, but '{reader.TokenType}' found. (Ln {reader.LineNumber} Col {reader.LinePosition})");
     }
 }
Ejemplo n.º 5
0
 public static void CheckProperty(this ITrwReader reader, string propertyName)
 {
     reader.Check(TrwTokenType.PropertyName);
     if (reader.ValueAsString != propertyName)
     {
         throw new Exception($"Property '{propertyName}' was expected, but '{reader.ValueAsString}' found. (Ln {reader.LineNumber} Col {reader.LinePosition})");
     }
 }
Ejemplo n.º 6
0
 public static dynamic ReadAsDynamic(this ITrwReader reader)
 {
     if (!reader.MoveNext())
     {
         return(null);
     }
     return(ReadValue(reader));
 }
Ejemplo n.º 7
0
 public ITrwReader ConvertGeneratedResourceInfoReader(ITrwReader reader, int fromVersion, int toVersion)
 {
     if (fromVersion == toVersion)
     {
         return(reader);
     }
     throw new NotImplementedException();
 }
 public TrwSerializationReadContext(ITrwReader reader, ITrwSerializationHandlerContainer handlers, IReadOnlyDictionary <string, Type> typeAliases, TrwSerializationOptions options)
 {
     Reader           = reader;
     this.handlers    = handlers;
     this.typeAliases = typeAliases;
     this.options     = options;
     Bag = new Dictionary <string, object>();
     reader.MoveNext();
 }
Ejemplo n.º 9
0
        private ITrwSerializationReadContext BasicReadContext(ITrwReader reader, IReadOnlyDictionary <string, Type> typeAliases)
        {
            var serializationType = SaveLoadConstants.BasicSerializationType;
            var handlers          = serializationNecessities.GetTrwHandlerContainer(serializationType);

            return(new TrwSerializationReadContext(reader, handlers, typeAliases, new TrwSerializationOptions
            {
                ExplicitTypes = TrwSerializationExplicitTypes.Never,
                AliasTypes = typeAliases != null,
            }));
        }
Ejemplo n.º 10
0
        public ITrwReader ConvertAliasesReader(ITrwReader reader, int fromVersion, int toVersion)
        {
            var result = reader;

            while (fromVersion < toVersion)
            {
                fromVersion++;
                result = new SaveLoadTypeAliasesConverter(result, typeRenames[fromVersion]);
            }
            return(result);
        }
Ejemplo n.º 11
0
        public void OnRead(ITrwReader reader)
        {
            if (popNext)
            {
                prev    = pathStack.Pop();
                popNext = false;
            }

            switch (reader.TokenType)
            {
            case TrwTokenType.None:
                break;

            case TrwTokenType.StartObject:
                OnStartReadValue();
                prev = new PathElem {
                    PropName = "DUMMY"
                };
                break;

            case TrwTokenType.EndObject:
                popNext = true;
                break;

            case TrwTokenType.StartArray:
                OnStartReadValue();
                prev = new PathElem {
                    ArrayIndex = -1
                };
                break;

            case TrwTokenType.EndArray:
                popNext = true;
                break;

            case TrwTokenType.PropertyName:
                pathStack.Push(new PathElem {
                    PropName = reader.ValueAsString
                });
                break;

            case TrwTokenType.Null:
            case TrwTokenType.Boolean:
            case TrwTokenType.Integer:
            case TrwTokenType.Float:
            case TrwTokenType.String:
                OnStartReadValue();
                popNext = true;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 12
0
        public ITrwSerializationReadContext WorldReadContext(ITrwReader reader, IReadOnlyDictionary <string, Type> typeAliases)
        {
            var serializationType = SaveLoadConstants.WorldSerializationType;
            var handlers          = serializationNecessities.GetTrwHandlerContainer(serializationType);

            return(new TrwSerializationReadContext(reader, handlers, typeAliases, new TrwSerializationOptions
            {
                ExplicitTypes = TrwSerializationExplicitTypes.WhenObject,
                AliasTypes = true,
                TypePropertyName = "type",
                ValuePropertyName = "value"
            }));
        }
Ejemplo n.º 13
0
        public ITrwReader ConvertWorldReader(ITrwReader reader, int fromVersion, int toVersion)
        {
            if (fromVersion == toVersion)
            {
                return(reader);
            }

            var converterChain = Enumerable.Range(fromVersion + 1, toVersion - fromVersion)
                                 .Select(x => worldConverters[x]())
                                 .ToArray();

            converterChain[0].ChainFrom(reader);
            for (int i = 1; i < converterChain.Length; i++)
            {
                converterChain[i].ChainFrom(converterChain[i - 1]);
            }

            return(converterChain.Last().GetResultAsReader());
        }
Ejemplo n.º 14
0
        private static object ReadValue(ITrwReader reader)
        {
            switch (reader.TokenType)
            {
            case TrwTokenType.StartObject: return(ReadObject(reader));

            case TrwTokenType.StartArray: return(ReadArray(reader));

            case TrwTokenType.Null: return(null);

            case TrwTokenType.Boolean: return(reader.ValueAsBool);

            case TrwTokenType.Integer: return(reader.ValueAsSInt32);

            case TrwTokenType.Float: return(reader.ValueAsFloat64);

            case TrwTokenType.String: return(reader.ValueAsString);

            default: throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 15
0
 public void OnSkip(ITrwReader reader)
 {
     prev    = pathStack.Pop();
     popNext = false;
     OnRead(reader);
 }
Ejemplo n.º 16
0
 public void ChainFrom(ITrwReader reader)
 {
     source = reader;
 }
Ejemplo n.º 17
0
 public ITrwSerializationReadContext AliasesReadContext(ITrwReader reader) => BasicReadContext(reader, null);
Ejemplo n.º 18
0
 public static void CheckPropertyAndMoveNext(this ITrwReader reader, string propertyName)
 {
     reader.CheckProperty(propertyName);
     reader.MoveNext();
 }
Ejemplo n.º 19
0
 public ITrwSerializationReadContext AssetsInfoReadContext(ITrwReader reader, IReadOnlyDictionary <string, Type> typeAliases) => BasicReadContext(reader, typeAliases);
Ejemplo n.º 20
0
 public static void CheckAndMoveNext(this ITrwReader reader, TrwTokenType tokenType)
 {
     reader.Check(tokenType);
     reader.MoveNext();
 }
 public SaveLoadTypeAliasesConverter(ITrwReader previous, IReadOnlyDictionary <string, SaveLoadRenamedTypeDescription> typeRenames)
 {
     this.previous    = previous;
     this.typeRenames = typeRenames;
 }
Ejemplo n.º 22
0
 public ITrwSerializationReadContext GeneratedResourceInfoReadContext(ITrwReader reader, IReadOnlyDictionary <string, Type> typeAliases) => BasicReadContext(reader, typeAliases);