fork(2) download
  1. #!/usr/bin/python3
  2. import os, json, sys
  3.  
  4. # default value
  5. MinecraftDir = './.minecraft'
  6. Version = ''
  7. maxMen = '2048m'
  8. ID = ''
  9.  
  10. # get jar files path from the json file
  11. def getJars(minecraftDir, version):
  12. jsonFilePath = minecraftDir+'/versions/'+version+'/'+version+'.json'
  13.  
  14. with open(jsonFilePath, 'r') as jsonFile :
  15. jsonFileContent = jsonFile.read()
  16.  
  17. jsonFileKeys = json.loads( jsonFileContent )
  18.  
  19. jars = ''
  20. for x in jsonFileKeys['libraries']:
  21. jarFileParts = x['name'].split(':')
  22. jarFile = MinecraftDir+'/libraries/' + \
  23. jarFileParts[0].replace('.','/')+'/'+jarFileParts[1] +'/'+jarFileParts[2] + '/' + \
  24. jarFileParts[1]+'-'+jarFileParts[2]+'.jar'
  25.  
  26. jars += jarFile+':'
  27.  
  28. if 'inheritsFrom' in jsonFileKeys.keys():
  29. jars += getJars(minecraftDir, jsonFileKeys['inheritsFrom'])
  30.  
  31. return jars
  32.  
  33. # generate the Minecraft Argvs from the json file
  34. def getMinecraftArgvs(minecraftDir, version, ID):
  35. jsonFilePath = minecraftDir+'/versions/'+version+'/'+version+'.json'
  36. with open(jsonFilePath, 'r') as jsonFile :
  37. jsonFileContent = jsonFile.read()
  38.  
  39. jsonFileKeys = json.loads( jsonFileContent )
  40. argvs = jsonFileKeys['minecraftArguments']
  41. argvs = argvs .replace('${auth_player_name}', ID)\
  42. .replace('${version_name}', '{}')\
  43. .replace('${game_directory}', minecraftDir)\
  44. .replace('${assets_root}', minecraftDir+'/assets')\
  45. .replace('${assets_index_name}', jsonFileKeys['assets'])\
  46. .replace('${auth_uuid}', '{}')\
  47. .replace('${auth_access_token}', '{}')\
  48. .replace('${user_type}', 'Legacy')\
  49. .replace('${version_type}', jsonFileKeys['type'])\
  50. .replace('${user_properties}', '{}')
  51. argvs = jsonFileKeys['mainClass'] + ' ' + argvs
  52. return argvs
  53.  
  54.  
  55. # process argvs for this script
  56. job = 0
  57. theUsageText = 'Usage:\t\t'+sys.argv[0]+' [-d Minecraft_dir] [-v version] [-m 2048m] Username\n\t\t\tLaunch Minecraft\n\n\t\t'+sys.argv[0]+' [-d Minecraft_dir] -l\n\t\t\tList Minecraft versions'
  58.  
  59. if len(sys.argv) == 1:
  60. print(theUsageText)
  61. sys.exit()
  62. else:
  63. t=1
  64. while t<len(sys.argv):
  65. if sys.argv[t] == '-d':
  66. t+=1
  67. MinecraftDir = sys.argv[t]
  68. elif sys.argv[t] == '-v':
  69. t+=1
  70. Version = sys.argv[t]
  71. elif sys.argv[t] == '-m':
  72. t+=1
  73. maxMen = sys.argv[t]
  74. elif sys.argv[t] == '-h':
  75. print(theUsageText)
  76. sys.exit()
  77. elif sys.argv[t] == '-l':
  78. job = 1
  79. else:
  80. ID = sys.argv[t]
  81.  
  82. t+=1
  83.  
  84. if job == 1: #if the job is list Minecraft versions, do it and exit
  85. print('Minecraft versions:')
  86. versions = os.listdir(MinecraftDir+'/versions/')
  87. versions.sort(reverse=True)
  88. for x in versions:
  89. print( '\t'+x )
  90. sys.exit()
  91.  
  92. if ID == '': #if do not specify a ID, print the usage and exit
  93. print(theUsageText)
  94. sys.exit()
  95.  
  96. if Version == '': #if do not specify a version, set the Version value to the latest version
  97. versions = os.listdir(MinecraftDir+'/versions/')
  98. versions.sort(reverse=True)
  99. Version = versions[0]
  100. # end of this part
  101.  
  102.  
  103. # generate command to launch Minecraft
  104. befour = 'java -Xincgc -XX:-UseAdaptiveSizePolicy -XX:-OmitStackTraceInFastThrow -Xmn128m -Xmx'+maxMen+' -Djava.library.path='+MinecraftDir+'/versions/'+Version+'/'+Version+'-natives -Dfml.ignoreInvalidMinecraftCertificates=true -Dfml.ignorePatchDiscrepancies=true -Duser.home=/ -cp "'
  105.  
  106. after = getMinecraftArgvs(MinecraftDir, Version ,ID)
  107.  
  108. jars = getJars(MinecraftDir, Version)
  109. jars += MinecraftDir+'/versions/'+Version+'/'+Version+'.jar" '
  110.  
  111. cmd = befour + jars + after
  112. # end of this part
  113.  
  114.  
  115. # launch Minecraft
  116. os.system(cmd)
  117.  
Success #stdin #stdout 0.03s 10240KB
stdin
Standard input is empty
stdout
Usage:		./prog [-d Minecraft_dir] [-v version] [-m 2048m] Username
			Launch Minecraft

		./prog [-d Minecraft_dir] -l
			List Minecraft versions