protected void PopulateHeal( EventReader reader )
 {
     amount = reader.ReadInt32();
     overkill = reader.ReadInt32();
     absorbed = reader.ReadInt32();
     critical = reader.ReadString() != null;
 }
Exemple #2
0
        private MapNode DeserializeIsolatedNode(EventReader r)
        {
            string text = null; NodePosition pos = NodePosition.Undefined; string id = null;

            string prop = r.Peek<Scalar>().Value;

            if (prop != null && prop.Equals(Text))
            {
                r.Expect<Scalar>();
                text = r.Expect<Scalar>().Value;
                prop = r.Peek<Scalar>().Value;
            }

            if (prop != null && prop.Equals(Pos))
            {
                r.Expect<Scalar>();
                pos = (NodePosition)Enum.Parse(typeof(NodePosition), r.Expect<Scalar>().Value);
                //prop = r.Peek<Scalar>().Value;
            }

            // Isolated nodes donot have Id
            //if (prop != null && prop.Equals(Id))
            //{
            //    r.Expect<Scalar>();
            //    id = r.Expect<Scalar>().Value;
            //    //prop = r.Peek<Scalar>().Value;
            //}

            MapNode node = MapNode.CreateIsolatedNode(pos);
            node.Text = text;
            return node;
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="YamlScalarNode"/> class.
		/// </summary>
		/// <param name="events">The events.</param>
		/// <param name="state">The state.</param>
		internal YamlScalarNode(EventReader events, DocumentLoadingState state)
		{
			Scalar scalar = events.Expect<Scalar>();
			Load(scalar, state);
			Value = scalar.Value;
			Style = scalar.Style;
		}
 protected override void InternalPopulate( EventReader reader )
 {
     base.InternalPopulate(reader);
     enchant = reader.ReadString();
     itemId = reader.ReadInt32();
     itemName = reader.ReadString();
 }
Exemple #5
0
		/// <summary>
		/// Parses the node represented by the next event in <paramref name="events" />.
		/// </summary>
		/// <param name="events">The events.</param>
		/// <param name="state">The state.</param>
		/// <returns>Returns the node that has been parsed.</returns>
		static internal YamlNode ParseNode(EventReader events, DocumentLoadingState state)
		{
			if (events.Accept<Scalar>())
			{
				return new YamlScalarNode(events, state);
			}

			if (events.Accept<SequenceStart>())
			{
				return new YamlSequenceNode(events, state);
			}

			if (events.Accept<MappingStart>())
			{
				return new YamlMappingNode(events, state);
			}

			if (events.Accept<AnchorAlias>())
			{
				AnchorAlias alias = events.Expect<AnchorAlias>();
				return state.GetNode(alias.Value, false, alias.Start, alias.End) ?? new YamlAliasNode(alias.Value);
			}

			throw new ArgumentException("The current event is of an unsupported type.", "events");
		}
Exemple #6
0
 protected virtual Script DeserializeScript(ReloadedScriptEntry reloadedScript)
 {
     var eventReader = new EventReader(new MemoryParser(reloadedScript.YamlEvents));
     var scriptCollection = (ScriptCollection)YamlSerializer.Deserialize(eventReader, null, typeof(ScriptCollection), log != null ? new SerializerContextSettings { Logger = new YamlForwardLogger(log) } : null);
     var script = scriptCollection.Count == 1 ? scriptCollection[0] : null;
     return script;
 }
Exemple #7
0
		/// <summary>
		/// Initializes a new instance of the <see cref="YamlDocument"/> class.
		/// </summary>
		/// <param name="events">The events.</param>
		internal YamlDocument(EventReader events)
		{
			DocumentLoadingState state = new DocumentLoadingState();

			events.Expect<DocumentStart>();

			while (!events.Accept<DocumentEnd>())
			{
				Debug.Assert(RootNode == null);
				RootNode = YamlNode.ParseNode(events, state);

				if (RootNode is YamlAliasNode)
				{
					throw new YamlException();
				}
			}

			state.ResolveAliases();

#if DEBUG
			foreach (var node in AllNodes)
			{
				if (node is YamlAliasNode)
				{
					throw new InvalidOperationException("Error in alias resolution.");
				}
			}
#endif

			events.Expect<DocumentEnd>();
		}
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            var nodeEvent = reader.Peek<NodeEvent>();

            var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

            try
            {
                foreach (var deserializer in deserializers)
                {
                    object value;
                    if (deserializer.Deserialize(reader, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out value))
                    {
                        return value;
                    }
                }
            }
            catch (YamlException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new YamlException(nodeEvent.Start, nodeEvent.End, "Exception during deserialization", ex);
            }

            throw new YamlException(
                nodeEvent.Start,
                nodeEvent.End,
                string.Format(
                    "No node deserializer was able to deserialize the node into type {0}",
                    expectedType.AssemblyQualifiedName
                )
            );
        }
 // Use this for initialization
 void Start () {
     var input = new StringReader(Document);
     
     var deserializer = new Deserializer();
     
     var reader = new EventReader(new Parser(input));
     
     // Consume the stream start event "manually"
     reader.Expect<StreamStart>();
     
     var output = new StringBuilder();
     while(reader.Accept<DocumentStart>())
     {
         // Deserialize the document
         var doc = deserializer.Deserialize<List<string>>(reader);
     
         output.AppendLine("## Document");
         foreach(var item in doc)
         {
             output.AppendLine(item);
         }
     }    
     Debug.Log(output);
     
 }
		bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
		{
			if (!typeof(IList).IsAssignableFrom(expectedType))
			{
				value = false;
				return false;
			}

			reader.Expect<SequenceStart>();

			var list = (IList)_objectFactory.Create(expectedType);
			while (!reader.Accept<SequenceEnd>())
			{
				var item = nestedObjectDeserializer(reader, typeof(object));
				var promise = item as IValuePromise;
				if (promise == null)
				{
					list.Add(item);
				}
				else
				{
					var index = list.Count;
					list.Add(null);
					promise.ValueAvailable += v => list[index] = v;
				}
			}
			value = list;

			reader.Expect<SequenceEnd>();

			return true;
		}
 protected void PopulateAuraEvent( EventReader reader )
 {
     auraKind = reader.ReadEnum<CombatLogAuraKind>();
     if( reader.HasNextValue ) {
         // Note: seems to be used for absorb effects.
         amount = reader.ReadInt32();
     }
 }
Exemple #12
0
        /// <summary>
        /// Throws YamlException
        /// </summary>
        /// <param name="metaModel"></param>
        /// <param name="textReader"></param>
        public void Deserialize(MetaModel.MetaModel metaModel, TextReader textReader)
        {
            Parser p = new Parser(textReader);
            EventReader r = new EventReader(p);

            r.Expect<StreamStart>();
            r.Expect<DocumentStart>();
            r.Expect<MappingStart>();

            Scalar section = r.Peek<Scalar>();
            if (section.Value.Equals(Icons))
            {
                r.Expect<Scalar>();
                DeserializeIcons(metaModel, r);
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(RecentFiles))
            {
                r.Expect<Scalar>();
                DeserializeRecentFiles(metaModel, r);
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(LastOpenedFile))
            {
                r.Expect<Scalar>();
                metaModel.LastOpenedFile = r.Expect<Scalar>().Value;
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(MapBackColor))
            {
                r.Expect<Scalar>();
                metaModel.MapEditorBackColor = (Color)(new ColorConverter().ConvertFromString(r.Expect<Scalar>().Value));
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(NoteBackColor))
            {
                r.Expect<Scalar>();
                metaModel.NoteEditorBackColor = (Color)(new ColorConverter().ConvertFromString(r.Expect<Scalar>().Value));
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(NodeStyles))
            {
                r.Expect<Scalar>();
                DeserializeNodeStyles(metaModel, r);
                //section = r.Peek<Scalar>(); //uncomment when adding another section
            }

            r.Expect<MappingEnd>();
            r.Expect<DocumentEnd>();
            r.Expect<StreamEnd>();
        }
 protected override void InternalPopulate( EventReader reader )
 {
     base.InternalPopulate(reader);
     spell = reader.ReadSpell();
     PopulateAuraEvent(reader);
     if( reader.HasNextValue ) {
         unknown1 = reader.ReadString();
         unknown2 = reader.ReadString();
     }
 }
Exemple #14
0
        /// <summary>
        /// Deserializes an isolated node
        /// </summary>
        /// <param name="r"></param>
        /// <returns></returns>
        public MapNode Deserialize(EventReader r)
        {
            r.Expect<MappingStart>();

            MapNode n = DeserializeIsolatedNode(r);
            DeserializeScalarProperties(n, r);

            r.Expect<MappingEnd>();

            return n;
        }
		bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
		{
			var converter = converters.FirstOrDefault(c => c.Accepts(expectedType));
			if (converter == null)
			{
				value = null;
				return false;
			}

			value = converter.ReadYaml(reader.Parser, expectedType);
			return true;
		}
 protected void PopulateDamageEvent( EventReader reader )
 {
     amount = reader.ReadInt32();
     overkill = reader.ReadInt32();
     damageSchool = (CombatLogSpellSchool)reader.ReadUInt32();
     resisted = reader.ReadInt32();
     blocked = reader.ReadInt32();
     absorbed = reader.ReadInt32();
     critical = reader.ReadString() != null;
     glancing = reader.ReadString() != null;
     crushing = reader.ReadString() != null;
 }
		bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
		{
			value = null;
			var evt = reader.Peek<NodeEvent>();
			var isNull = evt != null
				&& NodeIsNull(evt);

			if (isNull)
			{
				reader.SkipThisAndNestedEvents();
			}
			return isNull;
		}
 public bool Deserialize(EventReader reader, Type expectedType,
     Func<EventReader, Type, object> nestedObjectDeserializer,
     out object value)
 {
     if (_nodeDeserializer.Deserialize(reader, expectedType,
         nestedObjectDeserializer, out value))
     {
         if (((Data)value).Name == null) 
         {
             Debug.Log("ValidatingNodeDeserializer found that 'Name' was missing or null");
         }
         return true;
     }
     return false;
 }
    private static object GetData(EventReader r)
    {
        if(r.Accept<Scalar>())
        {
            return r.Expect<Scalar>().Value;
        }
        else if(r.Accept<SequenceStart>()) {
            var seq = new ArrayList();

            r.Expect<SequenceStart>();

            while(!r.Accept<SequenceEnd>())
            {
                seq.Add(GetData(r));
            }

            r.Expect<SequenceEnd>();

            // Arrays are IStructuralEquatable, ArrayLists are not.
            return seq.ToArray();
        }
        else if(r.Accept<MappingStart>())
        {
            // Since we use sequences as keys...
            var map = new OrderedDictionary(StructuralComparisons.StructuralEqualityComparer);

            r.Expect<MappingStart>();

            while(!r.Accept<MappingEnd>())
            {
                object key = GetData(r);
                object value = GetData(r);
                map.Add(key, value);
            }

            r.Expect<MappingEnd>();

            return map;
        }
        else {
            throw new YamlException();
        }
    }
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            if (!expectedType.IsArray)
            {
                value = false;
                return false;
            }

            var itemType = expectedType.GetElementType();

            var items = new ArrayList();
            CollectionNodeDeserializer.DeserializeHelper(itemType, reader, expectedType, nestedObjectDeserializer, items, true);

            var array = Array.CreateInstance(itemType, items.Count);
            items.CopyTo(array, 0);

            value = array;
            return true;
        }
		bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
		{
			var mapping = reader.Allow<MappingStart>();
			if (mapping == null)
			{
				value = null;
				return false;
			}
			
			value = _objectFactory.Create(expectedType);
			while (!reader.Accept<MappingEnd>())
			{
				var propertyName = reader.Expect<Scalar>();
				var property = _typeDescriptor.GetProperty(expectedType, null, propertyName.Value, _ignoreUnmatched);
				if (property == null)
				{
					reader.SkipThisAndNestedEvents();
					continue;
				}

				var propertyValue = nestedObjectDeserializer(reader, property.Type);
				var propertyValuePromise = propertyValue as IValuePromise;
				if (propertyValuePromise == null)
				{
					var convertedValue = TypeConverter.ChangeType(propertyValue, property.Type);
					property.Write(value, convertedValue);
				}
				else
				{
					var valueRef = value;
					propertyValuePromise.ValueAvailable += v =>
					{
						var convertedValue = TypeConverter.ChangeType(v, property.Type);
						property.Write(valueRef, convertedValue);
					};
				}
			}

			reader.Expect<MappingEnd>();
			return true;
		}
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            Type gp;

            if (!_gpCache.TryGetValue(expectedType, out gp))
            {
                var collectionType = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(ICollection <>));
                if (collectionType != null)
                {
                    gp = collectionType.GetGenericArguments()[0];
                }
                _gpCache[expectedType] = gp;
            }
            if (gp == null)
            {
                value = false;
                return(false);
            }

            value = _objectFactory.Create(expectedType);
            Action <EventReader, Type, Func <EventReader, Type, object>, object> action;

            if (!_actionCache.TryGetValue(gp, out action))
            {
                var dm = new DynamicMethod(string.Empty, typeof(void), new[] { typeof(EventReader), typeof(Type), typeof(Func <EventReader, Type, object>), typeof(object) });
                var il = dm.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Ldarg_2);
                il.Emit(OpCodes.Ldarg_3);
                il.Emit(OpCodes.Castclass, typeof(ICollection <>).MakeGenericType(gp));
                il.Emit(OpCodes.Call, DeserializeHelperMethod.MakeGenericMethod(gp));
                il.Emit(OpCodes.Ret);
                action           = (Action <EventReader, Type, Func <EventReader, Type, object>, object>)dm.CreateDelegate(typeof(Action <EventReader, Type, Func <EventReader, Type, object>, object>));
                _actionCache[gp] = action;
            }

            action(reader, expectedType, nestedObjectDeserializer, value);
            return(true);
        }
Exemple #23
0
        /// <summary>
        /// Deserializes an object of the specified type.
        /// </summary>
        /// <param name="reader">The <see cref="EventReader" /> where to deserialize the object.</param>
        /// <param name="type">The static type of the object to deserialize.</param>
        /// <returns>Returns the deserialized object.</returns>
        public object Deserialize(EventReader reader, Type type, IValueDeserializer deserializer = null)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var hasStreamStart = reader.Allow <StreamStart>() != null;

            var hasDocumentStart = reader.Allow <DocumentStart>() != null;

            deserializer = deserializer ?? _valueDeserializer;
            object result = null;

            if (!reader.Accept <DocumentEnd>() && !reader.Accept <StreamEnd>())
            {
                using (var state = new SerializerState())
                {
                    result = deserializer.DeserializeValue(reader, type, state, deserializer);
                    state.OnDeserialization();
                }
            }

            if (hasDocumentStart)
            {
                reader.Expect <DocumentEnd>();
            }

            if (hasStreamStart)
            {
                reader.Expect <StreamEnd>();
            }

            return(result);
        }
Exemple #24
0
        /// <summary>
        /// Deserializes the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="options">The mode.</param>
        /// <param name="context">Returns additional information about the deserialization process.</param>
        /// <returns></returns>
        public object Deserialize(EventReader reader, DeserializationOptions options, out IDeserializationContext context)
        {
            if (serializedType == null)
            {
                throw new InvalidOperationException("Cannot deserialize when the serialized type is not specified in the constructor.");
            }

            var internalContext = new DeserializationContext(options);

            bool hasStreamStart = reader.Accept <StreamStart>();

            if (hasStreamStart)
            {
                reader.Expect <StreamStart>();
            }

            bool hasDocumentStart = reader.Accept <DocumentStart>();

            if (hasDocumentStart)
            {
                reader.Expect <DocumentStart>();
            }

            object result = DeserializeValue(reader, serializedType, internalContext);

            if (hasDocumentStart)
            {
                reader.Expect <DocumentEnd>();
            }

            if (hasStreamStart)
            {
                reader.Expect <StreamEnd>();
            }

            context = internalContext;

            return(result);
        }
 bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
 {
     Type type;
     if (ReferenceEquals(expectedType, typeof(IEnumerable)))
     {
         type = typeof(object);
     }
     else
     {
         Type implementedGenericInterface = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(IEnumerable<>));
         if (!ReferenceEquals(implementedGenericInterface, expectedType))
         {
             value = null;
             return false;
         }
         type = implementedGenericInterface.GetGenericArguments()[0];
     }
     Type[] typeArguments = new Type[] { type };
     Type type3 = typeof(List<>).MakeGenericType(typeArguments);
     value = nestedObjectDeserializer(reader, type3);
     return true;
 }
    public static void Main()
    {
        var yaml = @"
    anchor: &default
      key1: &myValue value1
      key2: value2
    alias:
      <<: *default
      key2: Overriding key2
      key3: value3
    ";

        var reader = new EventReader(new MergingParser(new Parser(new StringReader(yaml))));

        var deserializer = new Deserializer();
        var result       = deserializer.Deserialize <Dictionary <string, Dictionary <string, string> > >(reader);

        var alias = result["alias"];

        Console.WriteLine("key1 = {0}", alias["key1"]);
        Console.WriteLine("key2 = {0}", alias["key2"]);
    }
Exemple #27
0
        public void DeserializeTwoDocuments()
        {
            var yaml       = @"---
Name: Andy
---
Name: Brad
...";
            var serializer = new Serializer();
            var reader     = new EventReader(new Parser(new StringReader(yaml)));

            reader.Expect <StreamStart>();

            var andy = serializer.Deserialize <Person>(reader);

            Assert.NotNull(andy);
            Assert.AreEqual("Andy", andy.Name);

            var brad = serializer.Deserialize <Person>(reader);

            Assert.NotNull(brad);
            Assert.AreEqual("Brad", brad.Name);
        }
Exemple #28
0
        internal static YamlNode ParseNode(EventReader events, DocumentLoadingState state)
        {
            if (events.Accept <Scalar>())
            {
                return(new YamlScalarNode(events, state));
            }
            if (events.Accept <SequenceStart>())
            {
                return(new YamlSequenceNode(events, state));
            }
            if (events.Accept <MappingStart>())
            {
                return(new YamlMappingNode(events, state));
            }
            if (!events.Accept <AnchorAlias>())
            {
                throw new ArgumentException("The current event is of an unsupported type.", "events");
            }
            AnchorAlias alias = events.Expect <AnchorAlias>();

            return(state.GetNode(alias.Value, false, alias.Start, alias.End) ?? new YamlAliasNode(alias.Value));
        }
Exemple #29
0
        /** <inheritDoc /> */
        public T WaitForLocal <T>(IEventFilter <T> filter, params int[] types) where T : IEvent
        {
            var hnd = GetFilterHandle(filter);

            try
            {
                return(DoOutInOp((int)Op.WaitForLocal,
                                 writer =>
                {
                    writer.WriteObject(hnd);
                    WriteEventTypes(types, writer);
                },
                                 reader => EventReader.Read <T>(Marshaller.StartUnmarshal(reader))));
            }
            finally
            {
                if (hnd != null)
                {
                    Ignite.HandleRegistry.Release(hnd.Value);
                }
            }
        }
Exemple #30
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AffinityFunctionContext"/> class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        internal AffinityFunctionContext(BinaryReader reader)
        {
            Debug.Assert(reader != null);

            _currentTopologySnapshot = IgniteUtils.ReadNodes(reader);
            _backups = reader.ReadInt();
            _currentTopologyVersion = new AffinityTopologyVersion(reader.ReadLong(), reader.ReadInt());
            _discoveryEvent         = EventReader.Read <DiscoveryEvent>(reader);

            // Prev assignment
            var cnt = reader.ReadInt();

            if (cnt > 0)
            {
                _previousAssignment = new List <List <IClusterNode> >(cnt);

                for (var i = 0; i < cnt; i++)
                {
                    _previousAssignment.Add(IgniteUtils.ReadNodes(reader));
                }
            }
        }
Exemple #31
0
        /// <summary>
        /// Creates a new instance of the loader.
        /// </summary>
        /// <param name="opts">Application configuration.</param>
        public YamlCtfConfigurationLoader(IOptions <ConfigurationRoot> opts)
        {
            var des = new SerializerSettings
            {
                ObjectFactory = new YamlObjectFactory()
            };

            des.RegisterSerializerFactory(new YamlTimeSpanConverter());
            des.RegisterSerializerFactory(new YamlDateTimeOffsetConverter());
            des.RegisterSerializerFactory(new YamlUriConverter());

            var deserializer = new Serializer(des);

            var optv = opts.Value;
            var fi   = new FileInfo(optv.EventConfiguration);

            using (var fs = fi.OpenRead())
                using (var sr = new StreamReader(fs, AbstractionUtilities.UTF8))
                {
                    var parser = new Parser(sr);
                    var er     = new EventReader(parser);
                    if (er.Expect <StreamStart>() == null)
                    {
                        throw new InvalidDataException("YAML configuration is malformed.");
                    }

                    this._eventConfig = ReadEvent(er, deserializer);
                    this._categories  = ReadCategories(er, deserializer);

                    foreach (var cat in this._categories)
                    {
                        foreach (var chall in cat.Challenges)
                        {
                            (chall as YamlCtfChallenge).Category = cat;
                        }
                    }
                }
        }
Exemple #32
0
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            IList list;
            bool  canUpdate = true;
            Type  itemType;
            var   genericCollectionType = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(ICollection <>));

            if (genericCollectionType != null)
            {
                var genericArguments = genericCollectionType.GetGenericArguments();
                itemType = genericArguments[0];

                value = _objectFactory.Create(expectedType);
                list  = value as IList;
                if (list == null)
                {
                    var genericListType = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(IList <>));
                    canUpdate = genericListType != null;
                    list      = new GenericCollectionToNonGenericAdapter(value, genericCollectionType, genericListType);
                }
            }
            else if (typeof(IList).IsAssignableFrom(expectedType))
            {
                itemType = typeof(object);

                value = _objectFactory.Create(expectedType);
                list  = (IList)value;
            }
            else
            {
                value = false;
                return(false);
            }

            DeserializeHelper(itemType, reader, expectedType, nestedObjectDeserializer, list, canUpdate);

            return(true);
        }
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            IDictionary dictionary;
            Type        keyType, valueType;
            var         genericDictionaryType = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(IDictionary <,>));

            if (genericDictionaryType != null)
            {
                var genericArguments = genericDictionaryType.GetGenericArguments();
                keyType   = genericArguments[0];
                valueType = genericArguments[1];

                value = _objectFactory.Create(expectedType);

                dictionary = value as IDictionary;
                if (dictionary == null)
                {
                    dictionary = new GenericDictionaryToNonGenericAdapter(value, genericDictionaryType);
                }
            }
            else if (typeof(IDictionary).IsAssignableFrom(expectedType))
            {
                keyType   = typeof(object);
                valueType = typeof(object);

                value      = _objectFactory.Create(expectedType);
                dictionary = (IDictionary)value;
            }
            else
            {
                value = null;
                return(false);
            }

            DeserializeHelper(keyType, valueType, reader, expectedType, nestedObjectDeserializer, dictionary);

            return(true);
        }
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            object value;
            var    alias = reader.Allow <AnchorAlias>();

            if (alias != null)
            {
                var aliasState = state.Get <AliasState>();
                if (aliasState.TryGetValue(alias.Value, out value))
                {
                    return(value);
                }

                throw new AnchorNotFoundException(alias.Start, alias.End, string.Format(
                                                      "Anchor '{0}' not found",
                                                      alias.Value
                                                      ));
            }

            string anchor = null;

            var nodeEvent = reader.Peek <NodeEvent>();

            if (nodeEvent != null && !string.IsNullOrEmpty(nodeEvent.Anchor))
            {
                anchor = nodeEvent.Anchor;
            }

            value = innerDeserializer.DeserializeValue(reader, expectedType, state, nestedObjectDeserializer);

            if (anchor != null)
            {
                var aliasState = state.Get <AliasState>();
                aliasState.Add(anchor, value);
            }

            return(value);
        }
Exemple #35
0
        /// <summary>
        /// Constructs a new binary log reader for the given stream
        /// </summary>
        public BinaryLogReader(Stream logStream, PipExecutionContext context, bool closeStreamOnDispose = true)
        {
            Contract.Requires(logStream != null);
            Contract.Requires(context != null);

            LogStream = logStream;
            m_context = context;
            m_closeStreamOnDispose = closeStreamOnDispose;
            m_capturedPaths        = new ConcurrentDenseIndex <AbsolutePath>(debug: false);
            m_logStreamReader      = new EventReader(this);
            m_handlers             = new EventHandler[1024];

            var logIdBytes = new byte[BinaryLogger.LogIdByteLength];

            if (logStream.Read(logIdBytes, 0, BinaryLogger.LogIdByteLength) == BinaryLogger.LogIdByteLength)
            {
                LogId = new Guid(logIdBytes);
            }
            else
            {
                LogId = null;
            }
        }
Exemple #36
0
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            var nodeEvent = reader.Peek <NodeEvent>();

            var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

            foreach (var deserializer in deserializers)
            {
                object value;
                if (deserializer.Deserialize(reader, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out value))
                {
                    return(value);
                }
            }

            throw new SerializationException(
                      string.Format(
                          "No node deserializer was able to deserialize the node at {0} into type {1}",
                          reader.Parser.Current.Start,
                          expectedType.AssemblyQualifiedName
                          )
                      );
        }
        /// <summary>
        /// Name and Title are mandatory while shortcut is optional
        /// </summary>
        /// <param name="metaModel"></param>
        /// <param name="r"></param>
        private void DeserializeIcon(MetaModel.MetaModel metaModel, EventReader r)
        {
            string name = null, title = null, shortcut = null;

            r.Expect <MappingStart>();

            r.Expect <Scalar>(); //name
            name = r.Expect <Scalar>().Value;

            r.Expect <Scalar>();
            title = r.Expect <Scalar>().Value;

            if (r.Accept <Scalar>())
            {
                if (r.Expect <Scalar>().Value.Equals(Shortcut))
                {
                    shortcut = r.Expect <Scalar>().Value;
                }
            }
            r.Expect <MappingEnd>();

            metaModel.IconsList.Add(new ModelIcon(name, title, shortcut));
        }
		public object DeserializeValue (EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
		{
			var nodeEvent = reader.Peek<NodeEvent>();

			var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

			foreach (var deserializer in deserializers)
			{
				object value;
				if (deserializer.Deserialize(reader, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out value))
				{
					return value;
				}
			}

			throw new SerializationException(
				string.Format(
					"No node deserializer was able to deserialize the node at {0} into type {1}",
					reader.Parser.Current.Start,
					expectedType.AssemblyQualifiedName
				)
			);
		}
        public static void ReadConfigYAML()
        {
            var input = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"/AME.yaml");

            var deserializer = new Deserializer();

            var reader = new EventReader(new Parser(input));

            // Deserialize the document
            reader.Expect<StreamStart>();
            reader.Accept<DocumentStart>();
            var settings = deserializer.Deserialize<AMESettings>(reader);

            reader.Accept<DocumentStart>();
            var behaviorManifests = deserializer.Deserialize<BehaviorManifests>(reader);

            foreach (var behavior in behaviorManifests.FRLGBehaviors)
            {
                if(behavior.Value[0] == '&')
                    behaviorManifests.FRLGBehaviors[behavior.Key] = InternalStrings.ResourceManager.GetString(behavior.Value);
                Console.WriteLine("[{0}] {1}", behavior.Key.ToString("X"), behavior.Value);
            }
        }
		bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
		{
			Type itemsType;
			if (expectedType == typeof(IEnumerable))
			{
				itemsType = typeof(object);
			}
			else
			{
				var iEnumerable = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(IEnumerable<>));
				if (iEnumerable != expectedType)
				{
					value = null;
					return false;
				}

				itemsType = iEnumerable.GetGenericArguments()[0];
			}

			var collectionType = typeof(List<>).MakeGenericType(itemsType);
			value = nestedObjectDeserializer(reader, collectionType);
			return true;
		}
        public void ShouldReadAllFieldsFromEvent()
        {
            const int    eventId    = 32456;
            const int    categoryId = 32;
            const string message    = "An error occurred!!!";

            _testEventLog.WriteEntry(message, EventLogEntryType.Error, eventId, categoryId);

            var reader = new EventReader(_testEventLog, TimeSpan.FromMinutes(1));
            var events = reader.ReadEvents();

            Assert.That(events.Count(), Is.EqualTo(1));
            var firstEvent = events.First();

            Assert.That(firstEvent.ErrorLevel, Is.EqualTo(2)); // == EventLogEntryType.Error
            Assert.That(firstEvent.EventID, Is.EqualTo(eventId));
            Assert.That(firstEvent.CategoryNumber, Is.EqualTo(categoryId));
            Assert.That(firstEvent.Message, Is.EqualTo(message));
            Assert.That(firstEvent.EntryType, Is.EqualTo(EventLogEntryType.Error));
            Assert.That(firstEvent.InstanceID, Is.EqualTo(eventId));
            Assert.That(firstEvent.Source, Is.EqualTo(_testEventLog.Source));
            Assert.That(firstEvent.TimeGenerated, Is.EqualTo(DateTime.Now).Within(TimeSpan.FromSeconds(10)));
        }
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            if (!typeof(IList).IsAssignableFrom(expectedType))
            {
                value = false;
                return(false);
            }

            reader.Expect <SequenceStart>();

            var list = (IList)_objectFactory.Create(expectedType);

            while (!reader.Accept <SequenceEnd>())
            {
                var item = nestedObjectDeserializer(reader, typeof(object));
                list.Add(item);
            }
            value = list;

            reader.Expect <SequenceEnd>();

            return(true);
        }
Exemple #43
0
        public bool Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            if (reader == null)
            {
                value = null;
                return(false);
            }

            // only try this if we're targeting a boolean or an untyped object
            if (expectedType == typeof(object) || expectedType == typeof(bool))
            {
                // peek at the current token
                Scalar scalar = reader.Peek <Scalar>();

                // if it's unquoted
                if (scalar != null && scalar.Style == ScalarStyle.Plain)
                {
                    // and the value is actually true or false
                    switch (scalar.Value.ToUpperInvariant())
                    {
                    case "TRUE":
                        value = true;
                        reader.Allow <Scalar>();
                        return(true);

                    case "FALSE":
                        value = false;
                        reader.Allow <Scalar>();
                        return(true);
                    }
                }
            }

            // otherwise, fall thru
            value = null;
            return(false);
        }
Exemple #44
0
        //将解析好的内容再序列化成osb文件格式的文本内容
        private static void SerializeDecodeStoryboardContent(string beatmap_folder, bool parse_osb, string output_path)
        {
            try
            {
                var info       = BeatmapFolderInfoEx.Parse(beatmap_folder, null);
                var input_file = parse_osb ? info.osb_file_path : info.osu_file_path;
                output_path = string.IsNullOrWhiteSpace(output_path) ? input_file + ".parse_output" : output_path;

                Log.User($"Start serialize {input_file} ....");
                using (var writer = new StreamWriter(File.OpenWrite(output_path)))
                {
                    writer.WriteLine($"[{Section.Events.ToString()}]");

                    OsuFileReader reader = new OsuFileReader(input_file);

                    VariableCollection collection = new VariableCollection(new VariableReader(reader).EnumValues());

                    SectionReader section = new SectionReader(Section.Events, reader);

                    EventReader event_section = new EventReader(reader, collection);

                    foreach (var line in section.EnumValues())
                    {
                        var decode_line = event_section.LineProcessVariable(line);
                        writer.WriteLine(decode_line);
                    }
                }

                Log.User("Serialize successfully! it output to " + output_path);
                Exit("Serialize successfully! it output to " + output_path);
            }
            catch (Exception e)
            {
                Log.Error("Serialize failed!" + e.Message);
                Exit("Serialize failed!" + e.Message);
            }
        }
        public void Deserialize_FirstPropertyEmpty()
        {
            var sut  = new MapYamlSerializer();
            var node = MapNode.CreateIsolatedNode(NodePosition.Left);

            node.Link  = "link";
            node.Color = Color.Azure;

            var writer  = new StringWriter();
            var emitter = new Emitter(writer);

            emitter.Emit(new StreamStart());
            emitter.Emit(new DocumentStart());

            sut.Serialize(node, emitter);

            emitter.Emit(new DocumentEnd(true));
            emitter.Emit(new StreamEnd());

            string text = writer.ToString();

            var parser      = new Parser(new StringReader(text));
            var eventReader = new EventReader(parser);

            eventReader.Expect <StreamStart>();
            eventReader.Expect <DocumentStart>();
            var result = sut.Deserialize(eventReader);

            eventReader.Expect <DocumentEnd>();
            eventReader.Expect <StreamEnd>();

            Assert.AreEqual(Color.Azure, result.Color);
            Assert.IsNull(result.Image);
            Assert.IsNull(result.Label);
            Assert.IsNull(result.Text);
            Assert.AreEqual("link", result.Link);
        }
Exemple #46
0
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            var mapping = reader.Allow <MappingStart>();

            if (mapping == null)
            {
                value = null;
                return(false);
            }

            value = _objectFactory.Create(expectedType);
            while (!reader.Accept <MappingEnd>())
            {
                var propertyName = reader.Expect <Scalar>();

                var property             = _typeDescriptor.GetProperty(expectedType, null, propertyName.Value);
                var propertyValue        = nestedObjectDeserializer(reader, property.Type);
                var propertyValuePromise = propertyValue as IValuePromise;
                if (propertyValuePromise == null)
                {
                    var convertedValue = TypeConverter.ChangeType(propertyValue, property.Type);
                    property.SetValue(value, convertedValue);
                }
                else
                {
                    var valueRef = value;
                    propertyValuePromise.ValueAvailable += v =>
                    {
                        var convertedValue = TypeConverter.ChangeType(v, property.Type);
                        property.SetValue(valueRef, convertedValue);
                    };
                }
            }

            reader.Expect <MappingEnd>();
            return(true);
        }
        public bool Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            if (reader == null)
            {
                value = null;
                return false;
            }

            // only try this if we're targeting a boolean or an untyped object
            if (expectedType == typeof(object) || expectedType == typeof(bool) ) 
            {
                // peek at the current token
                Scalar scalar = reader.Peek<Scalar>();

                // if it's unquoted 
                if (scalar != null && scalar.Style == ScalarStyle.Plain)
                {
                    // and the value is actually true or false
                    switch (scalar.Value.ToUpperInvariant())
                    {
                        case "TRUE":
                            value = true;
                            reader.Allow<Scalar>();
                            return true;
                        case "FALSE":
                            value = false;
                            reader.Allow<Scalar>();
                            return true;

                    }
                }
            }

            // otherwise, fall thru
            value = null;
            return false;
        }
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            IList list;
            bool canUpdate = true;
            Type itemType;
            var genericCollectionType = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(ICollection<>));
            if (genericCollectionType != null)
            {
                var genericArguments = genericCollectionType.GetGenericArguments();
                itemType = genericArguments[0];

                value = _objectFactory.Create(expectedType);
                list = value as IList;
                if (list == null)
                {
                    var genericListType = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(IList<>));
                    canUpdate = genericListType != null;
                    list = new GenericCollectionToNonGenericAdapter(value, genericCollectionType, genericListType);
                }
            }
            else if (typeof(IList).IsAssignableFrom(expectedType))
            {
                itemType = typeof(object);

                value = _objectFactory.Create(expectedType);
                list = (IList)value;
            }
            else
            {
                value = false;
                return false;
            }

            DeserializeHelper(itemType, reader, expectedType, nestedObjectDeserializer, list, canUpdate);

            return true;
        }
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            IDictionary dictionary;
            Type keyType, valueType;
            var genericDictionaryType = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(IDictionary<,>));
            if (genericDictionaryType != null)
            {
                var genericArguments = genericDictionaryType.GetGenericArguments();
                keyType = genericArguments[0];
                valueType = genericArguments[1];

                value = _objectFactory.Create(expectedType);

                dictionary = value as IDictionary;
                if (dictionary == null)
                {
                    dictionary = new GenericDictionaryToNonGenericAdapter(value, genericDictionaryType);
                }
            }
            else if (typeof(IDictionary).IsAssignableFrom(expectedType))
            {
                keyType = typeof(object);
                valueType = typeof(object);

                value = _objectFactory.Create(expectedType);
                dictionary = (IDictionary)value;
            }
            else
            {
                value = null;
                return false;
            }

            DeserializeHelper(keyType, valueType, reader, expectedType, nestedObjectDeserializer, dictionary);

            return true;
        }
Exemple #50
0
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            if (!typeof(IDictionary).IsAssignableFrom(expectedType))
            {
                value = false;
                return(false);
            }

            reader.Expect <MappingStart>();

            var dictionary = (IDictionary)_objectFactory.Create(expectedType);

            while (!reader.Accept <MappingEnd>())
            {
                var key      = nestedObjectDeserializer(reader, typeof(object));
                var keyValue = nestedObjectDeserializer(reader, typeof(object));
                dictionary.Add(key, keyValue);
            }
            value = dictionary;

            reader.Expect <MappingEnd>();

            return(true);
        }
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            if(!typeof(IDictionary).IsAssignableFrom(expectedType))
            {
                value = false;
                return false;
            }

            reader.Expect<MappingStart>();

            var dictionary = (IDictionary)_objectFactory.Create(expectedType);
            while (!reader.Accept<MappingEnd>())
            {
                var key = nestedObjectDeserializer(reader, typeof(object));
                var keyPromise = key as IValuePromise;

                var keyValue = nestedObjectDeserializer(reader, typeof(object));
                var valuePromise = keyValue as IValuePromise;

                if (keyPromise == null)
                {
                    if (valuePromise == null)
                    {
                        // Happy path: both key and value are known
                        dictionary.Add(key, keyValue);
                    }
                    else
                    {
                        // Key is known, value is pending
                        valuePromise.ValueAvailable += v => dictionary.Add(key, v);
                    }
                }
                else
                {
                    if (valuePromise == null)
                    {
                        // Key is pending, value is known
                        keyPromise.ValueAvailable += v => dictionary.Add(v, keyValue);
                    }
                    else
                    {
                        // Both key and value are pending. We need to wait until both of them becom available.
                        var hasFirstPart = false;

                        keyPromise.ValueAvailable += v =>
                        {
                            if (hasFirstPart)
                            {
                                dictionary.Add(v, keyValue);
                            }
                            else
                            {
                                key = v;
                                hasFirstPart = true;
                            }
                        };

                        valuePromise.ValueAvailable += v =>
                        {
                            if (hasFirstPart)
                            {
                                dictionary.Add(key, v);
                            }
                            else
                            {
                                keyValue = v;
                                hasFirstPart = true;
                            }
                        };
                    }
                }
            }
            value = dictionary;

            reader.Expect<MappingEnd>();

            return true;
        }
 protected override void InternalPopulate( EventReader reader )
 {
     base.InternalPopulate(reader);
     spell = reader.ReadSpell();
     amount = reader.ReadInt32();
 }
Exemple #53
0
        /// <summary>
        /// Deserializes an object from the specified stream (expecting a YAML string).
        /// </summary>
        /// <param name="eventReader">A YAML event reader.</param>
        /// <param name="expectedType">The expected type.</param>
        /// <returns>An instance of the YAML data.</returns>
        public object Deserialize(EventReader eventReader, Type expectedType)
        {
            var serializer = GetYamlSerializer();

            return(serializer.Deserialize(eventReader, expectedType));
        }
Exemple #54
0
        public static bool MigrateAssetIfNeeded(AssetMigrationContext context, PackageLoadingAssetFile loadAsset)
        {
            var assetFullPath = loadAsset.FilePath.FullPath;

            // Determine if asset was Yaml or not
            var assetFileExtension = Path.GetExtension(assetFullPath);
            if (assetFileExtension == null)
                return false;

            assetFileExtension = assetFileExtension.ToLowerInvariant();

            var serializer = AssetSerializer.FindSerializer(assetFileExtension);
            if (!(serializer is AssetYamlSerializer))
                return false;

            // We've got a Yaml asset, let's get expected and serialized versions
            var serializedVersion = 0;
            int expectedVersion;
            Type assetType;

            // Read from Yaml file the asset version and its type (to get expected version)
            // Note: It tries to read as few as possible (SerializedVersion is expected to be right after Id, so it shouldn't try to read further than that)
            using (var assetStream = loadAsset.OpenStream())
            using (var streamReader = new StreamReader(assetStream))
            {
                var yamlEventReader = new EventReader(new Parser(streamReader));

                // Skip header
                yamlEventReader.Expect<StreamStart>();
                yamlEventReader.Expect<DocumentStart>();
                var mappingStart = yamlEventReader.Expect<MappingStart>();

                var yamlSerializerSettings = YamlSerializer.GetSerializerSettings();
                var tagTypeRegistry = yamlSerializerSettings.TagTypeRegistry;
                bool typeAliased;
                assetType = tagTypeRegistry.TypeFromTag(mappingStart.Tag, out typeAliased);

                expectedVersion = AssetRegistry.GetCurrentFormatVersion(assetType);

                Scalar assetKey;
                while ((assetKey = yamlEventReader.Allow<Scalar>()) != null)
                {
                    // Only allow Id before SerializedVersion
                    if (assetKey.Value == "Id")
                    {
                        yamlEventReader.Skip();
                        continue;
                    }
                    if (assetKey.Value == "SerializedVersion")
                    {
                        serializedVersion = Convert.ToInt32(yamlEventReader.Expect<Scalar>().Value, CultureInfo.InvariantCulture);
                        break;
                    }
                }
            }

            if (serializedVersion > expectedVersion)
            {
                // Try to open an asset newer than what we support (probably generated by a newer Paradox)
                throw new InvalidOperationException(string.Format("Asset of type {0} has been serialized with newer version {1}, but only version {2} is supported. Was this asset created with a newer version of Paradox?", assetType, serializedVersion, expectedVersion));
            }

            if (serializedVersion < expectedVersion)
            {
                // Perform asset upgrade
                context.Log.Verbose("{0} needs update, from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion);

                // transform the stream into string.
                string assetAsString;
                using (var assetStream = loadAsset.OpenStream())
                using (var assetStreamReader = new StreamReader(assetStream, Encoding.UTF8))
                {
                    assetAsString = assetStreamReader.ReadToEnd();
                }

                // Load the asset as a YamlNode object
                var input = new StringReader(assetAsString);
                var yamlStream = new YamlStream();
                yamlStream.Load(input);
                var yamlRootNode = (YamlMappingNode)yamlStream.Documents[0].RootNode;

                // Check if there is any asset updater
                var assetUpgraders = AssetRegistry.GetAssetUpgraders(assetType);
                if (assetUpgraders == null)
                {
                    throw new InvalidOperationException(string.Format("Asset of type {0} should be updated from version {1} to {2}, but no asset migration path was found", assetType, serializedVersion, expectedVersion));
                }

                // Instantiate asset updaters
                var currentVersion = serializedVersion;
                while (currentVersion != expectedVersion)
                {
                    int targetVersion;
                    // This will throw an exception if no upgrader is available for the given version, exiting the loop in case of error.
                    var upgrader = assetUpgraders.GetUpgrader(currentVersion, out targetVersion);
                    upgrader.Upgrade(context, currentVersion, targetVersion, yamlRootNode, loadAsset);
                    currentVersion = targetVersion;
                }

                // Make sure asset is updated to latest version
                YamlNode serializedVersionNode;
                var newSerializedVersion = 0;
                if (yamlRootNode.Children.TryGetValue(new YamlScalarNode("SerializedVersion"), out serializedVersionNode))
                {
                    newSerializedVersion = Convert.ToInt32(((YamlScalarNode)serializedVersionNode).Value);
                }

                if (newSerializedVersion != expectedVersion)
                {
                    throw new InvalidOperationException(string.Format("Asset of type {0} was migrated, but still its new version {1} doesn't match expected version {2}.", assetType, newSerializedVersion, expectedVersion));
                }

                context.Log.Info("{0} updated from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion);

                var preferredIndent = YamlSerializer.GetSerializerSettings().PreferredIndent;

                // Save asset back to disk
                using (var memoryStream = new MemoryStream())
                {
                    using (var streamWriter = new StreamWriter(memoryStream))
                    {
                        yamlStream.Save(streamWriter, true, preferredIndent);
                    }
                    loadAsset.AssetContent = memoryStream.ToArray();
                }

                return true;
            }

            return false;
        }
Exemple #55
0
        public void TestSerialization()
        {
            var grid    = (Ignite)_grid1;
            var comp    = (ImplCompute)grid.GetCluster().ForLocal().GetCompute();
            var locNode = grid.GetCluster().GetLocalNode();

            var expectedGuid     = Guid.Parse("00000000-0000-0001-0000-000000000002");
            var expectedGridGuid = new IgniteGuid(expectedGuid, 3);

            using (var inStream = IgniteManager.Memory.Allocate().GetStream())
            {
                var result = comp.ExecuteJavaTask <bool>("org.apache.ignite.platform.PlatformEventsWriteEventTask",
                                                         inStream.MemoryPointer);

                Assert.IsTrue(result);

                inStream.SynchronizeInput();

                var reader = grid.Marshaller.StartUnmarshal(inStream);

                var cacheEvent = EventReader.Read <CacheEvent>(reader);
                CheckEventBase(cacheEvent);
                Assert.AreEqual("cacheName", cacheEvent.CacheName);
                Assert.AreEqual(locNode, cacheEvent.EventNode);
                Assert.AreEqual(1, cacheEvent.Partition);
                Assert.AreEqual(true, cacheEvent.IsNear);
                Assert.AreEqual(2, cacheEvent.Key);
                Assert.AreEqual(expectedGridGuid, cacheEvent.Xid);
                Assert.AreEqual(4, cacheEvent.NewValue);
                Assert.AreEqual(true, cacheEvent.HasNewValue);
                Assert.AreEqual(5, cacheEvent.OldValue);
                Assert.AreEqual(true, cacheEvent.HasOldValue);
                Assert.AreEqual(expectedGuid, cacheEvent.SubjectId);
                Assert.AreEqual("cloClsName", cacheEvent.ClosureClassName);
                Assert.AreEqual("taskName", cacheEvent.TaskName);
                Assert.IsTrue(cacheEvent.ToShortString().StartsWith("NODE_FAILED: IsNear="));

                var qryExecEvent = EventReader.Read <CacheQueryExecutedEvent>(reader);
                CheckEventBase(qryExecEvent);
                Assert.AreEqual("qryType", qryExecEvent.QueryType);
                Assert.AreEqual("cacheName", qryExecEvent.CacheName);
                Assert.AreEqual("clsName", qryExecEvent.ClassName);
                Assert.AreEqual("clause", qryExecEvent.Clause);
                Assert.AreEqual(expectedGuid, qryExecEvent.SubjectId);
                Assert.AreEqual("taskName", qryExecEvent.TaskName);
                Assert.AreEqual(
                    "NODE_FAILED: QueryType=qryType, CacheName=cacheName, ClassName=clsName, Clause=clause, " +
                    "SubjectId=00000000-0000-0001-0000-000000000002, TaskName=taskName", qryExecEvent.ToShortString());

                var qryReadEvent = EventReader.Read <CacheQueryReadEvent>(reader);
                CheckEventBase(qryReadEvent);
                Assert.AreEqual("qryType", qryReadEvent.QueryType);
                Assert.AreEqual("cacheName", qryReadEvent.CacheName);
                Assert.AreEqual("clsName", qryReadEvent.ClassName);
                Assert.AreEqual("clause", qryReadEvent.Clause);
                Assert.AreEqual(expectedGuid, qryReadEvent.SubjectId);
                Assert.AreEqual("taskName", qryReadEvent.TaskName);
                Assert.AreEqual(1, qryReadEvent.Key);
                Assert.AreEqual(2, qryReadEvent.Value);
                Assert.AreEqual(3, qryReadEvent.OldValue);
                Assert.AreEqual(4, qryReadEvent.Row);
                Assert.AreEqual(
                    "NODE_FAILED: QueryType=qryType, CacheName=cacheName, ClassName=clsName, Clause=clause, " +
                    "SubjectId=00000000-0000-0001-0000-000000000002, TaskName=taskName, Key=1, Value=2, " +
                    "OldValue=3, Row=4", qryReadEvent.ToShortString());

                var cacheRebalancingEvent = EventReader.Read <CacheRebalancingEvent>(reader);
                CheckEventBase(cacheRebalancingEvent);
                Assert.AreEqual("cacheName", cacheRebalancingEvent.CacheName);
                Assert.AreEqual(1, cacheRebalancingEvent.Partition);
                Assert.AreEqual(locNode, cacheRebalancingEvent.DiscoveryNode);
                Assert.AreEqual(2, cacheRebalancingEvent.DiscoveryEventType);
                Assert.AreEqual(3, cacheRebalancingEvent.DiscoveryTimestamp);
                Assert.IsTrue(cacheRebalancingEvent.ToShortString().StartsWith(
                                  "NODE_FAILED: CacheName=cacheName, Partition=1, DiscoveryNode=GridNode"));

                var checkpointEvent = EventReader.Read <CheckpointEvent>(reader);
                CheckEventBase(checkpointEvent);
                Assert.AreEqual("cpKey", checkpointEvent.Key);
                Assert.AreEqual("NODE_FAILED: Key=cpKey", checkpointEvent.ToShortString());

                var discoEvent = EventReader.Read <DiscoveryEvent>(reader);
                CheckEventBase(discoEvent);
                Assert.AreEqual(grid.TopologyVersion, discoEvent.TopologyVersion);
                Assert.AreEqual(grid.GetNodes(), discoEvent.TopologyNodes);
                Assert.IsTrue(discoEvent.ToShortString().StartsWith("NODE_FAILED: EventNode=GridNode"));

                var jobEvent = EventReader.Read <JobEvent>(reader);
                CheckEventBase(jobEvent);
                Assert.AreEqual(expectedGridGuid, jobEvent.JobId);
                Assert.AreEqual("taskClsName", jobEvent.TaskClassName);
                Assert.AreEqual("taskName", jobEvent.TaskName);
                Assert.AreEqual(locNode, jobEvent.TaskNode);
                Assert.AreEqual(expectedGridGuid, jobEvent.TaskSessionId);
                Assert.AreEqual(expectedGuid, jobEvent.TaskSubjectId);
                Assert.IsTrue(jobEvent.ToShortString().StartsWith("NODE_FAILED: TaskName=taskName"));

                var taskEvent = EventReader.Read <TaskEvent>(reader);
                CheckEventBase(taskEvent);
                Assert.AreEqual(true, taskEvent.Internal);
                Assert.AreEqual(expectedGuid, taskEvent.SubjectId);
                Assert.AreEqual("taskClsName", taskEvent.TaskClassName);
                Assert.AreEqual("taskName", taskEvent.TaskName);
                Assert.AreEqual(expectedGridGuid, taskEvent.TaskSessionId);
                Assert.IsTrue(taskEvent.ToShortString().StartsWith("NODE_FAILED: TaskName=taskName"));
            }
        }
Exemple #56
0
 /// <summary>
 /// Deserializes an object from the specified <see cref="EventReader" /> with an expected specific type.
 /// </summary>
 /// <typeparam name="T">The expected type</typeparam>
 /// <param name="reader">The reader.</param>
 /// <param name="existingObject">The object to deserialize into.</param>
 /// <param name="context">The context used to deserialize this object.</param>
 /// <returns>A deserialized object.</returns>
 /// <exception cref="System.ArgumentNullException">reader</exception>
 public T DeserializeInto <T>(EventReader reader, T existingObject, out SerializerContext context)
 {
     return((T)Deserialize(reader, typeof(T), existingObject, null, out context));
 }
Exemple #57
0
        /// <summary>
        /// Deserializes an object from the specified <see cref="EventReader" /> with an expected specific type.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="expectedType">The expected type, maybe null.</param>
        /// <param name="existingObject">An existing object, may be null.</param>
        /// <param name="contextSettings">The context settings.</param>
        /// <returns>A deserialized object.</returns>
        /// <exception cref="System.ArgumentNullException">reader</exception>
        public object Deserialize(EventReader reader, Type expectedType, object existingObject = null, SerializerContextSettings contextSettings = null)
        {
            SerializerContext context;

            return(Deserialize(reader, expectedType, existingObject, contextSettings, out context));
        }
Exemple #58
0
        public static bool MigrateAssetIfNeeded(AssetMigrationContext context, PackageLoadingAssetFile loadAsset, string dependencyName, PackageVersion untilVersion = null)
        {
            var assetFullPath = loadAsset.FilePath.FullPath;

            // Determine if asset was Yaml or not
            var assetFileExtension = Path.GetExtension(assetFullPath);

            if (assetFileExtension == null)
            {
                return(false);
            }

            assetFileExtension = assetFileExtension.ToLowerInvariant();

            var serializer = AssetFileSerializer.FindSerializer(assetFileExtension);

            if (!(serializer is YamlAssetSerializer))
            {
                return(false);
            }

            // We've got a Yaml asset, let's get expected and serialized versions
            var            serializedVersion = PackageVersion.Zero;
            PackageVersion expectedVersion;
            Type           assetType;

            // Read from Yaml file the asset version and its type (to get expected version)
            // Note: It tries to read as few as possible (SerializedVersion is expected to be right after Id, so it shouldn't try to read further than that)
            using (var assetStream = loadAsset.OpenStream())
                using (var streamReader = new StreamReader(assetStream))
                {
                    var yamlEventReader = new EventReader(new Parser(streamReader));

                    // Skip header
                    yamlEventReader.Expect <StreamStart>();
                    yamlEventReader.Expect <DocumentStart>();
                    var mappingStart = yamlEventReader.Expect <MappingStart>();

                    var  tagTypeRegistry = AssetYamlSerializer.Default.GetSerializerSettings().TagTypeRegistry;
                    bool typeAliased;
                    assetType = tagTypeRegistry.TypeFromTag(mappingStart.Tag, out typeAliased);

                    var expectedVersions = AssetRegistry.GetCurrentFormatVersions(assetType);
                    expectedVersion = expectedVersions?.FirstOrDefault(x => x.Key == dependencyName).Value ?? PackageVersion.Zero;

                    Scalar assetKey;
                    while ((assetKey = yamlEventReader.Allow <Scalar>()) != null)
                    {
                        // Only allow Id before SerializedVersion
                        if (assetKey.Value == nameof(Asset.Id))
                        {
                            yamlEventReader.Skip();
                        }
                        else if (assetKey.Value == nameof(Asset.SerializedVersion))
                        {
                            // Check for old format: only a scalar
                            var scalarVersion = yamlEventReader.Allow <Scalar>();
                            if (scalarVersion != null)
                            {
                                serializedVersion = PackageVersion.Parse("0.0." + Convert.ToInt32(scalarVersion.Value, CultureInfo.InvariantCulture));

                                // Let's update to new format
                                using (var yamlAsset = loadAsset.AsYamlAsset())
                                {
                                    yamlAsset.DynamicRootNode.RemoveChild(nameof(Asset.SerializedVersion));
                                    AssetUpgraderBase.SetSerializableVersion(yamlAsset.DynamicRootNode, dependencyName, serializedVersion);

                                    var baseBranch = yamlAsset.DynamicRootNode["~Base"];
                                    if (baseBranch != null)
                                    {
                                        var baseAsset = baseBranch["Asset"];
                                        if (baseAsset != null)
                                        {
                                            baseAsset.RemoveChild(nameof(Asset.SerializedVersion));
                                            AssetUpgraderBase.SetSerializableVersion(baseAsset, dependencyName, serializedVersion);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                // New format: package => version mapping
                                yamlEventReader.Expect <MappingStart>();

                                while (!yamlEventReader.Accept <MappingEnd>())
                                {
                                    var packageName    = yamlEventReader.Expect <Scalar>().Value;
                                    var packageVersion = PackageVersion.Parse(yamlEventReader.Expect <Scalar>().Value);

                                    // For now, we handle only one dependency at a time
                                    if (packageName == dependencyName)
                                    {
                                        serializedVersion = packageVersion;
                                    }
                                }

                                yamlEventReader.Expect <MappingEnd>();
                            }
                            break;
                        }
                        else
                        {
                            // If anything else than Id or SerializedVersion, let's stop
                            break;
                        }
                    }
                }

            if (serializedVersion > expectedVersion)
            {
                // Try to open an asset newer than what we support (probably generated by a newer Stride)
                throw new InvalidOperationException($"Asset of type {assetType} has been serialized with newer version {serializedVersion}, but only version {expectedVersion} is supported. Was this asset created with a newer version of Stride?");
            }

            if (serializedVersion < expectedVersion)
            {
                // Perform asset upgrade
                context.Log.Verbose($"{Path.GetFullPath(assetFullPath)} needs update, from version {serializedVersion} to version {expectedVersion}");

                using (var yamlAsset = loadAsset.AsYamlAsset())
                {
                    var yamlRootNode = yamlAsset.RootNode;

                    // Check if there is any asset updater
                    var assetUpgraders = AssetRegistry.GetAssetUpgraders(assetType, dependencyName);
                    if (assetUpgraders == null)
                    {
                        throw new InvalidOperationException($"Asset of type {assetType} should be updated from version {serializedVersion} to {expectedVersion}, but no asset migration path was found");
                    }

                    // Instantiate asset updaters
                    var currentVersion = serializedVersion;
                    while (currentVersion != expectedVersion)
                    {
                        PackageVersion targetVersion;
                        // This will throw an exception if no upgrader is available for the given version, exiting the loop in case of error.
                        var upgrader = assetUpgraders.GetUpgrader(currentVersion, out targetVersion);

                        // Stop if the next version would be higher than what is expected
                        if (untilVersion != null && targetVersion > untilVersion)
                        {
                            break;
                        }

                        upgrader.Upgrade(context, dependencyName, currentVersion, targetVersion, yamlRootNode, loadAsset);
                        currentVersion = targetVersion;
                    }

                    // Make sure asset is updated to latest version
                    YamlNode       serializedVersionNode;
                    PackageVersion newSerializedVersion = null;
                    if (yamlRootNode.Children.TryGetValue(new YamlScalarNode(nameof(Asset.SerializedVersion)), out serializedVersionNode))
                    {
                        var newSerializedVersionForDefaultPackage = ((YamlMappingNode)serializedVersionNode).Children[new YamlScalarNode(dependencyName)];
                        newSerializedVersion = PackageVersion.Parse(((YamlScalarNode)newSerializedVersionForDefaultPackage).Value);
                    }

                    if (untilVersion == null && newSerializedVersion != expectedVersion)
                    {
                        throw new InvalidOperationException($"Asset of type {assetType} was migrated, but still its new version {newSerializedVersion} doesn't match expected version {expectedVersion}.");
                    }

                    context.Log.Verbose($"{Path.GetFullPath(assetFullPath)} updated from version {serializedVersion} to version {expectedVersion}");
                }

                return(true);
            }

            return(false);
        }
Exemple #59
0
        public async Task ProcessMissedEvents(IApiContext apiContext, DateTime?lastRunTime)
        {
            //get events since last run
            var filter = string.Empty;

            if (lastRunTime.HasValue)
            {
                filter = String.Format("createDate gt '{0}'", lastRunTime.Value.ToString("u"));
            }

            var eventReader = new EventReader {
                Context = apiContext, Filter = filter, PageSize = 200
            };
            var eventList = new List <Event>();

            while (await eventReader.ReadAsync())
            {
                eventList.AddRange(eventReader.Items);
            }

            if (!eventList.Any())
            {
                return;
            }

            var concise = from record in eventList
                          group record by new { record.EntityId, record.Topic }
            into g
            let recent = (
                from groupedItem in g
                orderby groupedItem.AuditInfo.UpdateDate descending
                select groupedItem
                ).First()
                         select recent;


            foreach (var evt in concise)
            {
                if (!evt.IsApproved())
                {
                    continue;
                }

                var aubEvent = evt.ToAubEvent();

                var entFilter   = String.Format("Id eq '{0}' and Status eq 'Processed'", aubEvent.Id);
                var evtInEntity = await GetEvents(apiContext.TenantId, entFilter);

                var evtList = evtInEntity.ToList();

                if (evtList.Any())
                {
                    var ent = evtList.FirstOrDefault();
                    if (ent != null && ent.ProcessedDateTime > evt.AuditInfo.UpdateDate)
                    {
                        continue;
                    }
                }

                var statusCode = StatusCode.New;
                if (evt.Topic.Contains(".updated"))
                {
                    statusCode = StatusCode.Active;
                }

                await _eventProcessor.ProcessEvent(apiContext, evt, statusCode);
            }
        }
Exemple #60
0
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            var scalar = reader.Allow <Scalar>();

            if (scalar == null)
            {
                value = null;
                return(false);
            }

            if (expectedType.IsEnum)
            {
                value = Enum.Parse(expectedType, scalar.Value);
            }
            else
            {
                TypeCode typeCode = Type.GetTypeCode(expectedType);
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    value = bool.Parse(scalar.Value);
                    break;

                case TypeCode.Byte:
                    value = Byte.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.Int16:
                    value = Int16.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.Int32:
                    value = Int32.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.Int64:
                    value = Int64.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.SByte:
                    value = SByte.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.UInt16:
                    value = UInt16.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.UInt32:
                    value = UInt32.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.UInt64:
                    value = UInt64.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.Single:
                    value = Single.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.Double:
                    value = Double.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.Decimal:
                    value = Decimal.Parse(scalar.Value, numberFormat);
                    break;

                case TypeCode.String:
                    value = scalar.Value;
                    break;

                case TypeCode.Char:
                    value = scalar.Value[0];
                    break;

                case TypeCode.DateTime:
                    // TODO: This is probably incorrect. Use the correct regular expression.
                    value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture);
                    break;

                default:
                    if (expectedType == typeof(object))
                    {
                        // Default to string
                        value = scalar.Value;
                    }
                    else
                    {
                        value = TypeConverter.ChangeType(scalar.Value, expectedType);
                    }
                    break;
                }
            }
            return(true);
        }