|
| 1 | +from os.path import exists |
| 2 | +from json import loads |
| 3 | +import zipfile |
| 4 | + |
| 5 | + |
| 6 | +class JavaMinecraftArgumentsBuilder(): |
| 7 | + def __init__(self, mcDir: str, version: str, javawPath: str, maxMem: str, userName: str, width: str, height: str): |
| 8 | + self.McDir = mcDir |
| 9 | + self.Version = version |
| 10 | + self.JavawPath = javawPath |
| 11 | + self.MaxMem = maxMem |
| 12 | + self.UserName = userName |
| 13 | + self.Width = width |
| 14 | + self.Height = height |
| 15 | + |
| 16 | + def unpress(self, filename: str, path: str): |
| 17 | + Zip = zipfile.ZipFile(filename) |
| 18 | + for z in Zip.namelist(): |
| 19 | + Zip.extract(z, path) |
| 20 | + Zip.close() |
| 21 | + |
| 22 | + def isMyversion(self, version: str, mcdir: str): |
| 23 | + if (exists(f"{mcdir}\\versions\\{version}\\{version}.json")): |
| 24 | + return True |
| 25 | + else: |
| 26 | + return False |
| 27 | + |
| 28 | + def Launch(self) -> str: |
| 29 | + commandLine = str("") |
| 30 | + JVM = str("") |
| 31 | + classPath = str("") |
| 32 | + mcArgs = str("") |
| 33 | + |
| 34 | + if ((not self.JavawPath == "") |
| 35 | + and (not self.Version == "") |
| 36 | + and (not self.MaxMem == "") |
| 37 | + and (not self.UserName == "") |
| 38 | + and (not self.McDir == "")): |
| 39 | + if (self.isMyversion(self.Version, self.McDir)): |
| 40 | + json = open( |
| 41 | + f"{self.McDir}\\versions\\{self.Version}\\{self.Version}.json", "r") |
| 42 | + dic = loads(json.read()) |
| 43 | + json.close() |
| 44 | + for lib in dic["libraries"]: |
| 45 | + if "classifiers" in lib['downloads']: |
| 46 | + for native in lib['downloads']: |
| 47 | + if native == "artifact": |
| 48 | + path = f"{self.McDir}\\versions\\{self.Version}\\natives" |
| 49 | + filePath = f"{self.McDir}\\libraries\\{lib['downloads'][native]['path']}" |
| 50 | + try: |
| 51 | + self.unpress(filePath, path) |
| 52 | + except: |
| 53 | + pass |
| 54 | + elif native == 'classifiers': |
| 55 | + for n in lib['downloads'][native].values(): |
| 56 | + path = f"{self.McDir}\\versions\\{self.Version}\\natives" |
| 57 | + filePath = f'{self.McDir}\\libraries\\{n["path"]}' |
| 58 | + try: |
| 59 | + self.unpress(filePath, path) |
| 60 | + except: |
| 61 | + pass |
| 62 | + JVM = '"' + self.JavawPath + '" -XX:+UseG1GC -XX:-UseAdaptiveSizePolicy' +\ |
| 63 | + ' -XX:-OmitStackTraceInFastThrow -Dfml.ignoreInvalidMinecraftCertificates=True ' +\ |
| 64 | + '-Dfml.ignorePatchDiscrepancies=True -Dlog4j2.formatMsgNoLookups=true ' +\ |
| 65 | + '-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump ' +\ |
| 66 | + '-Dos.name="Windows 10" -Dos.version=10.0 -Djava.library.path="' +\ |
| 67 | + self.McDir + "\\versions\\" + self.Version + "\\" + "natives" +\ |
| 68 | + '" -Dminecraft.launcher.brand=launcher ' +\ |
| 69 | + '-Dminecraft.launcher.version=1.0.0 -cp' |
| 70 | + classPath += '"' |
| 71 | + for lib in dic["libraries"]: |
| 72 | + if not 'classifiers' in lib["downloads"]: |
| 73 | + normal = f'{self.McDir}\\libraries\\{lib["downloads"]["artifact"]["path"]}' |
| 74 | + classPath += normal + ";" |
| 75 | + classPath = f'{classPath}{self.McDir}\\versions\\{self.Version}\\{self.Version}.jar"' |
| 76 | + JVM = f"{JVM} {classPath} -Xmx{self.MaxMem} -Xmn256m -Dlog4j.formatMsgNoLookups=true" |
| 77 | + |
| 78 | + mcArgs += dic["mainClass"] + " " |
| 79 | + if "minecraftArguments" in dic: |
| 80 | + mcArgs += dic["minecraftArguments"] |
| 81 | + mcArgs = mcArgs.replace( |
| 82 | + "${auth_player_name}", self.UserName) # 玩家名称 |
| 83 | + mcArgs = mcArgs.replace( |
| 84 | + "${version_name}", self.Version) # 版本名称 |
| 85 | + mcArgs = mcArgs.replace( |
| 86 | + "${game_directory}", self.McDir) # mc路径 |
| 87 | + mcArgs = mcArgs.replace( |
| 88 | + "${assets_root}", self.McDir + "\\assets") # 资源文件路径 |
| 89 | + mcArgs = mcArgs.replace( |
| 90 | + "${assets_index_name}", dic["assetIndex"]["id"]) # 资源索引文件名称 |
| 91 | + mcArgs = mcArgs.replace( |
| 92 | + "${auth_uuid}", "{}") # 由于没有写微软登录,所以uuid为空的 |
| 93 | + mcArgs = mcArgs.replace( |
| 94 | + "${auth_access_token}", "{}") # 同上 |
| 95 | + mcArgs = mcArgs.replace("${clientid}", self.Version) # 客户端id |
| 96 | + mcArgs = mcArgs.replace("${auth_xuid}", "{}") # 离线登录,不填 |
| 97 | + mcArgs = mcArgs.replace( |
| 98 | + "${user_type}", "Legacy") # 用户类型,离线模式是Legacy |
| 99 | + mcArgs = mcArgs.replace( |
| 100 | + "${version_type}", dic["type"]) # 版本类型 |
| 101 | + mcArgs = mcArgs.replace("${user_properties}", "{}") |
| 102 | + mcArgs += f"--width {self.Width}" |
| 103 | + mcArgs += f" --height {self.Height}" |
| 104 | + else: |
| 105 | + for arg in dic["arguments"]["game"]: |
| 106 | + if isinstance(arg, str): |
| 107 | + mcArgs += arg + " " |
| 108 | + elif isinstance(arg, dict): |
| 109 | + if isinstance(arg["value"], list): |
| 110 | + for a in arg["value"]: |
| 111 | + mcArgs += a + " " |
| 112 | + elif isinstance(arg["value"], str): |
| 113 | + mcArgs += arg["value"] + " " |
| 114 | + |
| 115 | + mcArgs = mcArgs.replace( |
| 116 | + "${auth_player_name}", self.UserName) # 玩家名称 |
| 117 | + mcArgs = mcArgs.replace( |
| 118 | + "${version_name}", self.Version) # 版本名称 |
| 119 | + mcArgs = mcArgs.replace( |
| 120 | + "${game_directory}", self.McDir) # mc路径 |
| 121 | + mcArgs = mcArgs.replace( |
| 122 | + "${assets_root}", self.McDir + "\\assets") # 资源文件路径 |
| 123 | + mcArgs = mcArgs.replace( |
| 124 | + "${assets_index_name}", dic["assetIndex"]["id"]) # 资源索引文件名称 |
| 125 | + mcArgs = mcArgs.replace( |
| 126 | + "${auth_uuid}", "{}") # 由于没有写微软登录,所以uuid为空的 |
| 127 | + mcArgs = mcArgs.replace( |
| 128 | + "${auth_access_token}", "{}") # 同上 |
| 129 | + mcArgs = mcArgs.replace("${clientid}", self.Version) # 客户端id |
| 130 | + mcArgs = mcArgs.replace("${auth_xuid}", "{}") # 离线登录,不填 |
| 131 | + mcArgs = mcArgs.replace( |
| 132 | + "${user_type}", "Legacy") # 用户类型,离线模式是Legacy |
| 133 | + mcArgs = mcArgs.replace( |
| 134 | + "${version_type}", dic["type"]) # 版本类型 |
| 135 | + mcArgs = mcArgs.replace( |
| 136 | + "${resolution_width}", self.Width) # 窗口宽度 |
| 137 | + mcArgs = mcArgs.replace( |
| 138 | + "${resolution_height}", self.Height) # 窗口高度 |
| 139 | + mcArgs = mcArgs.replace("-demo ", "") # 去掉-demo参数,退出试玩版 |
| 140 | + |
| 141 | + commandLine = JVM + " " + mcArgs |
| 142 | + return commandLine |
0 commit comments