public GameServer(MiniYaml yaml) { FieldLoader.Load(this, yaml); Manifest mod; ExternalMod external; var modVersion = Mods.Split('@'); ModLabel = "Unknown mod: {0}".F(Mods); if (modVersion.Length == 2) { ModId = modVersion[0]; ModVersion = modVersion[1]; var externalKey = ExternalMod.MakeKey(modVersion[0], modVersion[1]); if (Game.ExternalMods.TryGetValue(externalKey, out external) && external.Version == modVersion[1]) { ModLabel = "{0} ({1})".F(external.Title, external.Version); IsCompatible = true; } else if (Game.Mods.TryGetValue(modVersion[0], out mod)) { // Use internal mod data to populate the section header, but // on-connect switching must use the external mod plumbing. ModLabel = "{0} ({1})".F(mod.Metadata.Title, modVersion[1]); } } var mapAvailable = Game.Settings.Game.AllowDownloading || Game.ModData.MapCache[Map].Status == MapStatus.Available; IsJoinable = IsCompatible && State == 1 && mapAvailable; }
public SettingsLogic(Widget widget, Action onExit, ModData modData, WorldRenderer worldRenderer, Dictionary <string, MiniYaml> logicArgs) { this.worldRenderer = worldRenderer; this.modData = modData; this.logicArgs = logicArgs; panelContainer = widget.Get("SETTINGS_PANEL"); tabContainer = widget.Get("TAB_CONTAINER"); RegisterSettingsPanel(PanelType.Display, InitDisplayPanel, ResetDisplayPanel, "DISPLAY_PANEL", "DISPLAY_TAB"); RegisterSettingsPanel(PanelType.Audio, InitAudioPanel, ResetAudioPanel, "AUDIO_PANEL", "AUDIO_TAB"); RegisterSettingsPanel(PanelType.Input, InitInputPanel, ResetInputPanel, "INPUT_PANEL", "INPUT_TAB"); RegisterSettingsPanel(PanelType.Hotkeys, InitHotkeysPanel, ResetHotkeysPanel, "HOTKEYS_PANEL", "HOTKEYS_TAB"); RegisterSettingsPanel(PanelType.Advanced, InitAdvancedPanel, ResetAdvancedPanel, "ADVANCED_PANEL", "ADVANCED_TAB"); panelContainer.Get <ButtonWidget>("BACK_BUTTON").OnClick = () => { leavePanelActions[settingsPanel](); var current = Game.Settings; current.Save(); Action closeAndExit = () => { Ui.CloseWindow(); onExit(); }; if (current.Sound.Device != OriginalSoundDevice || current.Graphics.Mode != OriginalGraphicsMode || current.Graphics.WindowedSize != OriginalGraphicsWindowedSize || current.Graphics.FullscreenSize != OriginalGraphicsFullscreenSize || current.Server.DiscoverNatDevices != OriginalServerDiscoverNatDevices) { Action restart = () => { var external = Game.ExternalMods[ExternalMod.MakeKey(Game.ModData.Manifest)]; Game.SwitchToExternalMod(external, null, closeAndExit); }; ConfirmationDialogs.ButtonPrompt( title: "Restart Now?", text: "Some changes will not be applied until\nthe game is restarted. Restart now?", onConfirm: restart, onCancel: closeAndExit, confirmText: "Restart Now", cancelText: "Restart Later"); } else { closeAndExit(); } }; panelContainer.Get <ButtonWidget>("RESET_BUTTON").OnClick = () => { resetPanelActions[settingsPanel](); Game.Settings.Save(); }; }
void IUtilityCommand.Run(Utility utility, string[] args) { ModRegistration type = 0; if (args[1] == "system" || args[1] == "both") { type |= ModRegistration.System; } if (args[1] == "user" || args[1] == "both") { type |= ModRegistration.User; } var mods = new ExternalMods(); ExternalMod activeMod = null; mods.TryGetValue(ExternalMod.MakeKey(utility.ModData.Manifest), out activeMod); mods.ClearInvalidRegistrations(activeMod, type); }
public GameServer(MiniYaml yaml) { FieldLoader.Load(this, yaml); // Games advertised using the old API used a single Mods field if (Mod == null || Version == null) { var modsNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Mods"); if (modsNode != null) { var modVersion = modsNode.Value.Value.Split('@'); Mod = modVersion[0]; Version = modVersion[1]; } } // Games advertised using the old API calculated the play time locally if (State == 2 && PlayTime < 0) { if (DateTime.TryParse(Started, out var startTime)) { PlayTime = (int)(DateTime.UtcNow - startTime).TotalSeconds; } } var externalKey = ExternalMod.MakeKey(Mod, Version); if (Game.ExternalMods.TryGetValue(externalKey, out var external) && external.Version == Version) { IsCompatible = true; } // Games advertised using the old API used local mod metadata if (string.IsNullOrEmpty(ModTitle)) { if (external != null && external.Version == Version) { // Use external mod registration to populate the section header ModTitle = external.Title; } else if (Game.Mods.TryGetValue(Mod, out var mod)) { // Use internal mod data to populate the section header, but // on-connect switching must use the external mod plumbing. ModTitle = mod.Metadata.Title; } else { // Some platforms (e.g. macOS) package each mod separately, so the Mods check above won't work. // Guess based on the most recent ExternalMod instead. var guessMod = Game.ExternalMods.Values .OrderByDescending(m => m.Version) .FirstOrDefault(m => m.Id == Mod); if (guessMod != null) { ModTitle = "{0}".F(guessMod.Title); } else { ModTitle = "Unknown mod: {0}".F(Mod); } } } var mapAvailable = Game.Settings.Game.AllowDownloading || Game.ModData.MapCache[Map].Status == MapStatus.Available; IsJoinable = IsCompatible && State == 1 && mapAvailable; }
internal static void ProcessOrder(OrderManager orderManager, World world, int clientId, Order order) { switch (order.OrderString) { // Server message case "Message": Game.AddSystemLine(order.TargetString); break; // Reports that the target player disconnected case "Disconnected": { var client = orderManager.LobbyInfo.ClientWithIndex(clientId); if (client != null) { client.State = Session.ClientState.Disconnected; var player = world?.FindPlayerByClient(client); if (player != null) { world.OnPlayerDisconnected(player); } } break; } case "Chat": { var client = orderManager.LobbyInfo.ClientWithIndex(clientId); if (client == null) { break; } // Cut chat messages to the hard limit to avoid exploits var message = order.TargetString; if (message.Length > ChatMessageMaxLength) { message = order.TargetString.Substring(0, ChatMessageMaxLength); } // ExtraData 0 means this is a normal chat order, everything else is team chat if (order.ExtraData == 0) { var p = world != null?world.FindPlayerByClient(client) : null; var suffix = (p != null && p.WinState == WinState.Lost) ? " (Dead)" : ""; suffix = client.IsObserver ? " (Spectator)" : suffix; if (orderManager.LocalClient != null && client != orderManager.LocalClient && client.Team > 0 && client.Team == orderManager.LocalClient.Team) { suffix += " (Ally)"; } Game.AddChatLine(client.Name + suffix, client.Color, message); break; } // We are still in the lobby if (world == null) { var prefix = order.ExtraData == uint.MaxValue ? "[Spectators] " : "[Team] "; if (orderManager.LocalClient != null && client.Team == orderManager.LocalClient.Team) { Game.AddChatLine(prefix + client.Name, client.Color, message); } break; } var player = world.FindPlayerByClient(client); var localClientIsObserver = world.IsReplay || (orderManager.LocalClient != null && orderManager.LocalClient.IsObserver) || (world.LocalPlayer != null && world.LocalPlayer.WinState != WinState.Undefined); // ExtraData gives us the team number, uint.MaxValue means Spectators if (order.ExtraData == uint.MaxValue && localClientIsObserver) { // Validate before adding the line if (client.IsObserver || (player != null && player.WinState != WinState.Undefined)) { Game.AddChatLine("[Spectators] " + client.Name, client.Color, message); } break; } var valid = client.Team == order.ExtraData && player != null && player.WinState == WinState.Undefined; var isSameTeam = orderManager.LocalClient != null && order.ExtraData == orderManager.LocalClient.Team && world.LocalPlayer != null && world.LocalPlayer.WinState == WinState.Undefined; if (valid && (isSameTeam || world.IsReplay)) { Game.AddChatLine("[Team" + (world.IsReplay ? " " + order.ExtraData : "") + "] " + client.Name, client.Color, message); } break; } case "StartGame": { if (Game.ModData.MapCache[orderManager.LobbyInfo.GlobalSettings.Map].Status != MapStatus.Available) { Game.Disconnect(); Game.LoadShellMap(); // TODO: After adding a startup error dialog, notify the replay load failure. break; } if (!string.IsNullOrEmpty(order.TargetString)) { var data = MiniYaml.FromString(order.TargetString); var saveLastOrdersFrame = data.FirstOrDefault(n => n.Key == "SaveLastOrdersFrame"); if (saveLastOrdersFrame != null) { orderManager.GameSaveLastFrame = FieldLoader.GetValue <int>("saveLastOrdersFrame", saveLastOrdersFrame.Value.Value); } var saveSyncFrame = data.FirstOrDefault(n => n.Key == "SaveSyncFrame"); if (saveSyncFrame != null) { orderManager.GameSaveLastSyncFrame = FieldLoader.GetValue <int>("SaveSyncFrame", saveSyncFrame.Value.Value); } } else { Game.AddSystemLine("The game has started."); } Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map, WorldType.Regular); break; } case "SaveTraitData": { var data = MiniYaml.FromString(order.TargetString)[0]; var traitIndex = int.Parse(data.Key); world?.AddGameSaveTraitData(traitIndex, data.Value); break; } case "GameSaved": if (!orderManager.World.IsReplay) { Game.AddSystemLine("Game saved"); } foreach (var nsr in orderManager.World.WorldActor.TraitsImplementing <INotifyGameSaved>()) { nsr.GameSaved(orderManager.World); } break; case "PauseGame": { var client = orderManager.LobbyInfo.ClientWithIndex(clientId); if (client != null) { var pause = order.TargetString == "Pause"; // Prevent injected unpause orders from restarting a finished game if (orderManager.World.PauseStateLocked && !pause) { break; } if (orderManager.World.Paused != pause && world != null && world.LobbyInfo.NonBotClients.Count() > 1) { var pausetext = "The game is {0} by {1}".F(pause ? "paused" : "un-paused", client.Name); Game.AddSystemLine(pausetext); } orderManager.World.Paused = pause; orderManager.World.PredictedPaused = pause; } break; } case "HandshakeRequest": { // Switch to the server's mod if we need and are able to var mod = Game.ModData.Manifest; var request = HandshakeRequest.Deserialize(order.TargetString); var externalKey = ExternalMod.MakeKey(request.Mod, request.Version); if ((request.Mod != mod.Id || request.Version != mod.Metadata.Version) && Game.ExternalMods.TryGetValue(externalKey, out var external)) { // The ConnectionFailedLogic will prompt the user to switch mods orderManager.ServerExternalMod = external; orderManager.Connection.Dispose(); break; } Game.Settings.Player.Name = Settings.SanitizedPlayerName(Game.Settings.Player.Name); Game.Settings.Save(); // Otherwise send the handshake with our current settings and let the server reject us var info = new Session.Client() { Name = Game.Settings.Player.Name, PreferredColor = Game.Settings.Player.Color, Color = Game.Settings.Player.Color, Faction = "Random", SpawnPoint = 0, Team = 0, State = Session.ClientState.Invalid }; var localProfile = Game.LocalPlayerProfile; var response = new HandshakeResponse() { Client = info, Mod = mod.Id, Version = mod.Metadata.Version, Password = orderManager.Password, Fingerprint = localProfile.Fingerprint, OrdersProtocol = ProtocolVersion.Orders }; if (request.AuthToken != null && response.Fingerprint != null) { response.AuthSignature = localProfile.Sign(request.AuthToken); } orderManager.IssueOrder(new Order("HandshakeResponse", null, false) { Type = OrderType.Handshake, IsImmediate = true, TargetString = response.Serialize() }); break; } case "ServerError": { orderManager.ServerError = order.TargetString; orderManager.AuthenticationFailed = false; break; } case "AuthenticationError": { // The ConnectionFailedLogic will prompt the user for the password orderManager.ServerError = order.TargetString; orderManager.AuthenticationFailed = true; break; } case "SyncInfo": { orderManager.LobbyInfo = Session.Deserialize(order.TargetString); Game.SyncLobbyInfo(); break; } case "SyncLobbyClients": { var clients = new List <Session.Client>(); var nodes = MiniYaml.FromString(order.TargetString); foreach (var node in nodes) { var strings = node.Key.Split('@'); if (strings[0] == "Client") { clients.Add(Session.Client.Deserialize(node.Value)); } } orderManager.LobbyInfo.Clients = clients; Game.SyncLobbyInfo(); break; } case "SyncLobbySlots": { var slots = new Dictionary <string, Session.Slot>(); var nodes = MiniYaml.FromString(order.TargetString); foreach (var node in nodes) { var strings = node.Key.Split('@'); if (strings[0] == "Slot") { var slot = Session.Slot.Deserialize(node.Value); slots.Add(slot.PlayerReference, slot); } } orderManager.LobbyInfo.Slots = slots; Game.SyncLobbyInfo(); break; } case "SyncLobbyGlobalSettings": { var nodes = MiniYaml.FromString(order.TargetString); foreach (var node in nodes) { var strings = node.Key.Split('@'); if (strings[0] == "GlobalSettings") { orderManager.LobbyInfo.GlobalSettings = Session.Global.Deserialize(node.Value); } } Game.SyncLobbyInfo(); break; } case "SyncClientPings": { var pings = new List <Session.ClientPing>(); var nodes = MiniYaml.FromString(order.TargetString); foreach (var node in nodes) { var strings = node.Key.Split('@'); if (strings[0] == "ClientPing") { pings.Add(Session.ClientPing.Deserialize(node.Value)); } } orderManager.LobbyInfo.ClientPings = pings; break; } case "Ping": { orderManager.IssueOrder(Order.FromTargetString("Pong", order.TargetString, true)); break; } default: { if (world == null) { break; } if (order.GroupedActors == null) { ResolveOrder(order, world, orderManager, clientId); } else { foreach (var subject in order.GroupedActors) { ResolveOrder(Order.FromGroupedOrder(order, subject), world, orderManager, clientId); } } break; } } }
public SettingsLogic(Widget widget, Action onExit, WorldRenderer worldRenderer, Dictionary <string, MiniYaml> logicArgs) { panelContainer = widget.Get("PANEL_CONTAINER"); var panelTemplate = panelContainer.Get <ContainerWidget>("PANEL_TEMPLATE"); panelContainer.RemoveChild(panelTemplate); tabContainer = widget.Get("SETTINGS_TAB_CONTAINER"); tabTemplate = tabContainer.Get <ButtonWidget>("BUTTON_TEMPLATE"); tabContainer.RemoveChild(tabTemplate); if (logicArgs.TryGetValue("ButtonStride", out var buttonStrideNode)) { buttonStride = FieldLoader.GetValue <int2>("ButtonStride", buttonStrideNode.Value); } if (logicArgs.TryGetValue("Panels", out var settingsPanels)) { panels = settingsPanels.ToDictionary(kv => kv.Value); foreach (var panel in panels) { var container = panelTemplate.Clone() as ContainerWidget; container.Id = panel.Key; panelContainer.AddChild(container); Game.LoadWidget(worldRenderer.World, panel.Key, container, new WidgetArgs() { { "registerPanel", (Action <string, string, Func <Widget, Func <bool> >, Func <Widget, Action> >)RegisterSettingsPanel }, { "panelID", panel.Key }, { "label", panel.Value } }); } } widget.Get <ButtonWidget>("BACK_BUTTON").OnClick = () => { needsRestart |= leavePanelActions[activePanel](); var current = Game.Settings; current.Save(); Action closeAndExit = () => { Ui.CloseWindow(); onExit(); }; if (needsRestart) { Action noRestart = () => ConfirmationDialogs.ButtonPrompt( title: "Restart Required", text: "Some changes will not be applied until\nthe game is restarted.", onCancel: closeAndExit, cancelText: "Continue"); if (!Game.ExternalMods.TryGetValue(ExternalMod.MakeKey(Game.ModData.Manifest), out var external)) { noRestart(); return; } ConfirmationDialogs.ButtonPrompt( title: "Restart Now?", text: "Some changes will not be applied until\nthe game is restarted. Restart now?", onConfirm: () => Game.SwitchToExternalMod(external, null, noRestart), onCancel: closeAndExit, confirmText: "Restart Now", cancelText: "Restart Later"); } else { closeAndExit(); } }; widget.Get <ButtonWidget>("RESET_BUTTON").OnClick = () => { Action reset = () => { resetPanelActions[activePanel](); Game.Settings.Save(); }; ConfirmationDialogs.ButtonPrompt( title: $"Reset \"{panels[activePanel]}\"", text: "Are you sure you want to reset\nall settings in this panel?", onConfirm: reset, onCancel: () => { }, confirmText: "Reset", cancelText: "Cancel"); }; }
internal static void ProcessOrder(OrderManager orderManager, World world, int clientId, Order order) { if (world != null) { if (!world.WorldActor.TraitsImplementing <IValidateOrder>().All(vo => vo.OrderValidation(orderManager, world, clientId, order))) { return; } } switch (order.OrderString) { case "Chat": { var client = orderManager.LobbyInfo.ClientWithIndex(clientId); // Cut chat messages to the hard limit to avoid exploits var message = order.TargetString; if (message.Length > ChatMessageMaxLength) { message = order.TargetString.Substring(0, ChatMessageMaxLength); } if (client != null) { var player = world != null?world.FindPlayerByClient(client) : null; var suffix = (player != null && player.WinState == WinState.Lost) ? " (Dead)" : ""; suffix = client.IsObserver ? " (Spectator)" : suffix; if (orderManager.LocalClient != null && client != orderManager.LocalClient && client.Team > 0 && client.Team == orderManager.LocalClient.Team) { suffix += " (Ally)"; } Game.AddChatLine(client.Color.RGB, client.Name + suffix, message); } else { Game.AddChatLine(Color.White, "(player {0})".F(clientId), message); } break; } case "Message": // Server message Game.AddChatLine(Color.White, ServerChatName, order.TargetString); break; case "Disconnected": /* reports that the target player disconnected */ { var client = orderManager.LobbyInfo.ClientWithIndex(clientId); if (client != null) { client.State = Session.ClientState.Disconnected; } break; } case "TeamChat": { var client = orderManager.LobbyInfo.ClientWithIndex(clientId); if (client != null) { if (world == null) { if (orderManager.LocalClient != null && client.Team == orderManager.LocalClient.Team) { Game.AddChatLine(client.Color.RGB, "[Team] " + client.Name, order.TargetString); } } else { var player = world.FindPlayerByClient(client); if (player != null && player.WinState == WinState.Lost) { Game.AddChatLine(client.Color.RGB, client.Name + " (Dead)", order.TargetString); } else if ((player != null && world.LocalPlayer != null && player.Stances[world.LocalPlayer] == Stance.Ally) || (world.IsReplay && player != null)) { Game.AddChatLine(client.Color.RGB, "[Team" + (world.IsReplay ? " " + client.Team : "") + "] " + client.Name, order.TargetString); } else if ((orderManager.LocalClient != null && orderManager.LocalClient.IsObserver && client.IsObserver) || (world.IsReplay && client.IsObserver)) { Game.AddChatLine(client.Color.RGB, "[Spectators] " + client.Name, order.TargetString); } } } break; } case "StartGame": { if (Game.ModData.MapCache[orderManager.LobbyInfo.GlobalSettings.Map].Status != MapStatus.Available) { Game.Disconnect(); Game.LoadShellMap(); // TODO: After adding a startup error dialog, notify the replay load failure. break; } Game.AddChatLine(Color.White, ServerChatName, "The game has started."); Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map, WorldType.Regular); break; } case "PauseGame": { var client = orderManager.LobbyInfo.ClientWithIndex(clientId); if (client != null) { var pause = order.TargetString == "Pause"; // Prevent injected unpause orders from restarting a finished game if (orderManager.World.PauseStateLocked && !pause) { break; } if (orderManager.World.Paused != pause && world != null && world.LobbyInfo.NonBotClients.Count() > 1) { var pausetext = "The game is {0} by {1}".F(pause ? "paused" : "un-paused", client.Name); Game.AddChatLine(Color.White, ServerChatName, pausetext); } orderManager.World.Paused = pause; orderManager.World.PredictedPaused = pause; } break; } case "HandshakeRequest": { // Switch to the server's mod if we need and are able to var mod = Game.ModData.Manifest; var request = HandshakeRequest.Deserialize(order.TargetString); var externalKey = ExternalMod.MakeKey(request.Mod, request.Version); ExternalMod external; if ((request.Mod != mod.Id || request.Version != mod.Metadata.Version) && Game.ExternalMods.TryGetValue(externalKey, out external)) { // The ConnectionFailedLogic will prompt the user to switch mods orderManager.ServerExternalMod = external; orderManager.Connection.Dispose(); break; } Game.Settings.Player.Name = Settings.SanitizedPlayerName(Game.Settings.Player.Name); Game.Settings.Save(); // Otherwise send the handshake with our current settings and let the server reject us var info = new Session.Client() { Name = Game.Settings.Player.Name, PreferredColor = Game.Settings.Player.Color, Color = Game.Settings.Player.Color, Faction = "Random", SpawnPoint = 0, Team = 0, State = Session.ClientState.Invalid }; var response = new HandshakeResponse() { Client = info, Mod = mod.Id, Version = mod.Metadata.Version, Password = orderManager.Password }; orderManager.IssueOrder(Order.HandshakeResponse(response.Serialize())); break; } case "ServerError": { orderManager.ServerError = order.TargetString; orderManager.AuthenticationFailed = false; break; } case "AuthenticationError": { // The ConnectionFailedLogic will prompt the user for the password orderManager.ServerError = order.TargetString; orderManager.AuthenticationFailed = true; break; } case "SyncInfo": { orderManager.LobbyInfo = Session.Deserialize(order.TargetString); SetOrderLag(orderManager); Game.SyncLobbyInfo(); break; } case "SyncLobbyClients": { var clients = new List <Session.Client>(); var nodes = MiniYaml.FromString(order.TargetString); foreach (var node in nodes) { var strings = node.Key.Split('@'); if (strings[0] == "Client") { clients.Add(Session.Client.Deserialize(node.Value)); } } orderManager.LobbyInfo.Clients = clients; Game.SyncLobbyInfo(); break; } case "SyncLobbySlots": { var slots = new Dictionary <string, Session.Slot>(); var nodes = MiniYaml.FromString(order.TargetString); foreach (var node in nodes) { var strings = node.Key.Split('@'); if (strings[0] == "Slot") { var slot = Session.Slot.Deserialize(node.Value); slots.Add(slot.PlayerReference, slot); } } orderManager.LobbyInfo.Slots = slots; Game.SyncLobbyInfo(); break; } case "SyncLobbyGlobalSettings": { var nodes = MiniYaml.FromString(order.TargetString); foreach (var node in nodes) { var strings = node.Key.Split('@'); if (strings[0] == "GlobalSettings") { orderManager.LobbyInfo.GlobalSettings = Session.Global.Deserialize(node.Value); } } SetOrderLag(orderManager); Game.SyncLobbyInfo(); break; } case "SyncClientPings": { var pings = new List <Session.ClientPing>(); var nodes = MiniYaml.FromString(order.TargetString); foreach (var node in nodes) { var strings = node.Key.Split('@'); if (strings[0] == "ClientPing") { pings.Add(Session.ClientPing.Deserialize(node.Value)); } } orderManager.LobbyInfo.ClientPings = pings; break; } case "Ping": { orderManager.IssueOrder(Order.Pong(order.TargetString)); break; } default: { if (!order.IsImmediate) { var self = order.Subject; if (!self.IsDead) { foreach (var t in self.TraitsImplementing <IResolveOrder>()) { t.ResolveOrder(self, order); } } } break; } } }