public static bool ProcessServerMessage(ChatData chatData) { //MyAPIGateway.Utilities.SendMessage(steamId, "CHECK", "ProcessServerMessage"); if (!_isInitialized || string.IsNullOrEmpty(chatData.TextCommand)) { //MyAPIGateway.Utilities.SendMessage(steamId, "CHECK", "ProcessServerMessage failed: not Initialized."); return(false); } var commands = chatData.TextCommand.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (commands.Length == 0) { //MyAPIGateway.Utilities.SendMessage(steamId, "CHECK", "ProcessServerMessage failed: no commands."); return(false); } var comandList = Commands.Where(k => k.Value.Commands.Any(a => a.Equals(commands[0], StringComparison.InvariantCultureIgnoreCase))).ToArray(); if (!comandList.Any()) { MyAPIGateway.Utilities.SendMessage(chatData.SenderSteamId, "CHECK", "ProcessServerMessage failed: matching commandList Empty."); } foreach (var command in comandList) { //if (MainChatCommandLogic.Instance.BlockCommandExecution) //{ // MyAPIGateway.Utilities.SendMessage(steamId, "Permission", "Loading permissions... Please try again later."); // return true; //} if (!HasRight(chatData.SenderSteamId, command.Value)) { MyAPIGateway.Utilities.SendMessage(chatData.SenderSteamId, "Permission", "You do not have the permission to use this command."); return(true); } if (command.Value.HasFlag(ChatCommandAccessibility.SingleplayerOnly) && !MyAPIGateway.Session.IsSinglePlayerOffline()) { MyAPIGateway.Utilities.SendMessage(chatData.SenderSteamId, "Command Service", "Command disabled in online mode."); return(true); } if (command.Value.HasFlag(ChatCommandAccessibility.MultiplayerOnly) && MyAPIGateway.Session.IsSinglePlayerOffline()) { MyAPIGateway.Utilities.SendMessage(chatData.SenderSteamId, "Command Service", "Command disabled in offline mode."); return(true); } if (command.Value.HasFlag(ChatCommandAccessibility.Server)) { try { //MyAPIGateway.Utilities.SendMessage(steamId, "CHECK", "ProcessServerMessage trying command."); if (command.Value.Invoke(chatData)) { return(true); } MyAPIGateway.Utilities.SendMessage(chatData.SenderSteamId, "Command failed", string.Format("Execution of command {0} failed. Use '/help {0}' for receiving a detailed instruction.", command.Value.Name)); command.Value.Help(chatData.SenderSteamId, true); return(true); } catch (Exception ex) { MainChatCommandLogic.Instance.ServerLogger.WriteException(ex, $"Occurred attempting to run {command.Value.GetType().Name} '{command.Key}' "); // Exception handling to prevent any crash in the ChatCommand's reaching the user. // Additional information for developers if (MainChatCommandLogic.Instance.ExperimentalCreatorList.Any(e => e == chatData.SenderSteamId)) { MyAPIGateway.Utilities.SendMissionScreen(chatData.SenderSteamId, $"Error in {command.Value.Name}", "Input: ", chatData.TextCommand, ex.ToString()); TextLogger.WriteGameLog($"##Mod## {MainChatCommandLogic.Instance.ModName} Exception caught. Message: {ex}"); continue; } var message = ex.Message.Replace("\r", " ").Replace("\n", " "); message = message.Substring(0, Math.Min(message.Length, 50)); MyAPIGateway.Utilities.SendMessage(chatData.SenderSteamId, "Error", "Occurred attempting to run {0} '{1}'.\r\n{2}", command.Value.GetType().Name, command.Key, message); } } else { MyAPIGateway.Utilities.SendMessage(chatData.SenderSteamId, "Command", "Has not been correctly registered as either Client or Server."); } } //MyAPIGateway.Utilities.SendMessage(steamId, "CHECK", "ProcessServerMessage failed: fallthrough."); return(false); }
/// <summary> /// Tests the Chat command for validility, and executes its content. /// </summary> /// <param name="chatData">Contains the sender details and the text command sent.</param> /// <returns>Returns true if the chat command was valid and processed successfully, otherwise returns false.</returns> public abstract bool Invoke(ChatData chatData);
/// <summary> /// This will use _commandShortcuts dictionary to only run the specific ChatCommands that has the specified command text registered. /// </summary> /// <param name="chatData"></param> /// <returns>Returns true if a valid command was found and successfuly invoked.</returns> public static bool ProcessClientMessage(ChatData chatData) { if (!_isInitialized || string.IsNullOrEmpty(chatData.TextCommand)) { return(false); } var commands = chatData.TextCommand.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (commands.Length == 0) { return(false); } var comandList = Commands.Where(k => k.Value.Commands.Any(a => a.Equals(commands[0], StringComparison.InvariantCultureIgnoreCase))); foreach (KeyValuePair <string, ChatCommand> command in comandList) { //if (MainChatCommandLogic.Instance.BlockCommandExecution) //{ // MyAPIGateway.Utilities.ShowMessage("Permission", "Loading permissions... Please try again later."); // return true; //} if (command.Value.Security == byte.MaxValue) { // this command has been disabled from use. return(false); } if (!HasRight(MyAPIGateway.Session.Player.SteamUserId, command.Value)) { MyAPIGateway.Utilities.ShowMessage("Permission", "You do not have the permission to use this command."); return(true); } if (command.Value.HasFlag(ChatCommandAccessibility.SingleplayerOnly) && !MyAPIGateway.Session.IsSinglePlayerOffline()) { MyAPIGateway.Utilities.ShowMessage("Command Service", "Command disabled in online mode."); return(true); } if (command.Value.HasFlag(ChatCommandAccessibility.MultiplayerOnly) && MyAPIGateway.Session.IsSinglePlayerOffline()) { MyAPIGateway.Utilities.ShowMessage("Command Service", "Command disabled in offline mode."); return(true); } //if (command.Value.HasFlag(ChatCommandAccessibility.Server)) //{ // // Send message to server to process. // ConnectionHelper.SendMessageToServer(new MessageChatCommand() // { // PlayerId = serverMessage.IdentityId, // TextCommand = messageText // }); // return true; //} if ((command.Value.HasFlag(ChatCommandAccessibility.Server) && MyAPIGateway.Session.IsSinglePlayerOffline()) || (command.Value.HasFlag(ChatCommandAccessibility.Server) && MyAPIGateway.Multiplayer.IsServer) || command.Value.HasFlag(ChatCommandAccessibility.Client)) { //MyAPIGateway.Utilities.ShowMessage("CHECK", "Command Client: {0}", command.Value.Flag); try { if (!MainChatCommandLogic.Instance.IsConnected) { MyAPIGateway.Utilities.ShowMessage("Please wait", $"Cannot execute command yet: {chatData.TextCommand}"); return(true); } if (command.Value.Invoke(chatData)) { return(true); } MyAPIGateway.Utilities.ShowMessage("Command failed", string.Format("Execution of command {0} failed. Use '/help {0}' for receiving a detailed instruction.", command.Value.Name)); command.Value.Help(0, true); return(true); } catch (Exception ex) { MainChatCommandLogic.Instance.ClientLogger.WriteException(ex, $"Occurred attempting to run {command.Value.GetType().Name} '{command.Key}' "); // Exception handling to prevent any crash in the ChatCommand's reaching the user. // Additional information for developers if (MyAPIGateway.Session.Player.IsExperimentalCreator()) { MyAPIGateway.Utilities.ShowMissionScreen($"Error in {command.Value.Name}", "Input: ", chatData.TextCommand, ex.ToString()); TextLogger.WriteGameLog($"##Mod## {MainChatCommandLogic.Instance.ModName} Exception caught. Message: {ex}"); continue; } var message = ex.Message.Replace("\r", " ").Replace("\n", " "); message = message.Substring(0, Math.Min(message.Length, 50)); MyAPIGateway.Utilities.ShowMessage("Error", "Occurred attempting to run {0} '{1}'.\r\n{2}", command.Value.GetType().Name, command.Key, message); } } else if (command.Value.HasFlag(ChatCommandAccessibility.Server)) { //MyAPIGateway.Utilities.ShowMessage("CHECK", "Command Server: {0}", command.Value.Flag); // Send message to server to process. PullChatCommand.SendMessage(chatData.IdentityId, chatData.TextCommand); return(true); } else { MyAPIGateway.Utilities.ShowMessage("Command", "Has not been correctly registered as either Client or Server."); } } return(false); }