2020-10-23 05:25:17 +00:00
|
|
|
#------------------#
|
2020-10-30 10:39:03 +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
|
|
|
#------------------#
|
2020-10-30 10:39:03 +00:00
|
|
|
|
|
|
|
class MultiThreadDownload(threading.Thread):
|
2020-11-07 09:42:03 +00:00
|
|
|
'''The Class for Multi-Thread Download'''
|
|
|
|
'''Get from Internet and improved by Ghink Network Studio'''
|
2020-10-30 10:39:03 +00:00
|
|
|
def __init__(self,url,startpos,endpos,f):
|
|
|
|
super(MultiThreadDownload,self).__init__()
|
|
|
|
self.url = url
|
|
|
|
self.startpos = startpos
|
|
|
|
self.endpos = endpos
|
|
|
|
self.fd = f
|
|
|
|
def download(self):
|
|
|
|
headers = {"Range":"bytes=%s-%s"%(self.startpos,self.endpos)}
|
|
|
|
res = requests.get(self.url,headers=headers)
|
|
|
|
self.fd.seek(self.startpos)
|
|
|
|
self.fd.write(res.content)
|
|
|
|
def run(self):
|
|
|
|
self.download()
|
|
|
|
|
2020-10-23 05:25:17 +00:00
|
|
|
class GMCLCore(object):
|
2020-10-30 10:39:03 +00:00
|
|
|
'''The Main Class of the Launcher Core'''
|
|
|
|
'''
|
2020-10-23 05:25:17 +00:00
|
|
|
self.__GamePath=GamePath
|
|
|
|
self.__Version=Version
|
2020-10-30 10:39:03 +00:00
|
|
|
'''
|
|
|
|
def __init__(self,LauncherName="GMCLCore",LauncherVersion="A0.2.0"):
|
2020-11-07 09:42:03 +00:00
|
|
|
'''The global variable set function'''
|
|
|
|
''':LauncherName The name of your launcher,default value is "GMCLCore".'''
|
|
|
|
''':LauncherVersion The version of your launcher,default value is the version of core.'''
|
2020-10-30 10:39:03 +00:00
|
|
|
self.__LauncherName=LauncherName
|
|
|
|
self.__LauncherVersion=LauncherVersion
|
2020-10-23 05:25:17 +00:00
|
|
|
self.__UserAgent={'User-Agent':LauncherName+'/'+LauncherVersion+' ((GMCL Core Alpha 0.2.0;Alpha))'}
|
2020-11-07 09:42:03 +00:00
|
|
|
def SearchFile(self,Path,Tag,Num=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'''
|
|
|
|
''':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'''
|
|
|
|
List=[]
|
|
|
|
i=0
|
|
|
|
for root,dirs,files in os.walk(Path,topdown=True):
|
|
|
|
if Tag in files:
|
|
|
|
i+=1
|
|
|
|
List.append(root+'\\'+Tag)
|
|
|
|
if(i==Num):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
return List
|
|
|
|
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'))
|
|
|
|
return md5.hexdigest()
|
2020-10-23 05:25:17 +00:00
|
|
|
else:
|
|
|
|
return "Error:Wrong Auth Way."
|
2020-11-07 09:42:03 +00:00
|
|
|
def GetJavaPath(self,Num=1):
|
|
|
|
'''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'''
|
|
|
|
if(platform.system()=="Windows"):
|
|
|
|
return self.SearchFile('C:\\','javaw.exe',Num)
|
|
|
|
elif(platform.system()=="Linux"):
|
|
|
|
return self.SearchFile('\\','java',Num)
|
|
|
|
else:
|
|
|
|
return self.SearchFile('\\','java',Num)
|
|
|
|
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-10-30 08:04:26 +00:00
|
|
|
if(self.__AuthWay=="online"):
|
|
|
|
if(Account.count("@")==1):
|
2020-11-07 09:42:03 +00:00
|
|
|
if(json.loads(requests.get("https://authserver.mojang.com/",headers=self.__UserAgent).text)['status']=="OK"):
|
|
|
|
result=json.loads(requests.post("https://authserver.mojang.com/authenticate",'{"agent":{"name":"Minecraft","Version":1},"username":"'+Account+'","password":"'+Password+'"}',headers=self.__UserAgent).text)
|
|
|
|
self.__Token=result['accessToken']
|
|
|
|
self.__UUID=result['selectedProfile']['id']
|
|
|
|
self.__ID=result['selectedProfile']['name']
|
|
|
|
else:
|
|
|
|
return "Error:Fail to request Mojang\'s Auth Server."
|
2020-10-30 08:04:26 +00:00
|
|
|
else:
|
|
|
|
return "Error:Wrong Account."
|
|
|
|
elif(self.__AuthWay=="offline"):
|
|
|
|
if(Account.count("@")==1):
|
|
|
|
return "Error:Wrong Account."
|
|
|
|
else:
|
2020-11-07 09:42:03 +00:00
|
|
|
self.__ID=Account
|
|
|
|
self.__UUID=self.GetUUID(Account)
|
|
|
|
self.__Token=""
|
2020-10-30 08:04:26 +00:00
|
|
|
else:
|
|
|
|
pass
|
2020-11-07 09:42:03 +00:00
|
|
|
def SetAuthWay(self,AuthWay):
|
|
|
|
'''The auth way set function'''
|
|
|
|
''':AuthWay Your auth way,must be "offline" or "online"'''
|
|
|
|
if(AuthWay=="offline"):
|
|
|
|
self.__AuthWay="offline"
|
|
|
|
elif(AuthWay=="online"):
|
|
|
|
self.__AuthWay="online"
|
2020-10-30 08:04:26 +00:00
|
|
|
else:
|
|
|
|
return "Error:Wrong Auth Way."
|
2020-10-30 12:00:43 +00:00
|
|
|
def SetRecomMem(self):
|
2020-10-30 08:04:26 +00:00
|
|
|
if(platform.system()=="Windows"):
|
|
|
|
path="C:/"
|
|
|
|
elif(platform.system()=="Linux"):
|
|
|
|
path="/"
|
|
|
|
else:
|
|
|
|
path="/"
|
|
|
|
i=0
|
|
|
|
for ipath in os.listdir(Path):
|
|
|
|
FullDir=os.path.join(path,ipath)
|
|
|
|
if(os.path.isfile(FullDir)):
|
|
|
|
if("javaw.exe" in os.path.split(FullDir)[1]):
|
|
|
|
return FullDir
|
|
|
|
i+=1
|
2020-10-30 10:39:03 +00:00
|
|
|
if(i==1):
|
2020-10-30 08:04:26 +00:00
|
|
|
break
|
2020-10-30 12:00:43 +00:00
|
|
|
def SetRecomMem(self):
|
|
|
|
self.__Memory = float(psutil.virtual_memory().free / 1024 ** 3) * 0.8
|
2020-10-30 10:39:03 +00:00
|
|
|
def SetJavaPath(self,path):
|
|
|
|
if(path==None):
|
|
|
|
return "Error:Wrong local address for java."
|
|
|
|
else:
|
2020-10-30 12:00:43 +00:00
|
|
|
if(os.path.exists(path)):
|
|
|
|
self.__JAVA=path
|
|
|
|
else:
|
|
|
|
return "Error:Wrong local address for java."
|
|
|
|
def SetGameMem(self,Memory):
|
|
|
|
try:
|
|
|
|
self.__Memory=float(Memory)
|
|
|
|
except:
|
|
|
|
return "Error:Wrong parameter for game memory."
|
2020-10-30 10:39:03 +00:00
|
|
|
def Download(self,DownloadFrom,DownloadTo,ThreadNum=3):
|
2020-10-30 08:04:26 +00:00
|
|
|
if(DownloadFrom=="" or DownloadTo==""):
|
2020-10-30 10:39:03 +00:00
|
|
|
return "Error:Wrong online address or local address for download."
|
2020-10-30 08:04:26 +00:00
|
|
|
else:
|
2020-10-30 10:39:03 +00:00
|
|
|
url = DownloadFrom
|
|
|
|
filename = DownloadTo
|
|
|
|
filesize = int(requests.head(url).headers['Content-Length'])
|
|
|
|
|
|
|
|
threadnum = ThreadNum
|
|
|
|
threading.BoundedSemaphore(threadnum)
|
|
|
|
step = filesize // threadnum
|
|
|
|
mtd_list = []
|
|
|
|
start = 0
|
|
|
|
end = -1
|
|
|
|
|
|
|
|
tempf = open(filename,'w')
|
|
|
|
tempf.close()
|
|
|
|
with open(filename,'rb+') as f:
|
|
|
|
fileno = f.fileno()
|
|
|
|
while end < filesize -1:
|
|
|
|
start = end +1
|
|
|
|
end = start + step -1
|
|
|
|
if end > filesize:
|
|
|
|
end = filesize
|
|
|
|
dup = os.dup(fileno)
|
|
|
|
fd = os.fdopen(dup,'rb+',-1)
|
|
|
|
t = MultiThreadDownload(url,start,end,fd)
|
|
|
|
t.start()
|
|
|
|
mtd_list.append(t)
|
|
|
|
|
|
|
|
for i in mtd_list:
|
|
|
|
i.join()
|