Example #1
0
        internal static void Construct <TValue>(ref TValue value, ISerializedTypeProvider provider)
        {
            if (RuntimeTypeInfoCache <TValue> .IsValueType)
            {
                return;
            }

            var serializedType = provider.GetSerializedType();

            if (null != serializedType)
            {
                if (!typeof(TValue).IsAssignableFrom(serializedType))
                {
                    throw new ArgumentException($"Type mismatch. DeclaredType=[{typeof(TValue)}] SerializedType=[{serializedType}]");
                }

                ConstructFromSerializedType(ref value, serializedType, provider);
                return;
            }

            if (RuntimeTypeInfoCache <TValue> .IsObjectType && null == value)
            {
                value = (TValue)provider.GetDefaultObject();
                return;
            }

            if (RuntimeTypeInfoCache <TValue> .IsAbstractOrInterface)
            {
                throw new ArgumentException();
            }

            ConstructFromDeclaredType(ref value, provider);
        }
            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));
                }
            }