public override bool Invoke(ulong steamId, long playerId, string messageText)
        {
            var match = Regex.Match(messageText, @"(/config|/cfg)\s+(?<Key>[^\s]+)((\s+(?<Value>.+))|)", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            if (match.Success)
            {
                var key   = match.Groups["Key"].Value;
                var value = match.Groups["Value"].Value.Trim();

                switch (key.ToLowerInvariant())
                {
                case "motd":
                case "messageoftheday":
                    ConnectionHelper.SendMessageToServer(new MessageOfTheDayMessage {
                        Content = value, FieldsToUpdate = MessageOfTheDayMessage.ChangedFields.Content
                    });
                    break;

                case "motdheadline":
                case "motdhl":
                    ConnectionHelper.SendMessageToServer(new MessageOfTheDayMessage {
                        HeadLine = value, FieldsToUpdate = MessageOfTheDayMessage.ChangedFields.HeadLine
                    });
                    break;

                case "motdsic":
                case "motdshowinchat":
                    bool?motdsic = Support.GetBool(value);
                    if (motdsic.HasValue)
                    {
                        ConnectionHelper.SendMessageToServer(new MessageOfTheDayMessage {
                            ShowInChat = motdsic.Value, FieldsToUpdate = MessageOfTheDayMessage.ChangedFields.ShowInChat
                        });
                    }
                    else
                    {
                        MyAPIGateway.Utilities.ShowMessage("Config", "{0} is an invalid argument for {1}. It must be eiter on or off.", new object[] { value, key });
                    }
                    break;

                case "adminlevel":
                    uint adminLevel;
                    if (uint.TryParse(value, out adminLevel))
                    {
                        ConnectionHelper.SendMessageToServer(new MessageConfig {
                            Config = new ServerConfigurationStruct {
                                AdminLevel = adminLevel
                            }, Action = ConfigAction.AdminLevel
                        });
                    }
                    else
                    {
                        MyAPIGateway.Utilities.ShowMessage("Config", "{0} is an invalid argument for {1}. It must be an integer higher or equal to {2} and lower or equal to {3}.", new object[] { value, key, uint.MinValue, uint.MaxValue });
                    }
                    break;

                case "nogrindindestructible":
                case "ngi":
                    bool noGrindIndestructible;
                    if (bool.TryParse(value, out noGrindIndestructible))
                    {
                        ConnectionHelper.SendMessageToServer(new MessageConfig {
                            Config = new ServerConfigurationStruct {
                                NoGrindIndestructible = noGrindIndestructible
                            }, Action = ConfigAction.NoGrindIndestructible
                        });
                    }
                    else
                    {
                        MyAPIGateway.Utilities.ShowMessage("Config", "{0} is an invalid argument for {1}. It must be eiter true or false.", new object[] { value, key });
                    }
                    break;

                case "save":
                    ConnectionHelper.SendMessageToServer(new MessageConfig {
                        Action = ConfigAction.Save
                    });
                    break;

                case "reload":
                    ConnectionHelper.SendMessageToServer(new MessageConfig {
                        Action = ConfigAction.Reload
                    });
                    break;

                case "show":
                    ConnectionHelper.SendMessageToServer(new MessageConfig {
                        Action = ConfigAction.Show
                    });
                    break;

                default:
                    MyAPIGateway.Utilities.ShowMessage("Config", "Invalid setting or action. Type '/help cfg' for help.");
                    MyAPIGateway.Utilities.ShowMessage("Available actions and settings:", "adminlevel, motd, motdhl, motdsic, ngi, save, show, reload");
                    break;
                }

                return(true);
            }
            return(false);
        }
        public override bool Invoke(ulong steamId, long playerId, string messageText)
        {
            MyObjectBuilder_Base content = null;

            string[] options;
            decimal  amount = 1;

            var match = Regex.Match(messageText, @"/invadd\s{1,}(?:(?<Key>.+)\s(?<Value>[+-]?((\d+(\.\d*)?)|(\.\d+)))|(?<Key>.+))", RegexOptions.IgnoreCase);

            if (match.Success)
            {
                var itemName  = match.Groups["Key"].Value;
                var strAmount = match.Groups["Value"].Value;
                if (!decimal.TryParse(strAmount, out amount))
                {
                    amount = 1;
                }

                if (!Support.FindPhysicalParts(_oreNames, _ingotNames, _physicalItemNames, _physicalItems, itemName, out content, out options) && options.Length > 0)
                {
                    MyAPIGateway.Utilities.ShowMessage("Did you mean", String.Join(", ", options) + " ?");
                    return(true);
                }
            }

            if (content != null)
            {
                if (amount < 0)
                {
                    amount = 1;
                }

                if (content.TypeId != typeof(MyObjectBuilder_Ore) && content.TypeId != typeof(MyObjectBuilder_Ingot))
                {
                    // must be whole numbers.
                    amount = Math.Round(amount, 0);
                }

                if (!MyAPIGateway.Multiplayer.MultiplayerActive)
                {
                    var definitionId = new MyDefinitionId(content.GetType(), content.SubtypeName);
                    if (!Support.InventoryAdd((MyEntity)MyAPIGateway.Session.Player.Character, (MyFixedPoint)amount, definitionId))
                    {
                        MyAPIGateway.Utilities.ShowMessage("Failed", "Inventory full. Could not add the item.");
                    }
                }
                else
                {
                    ConnectionHelper.SendMessageToServer(new MessageSyncCreateObject()
                    {
                        EntityId    = MyAPIGateway.Session.Player.Character.EntityId,
                        Type        = SyncCreateObjectType.Inventory,
                        TypeId      = content.TypeId.ToString(),
                        SubtypeName = content.SubtypeName,
                        Amount      = amount
                    });
                }
                return(true);
            }

            MyAPIGateway.Utilities.ShowMessage("Unknown Item", "Could not find the specified name.");
            return(true);
        }
Ejemplo n.º 3
0
        public void ProcessPlayerPermission(ulong steamId, string[] args)
        {
            var playerMessage = new MessagePlayerPermission();

            switch (args[1].ToLowerInvariant())
            {
            case "setlevel":
                if (args.Length < 4)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Player setlevel", "/perm player setlevel <playerName> <level>");
                    return;
                }

                uint level;
                if (uint.TryParse(args[3], out level))
                {
                    playerMessage.Action      = PlayerPermissionAction.Level;
                    playerMessage.PlayerName  = args[2];
                    playerMessage.PlayerLevel = level;
                }
                else
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", $"{args[3]} is no valid level. It must be an integer and can't be below 0.");
                    return;
                }
                break;

            case "extend":
                if (args.Length < 4)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Player extend", "/perm player extend <playerName> <commandName>");
                    Help(steamId, true);
                    return;
                }

                playerMessage.Action      = PlayerPermissionAction.Extend;
                playerMessage.PlayerName  = args[2];
                playerMessage.CommandName = args[3];
                break;

            case "restrict":
                if (args.Length < 4)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Player restrict", "/perm player restrict <playerName> <commandName>");
                    return;
                }

                playerMessage.Action      = PlayerPermissionAction.Restrict;
                playerMessage.PlayerName  = args[2];
                playerMessage.CommandName = args[3];
                break;

            case "upl":
            case "useplayerlevel":
                if (args.Length < 4)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Player useplayerlevel", "/perm player upl <playerName> <true|false>");
                    Help(steamId, true);
                    return;
                }

                bool usePlayerLevel;
                if (bool.TryParse(args[3], out usePlayerLevel))
                {
                    playerMessage.Action         = PlayerPermissionAction.UsePlayerLevel;
                    playerMessage.PlayerName     = args[2];
                    playerMessage.UsePlayerLevel = usePlayerLevel;
                }
                else
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", $"{args[3]} is no valid value. It must be either true or false.");
                    return;
                }
                break;

            case "list":
                string param = "";
                if (args.Length > 2)
                {
                    param = args[2];
                }

                playerMessage.Action     = PlayerPermissionAction.List;
                playerMessage.PlayerName = param;
                break;

            default:
                MyAPIGateway.Utilities.ShowMessage("Permissions", $"There is no action named {args[1]}. Available actions: setlevel, extend, restrict, useplayerlevel, list.");
                return;
            }

            ConnectionHelper.SendMessageToServer(playerMessage);
        }
Ejemplo n.º 4
0
        public void ProcessGroupPermission(ulong steamId, string[] args)
        {
            var groupMessage = new MessageGroupPermission();

            switch (args[1].ToLowerInvariant())
            {
            case "setlevel":
                if (args.Length < 4)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Group setlevel", "/perm group setlevel <groupName> <level>");
                    Help(steamId, true);
                    return;
                }

                uint level;
                if (uint.TryParse(args[3], out level))
                {
                    groupMessage.Action     = PermissionGroupAction.Level;
                    groupMessage.GroupName  = args[2];
                    groupMessage.GroupLevel = level;
                }
                else
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", $"{args[3]} is no valid level. It must be an integer and can't be below 0.");
                    return;
                }
                break;

            case "setname":
            case "rename":
                if (args.Length < 4)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Group setname", "/perm group setname <groupName> <newGroupName>");
                    return;
                }

                groupMessage.Action    = PermissionGroupAction.Name;
                groupMessage.GroupName = args[2];
                groupMessage.Name      = args[3];
                break;

            case "add":
            case "addplayer":
                if (args.Length < 4)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Group addplayer", "/perm group add <groupName> <playerName>");
                    return;
                }

                groupMessage.Action    = PermissionGroupAction.Add;
                groupMessage.GroupName = args[2];
                groupMessage.Name      = args[3];
                break;

            case "remove":
            case "removeplayer":
                if (args.Length < 4)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Group removeplayer", "/perm group remove <groupName> <playerName>");
                    return;
                }

                groupMessage.Action    = PermissionGroupAction.Remove;
                groupMessage.GroupName = args[2];
                groupMessage.Name      = args[3];
                break;

            case "create":
                if (args.Length < 4)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Group create", "/perm group create <groupName> <level>");
                    return;
                }

                if (uint.TryParse(args[3], out level))
                {
                    groupMessage.Action     = PermissionGroupAction.Create;
                    groupMessage.GroupName  = args[2];
                    groupMessage.GroupLevel = level;
                }
                else
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", $"{args[3]} is no valid level. It must be an integer and can't be below 0.");
                    return;
                }
                break;

            case "delete":
                if (args.Length < 3)
                {
                    MyAPIGateway.Utilities.ShowMessage("Permissions", "Not enough arguments.");
                    MyAPIGateway.Utilities.ShowMessage("Group delete", "/perm group create <groupName>");
                    return;
                }

                groupMessage.Action    = PermissionGroupAction.Delete;
                groupMessage.GroupName = args[2];
                break;

            case "list":
                string param = "";
                if (args.Length > 2)
                {
                    param = args[2];
                }

                groupMessage.Action    = PermissionGroupAction.List;
                groupMessage.GroupName = param;
                break;

            default:
                MyAPIGateway.Utilities.ShowMessage("Permissions", $"There is no action named {args[1]}. Available actions: setlevel, setname, add, remove, create, delete, list.");
                return;
            }

            ConnectionHelper.SendMessageToServer(groupMessage);
        }
        /// <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 (var command in comandList)
            {
                if (ChatCommandLogic.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(ChatCommandFlag.SingleplayerOnly) && MyAPIGateway.Session.OnlineMode != MyOnlineModeEnum.OFFLINE)
                {
                    MyAPIGateway.Utilities.ShowMessage("Command Service", "Command disabled in online mode.");
                    return(true);
                }

                if (command.Value.HasFlag(ChatCommandFlag.MultiplayerOnly) && MyAPIGateway.Session.OnlineMode == MyOnlineModeEnum.OFFLINE)
                {
                    MyAPIGateway.Utilities.ShowMessage("Command Service", "Command disabled in offline mode.");
                    return(true);
                }

                if ((command.Value.HasFlag(ChatCommandFlag.Server) && MyAPIGateway.Session.OnlineMode == MyOnlineModeEnum.OFFLINE) ||
                    (command.Value.HasFlag(ChatCommandFlag.Server) && MyAPIGateway.Multiplayer.IsServer) ||
                    command.Value.HasFlag(ChatCommandFlag.Client))
                {
                    //MyAPIGateway.Utilities.ShowMessage("CHECK", "Command Client: {0}", command.Value.Flag);
                    try
                    {
                        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)
                    {
                        // 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(string.Format("Error in {0}", command.Value.Name), "Input: ", messageText, ex.ToString(), null, null);
                            VRage.Utils.MyLog.Default.WriteLine(String.Format("##Mod## Admin Helper mod Exception caught. Message: {0}", ex));
                            continue;
                        }

                        var message = ex.Message.Replace("\r", " ").Replace("\n", " ");
                        message = message.Substring(0, Math.Min(message.Length, 50));
                        MyAPIGateway.Utilities.ShowMessage("Error", "Occured attempting to run {0}.\r\n{1}", command, message);
                    }
                }
                else if (command.Value.HasFlag(ChatCommandFlag.Server))
                {
                    //MyAPIGateway.Utilities.ShowMessage("CHECK", "Command Server: {0}", command.Value.Flag);

                    // Send message to server to process.
                    ConnectionHelper.SendMessageToServer(new MessageChatCommand()
                    {
                        PlayerId    = MyAPIGateway.Session.Player.IdentityId,
                        TextCommand = messageText
                    });
                    return(true);
                }
                else
                {
                    MyAPIGateway.Utilities.ShowMessage("Command", "Has not been correctly registered as either Client or Server.");
                }
            }

            return(false);
        }