private void InitServer() { _isServerRegistered = true; ServerLogger.Init(ServerLogFileName, ServerLoggingLevel, false, MyAPIGateway.Session.OnlineMode.Equals(MyOnlineModeEnum.OFFLINE) ? 0 : 5); // comment this out if logging is not required for the Server. ServerLogger.WriteInfo($"{ModName} Server Log Started"); ServerLogger.WriteInfo($"{ModName} Server Version {ModCommunicationVersion}"); //if (ServerLogger.IsActive) //if (ClientLogger.IsActive) // TODO: determine is this is needed any more? TextLogger.WriteGameLog($"##Mod## {ModName} Server Logging File: {ServerLogger.LogFile}"); ServerLogger.WriteStart("RegisterMessageHandler"); MyAPIGateway.Multiplayer.RegisterMessageHandler(ServerConnectionId, _serverMessageHandler); ServerLogger.Flush(); }
private void InitClient() { _isClientRegistered = true; ClientLogger.Init(ClientLogFileName, ClientLoggingLevel, false, MyAPIGateway.Session.OnlineMode.Equals(MyOnlineModeEnum.OFFLINE) ? 0 : 5); // comment this out if logging is not required for the Client. ClientLogger.WriteInfo($"{ModName} Client Log Started"); ClientLogger.WriteInfo($"{ModName} Client Version {ModCommunicationVersion}"); //if (ClientLogger.IsActive) // TODO: determine is this is needed any more? TextLogger.WriteGameLog($"##Mod## {ModName} Client Logging File: {ClientLogger.LogFile}"); MyAPIGateway.Utilities.MessageEntered += ChatMessageEntered; ClientLogger.WriteStart("RegisterMessageHandler"); MyAPIGateway.Multiplayer.RegisterMessageHandler(ClientConnectionId, _clientMessageHandler); // Offline connections can be re-attempted quickly. Online games needs to wait longer. DelayedConnectionRequestTimer = new Timer(MyAPIGateway.Session.OnlineMode == MyOnlineModeEnum.OFFLINE ? 500 : 10000); DelayedConnectionRequestTimer.Elapsed += DelayedConnectionRequestTimer_Elapsed; DelayedConnectionRequestTimer.Start(); // let the server know we are ready for connections PullConnectionRequest.SendMessage(ModCommunicationVersion, PrivateCommunicationKey); ClientLogger.Flush(); }
public static bool ProcessServerMessage(ulong steamId, long playerId, string messageText) { //MyAPIGateway.Utilities.SendMessage(steamId, "CHECK", "ProcessServerMessage"); if (!_isInitialized || string.IsNullOrEmpty(messageText)) { //MyAPIGateway.Utilities.SendMessage(steamId, "CHECK", "ProcessServerMessage failed: not Initialized."); return(false); } var commands = messageText.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(steamId, "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(steamId, command.Value)) { MyAPIGateway.Utilities.SendMessage(steamId, "Permission", "You do not have the permission to use this command."); return(true); } if (command.Value.HasFlag(ChatCommandAccessibility.SingleplayerOnly) && MyAPIGateway.Session.OnlineMode != MyOnlineModeEnum.OFFLINE) { MyAPIGateway.Utilities.SendMessage(steamId, "Command Service", "Command disabled in online mode."); return(true); } if (command.Value.HasFlag(ChatCommandAccessibility.MultiplayerOnly) && MyAPIGateway.Session.OnlineMode == MyOnlineModeEnum.OFFLINE) { MyAPIGateway.Utilities.SendMessage(steamId, "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(steamId, playerId, messageText)) { return(true); } MyAPIGateway.Utilities.SendMessage(steamId, "Command failed", string.Format("Execution of command {0} failed. Use '/help {0}' for receiving a detailed instruction.", command.Value.Name)); command.Value.Help(steamId, 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 == steamId)) { MyAPIGateway.Utilities.SendMissionScreen(steamId, $"Error in {command.Value.Name}", "Input: ", messageText, 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(steamId, "Error", "Occurred attempting to run {0} '{1}'.\r\n{2}", command.Value.GetType().Name, command.Key, message); } } else { MyAPIGateway.Utilities.SendMessage(steamId, "Command", "Has not been correctly registered as either Client or Server."); } } //MyAPIGateway.Utilities.SendMessage(steamId, "CHECK", "ProcessServerMessage failed: fallthrough."); return(false); }
/// <summary> /// This will use _commandShortcuts dictionary to only run the specific ChatCommands that has the specified command text registered. /// </summary> /// <param name="messageText"></param> /// <returns>Returns true if a valid command was found and successfuly invoked.</returns> public static bool ProcessClientMessage(string messageText) { if (!_isInitialized || string.IsNullOrEmpty(messageText)) { return(false); } var commands = messageText.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 == uint.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.OnlineMode != MyOnlineModeEnum.OFFLINE) { MyAPIGateway.Utilities.ShowMessage("Command Service", "Command disabled in online mode."); return(true); } if (command.Value.HasFlag(ChatCommandAccessibility.MultiplayerOnly) && MyAPIGateway.Session.OnlineMode == MyOnlineModeEnum.OFFLINE) { 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 = MyAPIGateway.Session.Player.IdentityId, // TextCommand = messageText // }); // return true; //} if ((command.Value.HasFlag(ChatCommandAccessibility.Server) && MyAPIGateway.Session.OnlineMode == MyOnlineModeEnum.OFFLINE) || (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: {messageText}"); return(true); } if (command.Value.Invoke(MyAPIGateway.Session.Player.SteamUserId, MyAPIGateway.Session.Player.IdentityId, messageText)) { 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: ", messageText, 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(MyAPIGateway.Session.Player.IdentityId, messageText); return(true); } else { MyAPIGateway.Utilities.ShowMessage("Command", "Has not been correctly registered as either Client or Server."); } } return(false); }