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); } }
public static void HostServer(ServerSettings settings, bool fromReplay, bool withSimulation = false, bool debugMode = false, bool logDesyncTraces = false) { Log.Message($"Starting the server"); var session = Multiplayer.session = new MultiplayerSession(); session.myFactionId = Faction.OfPlayer.loadID; session.localSettings = settings; session.gameName = settings.gameName; var localServer = new MultiplayerServer(settings); if (withSimulation) { localServer.savedGame = GZipStream.CompressBuffer(OnMainThread.cachedGameData); localServer.mapData = OnMainThread.cachedMapData.ToDictionary(kv => kv.Key, kv => GZipStream.CompressBuffer(kv.Value)); localServer.mapCmds = OnMainThread.cachedMapCmds.ToDictionary(kv => kv.Key, kv => kv.Value.Select(c => c.Serialize()).ToList()); } else { OnMainThread.ClearCaches(); } localServer.debugMode = debugMode; localServer.debugOnlySyncCmds = new HashSet <int>(Sync.handlers.Where(h => h.debugOnly).Select(h => h.syncId)); localServer.hostOnlySyncCmds = new HashSet <int>(Sync.handlers.Where(h => h.hostOnly).Select(h => h.syncId)); localServer.hostUsername = Multiplayer.username; localServer.coopFactionId = Faction.OfPlayer.loadID; localServer.rwVersion = session.mods.remoteRwVersion = VersionControl.CurrentVersionString; localServer.modNames = session.mods.remoteModNames = LoadedModManager.RunningModsListForReading.Select(m => m.Name).ToArray(); localServer.modIds = session.mods.remoteModIds = LoadedModManager.RunningModsListForReading.Select(m => m.PackageId).ToArray(); localServer.workshopModIds = session.mods.remoteWorkshopModIds = ModManagement.GetEnabledWorkshopMods().ToArray(); localServer.defInfos = session.mods.defInfo = Multiplayer.localDefInfos; Log.Message($"MP Host modIds: {string.Join(", ", localServer.modIds)}"); Log.Message($"MP Host workshopIds: {string.Join(", ", localServer.workshopModIds)}"); if (settings.steam) { localServer.NetTick += SteamIntegration.ServerSteamNetTick; } if (fromReplay) { localServer.gameTimer = TickPatch.Timer; } MultiplayerServer.instance = localServer; session.localServer = localServer; if (!fromReplay) { SetupGame(); } 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 a issue with the mod and would like some help resolving it, then please reach out to us on our discord server:", false); Multiplayer.session.AddMsg(new ChatMsg_Url("https://discord.gg/S4bxXpv"), false); if (withSimulation) { StartServerThread(); } else { var timeSpeed = TimeSpeed.Paused; Multiplayer.WorldComp.TimeSpeed = timeSpeed; foreach (var map in Find.Maps) { map.AsyncTime().TimeSpeed = timeSpeed; } Multiplayer.WorldComp.UpdateTimeSpeed(); Multiplayer.WorldComp.debugMode = debugMode; Multiplayer.WorldComp.logDesyncTraces = logDesyncTraces; LongEventHandler.QueueLongEvent(() => { SaveLoad.CacheGameData(SaveLoad.SaveAndReload()); SaveLoad.SendCurrentGameData(false); StartServerThread(); }, "MpSaving", false, null); } void StartServerThread() { var netStarted = localServer.StartListeningNet(); var lanStarted = localServer.StartListeningLan(); string text = "Server started."; if (netStarted != null) { text += (netStarted.Value ? $" Direct at {settings.bindAddress}:{localServer.NetPort}." : " Couldn't bind direct."); } if (lanStarted != null) { text += (lanStarted.Value ? $" LAN at {settings.lanAddress}:{localServer.LanPort}." : " Couldn't bind LAN."); } session.serverThread = new Thread(localServer.Run) { Name = "Local server thread" }; session.serverThread.Start(); Messages.Message(text, MessageTypeDefOf.SilentInput, false); Log.Message(text); } }
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); }