Add config

This commit is contained in:
Fierelier 2021-03-28 01:38:06 +01:00
parent 47f9336089
commit 37bec841fd
2 changed files with 52 additions and 28 deletions

12
minecraftLauncher.ini Normal file
View File

@ -0,0 +1,12 @@
[default]
# Set the path to the java executable. Don't add a file extension.
java=java
# Set the arguments to launch minecraft with. Formatted in json. Use [] for none.
jvmArguments = ["-Xms128M","-Xmx800M"]
# Enable/disable console.
console=1
# Override the OS name. Choose windows, linux or macos, if the detection doesn't work right.
osName=

View File

@ -17,16 +17,11 @@ else:
s = os.path.realpath(__file__) s = os.path.realpath(__file__)
sp = pUp(s) sp = pUp(s)
import configparser
import subprocess import subprocess
import json import json
import hashlib import hashlib
import platform import platform
p = os.path.join
os.environ["appdata"] = p(sp,"appdata")
java = p(sp,"java","bin","java.exe")
jvmArguments = ["-Xms256M","-Xmx800M"]
osName = platform.system().lower()
if osName == "darwin": osName = "macos"
def readJsonFile(file): def readJsonFile(file):
fileh = open(file,"r") fileh = open(file,"r")
@ -35,61 +30,69 @@ def readJsonFile(file):
return json.loads(data) return json.loads(data)
def main(): def main():
global version config = configparser.ConfigParser()
global name config.read(os.path.splitext(s)[0] + ".ini")
global osName os.environ["appdata"] = p(sp,"appdata")
lv = config["default"]
if lv["osName"] == "":
lv["osName"] = platform.system().lower()
if lv["osName"] == "darwin": lv["osName"] = "macos"
if len(sys.argv) > 1: if len(sys.argv) > 1:
for arg in sys.argv[1:]: for arg in sys.argv[1:]:
argSplit = arg.split("=",1) argSplit = arg.split("=",1)
if len(argSplit) > 1: if len(argSplit) > 1:
globals()[argSplit[0]] = argSplit[1] lv[argSplit[0]] = argSplit[1]
else:
lv[argSplit[0]] = True
if not osName in ["windows","linux","macos"]: if not lv["osName"] in ["windows","linux","macos"]:
print("Warning, unsupported OS detected: " +osName) print("Warning, unsupported OS detected: " +lv["osName"])
print("Needs to be either windows, linux or macos. Define it with osName=name in the commandline.") print("Needs to be either windows, linux or macos. Define it with osName=name in the config.")
print("") print("")
if not "version" in globals(): version = input("Version ID: ") if not "version" in lv: lv["version"] = input("Version ID: ")
if not "name" in globals(): name = input("Player name: ") if not "name" in lv: lv["name"] = input("Player name: ")
gamePath = p(os.environ["appdata"],".minecraft") gamePath = p(os.environ["appdata"],".minecraft")
versionPath = p(gamePath,"versions",version) versionPath = p(gamePath,"versions",lv["version"])
libraryPath = p(gamePath,"libraries") libraryPath = p(gamePath,"libraries")
nativesPath = p(gamePath,"natives") nativesPath = p(gamePath,"natives")
nativesOutPath = p(versionPath,"natives") nativesOutPath = p(versionPath,"natives-" +lv["osName"])
assetsPath = p(gamePath,"assets") assetsPath = p(gamePath,"assets")
print("Extracting natives...") print("Extracting natives...")
clientJson = readJsonFile(p(versionPath,version + ".json")) clientJson = readJsonFile(p(versionPath,lv["version"] + ".json"))
libraryList = "" libraryList = ""
separator = ";" separator = ";"
if osName != "windows": separator = ":" if lv["osName"] != "windows": separator = ":"
for library in clientJson["libraries"]: for library in clientJson["libraries"]:
if "artifact" in library["downloads"]: if "artifact" in library["downloads"]:
library["downloads"]["artifact"]["path"] = library["downloads"]["artifact"]["path"].replace("/",os.path.sep) library["downloads"]["artifact"]["path"] = library["downloads"]["artifact"]["path"].replace("/",os.path.sep)
libraryList += p(libraryPath,library["downloads"]["artifact"]["path"]) + separator libraryList += p(libraryPath,library["downloads"]["artifact"]["path"]) + separator
if "classifiers" in library["downloads"]: if "classifiers" in library["downloads"]:
if "natives-" +osName in library["downloads"]["classifiers"]: if "natives-" +lv["osName"] in library["downloads"]["classifiers"]:
native = library["downloads"]["classifiers"]["natives-" +osName] native = library["downloads"]["classifiers"]["natives-" +lv["osName"]]
native["path"] = native["path"].replace("/",os.path.sep) native["path"] = native["path"].replace("/",os.path.sep)
if not os.path.isdir(nativesOutPath): os.makedirs(nativesOutPath) if not os.path.isdir(nativesOutPath): os.makedirs(nativesOutPath)
subprocess.run(["7z","x",p(nativesPath,native["path"]),"-o" +nativesOutPath,"-aos"],check=True,stdout=subprocess.DEVNULL) subprocess.run(["7z","x",p(nativesPath,native["path"]),"-o" +nativesOutPath,"-aos"],check=True,stdout=subprocess.DEVNULL)
libraryList += p(versionPath,version + ".jar") libraryList += p(versionPath,lv["version"] + ".jar")
#print(libraryList) #print(libraryList)
launcherVariables = {} launcherVariables = {}
launcherVariables["auth_player_name"] = name launcherVariables["auth_player_name"] = lv["name"]
launcherVariables["version_name"] = version launcherVariables["version_name"] = lv["version"]
launcherVariables["game_directory"] = gamePath launcherVariables["game_directory"] = gamePath
launcherVariables["assets_root"] = assetsPath launcherVariables["assets_root"] = assetsPath
launcherVariables["assets_index_name"] = clientJson["assets"] launcherVariables["assets_index_name"] = clientJson["assets"]
launcherVariables["auth_access_token"] = "-" launcherVariables["auth_access_token"] = "-"
launcherVariables["auth_uuid"] = hashlib.md5(name.encode('utf-8')).hexdigest() launcherVariables["auth_uuid"] = hashlib.md5(lv["name"].encode('utf-8')).hexdigest()
launcherVariables["user_type"] = "offline" launcherVariables["user_type"] = "offline"
launcherVariables["version_type"] = clientJson["type"] launcherVariables["version_type"] = clientJson["type"]
args = jvmArguments args = json.loads(lv["jvmArguments"])
args.append("-Djava.library.path=" +nativesOutPath) args.append("-Djava.library.path=" +nativesOutPath)
args.append("-cp") args.append("-cp")
args.append(libraryList) args.append(libraryList)
@ -110,7 +113,16 @@ def main():
args.append(arg) args.append(arg)
print("Launching Minecraft...") print("Launching Minecraft...")
subprocess.call([java] + args) if lv["console"] == "1":
subprocess.run([lv["java"]] + args,check = True)
else:
pkwargs = {
"stdout": subprocess.DEVNULL,
"stdin": subprocess.DEVNULL,
"stderr": subprocess.DEVNULL,
"close_fds": True
}
if lv["osName"] == "windows": pkwargs["creationflags"] = 0x00000008
subprocess.Popen([lv["java"] + "w"] + args,**pkwargs)
main() main()