Пример #1
0
        static void TickAutosave()
        {
            if (Multiplayer.LocalServer is not {
            } server)
            {
                return;
            }

            if (server.settings.autosaveUnit == AutosaveUnit.Minutes)
            {
                var session = Multiplayer.session;
                session.autosaveCounter++;

                if (server.settings.autosaveInterval > 0 &&
                    session.autosaveCounter > server.settings.autosaveInterval * 60 * 60)
                {
                    session.autosaveCounter = 0;
                    MultiplayerSession.DoAutosave();
                }
            }
            else if (server.settings.autosaveUnit == AutosaveUnit.Days && server.settings.autosaveInterval > 0)
            {
                var anyMapCounterUp =
                    Multiplayer.game.mapComps
                    .Any(m => m.autosaveCounter > server.settings.autosaveInterval * 2500 * 24);

                if (anyMapCounterUp)
                {
                    Multiplayer.game.mapComps.Do(m => m.autosaveCounter = 0);
                    MultiplayerSession.DoAutosave();
                }
            }
        }
Пример #2
0
        public override bool Accept()
        {
            if (curName.Length == 0)
            {
                return(false);
            }

            try
            {
                LongEventHandler.QueueLongEvent(() => MultiplayerSession.SaveGameToFile(curName), "MpSaving", false, null);
                Close();
            }
            catch (Exception e)
            {
                Log.Error($"Exception saving replay {e}");
            }

            return(true);
        }
Пример #3
0
        public static void HostServer(ServerSettings settings, bool fromReplay, bool hadSimulation, bool asyncTime)
        {
            Log.Message($"Starting the server");

            var session = new MultiplayerSession();

            if (Multiplayer.session != null) // This is the case when hosting from a replay
            {
                session.dataSnapshot = Multiplayer.session.dataSnapshot;
            }

            Multiplayer.session = session;

            session.myFactionId         = Faction.OfPlayer.loadID;
            session.localServerSettings = settings;
            session.gameName            = settings.gameName;

            // Server already pre-inited in HostWindow
            var localServer = Multiplayer.LocalServer;

            MultiplayerServer.instance = Multiplayer.LocalServer;

            if (hadSimulation)
            {
                localServer.savedGame      = GZipStream.CompressBuffer(session.dataSnapshot.gameData);
                localServer.semiPersistent = GZipStream.CompressBuffer(session.dataSnapshot.semiPersistentData);
                localServer.mapData        = session.dataSnapshot.mapData.ToDictionary(kv => kv.Key, kv => GZipStream.CompressBuffer(kv.Value));
                localServer.mapCmds        = session.dataSnapshot.mapCmds.ToDictionary(kv => kv.Key, kv => kv.Value.Select(ScheduledCommand.Serialize).ToList());
            }

            localServer.commands.debugOnlySyncCmds = Sync.handlers.Where(h => h.debugOnly).Select(h => h.syncId).ToHashSet();
            localServer.commands.hostOnlySyncCmds  = Sync.handlers.Where(h => h.hostOnly).Select(h => h.syncId).ToHashSet();
            localServer.hostUsername  = Multiplayer.username;
            localServer.coopFactionId = Faction.OfPlayer.loadID;

            localServer.rwVersion  = VersionControl.CurrentVersionString;
            localServer.mpVersion  = MpVersion.Version;
            localServer.defInfos   = MultiplayerData.localDefInfos;
            localServer.serverData = JoinData.WriteServerData(settings.syncConfigs);

            if (settings.steam)
            {
                localServer.TickEvent += SteamIntegration.ServerSteamNetTick;
            }

            if (fromReplay)
            {
                localServer.gameTimer = TickPatch.Timer;
            }

            if (!fromReplay)
            {
                SetupGameFromSingleplayer();
            }

            foreach (var tickable in TickPatch.AllTickables)
            {
                tickable.Cmds.Clear();
            }

            Find.PlaySettings.usePlanetDayNightSystem = false;

            Multiplayer.RealPlayerFaction = Faction.OfPlayer;
            localServer.playerFactions[Multiplayer.username] = Faction.OfPlayer.loadID;

            SetupLocalClient();

            Find.MainTabsRoot.EscapeCurrentTab(false);

            Multiplayer.session.AddMsg("If you are having any issues with the mod and would like some help resolving them, then please reach out to us on our Discord server:", false);
            Multiplayer.session.AddMsg(new ChatMsg_Url("https://discord.gg/S4bxXpv"), false);

            if (hadSimulation)
            {
                StartServerThread();
            }
            else
            {
                Multiplayer.WorldComp.TimeSpeed = TimeSpeed.Paused;
                foreach (var map in Find.Maps)
                {
                    map.AsyncTime().TimeSpeed = TimeSpeed.Paused;
                }

                Multiplayer.WorldComp.UpdateTimeSpeed();

                Multiplayer.GameComp.asyncTime       = asyncTime;
                Multiplayer.GameComp.debugMode       = settings.debugMode;
                Multiplayer.GameComp.logDesyncTraces = settings.desyncTraces;

                LongEventHandler.QueueLongEvent(() =>
                {
                    Multiplayer.session.dataSnapshot = SaveLoad.CreateGameDataSnapshot(SaveLoad.SaveAndReload());
                    SaveLoad.SendGameData(Multiplayer.session.dataSnapshot, false);

                    StartServerThread();
                }, "MpSaving", false, null);
            }

            void StartServerThread()
            {
                localServer.running = true;

                Multiplayer.LocalServer.serverThread = new Thread(localServer.Run)
                {
                    Name = "Local server thread"
                };
                Multiplayer.LocalServer.serverThread.Start();

                const string text = "Server started.";

                Messages.Message(text, MessageTypeDefOf.SilentInput, false);
                Log.Message(text);
            }
        }
Пример #4
0
 public void HandleCanRejoin(ByteReader data)
 {
     MultiplayerSession.DoRejoin();
 }