@@ -6,6 +6,7 @@ from PIL import ImageTk, Image
66from urllib .request import urlretrieve
77from urllib .error import *
88from zipfile import ZipFile
9+ import tkinter
910import os
1011import json
1112import uuid
@@ -27,8 +28,10 @@ import logging
2728import socket
2829import random
2930import platform
31+ import psutil
32+ import math
3033
31- VERSION = "0.10 "
34+ VERSION = "0.11 "
3235
3336def send_error_report (prog , fatal = False ):
3437 try :
@@ -45,7 +48,7 @@ def send_error_report(prog, fatal=False):
4548 if messagebox .askyesno ("Report issue" , f"Would you like to automatically open an issue?" ):
4649 newline = "\n "
4750 body = f"(insert description of bug here)%0A***%0ALog contents:%0A```%0A{ tb .replace (newline , '%0A' )} %0A```"
48- url = f"https://github.com/SpacePython12/AP-Launcher/issues/new?"
51+ url = f"https://github.com/SpacePython12/AP-Launcher/issues/new?body= { body } "
4952 webbrowser .open (url )
5053
5154class AboutPage (Frame ):
@@ -124,7 +127,7 @@ class App:
124127 os .remove ("launcher_process_old.exe" )
125128 logger .info ("Update files cleaned up." )
126129 except :
127- logger .info ("No update files found" )
130+ logger .info ("No update files found. " )
128131 pass
129132 logger .info ("Downloading background and icon..." )
130133 try :
@@ -143,24 +146,28 @@ class App:
143146 self .win .iconphoto (True , self .icon )
144147 self .background2 = Canvas (self .mainframe , width = self .background .width (), height = self .background .height ())
145148 self .background2 .grid (column = 0 , row = 0 , sticky = "nsew" )
146- self .win .bind ("<Configure>" , lambda e : threading .Thread (None , target = lambda : self .resize_widgets (e )).start ())
149+ self .background2 .bind ("<Configure>" , lambda e : threading .Thread (None , target = lambda : self .resize_widgets ()).start ())
147150 self .versionvar = StringVar ()
148151 self .get_versions ()
149152 logger .info ("Reading cache file..." )
150153 try :
151154 self .cache = json .load (open ("cache.json" ))
155+ self .cache ["launcherVersion" ] = VERSION
156+ json .dump (self .cache , open ("cache.json" , "w" ))
152157 logger .info ("Successfully read cache." )
153158 except FileNotFoundError :
154159 self .cache = {
160+ "launcherVersion" : VERSION ,
155161 "username" : "" ,
156162 "accessid" : {
157163 "id" : None ,
158164 "expiresAt" : None
159165 },
160- "premium" : True ,
166+ "premium" : False ,
161167 "selectedVersion" : None
162168 }
163169 logger .info ("No cache file found." )
170+ json .dump (self .cache , open ("cache.json" , "w" ))
164171 if type (self .cache ["selectedVersion" ]) is type (list ()):
165172 self .versionvar .set (f'{ self .cache ["selectedVersion" ][0 ]} ({ self .cache ["selectedVersion" ][1 ]} )' )
166173 elif type (self .cache ["selectedVersion" ]) is type (None ):
@@ -233,27 +240,57 @@ class App:
233240 java_home = os .path .join (os .getcwd (), "java" , [x [1 ] for x in os .walk ("java" )][0 ][0 ])
234241 self .profjavadir = LabeledEntry (self .profileframe , "Java Directory: " , f'{ java_home } \\ bin\\ javaw.exe' , elength = 30 )
235242 self .profjavadir .grid (column = 0 , row = 3 , sticky = "nsew" )
236- self .profjavargs = LabeledEntry (self .profileframe , "JVM Arguments: " , "-Xmx2G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M" , elength = 50 )
237- self .profjavargs .grid (column = 0 , row = 4 , sticky = "nsew" )
243+ self .jvmargs = "-Xmx2048M"
244+ self .allocramraw = re .search (r"\-Xmx[0-9]+[MGT]" , self .jvmargs ).group (0 )[4 :]
245+ if self .allocramraw [- 1 ] == "M" :
246+ self .scale = 0
247+ elif self .allocramraw [- 1 ] == "G" :
248+ self .scale = 1
249+ elif self .allocramraw [- 1 ] == "T" :
250+ self .scale = 2
251+ self .allocram = DoubleVar (self .win )
252+ self .allocram .set ((float (self .allocramraw [:- 1 ]) * (1024 ** (self .scale - 1 ))))
253+ self .allocramlabel = Label (self .profileframe , text = f"Allocated RAM: { self .allocram .get ()} GB" )
254+ self .allocramlabel .grid (column = 0 , row = 4 , sticky = "nsew" )
255+ self .allocramslider = tkinter .Scale (self .profileframe , from_ = 1.0 * (1024 ** (self .scale )), to = int ((math .ceil (math .log (psutil .virtual_memory ().total , 1024 )))) * (1024 ** (self .scale )), resolution = 0.1 , showvalue = False , orient = "horizontal" , variable = self .allocram , command = self .update_alloc_ram , length = 200 )
256+ self .allocramslider .grid (column = 0 , row = 5 , sticky = "nw" )
238257 self .profsave = Button (self .profileframe , text = "Save" )
239- self .profsave .grid (column = 0 , row = 5 , sticky = "w" )
258+ self .profsave .grid (column = 0 , row = 6 , sticky = "w" )
240259 self .proftrans = Label (self .profileframe , text = "OR" )
241- self .proftrans .grid (column = 0 , row = 6 , sticky = "w" )
260+ self .proftrans .grid (column = 0 , row = 7 , sticky = "w" )
242261 self .profadd = Button (self .profileframe , text = "Import new version" , command = lambda : self .open_install_archive ())
243- self .profadd .grid (column = 0 , row = 7 , sticky = "w" )
262+ self .profadd .grid (column = 0 , row = 8 , sticky = "w" )
244263 self .update_profiles (self .versionvar .get ())
245264 self .profilelist .bind ("<<ComboboxSelected>>" , lambda x : self .update_profiles (self .versionvar .get ()))
246265 self .aboutpage = AboutPage (self .win )
247266 self .aboutpage .columnconfigure (0 , weight = 1 )
248267 self .tabs .add (self .aboutpage , text = "About" )
249268 logger .info ("Successfully initiated window." )
250269
251- def resize_widgets (self , e , override = False ):
252- if ((self .win .winfo_width () != e .width ) and (self .win .winfo_height () != e .height )) or override :
253- self .bgfile = Image .open ("assets/background.png" )
254- self .bgfile = self .bgfile .resize ((e .width , e .height ), Image .ANTIALIAS )
255- self .background = ImageTk .PhotoImage (self .bgfile )
256- self .background2 .create_image (0 , 0 , image = self .background , anchor = "nw" )
270+ def update_alloc_ram (self , e ):
271+ self .allocramlabel .config (text = f"Allocated RAM: { self .allocram .get ()/ (1024 ** (self .scale ))} GB" )
272+ oldarg = re .search (r"\-Xmx[0-9]+[MGT]" , self .jvmargs ).group (0 )
273+ if self .scale == 0 :
274+ suffix = "M"
275+ elif self .scale == 1 :
276+ suffix = "G"
277+ elif self .scale == 2 :
278+ suffix = "T"
279+ if self .allocram .get ()/ (1024 ** (self .scale - 1 )) % 1024 == 0 :
280+ if suffix == "M" :
281+ suffix = "G"
282+ elif suffix == "G" :
283+ suffix = "T"
284+ self .jvmargs = self .jvmargs .replace (oldarg , f"-Xmx{ int (self .allocram .get ()/ (1024 ** (self .scale )))} { suffix } " )
285+ else :
286+ self .jvmargs = self .jvmargs .replace (oldarg , f"-Xmx{ int (self .allocram .get ()/ (1024 ** (self .scale - 1 )))} { suffix } " )
287+
288+ def resize_widgets (self ):
289+ self .bgfile = Image .open ("assets/background.png" )
290+ self .bgfile = self .bgfile .resize ((self .background2 .winfo_width (), self .background2 .winfo_height ()), Image .ANTIALIAS )
291+ self .background = ImageTk .PhotoImage (self .bgfile )
292+ self .background2 .create_image (0 , 0 , image = self .background , anchor = "nw" )
293+
257294
258295 def kill_process (self ):
259296 """Kills the running Minecraft process."""
@@ -330,14 +367,10 @@ class App:
330367 self .profjavadir .set (self .accounts ["profiles" ][self .nametoprofile [name ]]["javaDir" ])
331368 except :
332369 pass
333- try :
334- self .profjavargs .set (self .accounts ["profiles" ][self .nametoprofile [name ]]["javaArgs" ])
335- except :
336- pass
337370
338371 def save_profile (self , name ):
339372 """Saves the selected profile."""
340- self .accounts ["profiles" ][self .nametoprofile [name ]] = {"name" : self .profname .get (), "type" : "custom" , "lastVersionId" : self .accounts ["profiles" ][self .nametoprofile [name ]]["lastVersionId" ], "gameDir" : self .profgamedir .get (), "javaDir" : self .profjavadir .get (), "javaArgs" : self .profjavargs . get () }
373+ self .accounts ["profiles" ][self .nametoprofile [name ]] = {"name" : self .profname .get (), "type" : "custom" , "lastVersionId" : self .accounts ["profiles" ][self .nametoprofile [name ]]["lastVersionId" ], "gameDir" : self .profgamedir .get (), "javaDir" : self .profjavadir .get (), "javaArgs" : self .jvmargs }
341374 self .get_versions ()
342375 self .versionlist ["values" ] = self .versions
343376 self .profilelist ["values" ] = self .versions
@@ -404,7 +437,7 @@ class App:
404437 "-javaHome" ,
405438 self .profjavadir .get (),
406439 "-javaArgs" ,
407- self .profjavargs . get () ,
440+ self .jvmargs ,
408441 "-launcherServerSocket" ,
409442 str (sock )
410443 ]
@@ -598,9 +631,7 @@ if __name__ == "__main__":
598631 except BaseException as e :
599632 messagebox .showerror ("Fatal error" , "A fatal error has occurred during startup." )
600633 logger .error (e , exc_info = True )
601- with open ("launcher_logs/error.log" , "w" ) as err :
602- err .write (sys .exc_info ())
603- err .close ()
634+ traceback .print_exc (file = open ("launcher_logs/error.log" , "w" ))
604635 send_error_report ("gui" , fatal = True )
605636 sys .exit ()
606637 try :
@@ -609,9 +640,7 @@ if __name__ == "__main__":
609640 if not type (e ).__name__ == "SystemExit" :
610641 messagebox .showerror ("Fatal error" , "A fatal error has occurred during runtime." )
611642 logger .error (e , exc_info = True )
612- with open ("launcher_logs/error.log" , "w" ) as err :
613- err .write (sys .exc_info ())
614- err .close ()
643+ traceback .print_exc (file = open ("launcher_logs/error.log" , "w" ))
615644 send_error_report ("gui" , fatal = True )
616645 main .win .destroy ()
617646 sys .exit ()
0 commit comments