Beispiel #1
0
        public void GenerateDLL()
        {
            FixUnityAboutWindowBeforeEmit.Fix();

            const string NAME = FormatterEmitter.PRE_EMITTED_ASSEMBLY_NAME;

            var dirPath         = this.AOTFolderPath;
            var newDllPath      = dirPath + NAME;
            var fullDllPath     = newDllPath + ".dll";
            var fullLinkXmlPath = dirPath + "link.xml";

            var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName()
            {
                Name = NAME
            }, AssemblyBuilderAccess.Save, dirPath);
            var module = assembly.DefineDynamicModule(NAME);

            // The following is a fix for Unity's crappy Mono runtime that doesn't know how to do this sort
            //  of stuff properly
            //
            // We must manually remove the "Default Dynamic Assembly" module that is automatically defined,
            //  otherwise a reference to that non-existent module will be saved into the assembly's IL, and
            //  that will cause a multitude of issues.
            //
            // We do this by forcing there to be only one module - the one we just defined, and we set the
            //   manifest module to be that module as well.
            {
                var modulesField        = assembly.GetType().GetField("modules", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                var manifestModuleField = assembly.GetType().GetField("manifest_module", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

                if (modulesField != null)
                {
                    modulesField.SetValue(assembly, new ModuleBuilder[] { module });
                }

                if (manifestModuleField != null)
                {
                    manifestModuleField.SetValue(assembly, module);
                }
            }

            var type = module.DefineType(NAME + ".PreventCodeStrippingViaReferences", TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.NotPublic);

            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(typeof(PreserveAttribute).GetConstructor(Type.EmptyTypes), new object[0]);

            type.SetCustomAttribute(attributeBuilder);

            var staticConstructor = type.DefineTypeInitializer();
            var il = staticConstructor.GetILGenerator();

            HashSet <Type> seenTypes = new HashSet <Type>();

            //var endPoint = il.DefineLabel();
            //il.Emit(OpCodes.Br, endPoint);

            foreach (var typeEntry in this.supportSerializedTypes)
            {
                if (typeEntry.Type == null || !typeEntry.Emit)
                {
                    continue;
                }

                if (typeEntry.Type.IsAbstract || typeEntry.Type.IsInterface)
                {
                    Debug.LogError("Skipping type '" + typeEntry.Type.GetNiceFullName() + "'! Type is abstract or an interface.");
                    continue;
                }

                if (typeEntry.Type.IsGenericType && (typeEntry.Type.IsGenericTypeDefinition || !typeEntry.Type.IsFullyConstructedGenericType()))
                {
                    Debug.LogError("Skipping type '" + typeEntry.Type.GetNiceFullName() + "'! Type is a generic type definition, or its arguments contain generic parameters; type must be a fully constructed generic type.");
                    continue;
                }

                var serializedType = typeEntry.Type;

                if (seenTypes.Contains(serializedType))
                {
                    continue;
                }

                seenTypes.Add(serializedType);

                // Reference serialized type
                {
                    if (serializedType.IsValueType)
                    {
                        var local = il.DeclareLocal(serializedType);

                        il.Emit(OpCodes.Ldloca, local);
                        il.Emit(OpCodes.Initobj, serializedType);
                    }
                    else
                    {
                        var constructor = serializedType.GetConstructor(Type.EmptyTypes);

                        if (constructor != null)
                        {
                            il.Emit(OpCodes.Newobj, constructor);
                            il.Emit(OpCodes.Pop);
                        }
                    }
                }

                // Reference and/or create formatter type
                if (!FormatterUtilities.IsPrimitiveType(serializedType))
                {
                    var formatter = FormatterLocator.GetFormatter(serializedType, SerializationPolicies.Unity);

                    if (formatter.GetType().IsDefined <EmittedFormatterAttribute>())
                    {
                        //// Emit an actual AOT formatter into the generated assembly

                        //if (this.emitAOTFormatters)
                        //{
                        //    var emittedFormatter = FormatterEmitter.EmitAOTFormatter(typeEntry.Type, module, SerializationPolicies.Unity);
                        //    var emittedFormatterConstructor = emittedFormatter.GetConstructor(Type.EmptyTypes);

                        //    il.Emit(OpCodes.Newobj, emittedFormatterConstructor);
                        //    il.Emit(OpCodes.Pop);
                        //}
                    }
                    else
                    {
                        // Just reference the pre-existing formatter

                        var formatterConstructor = formatter.GetType().GetConstructor(Type.EmptyTypes);

                        if (formatterConstructor != null)
                        {
                            il.Emit(OpCodes.Newobj, formatterConstructor);
                            il.Emit(OpCodes.Pop);
                        }
                    }

                    // Make sure we have a proper reflection formatter variant if all else goes wrong
                    il.Emit(OpCodes.Newobj, typeof(ReflectionFormatter <>).MakeGenericType(typeEntry.Type).GetConstructor(Type.EmptyTypes));
                    il.Emit(OpCodes.Pop);
                }

                ConstructorInfo serializerConstructor;

                // Reference serializer variant
                if (serializedType.IsEnum)
                {
                    serializerConstructor = typeof(EnumSerializer <>).MakeGenericType(serializedType).GetConstructor(Type.EmptyTypes);
                }
                else
                {
                    serializerConstructor = typeof(ComplexTypeSerializer <>).MakeGenericType(serializedType).GetConstructor(Type.EmptyTypes);
                }

                il.Emit(OpCodes.Newobj, serializerConstructor);
                il.Emit(OpCodes.Pop);
            }

            //il.MarkLabel(endPoint);
            il.Emit(OpCodes.Ret);

            type.CreateType();

            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            if (File.Exists(fullDllPath))
            {
                File.Delete(fullDllPath);
            }

            if (File.Exists(fullDllPath + ".meta"))
            {
                File.Delete(fullDllPath + ".meta");
            }

            try
            {
                AssetDatabase.Refresh();
            }
            catch (Exception)
            {
                // Sigh, Unity 5.3.0
            }

            assembly.Save(NAME);

            File.Move(newDllPath, fullDllPath);
            File.WriteAllText(fullLinkXmlPath, LINK_XML_CONTENTS);

            try
            {
                AssetDatabase.Refresh();
            }
            catch (Exception)
            {
                // Sigh, Unity 5.3.0
            }

            var pluginImporter = PluginImporter.GetAtPath(fullDllPath) as PluginImporter;

            if (pluginImporter != null)
            {
                //pluginImporter.ClearSettings();

                pluginImporter.SetCompatibleWithEditor(false);
                pluginImporter.SetCompatibleWithAnyPlatform(true);

                // Disable for all standalones
                pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinux, false);
                pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinux64, false);
                pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinuxUniversal, false);

                // StandaloneOSXUniversal (<= 2017.2) / StandaloneOSX (>= 2017.3)
                pluginImporter.SetCompatibleWithPlatform((BuildTarget)2, false);

                if (!UnityVersion.IsVersionOrGreater(2017, 3))
                {
                    pluginImporter.SetCompatibleWithPlatform((BuildTarget)4, false);        // StandaloneOSXIntel
                    pluginImporter.SetCompatibleWithPlatform((BuildTarget)27, false);       // StandaloneOSXIntel64
                }

                pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, false);
                pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, false);

                //pluginImporter.SetCompatibleWithPlatform(BuildTarget.Android, false);

                pluginImporter.SaveAndReimport();
            }

            AssetDatabase.SaveAssets();
        }
        /// <summary>
        /// Writes a value of type <see cref="T" />.
        /// </summary>
        /// <param name="name">The name of the value to write.</param>
        /// <param name="value">The value to write.</param>
        /// <param name="writer">The writer to use.</param>
        public override void WriteValue(string name, T value, IDataWriter writer)
        {
            var context = writer.Context;

            if (context.Config.SerializationPolicy.AllowNonSerializableTypes == false && typeof(T).IsSerializable == false)
            {
                context.Config.DebugContext.LogError("The type " + typeof(T).Name + " is not marked as serializable.");
                return;
            }

            FireOnSerializedType();

            if (ComplexTypeIsValueType)
            {
                bool endNode = true;

                try
                {
                    writer.BeginStructNode(name, typeof(T));
                    FormatterLocator.GetFormatter <T>(context.Config.SerializationPolicy).Serialize(value, writer);
                }
                catch (SerializationAbortException ex)
                {
                    endNode = false;
                    throw ex;
                }
                finally
                {
                    if (endNode)
                    {
                        writer.EndNode(name);
                    }
                }
            }
            else
            {
                int    id;
                int    index;
                string strId;
                Guid   guid;

                bool endNode = true;

                if (object.ReferenceEquals(value, null))
                {
                    writer.WriteNull(name);
                }
                else if (context.TryRegisterExternalReference(value, out index))
                {
                    writer.WriteExternalReference(name, index);
                }
                else if (context.TryRegisterExternalReference(value, out guid))
                {
                    writer.WriteExternalReference(name, guid);
                }
                else if (context.TryRegisterExternalReference(value, out strId))
                {
                    writer.WriteExternalReference(name, strId);
                }
                else if (context.TryRegisterInternalReference(value, out id))
                {
                    // Get type of actual stored object
                    //
                    // Don't have it as a strongly typed T value, since people can "override" (shadow)
                    // GetType() on derived classes with the "new" operator. By referencing the type
                    // as a System.Object, we ensure the correct GetType() method is always called.
                    //
                    // (Yes, this has actually happened, and this was done to fix it.)

                    Type type = (value as object).GetType();

                    if (ComplexTypeMayBeBoxedValueType && FormatterUtilities.IsPrimitiveType(type)) // It's a boxed primitive type
                    {
                        try
                        {
                            writer.BeginReferenceNode(name, type, id);

                            var serializer = Serializer.Get(type);
                            serializer.WriteValueWeak(value, writer);
                        }
                        catch (SerializationAbortException ex)
                        {
                            endNode = false;
                            throw ex;
                        }
                        finally
                        {
                            if (endNode)
                            {
                                writer.EndNode(name);
                            }
                        }
                    }
                    else
                    {
                        var formatter = FormatterLocator.GetFormatter(type, context.Config.SerializationPolicy);

                        try
                        {
                            writer.BeginReferenceNode(name, type, id);
                            formatter.Serialize(value, writer);
                        }
                        catch (SerializationAbortException ex)
                        {
                            endNode = false;
                            throw ex;
                        }
                        finally
                        {
                            if (endNode)
                            {
                                writer.EndNode(name);
                            }
                        }
                    }
                }
                else
                {
                    writer.WriteInternalReference(name, id);
                }
            }
        }
        /// <summary>
        /// Skips the next entry value, unless it is an <see cref="EntryType.EndOfNode"/> or an <see cref="EntryType.EndOfArray"/>. If the next entry value is an <see cref="EntryType.StartOfNode"/> or an <see cref="EntryType.StartOfArray"/>, all of its contents will be processed, deserialized and registered in the deserialization context, so that internal reference values are not lost to entries further down the stream.
        /// </summary>
        public virtual void SkipEntry()
        {
            var peekedEntry = this.PeekEntry();

            if (peekedEntry == EntryType.StartOfNode)
            {
                Type type;

                bool exitNode = true;

                this.EnterNode(out type);

                try
                {
                    if (type != null)
                    {
                        // We have the necessary metadata to read this type, and register all of its reference values (perhaps including itself) in the serialization context
                        // Sadly, we have no choice but to risk boxing of structs here
                        // Luckily, this is a rare case

                        if (FormatterUtilities.IsPrimitiveType(type))
                        {
                            // It is a boxed primitive type; we read the value and register it
                            var    serializer = Serializer.Get(type);
                            object value      = serializer.ReadValueWeak(this);

                            if (this.CurrentNodeId >= 0)
                            {
                                this.Context.RegisterInternalReference(this.CurrentNodeId, value);
                            }
                        }
                        else
                        {
                            var    formatter = FormatterLocator.GetFormatter(type, this.Context.Config.SerializationPolicy);
                            object value     = formatter.Deserialize(this);

                            if (this.CurrentNodeId >= 0)
                            {
                                this.Context.RegisterInternalReference(this.CurrentNodeId, value);
                            }
                        }
                    }
                    else
                    {
                        // We have no metadata, and reference values might be lost
                        // We must read until a node on the same level terminates
                        while (true)
                        {
                            peekedEntry = this.PeekEntry();

                            if (peekedEntry == EntryType.EndOfStream)
                            {
                                break;
                            }
                            else if (peekedEntry == EntryType.EndOfNode)
                            {
                                break;
                            }
                            else if (peekedEntry == EntryType.EndOfArray)
                            {
                                this.ReadToNextEntry(); // Consume end of arrays that we can potentially get stuck on
                            }
                            else
                            {
                                this.SkipEntry();
                            }
                        }
                    }
                }
                catch (SerializationAbortException ex)
                {
                    exitNode = false;
                    throw ex;
                }
                finally
                {
                    if (exitNode)
                    {
                        this.ExitNode();
                    }
                }
            }
            else if (peekedEntry == EntryType.StartOfArray)
            {
                // We must read until an array on the same level terminates
                this.ReadToNextEntry(); // Consume start of array

                while (true)
                {
                    peekedEntry = this.PeekEntry();

                    if (peekedEntry == EntryType.EndOfStream)
                    {
                        break;
                    }
                    else if (peekedEntry == EntryType.EndOfArray)
                    {
                        this.ReadToNextEntry(); // Consume end of array and break
                        break;
                    }
                    else if (peekedEntry == EntryType.EndOfNode)
                    {
                        this.ReadToNextEntry(); // Consume end of nodes that we can potentially get stuck on
                    }
                    else
                    {
                        this.SkipEntry();
                    }
                }
            }
            else if (peekedEntry != EntryType.EndOfArray && peekedEntry != EntryType.EndOfNode) // We can't skip end of arrays and end of nodes
            {
                this.ReadToNextEntry();                                                         // We can just skip a single value entry
            }
        }
        /// <summary>
        /// Reads a value of type <see cref="T" />.
        /// </summary>
        /// <param name="reader">The reader to use.</param>
        /// <returns>
        /// The value which has been read.
        /// </returns>
        public override T ReadValue(IDataReader reader)
        {
            var context = reader.Context;

            if (context.Config.SerializationPolicy.AllowNonSerializableTypes == false && typeof(T).IsSerializable == false)
            {
                context.Config.DebugContext.LogError("The type " + typeof(T).Name + " is not marked as serializable.");
                return(default(T));
            }

            bool exitNode = true;

            string name;
            var    entry = reader.PeekEntry(out name);

            if (ComplexTypeIsValueType)
            {
                if (entry == EntryType.Null)
                {
                    context.Config.DebugContext.LogWarning("Expecting complex struct of type " + typeof(T).GetNiceFullName() + " but got null value.");
                    reader.ReadNull();
                    return(default(T));
                }
                else if (entry != EntryType.StartOfNode)
                {
                    context.Config.DebugContext.LogWarning("Unexpected entry '" + name + "' of type " + entry.ToString() + ", when " + EntryType.StartOfNode + " was expected. A value has likely been lost.");
                    reader.SkipEntry();
                    return(default(T));
                }

                try
                {
                    Type expectedType = typeof(T);
                    Type serializedType;

                    if (reader.EnterNode(out serializedType))
                    {
                        if (serializedType != expectedType)
                        {
                            if (serializedType != null)
                            {
                                context.Config.DebugContext.LogWarning("Expected complex struct value " + expectedType.Name + " but the serialized value is of type " + serializedType.Name + ".");

                                if (serializedType.IsCastableTo(expectedType))
                                {
                                    object value = FormatterLocator.GetFormatter(serializedType, context.Config.SerializationPolicy).Deserialize(reader);

                                    bool serializedTypeIsNullable = serializedType.IsGenericType && serializedType.GetGenericTypeDefinition() == typeof(Nullable <>);
                                    bool allowCastMethod          = !ComplexTypeIsNullable && !serializedTypeIsNullable;

                                    var castMethod = allowCastMethod ? serializedType.GetCastMethodDelegate(expectedType) : null;

                                    if (castMethod != null)
                                    {
                                        return((T)castMethod(value));
                                    }
                                    else
                                    {
                                        return((T)value);
                                    }
                                }
                                else
                                {
                                    context.Config.DebugContext.LogWarning("Can't cast serialized type " + serializedType.Name + " into expected type " + expectedType.Name + ". Value lost for node '" + name + "'.");
                                    return(default(T));
                                }
                            }
                            else
                            {
                                context.Config.DebugContext.LogWarning("Expected complex struct value " + expectedType.Name + " but the serialized type could not be resolved.");
                                return(default(T));
                            }
                        }
                        else
                        {
                            return(FormatterLocator.GetFormatter <T>(context.Config.SerializationPolicy).Deserialize(reader));
                        }
                    }
                    else
                    {
                        context.Config.DebugContext.LogError("Failed to enter node '" + name + "'.");
                        return(default(T));
                    }
                }
                catch (SerializationAbortException ex)
                {
                    exitNode = false;
                    throw ex;
                }
                catch (Exception ex)
                {
                    context.Config.DebugContext.LogException(ex);
                    return(default(T));
                }
                finally
                {
                    if (exitNode)
                    {
                        reader.ExitNode();
                    }
                }
            }
            else
            {
                switch (entry)
                {
                case EntryType.Null:
                {
                    reader.ReadNull();
                    return(default(T));
                }

                case EntryType.ExternalReferenceByIndex:
                {
                    int index;
                    reader.ReadExternalReference(out index);

                    object value = context.GetExternalObject(index);

                    try
                    {
                        return((T)value);
                    }
                    catch (InvalidCastException)
                    {
                        context.Config.DebugContext.LogWarning("Can't cast external reference type " + value.GetType().Name + " into expected type " + typeof(T).Name + ". Value lost for node '" + name + "'.");
                        return(default(T));
                    }
                }

                case EntryType.ExternalReferenceByGuid:
                {
                    Guid guid;
                    reader.ReadExternalReference(out guid);

                    object value = context.GetExternalObject(guid);

                    try
                    {
                        return((T)value);
                    }
                    catch (InvalidCastException)
                    {
                        context.Config.DebugContext.LogWarning("Can't cast external reference type " + value.GetType().Name + " into expected type " + typeof(T).Name + ". Value lost for node '" + name + "'.");
                        return(default(T));
                    }
                }

                case EntryType.ExternalReferenceByString:
                {
                    string id;
                    reader.ReadExternalReference(out id);

                    object value = context.GetExternalObject(id);

                    try
                    {
                        return((T)value);
                    }
                    catch (InvalidCastException)
                    {
                        context.Config.DebugContext.LogWarning("Can't cast external reference type " + value.GetType().Name + " into expected type " + typeof(T).Name + ". Value lost for node '" + name + "'.");
                        return(default(T));
                    }
                }

                case EntryType.InternalReference:
                {
                    int id;
                    reader.ReadInternalReference(out id);

                    object value = context.GetInternalReference(id);

                    try
                    {
                        return((T)value);
                    }
                    catch (InvalidCastException)
                    {
                        context.Config.DebugContext.LogWarning("Can't cast internal reference type " + value.GetType().Name + " into expected type " + typeof(T).Name + ". Value lost for node '" + name + "'.");
                        return(default(T));
                    }
                }

                case EntryType.StartOfNode:
                {
                    try
                    {
                        Type expectedType = typeof(T);
                        Type serializedType;
                        int  id;

                        if (reader.EnterNode(out serializedType))
                        {
                            id = reader.CurrentNodeId;

                            T result;

                            if (serializedType != null && expectedType != serializedType)         // We have type metadata different from the expected type
                            {
                                bool success     = false;
                                var  isPrimitive = FormatterUtilities.IsPrimitiveType(serializedType);

                                bool assignableCast;

                                if (ComplexTypeMayBeBoxedValueType && isPrimitive)
                                {
                                    // It's a boxed primitive type, so simply read that straight and register success
                                    var serializer = Serializer.Get(serializedType);
                                    result  = (T)serializer.ReadValueWeak(reader);
                                    success = true;
                                }
                                else if ((assignableCast = expectedType.IsAssignableFrom(serializedType)) || serializedType.HasCastDefined(expectedType, false))
                                {
                                    try
                                    {
                                        object value;

                                        if (isPrimitive)
                                        {
                                            var serializer = Serializer.Get(serializedType);
                                            value = serializer.ReadValueWeak(reader);
                                        }
                                        else
                                        {
                                            var alternateFormatter = FormatterLocator.GetFormatter(serializedType, context.Config.SerializationPolicy);
                                            value = alternateFormatter.Deserialize(reader);
                                        }

                                        if (assignableCast)
                                        {
                                            result = (T)value;
                                        }
                                        else
                                        {
                                            var castMethod = serializedType.GetCastMethodDelegate(expectedType);

                                            if (castMethod != null)
                                            {
                                                result = (T)castMethod(value);
                                            }
                                            else
                                            {
                                                // Let's just give it a go anyways
                                                result = (T)value;
                                            }
                                        }

                                        success = true;
                                    }
                                    catch (SerializationAbortException ex)
                                    {
                                        exitNode = false;
                                        throw ex;
                                    }
                                    catch (InvalidCastException)
                                    {
                                        success = false;
                                        result  = default(T);
                                    }
                                }
                                else
                                {
                                    // We couldn't cast or use the type, but we still have to deserialize it and register
                                    // the reference so the reference isn't lost if it is referred to further down
                                    // the data stream.

                                    var    alternateFormatter = FormatterLocator.GetFormatter(serializedType, context.Config.SerializationPolicy);
                                    object value = alternateFormatter.Deserialize(reader);

                                    if (id >= 0)
                                    {
                                        context.RegisterInternalReference(id, value);
                                    }

                                    result = default(T);
                                }

                                if (!success)
                                {
                                    // We can't use this
                                    context.Config.DebugContext.LogWarning("Can't cast serialized type " + serializedType.Name + " into expected type " + expectedType.Name + ". Value lost for node '" + name + "'.");
                                    result = default(T);
                                }
                            }
                            else if (ComplexTypeIsAbstract)
                            {
                                result = default(T);
                            }
                            else
                            {
                                result = FormatterLocator.GetFormatter <T>(context.Config.SerializationPolicy).Deserialize(reader);
                            }

                            if (id >= 0)
                            {
                                context.RegisterInternalReference(id, result);
                            }

                            return(result);
                        }
                        else
                        {
                            context.Config.DebugContext.LogError("Failed to enter node '" + name + "'.");
                            return(default(T));
                        }
                    }
                    catch (SerializationAbortException ex)
                    {
                        exitNode = false;
                        throw ex;
                    }
                    catch (Exception ex)
                    {
                        context.Config.DebugContext.LogException(ex);
                        return(default(T));
                    }
                    finally
                    {
                        if (exitNode)
                        {
                            reader.ExitNode();
                        }
                    }
                }

                //
                // The below cases are for when we expect an object, but have
                // serialized a straight primitive type. In such cases, we can
                // often box the primitive type as an object.
                //
                // Sadly, the exact primitive type might be lost in case of
                // integer and floating points numbers, as we don't know what
                // type to expect.
                //
                // To be safe, we read and box the most precise type available.
                //

                case EntryType.Boolean:
                {
                    if (!ComplexTypeMayBeBoxedValueType)
                    {
                        goto default;
                    }

                    bool value;
                    reader.ReadBoolean(out value);
                    return((T)(object)value);
                }

                case EntryType.FloatingPoint:
                {
                    if (!ComplexTypeMayBeBoxedValueType)
                    {
                        goto default;
                    }

                    double value;
                    reader.ReadDouble(out value);
                    return((T)(object)value);
                }

                case EntryType.Integer:
                {
                    if (!ComplexTypeMayBeBoxedValueType)
                    {
                        goto default;
                    }

                    long value;
                    reader.ReadInt64(out value);
                    return((T)(object)value);
                }

                case EntryType.String:
                {
                    if (!ComplexTypeMayBeBoxedValueType)
                    {
                        goto default;
                    }

                    string value;
                    reader.ReadString(out value);
                    return((T)(object)value);
                }

                case EntryType.Guid:
                {
                    if (!ComplexTypeMayBeBoxedValueType)
                    {
                        goto default;
                    }

                    Guid value;
                    reader.ReadGuid(out value);
                    return((T)(object)value);
                }

                default:

                    // Lost value somehow
                    context.Config.DebugContext.LogWarning("Unexpected entry of type " + entry.ToString() + ", when a reference or node start was expected. A value has been lost.");
                    reader.SkipEntry();
                    return(default(T));
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Writes a value of type <see cref="T" />.
        /// </summary>
        /// <param name="name">The name of the value to write.</param>
        /// <param name="value">The value to write.</param>
        /// <param name="writer">The writer to use.</param>
        public override void WriteValue(string name, T value, IDataWriter writer)
        {
            var context = writer.Context;

            if (context.Config.SerializationPolicy.AllowNonSerializableTypes == false && typeof(T).IsSerializable == false)
            {
                context.Config.DebugContext.LogError("The type " + typeof(T).Name + " is not marked as serializable.");
                return;
            }

            FireOnSerializedType();

            if (typeof(T).IsValueType)
            {
                bool endNode = true;

                try
                {
                    writer.BeginStructNode(name, typeof(T));
                    FormatterLocator.GetFormatter <T>(context.Config.SerializationPolicy).Serialize(value, writer);
                }
                catch (SerializationAbortException ex)
                {
                    endNode = false;
                    throw ex;
                }
                finally
                {
                    if (endNode)
                    {
                        writer.EndNode(name);
                    }
                }
            }
            else
            {
                int    id;
                int    index;
                string strId;
                Guid   guid;

                bool endNode = true;

                if (object.ReferenceEquals(value, null))
                {
                    writer.WriteNull(name);
                }
                else if (context.TryRegisterExternalReference(value, out index))
                {
                    writer.WriteExternalReference(name, index);
                }
                else if (context.TryRegisterExternalReference(value, out guid))
                {
                    writer.WriteExternalReference(name, guid);
                }
                else if (context.TryRegisterExternalReference(value, out strId))
                {
                    writer.WriteExternalReference(name, strId);
                }
                else if (context.TryRegisterInternalReference(value, out id))
                {
                    Type type = value.GetType();                                         // Get type of actual stored object

                    if (ComplexTypeIsObject && FormatterUtilities.IsPrimitiveType(type)) // It's a boxed primitive type
                    {
                        try
                        {
                            writer.BeginReferenceNode(name, type, id);

                            var serializer = Serializer.Get(type);
                            serializer.WriteValueWeak(value, writer);
                        }
                        catch (SerializationAbortException ex)
                        {
                            endNode = false;
                            throw ex;
                        }
                        finally
                        {
                            if (endNode)
                            {
                                writer.EndNode(name);
                            }
                        }
                    }
                    else
                    {
                        var formatter = FormatterLocator.GetFormatter(type, context.Config.SerializationPolicy);

                        try
                        {
                            writer.BeginReferenceNode(name, type, id);
                            formatter.Serialize(value, writer);
                        }
                        catch (SerializationAbortException ex)
                        {
                            endNode = false;
                            throw ex;
                        }
                        finally
                        {
                            if (endNode)
                            {
                                writer.EndNode(name);
                            }
                        }
                    }
                }
                else
                {
                    writer.WriteInternalReference(name, id);
                }
            }
        }