Minecraft fixes (#388)

This commit is contained in:
black-sliver 2022-04-02 04:49:27 +02:00 committed by GitHub
parent 938ab32cda
commit a93b3d79aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 16 deletions

View File

@ -58,9 +58,9 @@ def open_patch():
suffixes += c.file_identifier.suffixes if c.type == Type.CLIENT and \ suffixes += c.file_identifier.suffixes if c.type == Type.CLIENT and \
isinstance(c.file_identifier, SuffixIdentifier) else [] isinstance(c.file_identifier, SuffixIdentifier) else []
filename = tkinter.filedialog.askopenfilename(filetypes=(('Patches', ' '.join(suffixes)),)) filename = tkinter.filedialog.askopenfilename(filetypes=(('Patches', ' '.join(suffixes)),))
file, component = identify(filename) file, _, component = identify(filename)
if file and component: if file and component:
subprocess.Popen([*get_exe(component), file]) launch([*get_exe(component), file], component.cli)
def browse_files(): def browse_files():
@ -142,7 +142,7 @@ components: Iterable[Component] = (
# Factorio # Factorio
Component('Factorio Client', 'FactorioClient'), Component('Factorio Client', 'FactorioClient'),
# Minecraft # Minecraft
Component('Minecraft Client', 'MinecraftClient', icon='mcicon', Component('Minecraft Client', 'MinecraftClient', icon='mcicon', cli=True,
file_identifier=SuffixIdentifier('.apmc')), file_identifier=SuffixIdentifier('.apmc')),
# Ocarina of Time # Ocarina of Time
Component('OoT Client', 'OoTClient', Component('OoT Client', 'OoTClient',
@ -165,11 +165,11 @@ icon_paths = {
def identify(path: Union[None, str]): def identify(path: Union[None, str]):
if path is None: if path is None:
return None, None return None, None, None
for component in components: for component in components:
if component.handles_file(path): if component.handles_file(path):
return path, component.script_name return path, component.script_name, component
return (None, None) if '/' in path or '\\' in path else (None, path) return (None, None, None) if '/' in path or '\\' in path else (None, path, None)
def get_exe(component: Union[str, Component]) -> Optional[Sequence[str]]: def get_exe(component: Union[str, Component]) -> Optional[Sequence[str]]:
@ -284,7 +284,7 @@ def main(args: Optional[Union[argparse.Namespace, dict]] = None):
args = {} args = {}
if "Patch|Game|Component" in args: if "Patch|Game|Component" in args:
file, component = identify(args["Patch|Game|Component"]) file, component, _ = identify(args["Patch|Game|Component"])
if file: if file:
args['file'] = file args['file'] = file
if component: if component:

View File

@ -2,6 +2,7 @@ import argparse
import os, sys import os, sys
import re import re
import atexit import atexit
import shutil
from subprocess import Popen from subprocess import Popen
from shutil import copyfile from shutil import copyfile
from time import strftime from time import strftime
@ -16,6 +17,7 @@ atexit.register(input, "Press enter to exit.")
# 1 or more digits followed by m or g, then optional b # 1 or more digits followed by m or g, then optional b
max_heap_re = re.compile(r"^\d+[mMgG][bB]?$") max_heap_re = re.compile(r"^\d+[mMgG][bB]?$")
forge_version = "1.17.1-37.1.1" forge_version = "1.17.1-37.1.1"
is_windows = sys.platform in ("win32", "cygwin", "msys")
def prompt_yes_no(prompt): def prompt_yes_no(prompt):
@ -158,10 +160,16 @@ def find_jdk_dir() -> str:
# get the java exe location # get the java exe location
def find_jdk() -> str: def find_jdk() -> str:
if is_windows:
jdk = find_jdk_dir() jdk = find_jdk_dir()
jdk_exe = os.path.join(jdk, "bin", "java.exe") jdk_exe = os.path.join(jdk, "bin", "java.exe")
if os.path.isfile(jdk_exe): if os.path.isfile(jdk_exe):
return jdk_exe return jdk_exe
else:
jdk_exe = shutil.which(options["minecraft_options"].get("java", "java"))
if not jdk_exe:
raise Exception("Could not find Java. Is Java installed on the system?")
return jdk_exe
# Download Corretto 16 (Amazon JDK) # Download Corretto 16 (Amazon JDK)
@ -203,7 +211,7 @@ def install_forge(directory: str):
f.write(resp.content) f.write(resp.content)
print(f"Installing Forge...") print(f"Installing Forge...")
argstring = ' '.join([jdk, "-jar", "\"" + forge_install_jar+ "\"", "--installServer", "\"" + directory + "\""]) argstring = ' '.join([jdk, "-jar", "\"" + forge_install_jar+ "\"", "--installServer", "\"" + directory + "\""])
install_process = Popen(argstring) install_process = Popen(argstring, shell=not is_windows)
install_process.wait() install_process.wait()
os.remove(forge_install_jar) os.remove(forge_install_jar)
@ -220,7 +228,8 @@ def run_forge_server(forge_dir: str, heap_arg):
heap_arg = heap_arg[:-1] heap_arg = heap_arg[:-1]
heap_arg = "-Xmx" + heap_arg heap_arg = "-Xmx" + heap_arg
args_file = os.path.join(forge_dir, "libraries", "net", "minecraftforge", "forge", forge_version, "win_args.txt") os_args = "win_args.txt" if is_windows else "unix_args.txt"
args_file = os.path.join(forge_dir, "libraries", "net", "minecraftforge", "forge", forge_version, os_args)
win_args = [] win_args = []
with open(args_file) as argfile: with open(args_file) as argfile:
for line in argfile: for line in argfile:
@ -229,7 +238,7 @@ def run_forge_server(forge_dir: str, heap_arg):
argstring = ' '.join([java_exe, heap_arg] + win_args + ["-nogui"]) argstring = ' '.join([java_exe, heap_arg] + win_args + ["-nogui"])
logging.info(f"Running Forge server: {argstring}") logging.info(f"Running Forge server: {argstring}")
os.chdir(forge_dir) os.chdir(forge_dir)
return Popen(argstring) return Popen(argstring, shell=not is_windows)
if __name__ == '__main__': if __name__ == '__main__':
@ -252,13 +261,19 @@ if __name__ == '__main__':
max_heap = options["minecraft_options"]["max_heap_size"] max_heap = options["minecraft_options"]["max_heap_size"]
if args.install: if args.install:
if is_windows:
print("Installing Java and Minecraft Forge") print("Installing Java and Minecraft Forge")
download_java() download_java()
else:
print("Installing Minecraft Forge")
install_forge(forge_dir) install_forge(forge_dir)
sys.exit(0) sys.exit(0)
if apmc_file is not None and not os.path.isfile(apmc_file): if apmc_file is not None and not os.path.isfile(apmc_file):
raise FileNotFoundError(f"Path {apmc_file} does not exist or could not be accessed.") raise FileNotFoundError(f"Path {apmc_file} does not exist or could not be accessed.")
if not os.path.isdir(forge_dir):
if prompt_yes_no("Did not find forge directory. Download and install forge now?"):
install_forge(forge_dir)
if not os.path.isdir(forge_dir): if not os.path.isdir(forge_dir):
raise NotADirectoryError(f"Path {forge_dir} does not exist or could not be accessed.") raise NotADirectoryError(f"Path {forge_dir} does not exist or could not be accessed.")
if not max_heap_re.match(max_heap): if not max_heap_re.match(max_heap):