Esempio n. 1
0
        public static void WriteSyncObject(ByteWriter data, object obj, SyncType syncType)
        {
            MpContext context = data.MpContext();
            Type      type    = syncType.type;

            LoggingByteWriter log = data as LoggingByteWriter;

            log?.LogEnter(type.FullName + ": " + (obj ?? "null"));

            if (obj != null && !type.IsAssignableFrom(obj.GetType()))
            {
                throw new SerializationException($"Serializing with type {type} but got object of type {obj.GetType()}");
            }

            try
            {
                if (typeof(object) == type)
                {
                    return;
                }

                if (type.IsByRef)
                {
                    return;
                }

                if (syncWorkersEarly.TryGetValue(type, out var syncWorkerEntryEarly))
                {
                    syncWorkerEntryEarly.Invoke(new WritingSyncWorker(data), ref obj);

                    return;
                }

                if (syncType.expose)
                {
                    if (!typeof(IExposable).IsAssignableFrom(type))
                    {
                        throw new SerializationException($"Type {type} can't be exposed because it isn't IExposable");
                    }

                    IExposable exposable = obj as IExposable;
                    data.WritePrefixedBytes(ScribeUtil.WriteExposable(exposable));

                    return;
                }

                if (typeof(ISynchronizable).IsAssignableFrom(type))
                {
                    ((ISynchronizable)obj).Sync(new WritingSyncWorker(data));

                    return;
                }

                if (type.IsEnum)
                {
                    Type enumType = Enum.GetUnderlyingType(type);

                    WriteSyncObject(data, Convert.ChangeType(obj, enumType), enumType);

                    return;
                }

                if (type.IsArray && type.GetArrayRank() == 1)
                {
                    Type  elementType = type.GetElementType();
                    Array arr         = (Array)obj;

                    if (arr.Length > ushort.MaxValue)
                    {
                        throw new Exception($"Tried to serialize a {elementType}[] with too many ({arr.Length}) items.");
                    }

                    data.WriteUShort((ushort)arr.Length);
                    foreach (object e in arr)
                    {
                        WriteSyncObject(data, e, elementType);
                    }

                    return;
                }

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))
                {
                    ListType specialList = ListType.Normal;
                    Type     listType    = type.GetGenericArguments()[0];

                    if (listType == typeof(Thing) && obj == Find.CurrentMap.listerThings.AllThings)
                    {
                        context.map = Find.CurrentMap;
                        specialList = ListType.MapAllThings;
                    }
                    else if (listType == typeof(Designation) && obj == Find.CurrentMap.designationManager.allDesignations)
                    {
                        context.map = Find.CurrentMap;
                        specialList = ListType.MapAllDesignations;
                    }

                    WriteSync(data, specialList);

                    if (specialList == ListType.Normal)
                    {
                        IList list = (IList)obj;

                        if (list.Count > ushort.MaxValue)
                        {
                            throw new Exception($"Tried to serialize a {type} with too many ({list.Count}) items.");
                        }

                        data.WriteUShort((ushort)list.Count);
                        foreach (object e in list)
                        {
                            WriteSyncObject(data, e, listType);
                        }
                    }

                    return;
                }

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    bool isNull = obj == null;
                    data.WriteBool(isNull);
                    if (isNull)
                    {
                        return;
                    }

                    bool hasValue = (bool)obj.GetPropertyOrField("HasValue");
                    data.WriteBool(hasValue);

                    Type nullableType = type.GetGenericArguments()[0];
                    if (hasValue)
                    {
                        WriteSyncObject(data, obj.GetPropertyOrField("Value"), nullableType);
                    }

                    return;
                }

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    IEnumerable e           = (IEnumerable)obj;
                    Type        elementType = type.GetGenericArguments()[0];
                    var         listType    = typeof(List <>).MakeGenericType(elementType);
                    IList       list        = (IList)Activator.CreateInstance(listType);

                    foreach (var o in e)
                    {
                        if (list.Count > ushort.MaxValue)
                        {
                            throw new Exception($"Tried to serialize a {type} with too many ({list.Count}) items.");
                        }
                        list.Add(o);
                    }

                    WriteSyncObject(data, list, listType);

                    return;
                }

                // special case
                if (typeof(Def).IsAssignableFrom(type))
                {
                    Def def = obj as Def;
                    data.WriteUShort(def != null ? def.shortHash : (ushort)0);

                    return;
                }

                // Where the magic happens
                if (syncWorkers.TryGetValue(type, out var syncWorkerEntry))
                {
                    syncWorkerEntry.Invoke(new WritingSyncWorker(data), ref obj);

                    return;
                }

                log?.LogNode("No writer for " + type);
                throw new SerializationException("No writer for type " + type);
            }
            catch (Exception e)
            {
                MpLog.Error($"Error writing type: {type}, obj: {obj}, {e}");
                throw;
            }
            finally
            {
                log?.LogExit();
            }
        }