/// <inheritdoc/>
        protected override object ReadCollectionItem(ref ObjectContext objectContext, Type itemType)
        {
            // Save the Yaml stream, in case loading fails we can keep this representation
            var parsingEvents = new List<ParsingEvent>();
            var reader = objectContext.Reader;
            var startDepth = reader.CurrentDepth;
            do
            {
                parsingEvents.Add(reader.Expect<ParsingEvent>());
            } while (reader.CurrentDepth > startDepth);

            // Save states
            var previousReader = objectContext.SerializerContext.Reader;
            var previousAllowErrors = objectContext.SerializerContext.AllowErrors;

            objectContext.SerializerContext.Reader = new EventReader(new MemoryParser(parsingEvents));
            objectContext.SerializerContext.AllowErrors = true;

            try
            {
                return objectContext.ObjectSerializerBackend.ReadCollectionItem(ref objectContext, itemType);
            }
            catch (YamlException)
            {
                // There was a failure, let's keep this object so that it can be serialized back later
                return new UnloadableScript(parsingEvents);
            }
            finally
            {
                // Restore states
                objectContext.SerializerContext.Reader = previousReader;
                objectContext.SerializerContext.AllowErrors = previousAllowErrors;
            }
        }
        public override string ReadMemberName(ref ObjectContext objectContext, string memberName)
        {
            var newMemberName = memberName.Trim(PostFixSealed, PostFixNew);

            if (newMemberName.Length != memberName.Length)
            {
                var overrideType = OverrideType.Base;
                if (memberName.Contains(PostFixNewSealed) || memberName.EndsWith(PostFixNewSealedAlt))
                {
                    overrideType = OverrideType.New | OverrideType.Sealed;
                }
                else if (memberName.EndsWith(PostFixNew))
                {
                    overrideType = OverrideType.New;
                }
                else if (memberName.EndsWith(PostFixSealed))
                {
                    overrideType = OverrideType.Sealed;
                }

                if (overrideType != OverrideType.Base)
                {
                    var objectType = objectContext.Instance.GetType();
                    if (cachedDescriptor == null || cachedDescriptor.Type != objectType)
                    {
                        cachedDescriptor = typeDescriptorFactory.Find(objectType);
                    }
                    var memberDescriptor = cachedDescriptor[newMemberName];
                    objectContext.Instance.SetOverride(memberDescriptor, overrideType);
                }
            }

            return base.ReadMemberName(ref objectContext, newMemberName);
        }
        public override void WriteMemberName(ref ObjectContext objectContext, SharpYaml.Serialization.IMemberDescriptor member, string memberName)
        {
            // Replace the key with SiliconStudio.Core.Reflection IMemberDescriptor
            // Cache previous 
            if (member != null)
            {
                var customDescriptor = (IMemberDescriptor)member.Tag;
                if (customDescriptor == null)
                {
                    customDescriptor = typeDescriptorFactory.Find(objectContext.Instance.GetType())[memberName];
                    member.Tag = customDescriptor;
                }

                if (customDescriptor != null)
                {
                    var overrideType = objectContext.Instance.GetOverride(customDescriptor);
                    if (!keepOnlySealedOverrides && (overrideType & OverrideType.New) != 0)
                    {
                        memberName += PostFixNew;
                    }
                    if ((overrideType & OverrideType.Sealed) != 0)
                    {
                        memberName += PostFixSealed;
                    }
                }
            }

            base.WriteMemberName(ref objectContext, member, memberName);
        }
Пример #4
0
 protected override void WriteScalar(ref ObjectContext objectContext, ScalarEventInfo scalar)
 {
     // TODO: if ParameterKey is written to an object, It will not serialized a tag
     scalar.Tag = null;
     scalar.IsPlainImplicit = true;
     base.WriteScalar(ref objectContext, scalar);
 }
        public override string ConvertTo(ref ObjectContext objectContext)
        {
            var attachedReference = AttachedReferenceManager.GetAttachedReference(objectContext.Instance);
            if (attachedReference == null)
                throw new YamlException(string.Format("Unable to extract asset reference from object [{0}]", objectContext.Instance));

            return string.Format("{0}:{1}", attachedReference.Id, attachedReference.Url);
        }
 protected override void WriteDictionaryItems(ref ObjectContext objectContext)
 {
     // Don't sort dictionary keys
     var savedSettings = objectContext.Settings.SortKeyForMapping;
     objectContext.Settings.SortKeyForMapping = false;
     base.WriteDictionaryItems(ref objectContext);
     objectContext.Settings.SortKeyForMapping = savedSettings;
 }
        protected override void WriteDictionaryItem(ref ObjectContext objectContext, KeyValuePair<object, object> keyValue, KeyValuePair<Type, Type> types)
        {
            var propertyKey = (PropertyKey)keyValue.Key;
            objectContext.SerializerContext.WriteYaml(propertyKey, types.Key);

            // Deduce expected value type from PropertyKey
            objectContext.SerializerContext.WriteYaml(keyValue.Value, propertyKey.PropertyType);
        }
Пример #8
0
 protected override void WriteDictionaryItems(ref ObjectContext objectContext)
 {
     //TODO: make SortKeyForMapping accessible in object context since it modifies the behavior of the serializer for children of the ComputeColorParameters
     var savedSettings = objectContext.Settings.SortKeyForMapping;
     objectContext.Settings.SortKeyForMapping = false;
     base.WriteDictionaryItems(ref objectContext);
     objectContext.Settings.SortKeyForMapping = savedSettings;
 }
Пример #9
0
        public override string ConvertTo(ref ObjectContext objectContext)
        {
            var attachedReference = AttachedReferenceManager.GetAttachedReference(objectContext.Instance);
            if (attachedReference == null)
                throw new YamlException($"Unable to extract asset reference from object [{objectContext.Instance}]");

            var referenceId = IdentifiableHelper.GetId(objectContext.Instance);
            return $"{referenceId}/{attachedReference.Id}:{attachedReference.Url}";
        }
Пример #10
0
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     PackageVersion packageVersion;
     if (!PackageVersion.TryParse(fromScalar.Value, out packageVersion))
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, "Invalid version format. Unable to decode [{0}]".ToFormat(fromScalar.Value));
     }
     return packageVersion;
 }
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     PackageReference packageReference;
     if (!PackageReference.TryParse(fromScalar.Value, out packageReference))
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode package reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value));
     }
     return packageReference;
 }
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     AssetReference assetReference;
     if (!AssetReference.TryParse(context.Descriptor.Type, fromScalar.Value, out assetReference))
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode asset reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value));
     }
     return assetReference;
 }
Пример #13
0
 public override object ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)
 {
     var parameterKey = ParameterKeys.FindByName(fromScalar.Value);
     if (parameterKey == null)
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to find registered ParameterKey [{0}]".ToFormat(fromScalar.Value));
     }
     return parameterKey;
 }
        protected override KeyValuePair<object, object> ReadDictionaryItem(ref ObjectContext objectContext, KeyValuePair<Type, Type> keyValueType)
        {
            // Read PropertyKey
            var keyResult = (PropertyKey)objectContext.SerializerContext.ReadYaml(null, keyValueType.Key);

            // Deduce expected value type from PropertyKey
            var valueResult = objectContext.SerializerContext.ReadYaml(null, keyResult.PropertyType);

            return new KeyValuePair<object, object>(keyResult, valueResult);
        }
Пример #15
0
 public override object ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)
 {
     Guid id;
     if (!Guid.TryParse(fromScalar.Value, out id))
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, $"Unable to parse id [{fromScalar.Value}]");
     }
     var materialNull = new MaterialNull();
     IdentifiableHelper.SetId(materialNull, id);
     return materialNull;
 }
        public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
        {
            Guid guid;
            UFile location;
            if (!AssetReference.TryParse(fromScalar.Value, out guid, out location))
            {
                throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode asset reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value));
            }

            return AttachedReferenceManager.CreateSerializableVersion(context.Descriptor.Type, guid, location);
        }
Пример #17
0
 public override object ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)
 {
     var parameterKey = ParameterKeys.FindByName(fromScalar.Value);
     if (parameterKey == null)
     {
         parameterKey = ParameterKeys.New<object>(null, fromScalar.Value);
         // Dont' throw an exception if keys was not found
         //throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to find registered ParameterKey [{0}]".ToFormat(fromScalar.Value));
     }
     return parameterKey;
 }
        protected override void TransformObjectAfterRead(ref ObjectContext objectContext)
        {
            if (!objectContext.SerializerContext.IsSerializing)
            {
                var settingsDictionary = (SettingsDictionary)objectContext.Instance;
                var settingsProfile = settingsDictionary.Profile;

                settingsProfile.Container.DecodeSettings(settingsDictionary, settingsProfile);

                objectContext.Instance = settingsProfile;
            }
        }
        protected override void TransformObjectAfterRead(ref ObjectContext objectContext)
        {
            if (!objectContext.SerializerContext.IsSerializing)
            {
                var settingsDictionary = (SettingsDictionary)objectContext.Instance;
                var settingsCollection = (SettingsCollection)settingsDictionary.Tags;

                settingsCollection.Profile.Group.DecodeSettings(settingsDictionary, settingsCollection.Profile);

                objectContext.Instance = settingsCollection;
            }
        }
Пример #20
0
        public override string ConvertTo(ref ObjectContext objectContext)
        {
            var propertyKey = (PropertyKey)objectContext.Instance;

            var className = objectContext.SerializerContext.TagFromType(propertyKey.OwnerType);
            var sb = new StringBuilder(className.Length + 1 + propertyKey.Name.Length);

            sb.Append(className, 1, className.Length - 1); // Ignore initial '!'
            sb.Append('.');
            sb.Append(propertyKey.Name);

            return sb.ToString();
        }
        protected override void WriteDictionaryItem(ref ObjectContext objectContext, KeyValuePair<object, object> keyValue, KeyValuePair<Type, Type> types)
        {
            var propertyKey = (UFile)keyValue.Key;
            objectContext.SerializerContext.WriteYaml(propertyKey, types.Key);

            // Deduce expected value type from PropertyKey
            var parsingEvents = (List<ParsingEvent>)keyValue.Value;
            var writer = objectContext.Writer;
            foreach (var parsingEvent in parsingEvents)
            {
                writer.Emit(parsingEvent);
            }
        }
        protected override void CreateOrTransformObject(ref ObjectContext objectContext)
        {
            var settingsProfile = (SettingsProfile)objectContext.Instance;
            var settingsDictionary = new SettingsDictionary { Profile = settingsProfile };

            if (objectContext.SerializerContext.IsSerializing)
            {
                settingsProfile.Container.EncodeSettings(settingsProfile, settingsDictionary);
            }

            objectContext.Instance = settingsDictionary;

            base.CreateOrTransformObject(ref objectContext);
        }
        protected override void CreateOrTransformObject(ref ObjectContext objectContext)
        {
            var settingsCollection = (SettingsCollection)objectContext.Instance;
            var settingsDictionary = new SettingsDictionary { Tags = settingsCollection };

            if (objectContext.SerializerContext.IsSerializing)
            {
                settingsCollection.Profile.Group.EncodeSettings(settingsCollection.Profile, settingsDictionary);
            }

            objectContext.Instance = settingsDictionary;

            base.CreateOrTransformObject(ref objectContext);
        }
        /// <inheritdoc/>
        protected override void WriteCollectionItem(ref ObjectContext objectContext, object item, Type itemType)
        {
            // Check if we have a Yaml representation (in case loading failed)
            var unloadableScript = item as UnloadableScript;
            if (unloadableScript != null)
            {
                var writer = objectContext.Writer;
                foreach (var parsingEvent in unloadableScript.ParsingEvents)
                {
                    writer.Emit(parsingEvent);
                }
                return;
            }

            base.WriteCollectionItem(ref objectContext, item, itemType);
        }
Пример #25
0
        protected override void ReadAddCollectionItem(ref ObjectContext objectContext, Type elementType, CollectionDescriptor collectionDescriptor, object thisObject, int index)
        {
            var scriptCollection = (ScriptCollection)objectContext.Instance;

            object value = null;
            bool needAdd = true; // If we could get existing value, no need add to collection
            if (scriptCollection.Count > index)
            {
                value = scriptCollection[index];
                needAdd = false;
            }

            value = ReadCollectionItem(ref objectContext, value, elementType);
            if (needAdd)
                collectionDescriptor.CollectionAdd(thisObject, value);
        }
Пример #26
0
        /// <inheritdoc/>
        protected override void TransformObjectAfterRead(ref ObjectContext objectContext)
        {
            if (recursionLevel >= 2)
            {
                // We are inside a Script
                if (!objectContext.SerializerContext.IsSerializing)
                {
                    if (objectContext.Instance is CloneReference)
                    {
                        objectContext.Instance = References[((CloneReference)objectContext.Instance).Id];
                        return;
                    }
                }
            }

            base.TransformObjectAfterRead(ref objectContext);
        }
        protected override void ReadAddCollectionItem(ref ObjectContext objectContext, Type elementType, CollectionDescriptor collectionDescriptor, object thisObject, int index)
        {
            var scriptCollection = (EntityComponentCollection)objectContext.Instance;

            EntityComponent value = null;
            bool needAdd = true; // If we could get existing value, no need add to collection
            if (index < scriptCollection.Count)
            {
                value = scriptCollection[index];
                needAdd = false;
            }

            value = (EntityComponent)ReadCollectionItem(ref objectContext, value, elementType, index);
            if (needAdd)
            {
                scriptCollection.Add(value);
            }
        }
        protected override void CreateOrTransformObject(ref ObjectContext objectContext)
        {
            if (isSerializingAsReference)
            {
                // Create appropriate reference type for both serialization and deserialization
                if (objectContext.SerializerContext.IsSerializing)
                {
                    var entityComponent = objectContext.Instance as EntityComponent;
                    var entityScript = objectContext.Instance as Script;
                    if (entityComponent != null)
                    {
                        objectContext.Instance = new EntityComponentReference(entityComponent);
                    }
                    else if (entityScript != null && scriptLevel > 1)
                    {
                        var script = new EntityScriptReference(entityScript);
                        objectContext.Instance = script;
                        objectContext.Tag = objectContext.Settings.TagTypeRegistry.TagFromType(entityScript.GetType());
                    }
                    else if (objectContext.Instance is Entity)
                    {
                        objectContext.Instance = new EntityReference { Id = ((Entity)objectContext.Instance).Id };
                    }
                }
                else
                {
                    var type = objectContext.Descriptor.Type;
                    if (typeof(EntityComponent).IsAssignableFrom(type))
                    {
                        objectContext.Instance = new EntityComponentReference();
                    }
                    else if (typeof(Script).IsAssignableFrom(type) && scriptLevel > 1)
                    {
                        objectContext.Instance = new EntityScriptReference { ScriptType = objectContext.Descriptor.Type };
                    }
                    else if (type == typeof(Entity))
                    {
                        objectContext.Instance = new EntityReference();
                    }
                }
            }

            base.CreateOrTransformObject(ref objectContext);
        }
        protected override KeyValuePair<object, object> ReadDictionaryItem(ref ObjectContext objectContext, KeyValuePair<Type, Type> keyValueType)
        {
            // Read PropertyKey
            var keyResult = (UFile)objectContext.SerializerContext.ReadYaml(null, keyValueType.Key);

            // Save the Yaml stream, in case loading fails we can keep this representation
            var parsingEvents = new List<ParsingEvent>();
            var reader = objectContext.Reader;
            var startDepth = reader.CurrentDepth;
            do
            {
                parsingEvents.Add(reader.Expect<ParsingEvent>());
            } while (reader.CurrentDepth > startDepth);


            var valueResult = parsingEvents;

            return new KeyValuePair<object, object>(keyResult, valueResult);
        }
Пример #30
0
        protected override void CreateOrTransformObject(ref ObjectContext objectContext)
        {
            if (recursionLevel >= recursionMaxExpectedDepth)
            {
                if (objectContext.SerializerContext.IsSerializing)
                {
                    var entityComponent = objectContext.Instance as EntityComponent;
                    var entityScript = objectContext.Instance as Script;
                    if (entityComponent != null)
                    {
                        objectContext.Instance = new EntityComponentReference(entityComponent);
                    }
                    else if (entityScript != null && levelSinceScriptComponent != 1)
                    {
                        var script = new EntityScriptReference(entityScript);
                        objectContext.Instance = script;
                        objectContext.Tag = objectContext.Settings.TagTypeRegistry.TagFromType(entityScript.GetType());
                    }
                    else if (objectContext.Instance is Entity)
                    {
                        objectContext.Instance = new EntityReference { Id = ((Entity)objectContext.Instance).Id };
                    }
                }
                else
                {
                    var type = objectContext.Descriptor.Type;
                    if (typeof(EntityComponent).IsAssignableFrom(type))
                    {
                        objectContext.Instance = new EntityComponentReference();
                    }
                    else if (typeof(Script).IsAssignableFrom(type) && levelSinceScriptComponent != 1)
                    {
                        objectContext.Instance = new EntityScriptReference { ScriptType = objectContext.Descriptor.Type };
                    }
                    else if (type == typeof(Entity))
                    {
                        objectContext.Instance = new EntityReference();
                    }
                }
            }

            base.CreateOrTransformObject(ref objectContext);
        }