MissingComponent CreateMissingComponent(UnsafeValueView view)
            {
                var missingBehaviour = m_GameObject.AddComponent <MissingComponent>();

                var value   = new JsonObject();
                var visitor = new JsonSceneReader(m_SerializationMetadata);

                visitor.SetView(view);
                PropertyContainer.Visit(ref value, visitor);
                missingBehaviour.JsonString = JsonSerialization.ToJson(value);
                return(missingBehaviour);
            }
            public object Construct(Type type, UnsafeValueView view)
            {
                if (type != typeof(Component) || m_GameObject == null)
                {
                    return(null);
                }

                try
                {
                    var componentType = m_GenericConstructor.GetSerializedType();
                    if (componentType == null)
                    {
                        return(null);
                    }

#if !NET_DOTS && !ENABLE_IL2CPP
                    SceneSerialization.RegisterPropertyBag(componentType);
#endif

                    var properties = PropertyBagStore.GetPropertyBag(componentType);
                    if (properties == null)
                    {
                        Debug.LogWarning($"Could not resolve component type {componentType} deserializing {m_GameObject.name}. Will preserve serialized contents as JSON string");
                        return(CreateMissingComponent(view));
                    }

                    if (componentType == typeof(MissingComponent))
                    {
                        try
                        {
                            var value   = new JsonObject();
                            var visitor = new JsonSceneReader(m_SerializationMetadata);
                            visitor.SetView(view);
                            PropertyContainer.Visit(ref value, visitor);
                            var jsonString = value[nameof(MissingComponent.JsonString)].ToString();

                            // For some reason, newlines are read as null characters which break parsing
                            jsonString = jsonString.Replace('\0', '\n');

                            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
                            {
                                using (var reader = new SerializedObjectReader(stream))
                                {
                                    reader.Read(out var document);
                                    var componentVisitor = new ComponentVisitor(m_GameObject, document.AsUnsafe(),
                                                                                null, m_SerializationMetadata);

                                    Component missingComponent = null;
                                    componentVisitor.ReadValue(ref missingComponent, document.AsUnsafe());
                                    return(missingComponent);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Log($"Encountered an exception trying to deserialize MissingComponent. Preserving it as-is. Exception follows:\n{e}");
                        }
                    }

                    return(componentType == typeof(Transform) ? m_GameObject.GetComponent <Transform>() : m_GameObject.AddComponent(componentType));
                }
                catch (ArgumentException)
                {
                    Debug.LogWarning($"Could not resolve component type deserializing {m_GameObject.name}. Will preserve serialized contents as JSON string");
                    return(CreateMissingComponent(view));
                }
            }
Ejemplo n.º 3
0
 public UnsafeViewScope(JsonSceneReader visitor, UnsafeValueView view)
 {
     m_View           = visitor.m_View;
     m_Visitor        = visitor;
     m_Visitor.m_View = view;
 }
Ejemplo n.º 4
0
 public SerializedTypeScope(JsonSceneReader visitor, Type type)
 {
     m_SerializedType           = visitor.m_SerializedType;
     m_Visitor                  = visitor;
     m_Visitor.m_SerializedType = type;
 }
Ejemplo n.º 5
0
 public SerializedContainerMetadataScope(JsonSceneReader visitor, SerializedContainerMetadata metadata)
 {
     m_Visitor            = visitor;
     m_Metadata           = m_Visitor.m_Metadata;
     m_Visitor.m_Metadata = metadata;
 }