示例#1
0
        public static void HostServer(ServerSettings settings, bool fromReplay)
        {
            Log.Message($"Starting the server");

            OnMainThread.ClearCaches();

            var session = Multiplayer.session = new MultiplayerSession();

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

            var localServer = new MultiplayerServer(settings);

            localServer.debugOnlySyncCmds = new HashSet <int>(Sync.handlers.Where(h => h.debugOnly).Select(h => h.SyncId));
            localServer.hostUsername      = Multiplayer.username;
            localServer.coopFactionId     = Faction.OfPlayer.loadID;

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

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

            MultiplayerServer.instance = localServer;
            session.localServer        = localServer;

            if (!fromReplay)
            {
                SetupGame();
            }

            Find.PlaySettings.usePlanetDayNightSystem = false;

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

            SetupLocalClient();

            Find.MainTabsRoot.EscapeCurrentTab(false);

            Multiplayer.session.AddMsg("Wiki on desyncs:");
            Multiplayer.session.AddMsg(new ChatMsg_Url("https://github.com/Zetrith/Multiplayer/wiki/Desyncs"));
            Multiplayer.session.hasUnread = false;

            LongEventHandler.QueueLongEvent(() =>
            {
                SaveLoad.CacheGameData(SaveLoad.SaveAndReload());
                SaveLoad.SendCurrentGameData(false);

                localServer.StartListening();

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

                string text = "Server started.";
                if (settings.bindAddress != null)
                {
                    text += $" Bound to {settings.bindAddress}:{localServer.NetPort}.";
                }
                if (settings.lanAddress != null)
                {
                    text += $" LAN at {settings.lanAddress}:{localServer.LanPort}.";
                }

                Messages.Message(text, MessageTypeDefOf.SilentInput, false);
                Log.Message(text);
            }, "MpSaving", false, null);
        }