This repository has been archived on 2022-12-28. You can view files and clone it, but cannot push or open issues or pull requests.
gmclcore/GMCLCore.py

271 lines
15 KiB
Python
Raw Normal View History

2020-10-23 05:25:17 +00:00
#------------------#
import os,requests,json,time,sys,psutil,string,platform,glob,getpass,shutil,hashlib,random,subprocess,math,zipfile,threading
2020-10-23 05:25:17 +00:00
#------------------#
class GMCLCore(object):
'''The Main Class of the Launcher Core'''
2020-11-21 13:53:29 +00:00
#-----------------------------------------------------------------------#
#Class Functions
2021-01-02 07:02:26 +00:00
def __init__(self,LauncherName=None,LauncherVersion=None,LauncherConfig=None):
'''The global variable set function'''
2020-11-15 04:30:26 +00:00
''':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'''
2021-01-02 07:02:26 +00:00
''':LauncherConfig The custom config you want to post in'''
CoreName="GMCLCore"
CoreVersion=("A0.2.0","Alpha")
LauncherName=CoreName if(LauncherName==None) else LauncherName
LauncherVersion=CoreVersion if(LauncherVersion==None) else LauncherVersion
LauncherConfig={} if(LauncherConfig==None) else LauncherConfig
self.__SystemConfig={
"Core":{
"Name":CoreName,
"Version":CoreVersion,
"Header":{'User-Agent':LauncherName+'/'+LauncherVersion[0]+' (('+CoreName+' '+CoreVersion[0]+';'+CoreVersion[1]+'))'},
"Functions":{
"Log":{
"AutoPrint":False,
"ErrorOutput":False
},
"Debug":False,
"Aria":False
},
"Users":[]
},
"Launcher":{
"Name":LauncherName,
"Version":LauncherVersion,
"Config":LauncherConfig,
}
}
2020-11-15 04:30:26 +00:00
self.__Log=[]
2020-11-20 18:17:32 +00:00
self.__AuthWay=""
self.Log("info","Successful.","__init__")
2020-11-21 13:53:29 +00:00
#-----------------------------------------------------------------------#
#Log Functions
2020-11-18 07:38:23 +00:00
def Log(self,Type,Text,Function="Anonymous Function",ErrorType="TypeError"):
2020-11-15 07:54:41 +00:00
'''The function which was used to manager the log output system'''
''':Type Type of the log info'''
''':Text The main text of log info'''
2020-11-18 07:38:23 +00:00
''':Function The function that cause the log,the default value is "Anonymous Function"'''
2020-11-18 07:46:35 +00:00
''':Type The type of the error,the default value is "TypeError",Support "TypeError","ValueError","UserWarning"'''
2020-12-06 03:48:13 +00:00
if(Text!=""):
2021-01-02 07:02:26 +00:00
if(Type=="deadly"):
self.__Log.append(("DEADLY_ERROR",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),Function,Text,ErrorType))
if(self.__SystemConfig["Core"]["Functions"]["Log"]["ErrorOutput"]):
self.MakeDir('.'+self.__SystemConfig["Launcher"]["Name"]+"\\logs")
self.OutputLog('.'+self.__SystemConfig["Launcher"]["Name"]+"\\logs\\"+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+".txt")
2020-11-18 07:46:35 +00:00
if(ErrorType=="TypeError"):
raise TypeError(Text)
elif(ErrorType=="ValueError"):
raise ValueError(Text)
else:
raise UserWarning(Text)
2021-01-02 07:02:26 +00:00
elif(Type=="info" or "warn" or "error"):
self.__Log.append((Type.upper(),time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),Function,Text))
if(self.__SystemConfig["Core"]["Functions"]["Log"]["AutoPrint"]==True):
print("["+Type.upper()+"]["+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"]["+Function+"]"+Text)
return (Type.upper(),Text)
2020-11-15 07:54:41 +00:00
else:
2020-11-21 13:53:29 +00:00
self.__Log.append(("OTHER",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),Function,Text))
2021-01-02 07:02:26 +00:00
if(self.__SystemConfig["Core"]["Functions"]["Log"]["AutoPrint"]==True):
2020-11-21 13:53:29 +00:00
print("[OTHER]["+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"]["+Function+"]"+Text)
2020-11-15 07:54:41 +00:00
return ("OTHER",Text)
2020-11-21 13:53:29 +00:00
def OutputLog(self,Path):
'''The function which was used to output text file of program logs'''
if(os.path.isdir(Path)):
2021-01-02 07:02:26 +00:00
return self.Log("warn","Exist duplication name dir.","OutputLog")
2020-11-21 13:53:29 +00:00
else:
self.Log("info","Successful.","OutputLog")
with open(Path,"w+") as FileObject:
2021-01-02 07:02:26 +00:00
FileObject.write(self.__SystemConfig["Launcher"]["Name"]+"/"+self.__SystemConfig["Launcher"]["Version"][0]+" "+self.__SystemConfig["Launcher"]["Version"][1]+" | "+self.__CoreName+"/"+self.__CoreVersion[0]+" "+self.__CoreVersion[1]+" Logs "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"\n")
2020-11-21 13:53:29 +00:00
for text in self.__Log:
FileObject.write("["+text[0]+"]["+text[1]+"]["+text[2]+"]"+text[3]+"\n")
2020-11-20 18:17:32 +00:00
def ReturnLog(self):
2020-11-21 13:53:29 +00:00
'''The function which was used to return the list of program logs'''
2020-11-20 18:17:32 +00:00
self.Log("info","Successful.","ReturnLog")
return self.__Log
2020-11-21 13:53:29 +00:00
def PrintLastLog(self):
'''The function which was used to print the last logs of program'''
self.Log("info","Successful.","PrintLastLog")
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'''
2021-01-02 07:02:26 +00:00
self.__SystemConfig["Core"]["Functions"]["Log"]["AutoPrint"]=True
2020-11-21 13:53:29 +00:00
self.Log("info","Successful.","EnableAutoLogPrint")
def DisableAutoLogPrint(self):
'''The function which was used to disable the function of log auto print'''
2021-01-02 07:02:26 +00:00
self.__SystemConfig["Core"]["Functions"]["Log"]["AutoPrint"]=False
2020-11-21 13:53:29 +00:00
self.Log("info","Successful.","DisableAutoLogPrint")
2021-01-02 07:02:26 +00:00
def EnableErrorLogOutput(self):
'''The function which was used to enable the function of error log output'''
self.__SystemConfig["Core"]["Functions"]["Log"]["ErrorOutput"]=True
self.Log("info","Successful.","EnableErrorLogOutput")
def DisableErrorLogOutput(self):
'''The function which was used to disable the function of error log output'''
self.__SystemConfig["Core"]["Functions"]["Log"]["ErrorOutput"]=False
self.Log("info","Successful.","DisableErrorLogOutput")
2020-11-21 13:53:29 +00:00
#-----------------------------------------------------------------------#
#Download Functions
2020-11-21 16:45:32 +00:00
def Download(self,DownloadFrom,DownloadTo):
2020-11-21 13:53:29 +00:00
'''The multi-thread download function'''
''':DownloadFrom The online url for the file'''
''':DownloadTo The local path for the file'''
if(DownloadFrom=="" or DownloadTo==""):
self.Log("warn","Wrong online address or local address for download.","Download")
else:
StartTime=time.time()
2021-01-02 07:02:26 +00:00
FileSize=int(requests.head(DownloadFrom,headers=self.__SystemConfig["Core"]["Header"]).headers['Content-Length'])
if(self.__SystemConfig["Core"]["Functions"]["Aria"]==True):
2020-11-21 13:53:29 +00:00
if(platform.system()=="Windows"):
2021-01-02 07:02:26 +00:00
if(os.path.isfile('.'+self.__SystemConfig["Launcher"]["Name"]+"\\rely\\"+"aria2c.exe")):
os.system('.'+self.__SystemConfig["Launcher"]["Name"]+"\\rely\\"+"aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__SystemConfig["Core"]["Header"]['User-Agent']+"\"")
2020-11-21 13:53:29 +00:00
else:
2020-11-21 16:45:32 +00:00
self.Log("warn","Missed Aria.","Download")
self.DownloadAriaWin()
2021-01-02 07:02:26 +00:00
os.system('.'+self.__SystemConfig["Launcher"]["Name"]+"\\rely\\"+"aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__SystemConfig["Core"]["Header"]['User-Agent']+"\"")
2020-11-21 13:53:29 +00:00
else:
2021-01-02 07:02:26 +00:00
os.system('.'+self.__SystemConfig["Launcher"]["Name"]+"\\rely\\"+"aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__SystemConfig["Core"]["Header"]['User-Agent']+"\"")
2020-11-21 13:53:29 +00:00
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))
2020-11-21 16:45:32 +00:00
def DownloadAriaWin(self):
'''The function which was used to download Aria environment for windows'''
2021-01-02 07:02:26 +00:00
if(not os.path.isdir('.'+self.__SystemConfig["Launcher"]["Name"]+"\\rely")):
self.MakeDir('.'+self.__SystemConfig["Launcher"]["Name"]+"\\rely")
2020-11-21 13:53:29 +00:00
if(os.path.isdir("C:\\Program Files (x86)")):
2021-01-02 07:02:26 +00:00
with open('.'+self.__SystemConfig["Launcher"]["Name"]+"\\rely\\"+"aria2c.exe","wb") as FileObject:
FileObject.write(requests.get("https://resource.ghink.net/aria2c/win64/aria2c.exe",headers=self.__SystemConfig["Core"]["Header"]).content)
2020-11-21 13:53:29 +00:00
else:
2021-01-02 07:02:26 +00:00
with open('.'+self.__SystemConfig["Launcher"]["Name"]+"\\rely\\"+"aria2c.exe","wb") as FileObject:
FileObject.write(requests.get("https://resource.ghink.net/aria2c/win32/aria2c.exe",headers=self.__SystemConfig["Core"]["Header"]).content)
2020-11-21 16:45:32 +00:00
self.Log("info","Success.","DownloadAriaWin")
2021-01-02 07:02:26 +00:00
def EnableAria(self):
'''The function which was used to enable the function of Aria'''
self.__SystemConfig["Core"]["Functions"]["Aria"]=True
self.Log("info","Successful.","EnableAria")
def DisableAria(self):
'''The function which was used to disable the function of Aria'''
self.__SystemConfig["Core"]["Functions"]["Aria"]=False
self.Log("info","Successful.","DisbleAria")
2020-11-21 13:53:29 +00:00
#-----------------------------------------------------------------------#
#File and Dir Functions
2020-11-15 07:54:41 +00:00
def MakeDir(self,Path):
'''The function which was used to create a dir with determination'''
''':Path The path of the dir you want to create'''
2020-12-06 03:48:13 +00:00
if(not os.path.isdir(Path)):
2020-11-21 13:53:29 +00:00
os.makedirs(Path)
2020-11-18 08:00:11 +00:00
self.Log("info","Successful.","MakeDir")
2020-11-15 07:54:41 +00:00
else:
return self.Log("warn","Exist.","MakeDir")
2020-11-21 07:29:48 +00:00
def SearchFile(self,Path,Tag,Depth=1):
'''The function which was used to search files'''
''':Path The root path you want to search'''
''':Tag The name keyword of the file you want to search'''
2020-11-21 07:29:48 +00:00
''':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'''
List=[]
i=0
for root,dirs,files in os.walk(Path,topdown=True):
if Tag in files:
i+=1
List.append(root+'\\'+Tag)
2020-11-21 07:29:48 +00:00
if(i==Depth):
break
else:
pass
2020-11-18 08:00:11 +00:00
self.Log("info","Success.","SearchFile")
return List
2020-11-21 13:53:29 +00:00
#-----------------------------------------------------------------------#
#Game Environment Set Functions
def GetUUID(self,Account="Steve"):
'''The function which was used to generate UUID'''
''':Account Your account,the default value is "Steve"'''
if(self.__AuthWay=="offline"):
result="OfflinePlayer:"+Account
md5=hashlib.md5()
md5.update(result.encode(encoding='utf-8'))
2020-11-18 08:00:11 +00:00
self.Log("info","Success.","GetUUID")
return md5.hexdigest()
2020-10-23 05:25:17 +00:00
else:
2020-11-18 08:00:11 +00:00
return self.Log("warn","Wrong auth way","GetUUID")
2020-11-21 13:53:29 +00:00
def GetJavaPath(self,Depth=1):
'''The function which was used to search java'''
2020-11-21 13:53:29 +00:00
''':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"):
2020-11-18 08:00:11 +00:00
self.Log("info","Success.","GetJavaPath")
2020-11-21 13:53:29 +00:00
return self.SearchFile('C:\\','javaw.exe',Depth)
elif(platform.system()=="Linux"):
2020-11-18 08:00:11 +00:00
self.Log("info","Success.","GetJavaPath")
2020-11-21 13:53:29 +00:00
return self.SearchFile('\\','java',Depth)
else:
2020-11-18 08:00:11 +00:00
self.Log("info","Success.","GetJavaPath")
2020-11-21 13:53:29 +00:00
return self.SearchFile('\\','java',Depth)
def SetAccount(self,Account="Steve",Password=""):
'''The function which was used to set game account data'''
''':Account Your account,the default value is "Steve"'''
''':Password Your password'''
2020-12-06 03:48:13 +00:00
if(self.__AuthWay=="online_mojang"):
2020-10-30 08:04:26 +00:00
if(Account.count("@")==1):
2021-01-02 07:02:26 +00:00
if(json.loads(requests.get("https://authserver.mojang.com/",headers=self.__SystemConfig["Core"]["Header"]).text)['status']=="OK"):
result=json.loads(requests.post("https://authserver.mojang.com/authenticate",'{"agent":{"name":"Minecraft","Version":1},"username":"'+Account+'","password":"'+Password+'"}',headers=self.__SystemConfig["Core"]["Header"]).text)
self.__Token=result['accessToken']
self.__UUID=result['selectedProfile']['id']
self.__ID=result['selectedProfile']['name']
2020-11-18 08:00:11 +00:00
self.Log("info","Success.","GetAccount")
else:
2020-11-20 18:17:32 +00:00
return self.Log("warn","Fail to request Mojang\'s auth server.","SetAccount")
2020-10-30 08:04:26 +00:00
else:
2020-11-20 18:17:32 +00:00
return self.Log("warn","Wrong Account.","SetAccount")
2020-10-30 08:04:26 +00:00
elif(self.__AuthWay=="offline"):
if(Account.count("@")==1):
2020-11-20 18:17:32 +00:00
return self.Log("warn","Wrong Account.","SetAccount")
2020-10-30 08:04:26 +00:00
else:
self.__ID=Account
self.__UUID=self.GetUUID(Account)
self.__Token=""
2020-11-18 08:00:11 +00:00
self.Log("info","Success.","GetAccount")
2020-10-30 08:04:26 +00:00
else:
pass
def SetAuthWay(self,AuthWay):
'''The auth way set function'''
''':AuthWay Your auth way,must be "offline" or "online"'''
if(AuthWay=="offline"):
self.__AuthWay="offline"
2020-11-20 18:17:32 +00:00
self.Log("info","Success.","SetAuthWay")
elif(AuthWay=="online"):
self.__AuthWay="online"
2020-11-20 18:17:32 +00:00
self.Log("info","Success.","SetAuthWay")
2020-10-30 08:04:26 +00:00
else:
2020-11-21 07:29:48 +00:00
return self.Log("warn","Wrong Auth Way.","SetAccount")
2020-10-30 12:00:43 +00:00
def SetRecomMem(self):
2020-11-18 07:49:12 +00:00
'''The recommendation memory value set function'''
2020-10-30 12:00:43 +00:00
self.__Memory = float(psutil.virtual_memory().free / 1024 ** 3) * 0.8
2020-11-20 18:17:32 +00:00
self.Log("info","Success.","SetRecomMem")
def SetJavaPath(self,path):
2020-11-18 07:52:35 +00:00
'''The java path set function'''
''':Path Your java path'''
if(path==None):
2020-11-21 07:29:48 +00:00
return self.Log("warn","Wrong local address for java.","SetJavaPath")
else:
2020-10-30 12:00:43 +00:00
if(os.path.exists(path)):
self.__JAVA=path
2020-11-20 18:17:32 +00:00
self.Log("info","Success.","SetJavaPath")
2020-10-30 12:00:43 +00:00
else:
2020-11-21 07:29:48 +00:00
return self.Log("warn","Wrong local address for java.","SetJavaPath")
2020-10-30 12:00:43 +00:00
def SetGameMem(self,Memory):
2020-11-18 07:52:35 +00:00
'''The game memory value set function'''
''':Memory The memory of the game,the unit is GiBytes'''
2020-10-30 12:00:43 +00:00
try:
self.__Memory=float(Memory)
2020-11-20 18:17:32 +00:00
self.Log("info","Success.","SetGameMem")
2020-10-30 12:00:43 +00:00
except:
2020-11-21 07:29:48 +00:00
return self.Log("warn","Wrong parameter for game memory.","SetGameMem")
2020-11-21 13:53:29 +00:00
#-----------------------------------------------------------------------#
#Game Resouces Functions
#-----------------------------------------------------------------------#
2021-01-02 07:02:26 +00:00
#Game Launch Function