Many huuuuuuuuge updates

This commit is contained in:
Bigsk 2020-11-21 21:53:29 +08:00
parent 49e7b89870
commit 4f611ea148

160
core.py
View File

@ -4,25 +4,23 @@ import os,requests,json,time,sys,psutil,string,platform,glob,getpass,shutil,hash
class GMCLCore(object): class GMCLCore(object):
'''The Main Class of the Launcher Core''' '''The Main Class of the Launcher Core'''
def __init__(self,LauncherName="GMCLCore",LauncherVersion="A0.2.0",LogOutput=False): #-----------------------------------------------------------------------#
#Class Functions
def __init__(self,LauncherName="GMCLCore",LauncherVersion="A0.2.0"):
'''The global variable set function''' '''The global variable set function'''
''':LauncherName The name of your launcher,the default value is "GMCLCore"''' ''':LauncherName The name of your launcher,the default value is "GMCLCore"'''
''':LauncherVersion The version of your launcher,the default value is the version of core''' ''':LauncherVersion The version of your launcher,the default value is the version of core'''
''':LogOutput The switch of the log output function,the default value is False'''
self.__LauncherName=LauncherName self.__LauncherName=LauncherName
self.__LauncherVersion=LauncherVersion self.__LauncherVersion=LauncherVersion
self.__LogOutput=LogOutput
self.__UserAgent={'User-Agent':LauncherName+'/'+LauncherVersion+' ((GMCL Core Alpha 0.2.0;Alpha))'} self.__UserAgent={'User-Agent':LauncherName+'/'+LauncherVersion+' ((GMCL Core Alpha 0.2.0;Alpha))'}
self.__Log=[] self.__Log=[]
self.__AuthWay="" self.__AuthWay=""
self.__AutoLogOutput=False
self.__AutoLogPrint=False
self.__MultiThreadDownload=False self.__MultiThreadDownload=False
self.Log("info","Successful.","__init__") self.Log("info","Successful.","__init__")
def __del__(self): #-----------------------------------------------------------------------#
self.Log("info","Successful.","__del__") #Log Functions
def EnableMultiThreadDownload(self):
'''The function which was used to enable the function of multi-thread download'''
self.__MultiThreadDownload=True
self.Log("info","Successful.","EnableMultiThreadDownload")
def Log(self,Type,Text,Function="Anonymous Function",ErrorType="TypeError"): def Log(self,Type,Text,Function="Anonymous Function",ErrorType="TypeError"):
'''The function which was used to manager the log output system''' '''The function which was used to manager the log output system'''
''':Type Type of the log info''' ''':Type Type of the log info'''
@ -31,13 +29,17 @@ class GMCLCore(object):
''':Type The type of the error,the default value is "TypeError",Support "TypeError","ValueError","UserWarning"''' ''':Type The type of the error,the default value is "TypeError",Support "TypeError","ValueError","UserWarning"'''
if(Text!="" or None): if(Text!="" or None):
if(Type=="info"): if(Type=="info"):
self.__Log.append(("INFO",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),Function,Text)) self.__Log.append(("INFO",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),Function,Text))
if(self.__AutoLogPrint==True):
print("[INFO]["+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"]["+Function+"]"+Text)
return ("INFO",Text) return ("INFO",Text)
elif(Type=="warn"): elif(Type=="warn"):
self.__Log.append(("WARN",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),Function,Text)) self.__Log.append(("WARN",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),Function,Text))
if(self.__AutoLogPrint==True):
print("[WARN]["+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"]["+Function+"]"+Text)
return ("WARN",Text) return ("WARN",Text)
elif(Type=="error"): elif(Type=="error"):
self.__Log.append(("ERROR",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),Function,Text,ErrorType)) self.__Log.append(("ERROR",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),Function,Text,ErrorType))
if(ErrorType=="TypeError"): if(ErrorType=="TypeError"):
raise TypeError(Text) raise TypeError(Text)
elif(ErrorType=="ValueError"): elif(ErrorType=="ValueError"):
@ -45,21 +47,92 @@ class GMCLCore(object):
else: else:
raise UserWarning(Text) raise UserWarning(Text)
else: else:
self.__Log.append(("OTHER",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),Function,Text)) self.__Log.append(("OTHER",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),Function,Text))
if(self.__AutoLogPrint==True):
print("[OTHER]["+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"]["+Function+"]"+Text)
return ("OTHER",Text) return ("OTHER",Text)
def OutputLog(self,Path):
'''The function which was used to output text file of program logs'''
if(os.path.isdir(Path)):
return self.Log("warn","Not File.","OutputLog")
else:
self.Log("info","Successful.","OutputLog")
with open(Path,"w+") as FileObject:
FileObject.write(self.__LauncherName+"/"+self.__LauncherVersion+" Logs "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"\n")
for text in self.__Log:
FileObject.write("["+text[0]+"]["+text[1]+"]["+text[2]+"]"+text[3]+"\n")
def ReturnLog(self): def ReturnLog(self):
'''The function which was used to return the list of program log''' '''The function which was used to return the list of program logs'''
self.Log("info","Successful.","ReturnLog") self.Log("info","Successful.","ReturnLog")
return self.__Log return self.__Log
def PrintLog(self): def PrintLastLog(self):
'''The function which was used to print the list of program log''' '''The function which was used to print the last logs of program'''
self.Log("info","Successful.","PrintLog") self.Log("info","Successful.","PrintLastLog")
print(self.__Log) print("["+self.__Log[-1][0]+"]["+self.__Log[-1][1]+"]["+self.__Log[-1][2]+"]"+self.__Log[-1][3])
def PrintAllLog(self):
'''The function which was used to print all logs of program'''
self.Log("info","Successful.","PrintAllLog")
for text in self.__Log:
print("["+text[0]+"]["+text[1]+"]["+text[2]+"]"+text[3])
def EnableAutoLogPrint(self):
'''The function which was used to enable the function of log auto print'''
self.__AutoLogPrint=True
self.Log("info","Successful.","EnableAutoLogPrint")
def DisableAutoLogPrint(self):
'''The function which was used to disable the function of log auto print'''
self.__AutoLogPrint=False
self.Log("info","Successful.","DisableAutoLogPrint")
#-----------------------------------------------------------------------#
#Download Functions
def Download(self,DownloadFrom,DownloadTo,ThreadNum=20):
'''The multi-thread download function'''
''':DownloadFrom The online url for the file'''
''':DownloadTo The local path for the file'''
''':ThreadNum The number of the thread,the default value is 20'''
if(DownloadFrom=="" or DownloadTo==""):
self.Log("warn","Wrong online address or local address for download.","Download")
else:
StartTime=time.time()
FileSize=int(requests.head(DownloadFrom,headers=self.__UserAgent).headers['Content-Length'])
if(self.__MultiThreadDownload==True):
if(platform.system()=="Windows"):
if(os.path.isfile("aria2c.exe")):
os.system("aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__UserAgent['User-Agent']+"\"")
else:
self.Log("warn","Missed Aria2C.","Download")
self.DownloadAria2CWin()
os.system("aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__UserAgent['User-Agent']+"\"")
else:
os.system("aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__UserAgent['User-Agent']+"\"")
else:
with open(DownloadTo,"wb") as FileObject:
FileObject.write(requests.get(DownloadFrom).content)
self.Log("info","Success.","Download")
return (str(time.time()-StartTime),FileSize // (time.time()-StartTime))
def DownloadAria2CWin(self):
'''The function which was used to download Aria2C environment for windows'''
if(os.path.isdir("C:\\Program Files (x86)")):
with open("aria2c.exe","wb") as FileObject:
FileObject.write(requests.get("https://resource.ghink.net/aria2c/win64/aria2c.exe",headers=self.__UserAgent).content)
else:
with open("aria2c.exe","wb") as FileObject:
FileObject.write(requests.get("https://resource.ghink.net/aria2c/win32/aria2c.exe",headers=self.__UserAgent).content)
self.Log("info","Success.","DownloadAria2CWin")
def EnableMultiThreadDownload(self):
'''The function which was used to enable the function of multi-thread download'''
self.__MultiThreadDownload=True
self.Log("info","Successful.","EnableMultiThreadDownload")
def DisableMultiThreadDownload(self):
'''The function which was used to disable the function of multi-thread download'''
self.__MultiThreadDownload=False
self.Log("info","Successful.","DisbleMultiThreadDownload")
#-----------------------------------------------------------------------#
#File and Dir Functions
def MakeDir(self,Path): def MakeDir(self,Path):
'''The function which was used to create a dir with determination''' '''The function which was used to create a dir with determination'''
''':Path The path of the dir you want to create''' ''':Path The path of the dir you want to create'''
if(os.path.isdir(Path)==False): if(os.path.isdir(Path)==False):
os.mkdir(Path) os.makedirs(Path)
self.Log("info","Successful.","MakeDir") self.Log("info","Successful.","MakeDir")
else: else:
return self.Log("warn","Exist.","MakeDir") return self.Log("warn","Exist.","MakeDir")
@ -80,6 +153,8 @@ class GMCLCore(object):
pass pass
self.Log("info","Success.","SearchFile") self.Log("info","Success.","SearchFile")
return List return List
#-----------------------------------------------------------------------#
#Game Environment Set Functions
def GetUUID(self,Account="Steve"): def GetUUID(self,Account="Steve"):
'''The function which was used to generate UUID''' '''The function which was used to generate UUID'''
''':Account Your account,the default value is "Steve"''' ''':Account Your account,the default value is "Steve"'''
@ -91,18 +166,18 @@ class GMCLCore(object):
return md5.hexdigest() return md5.hexdigest()
else: else:
return self.Log("warn","Wrong auth way","GetUUID") return self.Log("warn","Wrong auth way","GetUUID")
def GetJavaPath(self,Num=1): def GetJavaPath(self,Depth=1):
'''The function which was used to search java''' '''The function which was used to search java'''
''':Num How much result you want to get,you should set as "all" if you want to return all result,the default value is 1''' ''':Depth The depth of dir you want to search,you should set as "all" if you want to search all dirs,the default value is 1'''
if(platform.system()=="Windows"): if(platform.system()=="Windows"):
self.Log("info","Success.","GetJavaPath") self.Log("info","Success.","GetJavaPath")
return self.SearchFile('C:\\','javaw.exe',Num) return self.SearchFile('C:\\','javaw.exe',Depth)
elif(platform.system()=="Linux"): elif(platform.system()=="Linux"):
self.Log("info","Success.","GetJavaPath") self.Log("info","Success.","GetJavaPath")
return self.SearchFile('\\','java',Num) return self.SearchFile('\\','java',Depth)
else: else:
self.Log("info","Success.","GetJavaPath") self.Log("info","Success.","GetJavaPath")
return self.SearchFile('\\','java',Num) return self.SearchFile('\\','java',Depth)
def SetAccount(self,Account="Steve",Password=""): def SetAccount(self,Account="Steve",Password=""):
'''The function which was used to set game account data''' '''The function which was used to set game account data'''
''':Account Your account,the default value is "Steve"''' ''':Account Your account,the default value is "Steve"'''
@ -163,36 +238,7 @@ class GMCLCore(object):
self.Log("info","Success.","SetGameMem") self.Log("info","Success.","SetGameMem")
except: except:
return self.Log("warn","Wrong parameter for game memory.","SetGameMem") return self.Log("warn","Wrong parameter for game memory.","SetGameMem")
def Download(self,DownloadFrom,DownloadTo,ThreadNum=20): #-----------------------------------------------------------------------#
'''The multi-thread download function''' #Game Resouces Functions
''':DownloadFrom The online url for the file''' #-----------------------------------------------------------------------#
''':DownloadTo The local path for the file''' #Game Launch Function
''':ThreadNum The number of the thread,the default value is 20'''
if(DownloadFrom=="" or DownloadTo==""):
self.Log("warn","Wrong online address or local address for download.","Download")
else:
StartTime=time.time()
FileSize=int(requests.head(DownloadFrom,headers=self.__UserAgent).headers['Content-Length'])
if(self.__MultiThreadDownload==True):
if(platform.system()=="Windows"):
if(os.path.isfile("aria2c.exe")):
os.system("aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__UserAgent['User-Agent']+"\"")
else:
if(os.path.isdir("C:\\Program Files (x86)")):
with open("aria2c.exe","wb") as FileObject:
FileObject.write(requests.get("https://resource.ghink.net/aria2c/win64/aria2c.exe",headers=self.__UserAgent).content)
else:
with open("aria2c.exe","wb") as FileObject:
FileObject.write(requests.get("https://resource.ghink.net/aria2c/win32/aria2c.exe",headers=self.__UserAgent).content)
os.system("aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__UserAgent['User-Agent']+"\"")
else:
os.system("aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__UserAgent['User-Agent']+"\"")
else:
with open(DownloadTo,"wb") as FileObject:
FileObject.write(requests.get(DownloadFrom).content)
self.Log("info","Success.","Download")
return (str(time.time()-StartTime),FileSize // (time.time()-StartTime))
launcher=GMCLCore()
launcher.EnableMultiThreadDownload()
launcher.Download("http://mirrors.163.com/centos/8-stream/isos/x86_64/CentOS-Stream-8-x86_64-20201030-boot.iso","test.iso")