From d554ace7da89b8c8037c4939aceb79c6145d3ba7 Mon Sep 17 00:00:00 2001 From: gsd Date: Tue, 19 Oct 2021 09:53:44 +0000 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=8C=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D1=82=D1=8C=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D0=B8?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=82=D0=B5=D0=BA=D1=83=D1=89=D0=B5=D1=8E=20?= =?UTF-8?q?=D0=BA=D0=B0=D1=80=D1=82=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SourceManager.py | 124 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 4 deletions(-) diff --git a/SourceManager.py b/SourceManager.py index c99968a..ae8302f 100755 --- a/SourceManager.py +++ b/SourceManager.py @@ -2,6 +2,7 @@ VERSION = 1.0 #SYS IMPORTS +from genericpath import isfile import sys import os import types @@ -202,6 +203,27 @@ class Server: def upgrade_metamod(self): pass + def add_plugin(self, plugin_path, need_reloads_plugins = True): + plugins_list = glob(f"{self.root}/addons/sourcemod/plugins/*.smx") + glob(f"{self.root}/addons/sourcemod/plugins/*/*.smx") + plugin_file = plugin_path.split("/")[-1] + + for plugin in plugins_list: + if plugin.split("/")[-1] == plugin_file: + self.o.info("Plugin already added!") + return True + ############################################ + new_plugin_path = f"{self.root}/addons/sourcemod/plugins/{plugin_file}" + + self.o.info(f"Add new plugins ({plugin_path}) to ({new_plugin_path})?") + if self.wait_input(): + shutil.copyfile(plugin_path, new_plugin_path) + + if need_reloads_plugins: + self.o.info("Send sm plugins refresh command...", end = "\t") + self.rcon("sm plugins refresh", hide_server_name=True) + + return True + def remove_plugin(self, plugin_name, need_reloads_plugins = True): plugins_list = glob(f"{self.root}/addons/sourcemod/plugins/*.smx") + glob(f"{self.root}/addons/sourcemod/plugins/*/*.smx") for plugin in plugins_list: @@ -214,7 +236,6 @@ class Server: self.rcon("sm plugins refresh", hide_server_name=True) return True - def upgrade_plugin(self, fresh_path, need_reloads_plugins = True): new_hash = hashfile(fresh_path) fresh_plugin_name = fresh_path.split("/")[-1:][0] @@ -267,6 +288,78 @@ class Server: else: print("{} | {}/{} players | {} ms".format(self, player_count, max_count, ping)) + @property + def map_config(self): + map = self.current_map() + if "/" in map: + sub_dir, map = map.split("/") + configs_directory = f"{self.root}/cfg/{sub_dir}" + else: + configs_directory = f"{self.root}/cfg" + + if not check_dir(configs_directory): + os.mkdir(configs_directory) + + return f"{configs_directory}/{map}.cfg" + + def check2map_config(self, cfg_line): + config = self.map_config + try: + with open(config, "r") as config_file: + for line in config_file: + line = line.replace("\n", "") + if cfg_line == line: + return True + return False + except IOError: + return False + + def remove2map_config(self, line): + config = self.map_config + + if not self.check2map_config(line): + print(f"{self} | Line not currently in: {config}") + + buffer = [] + + with open(config, "r") as config_file: + pass + + + + def add2map_config(self, line): + config = self.map_config + + if self.check2map_config(line): + print(f"{self} | Line currently in: {config}") + return False + + with open(config, "a") as config_file: + config_file.write(f"{line}\n") + + if self.check2map_config(line): + print(f"{self} | Line: {line}\nAppend to: {config}") + return True + + print(f"{self} | Line: {line}\nNot append in: {config}") + return False + + def map(self): + current_map = self.current_map() + if current_map: + print("{} | {}".format(self, current_map)) + else: + print("{} | {}".format(self, "not running")) + + def current_map(self): + try: + with RCON(self.address, self.password) as rcon: + rcon_response = rcon.execute("status") + return rcon_response.body.decode("utf8","ignore").split("\n")[5].split(": ")[1].split(" ")[0] + except: + traceback.print_exc() + return None + def count_players(self): try: ping = 0.0 @@ -442,7 +535,7 @@ class Server: except Exception as rcon_error: traceback.print_exc() - print({}) + print(rcon_error) def rcon(self, command, result = False, hide_server_name = False): if not hide_server_name: @@ -506,15 +599,18 @@ if __name__ == "__main__": parser.add_argument("--status", "-s", help = "Show current number players on server", default = False, action = "store_true") parser.add_argument("--yes", "-y", help = "Say YES to all response", default = False, action = "store_true") parser.add_argument("--netstatus","-ns", help = "Show json net_status", default = False, action = "store_true") + parser.add_argument("--map","-m", help = "Show map on server", default = False, action = "store_true") + parser.add_argument("--add_map_config", "-amc", help = "Add line to config map", default = "", type = str, nargs="+") ################################################################################################################################################ parser.add_argument("--CopyFile", "-cp", help = "Path of file to copy in root directory\nNeed second argument: --DestDir", type = str, default = "") parser.add_argument("--SymLinkFile", "-ln", help = "Path of file to create symbolic link in root directory\nNeed second argument: --DestDir", type = str, default = "") parser.add_argument("--DestDir", "-dd", help = "Destonation directory, aka: addons/sourcemod/plugins", type = str, default = "/") parser.add_argument("--ShowDir", "-ls", help = "Show ls command on dest directory", default = False, action = "store_true") ################################################################################################################################################ - parser.add_argument("--RemovePlugin", "-rmv", help = "Name plugin to remove. Names must match.", type = str, default = "") + parser.add_argument("--AddPlugin", "-ap", help = "Path to new plugin.", type = str, default = "") + parser.add_argument("--RemovePlugin", "-rp", help = "Name plugin to remove. Names must match.", type = str, default = "") parser.add_argument("--UpgradePlugin", "-upg", help = "Path of file to uprade. Names must match.", type = str, default = "") - parser.add_argument("--NoReloadPlugins", "-nrp", help = "Upgrade plugins without send sm plugins refresh command", default = False, action = "store_true"); + parser.add_argument("--NoReloadPlugins", "-nrp", help = "Upgrade plugins without send sm plugins refresh command", default = False, action = "store_true") ################################################################################################################################################ args = parser.parse_args() ALL_YES = 1 if args.yes else 0 @@ -536,6 +632,14 @@ if __name__ == "__main__": manager.execute("rcon", command) sys.exit(0) ################################## + if args.add_map_config: + append_line = "" + for word in args.add_map_config: + append_line += word + " " + append_line = append_line[:-1] + manager.execute("add2map_config", append_line) + sys.exit(0) + ################################## if args.netstatus: manager.execute("net_status_json") sys.exit(0) @@ -544,6 +648,10 @@ if __name__ == "__main__": manager.execute("status") sys.exit(0) ################################## + if args.map: + manager.execute("map") + sys.exit(0) + ################################## if args.ShowDir: if args.DestDir: manager.execute("show_directory", args.DestDir) @@ -568,6 +676,14 @@ if __name__ == "__main__": o.error("Invalid path of source file!") sys.exit(1) ################################### + if args.AddPlugin: + if check_file(args.AddPlugin): + manager.execute("add_plugin", args.AddPlugin, not args.NoReloadPlugins) + sys.exit(0) + else: + o.error("Invalid path of upgraded plugin!") + sys.exit(1) + ################################### if args.UpgradePlugin: if check_file(args.UpgradePlugin): manager.execute("upgrade_plugin", args.UpgradePlugin, not args.NoReloadPlugins)