Created
June 10, 2017 23:19
-
-
Save Aareon/e80246edf21a4de0be506e9489e9694f 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
| import json | |
| import subprocess | |
| import asyncio | |
| import aiohttp | |
| import os | |
| from os.path import expanduser | |
| home = expanduser("~") | |
| def url2filename(url): | |
| return url.split('/')[-1] | |
| async def download(session, url): | |
| async with session.get(url) as response: | |
| assert response.status == 200 | |
| chunk = await response.read() | |
| with open(directory + url2filename(url), "wb") as f: | |
| f.write(chunk) | |
| print('[Download] Finished', url2filename(url)) | |
| class Launcher: | |
| def __init__(self, base: str, version: int): | |
| self.base = base | |
| self.version = base + "versions/" + str(version) + "/" + str(version) | |
| with open(self.version + ".json", 'r') as content_file: | |
| content = content_file.read() | |
| self.config = json.loads(content) | |
| self.libraries = [] | |
| self.urls = [] | |
| def get_urls(self, os: str="linux"): | |
| for library in self.config["libraries"]: | |
| if "rules" in library: | |
| rules = library["rules"] | |
| download = "deny" | |
| for rule in rules: | |
| if "os" in rule: | |
| if rule["os"]["name"] == os: | |
| download = rule["action"] | |
| else: | |
| download = rule["action"] | |
| if download != "allow": | |
| continue | |
| file = library["downloads"] | |
| if "artifact" in file: | |
| download = file["artifact"] | |
| elif "natives" in library: | |
| natives = library["natives"] | |
| if os not in natives: | |
| continue | |
| native = natives[os] | |
| download = file["classifiers"][native] | |
| else: | |
| continue | |
| url = download["url"].replace("https://libraries.minecraft.net/", "") | |
| self.libraries.append(self.base + "libraries/" + url) | |
| self.urls.append("https://libraries.minecraft.net/" + url) | |
| self.libraries.append(self.version + ".jar") | |
| return self.libraries, self.urls | |
| def build(self, uuid: str, username: str, access_token: str): | |
| cp = ":".join(self.libraries) | |
| command = ["java", "-Djava.library.path=/home/jscob/.minecraft/versions/1.12/1.12-natives-10067959929778", | |
| "-cp", cp, "net.minecraft.client.main.Main", "--version", str(self.version), "--gameDir", self.base, | |
| "--assetsDir", (self.base + "assets"), "--uuid", uuid, "--username", username, "--accessToken", | |
| access_token] | |
| return command | |
| def start(self, command): | |
| p = subprocess.Popen(command, stdout=subprocess.PIPE) | |
| output, err = p.communicate() | |
| return output | |
| directory = 'libraries/' | |
| if not os.path.exists(directory): | |
| os.makedirs(directory) | |
| launcher = Launcher(base='C:\\Users\\spork\\AppData\\Roaming\\.minecraft\\',version='1.12') | |
| libraries, urls = launcher.get_urls(os='windows') | |
| loop = asyncio.get_event_loop() | |
| for url in urls: | |
| print('[Download] Downloading ' + url2filename(url)) | |
| with aiohttp.ClientSession(loop=loop) as session: | |
| response = loop.run_until_complete(download(session, url=url)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment