Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. import os, sys
  3. import json
  4. import zipfile
  5. import platform
  6. import re
  7.  
  8. def getMinecraftPath():
  9. if sys.platform.startswith('linux'):
  10. return os.path.expanduser("~/.minecraft")
  11. elif sys.platform.startswith('win'):
  12. return os.path.join(os.getenv("APPDATA"), ".minecraft")
  13. elif sys.platform.startswith('darwin'):
  14. return os.path.expanduser("~/Library/Application Support/minecraft")
  15. else:
  16. print "Cannot detect of version : %s. Please report to your closest sysadmin"%sys.platform
  17. sys.exit()
  18.  
  19. def getNativesKeyword():
  20. if sys.platform.startswith('linux'):
  21. return "linux"
  22. elif sys.platform.startswith('win'):
  23. return "windows"
  24. elif sys.platform.startswith('darwin'):
  25. return "osx"
  26. else:
  27. print "Cannot detect of version : %s. Please report to your closest sysadmin"%sys.platform
  28. sys.exit()
  29.  
  30. def checkMCDir(src, version):
  31. #We check that our version of MC is available for analysis
  32. if not os.path.exists(src) \
  33. or not os.path.exists(os.path.join(src, "versions")) \
  34. or not os.path.exists(os.path.join(src, "libraries")) \
  35. or not os.path.exists(os.path.join(os.path.join(src, "versions"), version)):
  36. print ("ERROR : You should run the launcher at least once before starting MCP")
  37. sys.exit()
  38.  
  39. def getJSONFilename(src, version):
  40. return os.path.join(os.path.join(src, "versions"), version, "%s.json"%version)
  41.  
  42. def checkCacheIntegrity(root, jsonfile, osKeyword, version):
  43. libraries = getLibraries(root, jsonfile, osKeyword)
  44.  
  45. if libraries == None:
  46. return False
  47.  
  48. for library in libraries.values():
  49. if not checkLibraryExists(root, library):
  50. return False
  51.  
  52. if not checkMinecraftExists(root, version):
  53. return False
  54.  
  55. natives = getNatives(root, libraries)
  56.  
  57. for native in natives.keys():
  58. if not checkNativeExists(root, native, version):
  59. return False
  60.  
  61. return True
  62.  
  63. def checkLibraryExists(dst, library):
  64. if os.path.exists(os.path.join(dst, library['filename'])):
  65. return True
  66. else:
  67. return False
  68.  
  69. def checkMinecraftExists(root, version):
  70. if os.path.exists(os.path.join(root, "versions", version, '%s.jar'%version)) and \
  71. os.path.exists(os.path.join(root, "versions", version, '%s.json'%version)):
  72. return True
  73. else:
  74. return False
  75.  
  76. def checkNativeExists(root, native, version):
  77. nativePath = getNativePath(root, version)
  78. if (os.path.exists(os.path.join(nativePath, native))):
  79. return True
  80. else:
  81. return False
  82.  
  83. def getNatives(root, libraries):
  84. nativeList = {}
  85. for library in libraries.values():
  86. if library['extract']:
  87.  
  88. srcPath = os.path.join(root, library['filename'])
  89. jarFile = zipfile.ZipFile(srcPath)
  90. fileList = jarFile.namelist()
  91.  
  92. for _file in fileList:
  93. exclude = False;
  94. for entry in library['exclude']:
  95. if entry in _file:
  96. exclude = True
  97. if not exclude:
  98. nativeList[_file] = library['filename']
  99. return nativeList
  100.  
  101. def getNativePath(root, version):
  102. return os.path.join(root, "versions", version, "%s-natives"%version)
  103.  
  104. def getLibraries(root, jsonfile, osKeyword):
  105. #We check the json exits
  106. if not os.path.exists(jsonfile):
  107. return None
  108. #print ("ERROR : json file %s not found."%jsonfile)
  109. #print ("You should run the launcher at least once before starting MCP")
  110. #sys.exit()
  111.  
  112. #We parse the json file
  113. jsonFile = None
  114. try:
  115. jsonFile = json.load(open(jsonfile))
  116. except Exception as e:
  117. print "Error while parsing the library JSON file : %s"%e
  118. sys.exit()
  119.  
  120. mcLibraries = jsonFile['libraries']
  121. outLibraries = {}
  122.  
  123. for library in mcLibraries:
  124. libCononical = library['name'].split(':')[0]
  125. libSubdir = library['name'].split(':')[1]
  126. libVersion = library['name'].split(':')[2]
  127. libPath = libCononical.replace('.', '/')
  128. extract = False
  129. exclude = []
  130.  
  131. #Rule patch from Adam Greenfield
  132. if 'rules' in library:
  133. passRules = False
  134. for rule in library['rules']:
  135. ruleApplies = True
  136. if 'os' in rule:
  137. if rule['os']['name'] != osKeyword:
  138. ruleApplies = False
  139. else:
  140. if osKeyword == "osx":
  141. os_ver = platform.mac_ver()[0]
  142. else:
  143. os_ver = platform.release()
  144.  
  145. if 'version' in rule['os'] and not re.match(rule['os']['version'], os_ver):
  146. ruleApplies = False
  147.  
  148. if ruleApplies:
  149. if rule['action'] == "allow":
  150. passRules = True
  151. else:
  152. passRules = False
  153.  
  154. if not passRules:
  155. continue
  156.  
  157. if 'natives' in library:
  158. if osKeyword not in library['natives']:
  159. continue
  160. libFilename = "%s-%s-%s.jar"%(libSubdir, libVersion, substitueString(library['natives'][osKeyword]))
  161. else:
  162. libFilename = "%s-%s.jar"%(libSubdir, libVersion)
  163.  
  164. if 'extract' in library:
  165. extract = True
  166. if 'exclude' in library['extract']:
  167. exclude.extend(library['extract']['exclude'])
  168.  
  169. #libFullPath = os.path.join(os.path.join(root, "libraries"), libPath, libSubdir, libVersion, libFilename)
  170. libRelativePath = os.path.join("libraries", libPath, libSubdir, libVersion, libFilename)
  171.  
  172. #if not os.path.exists(libFullPath):
  173. # print ("Error while trying to access libraries. Couldn't find %s"%libFullPath)
  174. # sys.exit()
  175. if 'natives' in library:
  176. outLibraries['%s-%s' % (libSubdir, substitueString(library['natives'][osKeyword]))] = {'name':library['name'], 'filename':libRelativePath, 'extract':extract, 'exclude':exclude}
  177. else:
  178. outLibraries[libSubdir] = {'name':library['name'], 'filename':libRelativePath, 'extract':extract, 'exclude':exclude}
  179.  
  180. return outLibraries
  181.  
  182. def getArch():
  183. machine = platform.machine()
  184. if os.name == 'nt' and sys.version_info[:2] < (2,7):
  185. machine = os.environ.get("PROCESSOR_ARCHITEW6432", os.environ.get('PROCESSOR_ARCHITECTURE', ''))
  186. machine2bits = {'AMD64': '64', 'x86_64': '64', 'i386': '32', 'x86': '32'}
  187. return machine2bits.get(machine, None)
  188.  
  189. def substitueString(str):
  190. str = str.replace("${arch}", getArch())
  191. return str
  192.  
  193.  
  194. if __name__ == '__main__':
  195. osKeyword = getNativesKeyword()
  196. mcDir = getMinecraftPath()
  197. mcLibraries = getLibraries(mcDir, getJSONFilename(mcDir, "1.6.1"), osKeyword)
  198. mcNatives = getNatives(mcDir, mcLibraries)
  199.  
  200. for native in mcNatives.keys():
  201. if checkNativeExists("./jars", native, "1.6.1"):
  202. print 'Found %s %s'%(native, mcNatives[native])
  203. else:
  204. print 'Not found %s %s'%(native, mcNatives[native])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement