Beispiel #1
0
        public void Write(ByteWriter writer)
        {
            writer.WriteInt32(SessionId);
            writer.MpContext().map = map;

            SyncSerialization.WriteSync(writer, data);
        }
Beispiel #2
0
        private static void WriteDelegate(ByteWriter writer, Delegate del)
        {
            writer.WriteBool(del != null);
            if (del == null)
            {
                return;
            }

            SyncSerialization.WriteSync(writer, del.GetType());
            SyncSerialization.WriteSync(writer, del.Method.DeclaringType);
            SyncSerialization.WriteSync(writer, del.Method.Name); // todo Handle the signature for ambiguous methods

            writer.WriteBool(del.Target != null);
            if (del.Target != null)
            {
                var targetType = del.Target.GetType();
                SyncSerialization.WriteSync(writer, targetType);

                var fieldPaths = GetFields(targetType).ToArray();
                var fieldTypes = fieldPaths.Select(path => MpReflection.PathType(path)).ToArray();

                void SyncObj(object obj, Type type, string debugInfo)
                {
                    if (type.IsCompilerGenerated())
                    {
                        return;
                    }

                    if (writer is LoggingByteWriter log1)
                    {
                        log1.Log.Enter(debugInfo);
                    }

                    try
                    {
                        if (typeof(Delegate).IsAssignableFrom(type))
                        {
                            WriteDelegate(writer, (Delegate)obj);
                        }
                        else
                        {
                            SyncSerialization.WriteSyncObject(writer, obj, type);
                        }
                    }
                    finally
                    {
                        if (writer is LoggingByteWriter log2)
                        {
                            log2.Log.Exit();
                        }
                    }
                }

                for (int i = 0; i < fieldPaths.Length; i++)
                {
                    SyncObj(del.Target.GetPropertyOrField(fieldPaths[i]), fieldTypes[i], fieldPaths[i]);
                }
            }
        }
Beispiel #3
0
        public static TempGameData SaveAndReload()
        {
            //SimpleProfiler.Start();
            Multiplayer.reloading = true;

            var worldGridSaved     = Find.WorldGrid;
            var worldRendererSaved = Find.World.renderer;
            var tweenedPos         = new Dictionary <int, Vector3>();
            var drawers            = new Dictionary <int, MapDrawer>();
            var localFactionId     = Multiplayer.RealPlayerFaction.loadID;
            var mapCmds            = new Dictionary <int, Queue <ScheduledCommand> >();
            var planetRenderMode   = Find.World.renderer.wantedMode;
            var chatWindow         = ChatWindow.Opened;

            var selectedData = new ByteWriter();

            SyncSerialization.WriteSync(selectedData, Find.Selector.selected.OfType <ISelectable>().ToList());

            //Multiplayer.RealPlayerFaction = Multiplayer.DummyFaction;

            foreach (Map map in Find.Maps)
            {
                drawers[map.uniqueID] = map.mapDrawer;
                //RebuildRegionsAndRoomsPatch.copyFrom[map.uniqueID] = map.regionGrid;

                foreach (Pawn p in map.mapPawns.AllPawnsSpawned)
                {
                    tweenedPos[p.thingIDNumber] = p.drawer.tweener.tweenedPos;
                }

                mapCmds[map.uniqueID] = map.AsyncTime().cmds;
            }

            mapCmds[ScheduledCommand.Global] = Multiplayer.WorldComp.cmds;

            DeepProfiler.Start("Multiplayer SaveAndReload: Save");
            //WriteElementPatch.cachedVals = new Dictionary<string, object>();
            //WriteElementPatch.id = 0;
            var gameData = SaveGameData();

            DeepProfiler.End();
            //Log.Message($"Saving took {WriteElementPatch.cachedVals.Count} {WriteElementPatch.cachedVals.FirstOrDefault()}");

            MapDrawerRegenPatch.copyFrom      = drawers;
            WorldGridCachePatch.copyFrom      = worldGridSaved;
            WorldGridExposeDataPatch.copyFrom = worldGridSaved;
            WorldRendererCachePatch.copyFrom  = worldRendererSaved;

            MusicManagerPlay musicManager = null;

            if (Find.MusicManagerPlay.gameObjectCreated)
            {
                var musicTransform = Find.MusicManagerPlay.audioSource.transform;
                if (musicTransform.parent == Find.Root.soundRoot.sourcePool.sourcePoolCamera.cameraSourcesContainer.transform)
                {
                    musicTransform.parent = musicTransform.parent.parent;
                }

                musicManager = Find.MusicManagerPlay;
            }

            //SpawnSetupPatch.total = 0;
            //SpawnSetupPatch.total2 = new long[SpawnSetupPatch.total2.Length];

            LoadInMainThread(gameData);

            if (musicManager != null)
            {
                Current.Root_Play.musicManagerPlay = musicManager;
            }

            Multiplayer.RealPlayerFaction = Find.FactionManager.GetById(localFactionId);

            foreach (Map m in Find.Maps)
            {
                foreach (Pawn p in m.mapPawns.AllPawnsSpawned)
                {
                    if (tweenedPos.TryGetValue(p.thingIDNumber, out Vector3 v))
                    {
                        p.drawer.tweener.tweenedPos    = v;
                        p.drawer.tweener.lastDrawFrame = Time.frameCount;
                    }
                }

                m.AsyncTime().cmds = mapCmds[m.uniqueID];
            }

            if (chatWindow != null)
            {
                Find.WindowStack.Add_KeepRect(chatWindow);
            }

            var selectedReader = new ByteReader(selectedData.ToArray())
            {
                context = new MpContext()
                {
                    map = Find.CurrentMap
                }
            };

            Find.Selector.selected = SyncSerialization.ReadSync <List <ISelectable> >(selectedReader).AllNotNull().Cast <object>().ToList();

            Find.World.renderer.wantedMode = planetRenderMode;
            Multiplayer.WorldComp.cmds     = mapCmds[ScheduledCommand.Global];

            Multiplayer.reloading = false;
            //SimpleProfiler.Pause();

            //Log.Message($"allocs {(double)SpawnSetupPatch.total2.Sum() / Stopwatch.Frequency * 1000} ({SpawnSetupPatch.total2.Select((l,i) => $"{SpawnSetupPatch.methods[i]}: {(double)l / Stopwatch.Frequency * 1000}").Join(delimiter: "\n")}) {SpawnSetupPatch.total} {AllocsPrefixClass.allocs} {CustomXmlElement.n} {CustomXmlElement.m} {CustomXmlElement.n - CustomXmlElement.m} {(double)CustomXmlElement.n/CustomXmlElement.m}");

            return(gameData);
        }