Skip to content

Instantly share code, notes, and snippets.

@wgaylord
Created August 26, 2016 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wgaylord/f89febd2fdd1cb34e6d49ee15e7ad08f to your computer and use it in GitHub Desktop.
Save wgaylord/f89febd2fdd1cb34e6d49ee15e7ad08f to your computer and use it in GitHub Desktop.
# coding: utf-8
import urllib
import shutil
import zipfile
from jawa import ClassFile
import hashlib
import jawa
import json
import os
import sys
import gc
MCUrl = 'https://s3.amazonaws.com/Minecraft.Download/versions/<version>/<version>.jar'
ProfilePath ='/web/www/minecraft'
ClassFiles = {}
Hashes = {}
Profiles = {}
def DownloadJar(version):
print 'Downloading'
realUrl = MCUrl.replace('<version>',version)
zip = urllib.urlopen(realUrl).read()
open(os.getcwd()+'/jars/'+str(version)+'.jar','wb+').write(zip)
def ExtractJar(version):
print 'Extracting'
zipfile.ZipFile(os.getcwd()+'/jars/'+str(version)+'.jar').extractall(os.getcwd()+'/class_files/'+version)
shutil.rmtree(os.getcwd()+'/class_files/'+version+'/assets',ignore_errors=True)
shutil.rmtree(os.getcwd()+'/class_files/'+version+'/META-INF',ignore_errors=True)
shutil.rmtree(os.getcwd()+'/class_files/'+version+'/net/minecraft',ignore_errors=True)
for root, dirs, files in os.walk("./class_files/"+version, topdown=False):
for name in files:
if not name.endswith('.class'):
os.remove(root+'/'+name)
def BuildClassFiles(version):
print 'Building ClassFiles'
for root, dirs, files in os.walk("./class_files/"+version, topdown=False):
for name in files:
if name.endswith('.class'):
hasher = hashlib.md5()
t = open(root+'/'+name,'rb')
ClassFiles[name.split('.')[0]]=ClassFile(t)
t.seek(0)
hasher.update(t.read())
Hashes[name.replace(".class","")]=hasher.hexdigest()
t.close()
def Profile():
global Profiles
print 'Profiling Strings and Numbers'
for key in ClassFiles.keys():
Profiles[key] = {}
for x in ClassFiles[key].constants.find(type_=jawa.constants.ConstantString):
if(Profiles[key].has_key('string')):
Profiles[key]['string'].append(x.string.value)
else:
Profiles[key]['string'] = []
Profiles[key]['string'].append(x.string.value)
for x in ClassFiles[key].constants.find(type_=jawa.constants.ConstantNumber):
if(Profiles[key].has_key('number')):
Profiles[key]['number'].append(x.value)
else:
Profiles[key]['number'] = []
Profiles[key]['number'].append(x.value)
Profiles[key]['hash'] = Hashes[key]
def ExportProfile(version):
print 'Exporting Profile'
global Profiles
for s in Profiles.keys():
if Profiles[s]=={}:
del Profiles[s]
out = json.dumps(Profiles)
open(ProfilePath+'/profiles/'+version+'.json','w+').write(out)
def Cleanup(version):
print 'Cleaning up'
shutil.rmtree(os.getcwd()+'/class_files/'+version,ignore_errors=True)
os.remove(os.getcwd()+'/jars/'+version+'.jar')
if __name__ == "__main__":
version = sys.argv[1]
DownloadJar(version)
ExtractJar(version)
BuildClassFiles(version)
Profile()
ExportProfile(version)
Cleanup(version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment