#!/usr/bin/python2 #why does the bang not work? import urllib2, json, os, sys print "LinuxMint4Ever's EarlyLauncher version BETA 2.6" #globally used variables config = [] offlineData = [] main = (__name__ == "__main__") #When run directly, make sure that we are in the correct directory. if main: if os.path.isfile(os.path.join(os.getcwd(), "earlylauncher.py")): pass else: print "Error: Not running from the EarlyLauncher Directory" sys.exit(1) def readFile(fileName): fileReader = open(fileName, "r") fileContents = fileReader.read() fileReader.close() return fileContents def loadConfig(configFile = "config.txt"): global config #read the config file and if run directly, do error handling as well if main: try: configRaw=readFile(configFile).split("\n") except: print "Error: Could not read config file." sys.exit(1) else: configRaw=readFile(configFile).split("\n") #TODO: Raise an exception in case of an error #put config options and values in a two dimensional list for i in range(len(configRaw)): #ignore empty lines and comments if configRaw[i] == "": pass elif configRaw[i][0] == '#': pass else: config = config + [configRaw[i].split("=")] def loadOfflineData(offlineDataFile = "offline.txt"): global offlineData #read the config file and if run directly, do error handling as well if main: try: dataRaw=readFile(offlineDataFile).split("\n") except: print "Error: Could not read offline data file." sys.exit(1) else: #TODO: Raise an exception in case of an error dataRaw=readFile(offlineDataFile).split("\n") #put config options and values in a two dimensional list for i in range(len(dataRaw)): #ignore empty lines and comments if dataRaw[i] == "": pass elif dataRaw[i][0] == '#': pass else: offlineData = offlineData + [dataRaw[i].split("=")] def getDataOff2DArray(key, array): for i in range(len(array)): if array[i][0]==key: return array[i][1] def getToken(username, password): data = { "agent": { "name": "Minecraft", "version": 1 }, "username": username, "password": password } try: request = urllib2.Request(url='https://authserver.mojang.com/authenticate', data=json.dumps(data).encode(), headers={"Content-Type": "application/json"}) answer = json.loads(urllib2.urlopen(request).read().decode()) except urllib2.HTTPError: if main: print "Invalid user information! (or something went wrong)" sys.exit(1) else: #TODO: raise an exception return False, False, False except urllib2.URLError: if main: print "Authentication server not reachable. Re-launching last user session." loadOfflineData() return getDataOff2DArray("access_token", offlineData), getDataOff2DArray("profile_id", offlineData), getDataOff2DArray("username", offlineData) else: #TODO: raise an exception return False, False, False username = answer['selectedProfile']['name'] access_token = answer['accessToken'] profile_id = answer['selectedProfile']['id'] return access_token, profile_id, username def deRelative(cwd, path): return os.path.join(cwd, path) def run(java_executable, ram, alignTo, libs, username, gameDir, assets, uuid, accessToken, width=1366, height=768): cwd = os.getcwd() os.chdir(gameDir) exitCode = os.system(deRelative(cwd, java_executable)+" -Xmx"+ram+alignTo+" -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:-UseAdaptiveSizePolicy -Xmn128M -Djava.library.path="+os.path.join(deRelative(cwd, libs), "natives")+" -cp "+os.path.join(deRelative(cwd, libs),"jar")+os.sep+(":"+os.path.join(deRelative(cwd,libs),"jar")+os.sep).join(os.listdir(os.path.join(deRelative(cwd, libs),"jar")))+" net.minecraft.launchwrapper.Launch --username "+username+" --version 1.7.10-Forge10.13.4.1614-1.7.10 --gameDir "+deRelative(cwd,gameDir)+" --assetsDir "+deRelative(cwd, assets)+" --assetIndex 1.7.10 --uuid "+str(uuid)+" --accessToken "+accessToken+" --userProperties {} --userType mojang --width "+str(width)+" --height "+str(height)+" --tweakClass cpw.mods.fml.common.launcher.FMLTweaker") os.chdir(cwd) return exitCode if main: loadConfig() ram = getDataOff2DArray("ram", config) alignTo = getDataOff2DArray("alignTo", config) assets = getDataOff2DArray("assets", config) gameDir = getDataOff2DArray("gameDir", config) libs = getDataOff2DArray("libs", config) java_executable = getDataOff2DArray("java_executable", config) #TODO: allow the use of sys.argv to override config login=raw_input("eMail or username: ") pw=raw_input("Password: ") accessToken, uuid, username = getToken(str(login), str(pw)) try: offlineDataFile = open("offline.txt", "w") offlineDataFile.write("username="+username+"\naccess_token="+accessToken+"\nprofile_id="+uuid) offlineDataFile.close() except: print "Error creating offline login data. Proceeding with launch anyways." sys.exit(run(java_executable, ram, alignTo, libs, username, gameDir, assets, uuid, accessToken))