GetGlobalResolver() public method

public GetGlobalResolver ( Type type ) : ISaveGameRefResolver
type System.Type
return ISaveGameRefResolver
Esempio n. 1
0
        object DeserializeGameObject(TypeInfo typeInfo, int id)
        {
            var type = typeInfo.Type;

            object ob;
            bool   created;

            if (m_reader.TokenType == JsonToken.PropertyName && (string)m_reader.Value == "$sid")
            {
                Read();

                Trace.Assert(m_reader.TokenType == JsonToken.Integer);

                var sid = (int)(long)m_reader.Value;

                Read();

                var resolver = m_globalResolvers.GetGlobalResolver(type);

                ob = resolver.FromRef(sid);

                created = false;
            }
            else
            {
                ob = FormatterServices.GetUninitializedObject(type);

                created = true;
            }

            var deserializingMethods = typeInfo.OnDeserializingMethods;

            foreach (var method in deserializingMethods)
            {
                method.Invoke(ob, null);
            }

            if (id != -1)
            {
                m_referenceResolver.Add(id, ob);
            }

            var entries = typeInfo.GameMemberEntries;

            while (true)
            {
                if (m_reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                Trace.Assert(m_reader.TokenType == JsonToken.PropertyName);

                string propName = (string)m_reader.Value;

                Read();

                var idx = Array.FindIndex(entries, fi => fi.Name == propName);

                if (idx == -1 && typeInfo.HasDelegate && propName == "$del")
                {
                    var data = DeserializeObject(typeof(object));
                    ((ISaveGameDelegate)ob).RestoreSaveData(data);
                }
                else if (idx == -1)
                {
                    throw new Exception();
                }
                else
                {
                    var entry = entries[idx];

                    object value;

                    if (entry.ReaderWriter != null)
                    {
                        value = entry.ReaderWriter.Read(m_reader);
                    }
                    else
                    {
                        var memberType = entry.MemberType;

                        value = DeserializeObject(memberType);

                        if (entry.Converter != null)
                        {
                            value = entry.Converter.ConvertFromSerializable(value);
                        }
                    }

                    entry.SetValue(ob, value);
                }

                Read();
            }

            if (created)
            {
                typeInfo.DeserializeConstructor.Invoke(ob, new object[] { new SaveGameContext() });
            }

            var deserializedMethods = typeInfo.OnDeserializedMethods;

            foreach (var method in deserializedMethods)
            {
                method.Invoke(ob, null);
            }

            var postDeserMethods = typeInfo.OnGamePostDeserializationMethods;

            foreach (var method in postDeserMethods)
            {
                var dele = (PostDeserializationDelegate)Delegate.CreateDelegate(typeof(PostDeserializationDelegate), ob, method);
                m_postDeserMethods.Add(dele);
            }

            return(ob);
        }
Esempio n. 2
0
        void WriteGameObjectData(object ob, TypeInfo typeInfo)
        {
            var type = typeInfo.Type;

            if (m_globalResolvers != null)
            {
                var globalResolver = m_globalResolvers.GetGlobalResolver(type);
                if (globalResolver != null)
                {
                    var id = globalResolver.ToRefID(ob);
                    m_writer.WritePropertyName("$sid");
                    m_writer.WriteValue(id);
                }
            }

            var serializingMethods = typeInfo.OnSerializingMethods;

            foreach (var method in serializingMethods)
            {
                method.Invoke(ob, null);
            }

            var entries = typeInfo.GameMemberEntries;

            for (int i = 0; i < entries.Length; ++i)
            {
                var entry = entries[i];
                var value = entry.GetValue(ob);

                m_writer.WritePropertyName(entry.Name);

                if (entry.ReaderWriter != null)
                {
                    entry.ReaderWriter.Write(m_writer, value);
                }
                else
                {
                    var memberType = entry.MemberType;

                    if (entry.Converter != null)
                    {
                        value = entry.Converter.ConvertToSerializable(value);
                    }

                    SerializeObject(value, memberType);
                }
            }

            if (typeInfo.HasDelegate)
            {
                var data = ((ISaveGameDelegate)ob).GetSaveData();

                if (data != null)
                {
                    m_writer.WritePropertyName("$del");
                    SerializeObject(data, typeof(object));
                }
            }

            var serializedMethods = typeInfo.OnSerializedMethods;

            foreach (var method in serializedMethods)
            {
                method.Invoke(ob, null);
            }
        }