Created
October 2, 2025 18:51
-
-
Save FelixWolf/aff299e5fc8f62aa860664fd6195dd66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import tarfile | |
| import glob | |
| import os | |
| import tempfile | |
| import urllib.request | |
| import platform | |
| import time | |
| def installMetamod(mmsource, addonsdir): | |
| #Metamod should overwrite all files | |
| with tarfile.open(mmsource, "r:gz") as tar: | |
| for file in tar: | |
| if file.isfile(): | |
| spath = file.name | |
| epath = os.path.join(addonsdir, spath.split("/",1)[-1]) | |
| if os.path.exists(epath): | |
| # Don't overwrite configurations | |
| if spath.endswith((".vdf", ".ini", ".txt")): | |
| continue | |
| else: | |
| os.makedirs(os.path.split(epath)[0], exist_ok=True) | |
| tf = tar.extractfile(file) | |
| with open(epath, "wb") as f: | |
| print("Updating {}".format(spath)) | |
| f.write(tf.read()) | |
| def installSourcemod(sourcemod, addonsdir): | |
| with tarfile.open(sourcemod, "r:gz") as tar: | |
| for file in tar: | |
| if file.isfile(): | |
| spath = file.name | |
| epath = os.path.join(addonsdir, spath.split("/",1)[-1]) | |
| updateFile = False | |
| if spath.startswith(( | |
| "addons/metamod/", | |
| "addons/sourcemod/bin/", | |
| "addons/sourcemod/extensions/", | |
| "addons/sourcemod/gamedata/", | |
| "addons/sourcemod/translations/", | |
| "addons/sourcemod/plugins/", | |
| )): | |
| updateFile = True | |
| else: | |
| # Create new files if they don't exist | |
| if not os.path.exists(epath): | |
| updateFile = True | |
| if not spath.startswith("addons/"): | |
| updateFile = False | |
| if updateFile: | |
| os.makedirs(os.path.split(epath)[0], exist_ok=True) | |
| tf = tar.extractfile(file) | |
| with open(epath, "wb") as f: | |
| print("Updating {}".format(spath)) | |
| f.write(tf.read()) | |
| def downloadMetamod(version, osys = None, useCached = True, cacheAge = 60 * 60 *24): | |
| if osys == None: | |
| if platform.system() == "Windows": | |
| osys = "windows" | |
| elif platform.system() == "Linux": | |
| osys = "linux" | |
| elif platform.system() == "Darwin": | |
| osys = "mac" | |
| path = os.path.join(tempfile.gettempdir(), "mmsource-{}-latest-{}".format(version, osys)) | |
| if os.path.exists(path): | |
| mtime = os.path.getmtime(path) | |
| current_time = time.time() | |
| if current_time - mtime < cacheAge: | |
| return path | |
| url = "https://mms.alliedmods.net/mmsdrop/{}/".format(version) | |
| req = urllib.request.Request(url + "mmsource-latest-{}".format(osys), | |
| headers={"user-agent": "Server update"}) | |
| with urllib.request.urlopen(req) as res: | |
| req = urllib.request.Request(url + res.read().decode(), headers={"user-agent": "Server update"}) | |
| with urllib.request.urlopen(req) as res: | |
| with open(path, "wb") as f: | |
| f.write(res.read()) | |
| return path | |
| def downloadSourcemod(version, osys = None, useCached = True, cacheAge = 60 * 60 *24): | |
| if osys == None: | |
| if platform.system() == "Windows": | |
| osys = "windows" | |
| elif platform.system() == "Linux": | |
| osys = "linux" | |
| elif platform.system() == "Darwin": | |
| osys = "mac" | |
| path = os.path.join(tempfile.gettempdir(), "sourcemod-{}-latest-{}".format(version, osys)) | |
| if os.path.exists(path): | |
| mtime = os.path.getmtime(path) | |
| current_time = time.time() | |
| if current_time - mtime < cacheAge: | |
| return path | |
| url = "https://sm.alliedmods.net/smdrop/{}/".format(version) | |
| req = urllib.request.Request(url + "sourcemod-latest-{}".format(osys), | |
| headers={"user-agent": "Server update"}) | |
| with urllib.request.urlopen(req) as res: | |
| req = urllib.request.Request(url + res.read().decode(), headers={"user-agent": "Server update"}) | |
| with urllib.request.urlopen(req) as res: | |
| with open(path, "wb") as f: | |
| f.write(res.read()) | |
| return path | |
| def main(): | |
| import argparse | |
| parser = argparse.ArgumentParser( | |
| description='Metamod + Sourcemod updater' | |
| ) | |
| parser.add_argument("-m", "--metamod", | |
| help="Metamod version to update or install", | |
| default=None | |
| ) | |
| parser.add_argument("-s", "--sourcemod", | |
| help="Sourcemod version to update or install", | |
| default=None | |
| ) | |
| parser.add_argument("-a", "--addonsdir", | |
| help="Addons directory", | |
| default=os.getcwd() | |
| ) | |
| args = parser.parse_args() | |
| if args.metamod: | |
| install = downloadMetamod(args.metamod) | |
| installMetamod(install, args.addonsdir) | |
| if args.sourcemod: | |
| install = downloadSourcemod(args.sourcemod) | |
| installSourcemod(install, args.addonsdir) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment