From 3c86956131358d21802e03935d84579e0fd410bb Mon Sep 17 00:00:00 2001 From: Hayden Date: Sun, 6 Mar 2022 09:31:05 +0800 Subject: [PATCH] Add globally blocked and globally allowed helper commands --- src/AdminRestrictions/CommandRestrictor.cs | 197 +++++++++++++++++- .../Configuration/GlobalConfig.cs | 9 +- 2 files changed, 194 insertions(+), 12 deletions(-) diff --git a/src/AdminRestrictions/CommandRestrictor.cs b/src/AdminRestrictions/CommandRestrictor.cs index 795ad33..5755b19 100644 --- a/src/AdminRestrictions/CommandRestrictor.cs +++ b/src/AdminRestrictions/CommandRestrictor.cs @@ -70,7 +70,7 @@ bool IsCommandPermitted(ConsoleSystem.Arg arg) return true; // Is this command globally allowed? - for (int i = 0; i < _configuration.globallyAllowedCommands.Length; i++) + for (int i = 0; i < _configuration.globallyAllowedCommands.Count; i++) { if (string.Equals(_configuration.globallyAllowedCommands[i], arg.cmd.FullName)) { @@ -79,7 +79,7 @@ bool IsCommandPermitted(ConsoleSystem.Arg arg) } // Is this command globally blocked? - for (int i = 0; i < _configuration.globallyBlockedCommands.Length; i++) + for (int i = 0; i < _configuration.globallyBlockedCommands.Count; i++) { if (string.Equals(_configuration.globallyBlockedCommands[i], arg.cmd.FullName)) { @@ -182,7 +182,51 @@ void RegisterCommands() Variable = false, Call = new Action(ReloadCfgCommand) }; - ConsoleSystem.Index.Server.Dict[commandPrefix + "." + "reloadcfg"] = reloadCfgCommand; + ConsoleSystem.Index.Server.Dict[reloadCfgCommand.FullName] = reloadCfgCommand; + + ConsoleSystem.Command addGloballyAllowedCommand = new ConsoleSystem.Command() + { + Name = "addgloballyallowedcommand", + Parent = commandPrefix, + FullName = commandPrefix + "." + "addgloballyallowedcommand", + ServerAdmin = true, + Variable = false, + Call = new Action(AddGloballyAllowedCommand) + }; + ConsoleSystem.Index.Server.Dict[addGloballyAllowedCommand.FullName] = addGloballyAllowedCommand; + + ConsoleSystem.Command removeGloballyAllowedCommand = new ConsoleSystem.Command() + { + Name = "removegloballyallowedcommand", + Parent = commandPrefix, + FullName = commandPrefix + "." + "removegloballyallowedcommand", + ServerAdmin = true, + Variable = false, + Call = new Action(RemoveGloballyAllowedCommand) + }; + ConsoleSystem.Index.Server.Dict[removeGloballyAllowedCommand.FullName] = removeGloballyAllowedCommand; + + ConsoleSystem.Command addGloballyBlockedCommand = new ConsoleSystem.Command() + { + Name = "addgloballyblockedcommand", + Parent = commandPrefix, + FullName = commandPrefix + "." + "addgloballyblockedcommand", + ServerAdmin = true, + Variable = false, + Call = new Action(AddGloballyBlockedCommand) + }; + ConsoleSystem.Index.Server.Dict[addGloballyBlockedCommand.FullName] = addGloballyBlockedCommand; + + ConsoleSystem.Command removeGloballyBlockedCommand = new ConsoleSystem.Command() + { + Name = "removegloballyblockedcommand", + Parent = commandPrefix, + FullName = commandPrefix + "." + "removegloballyblockedcommand", + ServerAdmin = true, + Variable = false, + Call = new Action(RemoveGloballyBlockedCommand) + }; + ConsoleSystem.Index.Server.Dict[removeGloballyBlockedCommand.FullName] = removeGloballyBlockedCommand; ConsoleSystem.Command addAdminToGroupCommand = new ConsoleSystem.Command() { @@ -193,7 +237,7 @@ void RegisterCommands() Variable = false, Call = new Action(AddAdminToGroupCommand) }; - ConsoleSystem.Index.Server.Dict[commandPrefix + "." + "addadmintogroup"] = addAdminToGroupCommand; + ConsoleSystem.Index.Server.Dict[addAdminToGroupCommand.FullName] = addAdminToGroupCommand; ConsoleSystem.Command removeAdminFromGroupCommand = new ConsoleSystem.Command() { @@ -204,7 +248,7 @@ void RegisterCommands() Variable = false, Call = new Action(RemoveAdminFromGroupCommand) }; - ConsoleSystem.Index.Server.Dict[commandPrefix + "." + "removeadminfromgroup"] = removeAdminFromGroupCommand; + ConsoleSystem.Index.Server.Dict[removeAdminFromGroupCommand.FullName] = removeAdminFromGroupCommand; ConsoleSystem.Command[] allCommands = ConsoleSystem.Index.All.Concat(new ConsoleSystem.Command[] { reloadCfgCommand, addAdminToGroupCommand, removeAdminFromGroupCommand }).ToArray(); // Would be nice if this had a public setter, or better yet, a register command helper @@ -213,6 +257,108 @@ void RegisterCommands() .SetValue(null, allCommands); } + void AddGloballyAllowedCommand(ConsoleSystem.Arg arg) + { + if (!arg.HasArgs(1)) + { + arg.ReplyWith("[AdminRestrictions]: Invalid syntax: adminrestrictions.addgloballyallowedcommand [...]"); + return; + } + + int processed = 0; + for (int i = 0; i < arg.Args.Length; i++) + { + var command = arg.Args[i]; + if (string.IsNullOrWhiteSpace(command)) continue; + if (_configuration.globallyAllowedCommands.Contains(command)) continue; + _configuration.globallyAllowedCommands.Add(command); + processed++; + } + + if (processed > 0) + { + SaveConfiguration(); + } + + arg.ReplyWith("[AdminRestrictions]: Added " + processed + " command(s) to the globally allowed list"); + } + + void RemoveGloballyAllowedCommand(ConsoleSystem.Arg arg) + { + if (!arg.HasArgs(1)) + { + arg.ReplyWith("[AdminRestrictions]: Invalid syntax: adminrestrictions.removegloballyallowedcommand [...]"); + return; + } + + int processed = 0; + for (int i = 0; i < arg.Args.Length; i++) + { + var command = arg.Args[i]; + if (string.IsNullOrWhiteSpace(command)) continue; + if (!_configuration.globallyAllowedCommands.Remove(command)) continue; + processed++; + } + + if (processed > 0) + { + SaveConfiguration(); + } + + arg.ReplyWith("[AdminRestrictions]: Removed " + processed + " command(s) from the globally allowed list"); + } + + void AddGloballyBlockedCommand(ConsoleSystem.Arg arg) + { + if (!arg.HasArgs(1)) + { + arg.ReplyWith("[AdminRestrictions]: Invalid syntax: adminrestrictions.addgloballyblockedcommand [...]"); + return; + } + + int processed = 0; + for (int i = 0; i < arg.Args.Length; i++) + { + var command = arg.Args[i]; + if (string.IsNullOrWhiteSpace(command)) continue; + if (_configuration.globallyBlockedCommands.Contains(command)) continue; + _configuration.globallyAllowedCommands.Add(command); + processed++; + } + + if (processed > 0) + { + SaveConfiguration(); + } + + arg.ReplyWith("[AdminRestrictions]: Added " + processed + " command(s) to the globally blocked list"); + } + + void RemoveGloballyBlockedCommand(ConsoleSystem.Arg arg) + { + if (!arg.HasArgs(1)) + { + arg.ReplyWith("[AdminRestrictions]: Invalid syntax: adminrestrictions.removegloballyblockedcommand [...]"); + return; + } + + int processed = 0; + for (int i = 0; i < arg.Args.Length; i++) + { + var command = arg.Args[i]; + if (string.IsNullOrWhiteSpace(command)) continue; + if (!_configuration.globallyBlockedCommands.Remove(command)) continue; + processed++; + } + + if (processed > 0) + { + SaveConfiguration(); + } + + arg.ReplyWith("[AdminRestrictions]: Removed " + processed + " command(s) from the globally blocked list"); + } + void AddAdminToGroupCommand(ConsoleSystem.Arg arg) { if (!arg.HasArgs(2)) @@ -415,8 +561,11 @@ GlobalConfig GenerateDefaultConfiguration() return new GlobalConfig { enabled = false, - globallyBlockedCommands = new string[] + globallyBlockedCommands = new List { + "server.stop", + "spawn.fill_populations", + "spawn.fill_groups", "global.quit", "global.restart", "demo.record", @@ -425,15 +574,47 @@ GlobalConfig GenerateDefaultConfiguration() "demo.splitseconds", "demo.stop" }, - logToFile = true, + globallyAllowedCommands = new List + { + "global.teleport2player", + "global.teleport2owneditem", + "global.teleport2marker", + "global.teleportlos", + "global.teleportpos", + "global.spectate", + "global.teaminfo", + "server.snapshot", + "server.combatlog", + "server.combatlog_outgoing", + "global.god", + "global.sleep" + }, + logToFile = false, groupConfigs = new GroupConfig[] { new GroupConfig { - name = "demo-group-1", + name = "allow-all", + allowAll = true, + allowedCommands = new string[0], + steamIds = new List + { + 1234567890 + } + }, + new GroupConfig + { + name = "admins", allowAll = false, allowedCommands = new string[] { + "global.teleport2me", + "entity.spawn", + "inventory.give", + "inventory.giveall", + "inventory.givearm", + "inventory.giveid", + "inventory.giveto", "global.entid" }, steamIds = new List diff --git a/src/AdminRestrictions/Configuration/GlobalConfig.cs b/src/AdminRestrictions/Configuration/GlobalConfig.cs index 65ea635..ce52189 100644 --- a/src/AdminRestrictions/Configuration/GlobalConfig.cs +++ b/src/AdminRestrictions/Configuration/GlobalConfig.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using System.Collections.Generic; namespace AdminRestrictions.Configuration { @@ -8,10 +9,10 @@ internal class GlobalConfig public bool enabled = false; [JsonProperty(PropertyName = "Globally Blocked Commands")] - public string[] globallyBlockedCommands; + public List globallyBlockedCommands; [JsonProperty(PropertyName = "Globally Allowed Commands")] - public string[] globallyAllowedCommands; + public List globallyAllowedCommands; [JsonProperty(PropertyName = "Log to file")] public bool logToFile = true; @@ -21,8 +22,8 @@ internal class GlobalConfig public GlobalConfig() { - globallyBlockedCommands = globallyBlockedCommands ?? new string[0]; - globallyAllowedCommands = globallyAllowedCommands ?? new string[0]; + globallyBlockedCommands = globallyBlockedCommands ?? new List(); + globallyAllowedCommands = globallyAllowedCommands ?? new List(); groupConfigs = groupConfigs ?? new GroupConfig[0]; } }