diff --git a/utils/miner/achievements.py b/utils/miner/achievements.py new file mode 100644 index 0000000..f602c14 --- /dev/null +++ b/utils/miner/achievements.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python + +# General libs +import re +import json +import os +import sys + +# Tool libs +from utils import run, sanitize +import utils +import conf +from objects import GameAchievement + + +utils.title('ACHIEVEMENTS') + +if conf.SAVE: + sys.path.append('../../minecraftcodex') + os.environ['DJANGO_SETTINGS_MODULE'] = 'local_settings' + from database.models import Achievement + +### +# GLOBALS +### +ITEMS = [] + +### +# LOOK FOR CORRECT JAVA FILES +### +utils.sub("Looking for java files...") +#utils.sub("Keywords: %s" % ', '.join(conf.ACHIEVEMENTS_JAVA_KEYWORDS)) +for keyword in conf.ACHIEVEMENTS_JAVA_KEYWORDS: + cmd = run('grep \'%s\' ./classes/*' % keyword) + for result in cmd: + if result and result is not '': + java_file = os.path.basename(result.strip().split()[0][:-1]) + if java_file not in conf.ACHIEVEMENTS_FILES: + utils.echo("%s " % java_file, end='') + conf.ACHIEVEMENTS_FILES.append(java_file) + +utils.echo('\r') + +### +# GET ITEMS INFO FROM CLASSFILE +### +utils.sub('Looking for dataz', end='\n') + +# Old items for final count +try: + OLD_ITEMS = json.loads(open('achievements.json').read()) +except: + OLD_ITEMS = {} + OLD_ITEMS['list'] = [] + +for java_file in conf.ACHIEVEMENTS_FILES: + file_handler = open('./classes/%s' % java_file) + data = file_handler.read().split("\n") + + item_regex = re.compile(conf.ACHIEVEMENTS_PATTERN) + class_error_regex = re.compile('name \'(?P\w+)\' is not defined') + + for line in data: + if '"' in line: + t = item_regex.search(line) + if t: + item = t.groupdict() + if conf.DEBUG: + print("Line: " + item['code']) + + item['code'] = sanitize(item['code']) + + if conf.DEBUG: + print("Sanitize: " + item['code']) + + try: + obj = eval(item['code']) + except NameError as error: + # Create class for the given classname + class_name = class_error_regex.search(error.__str__()).group('name') + if conf.DEBUG: + print("Classname: %s" % class_name) + setattr(sys.modules[__name__], class_name, type(class_name, (GameAchievement,), {})) + obj = eval(item['code']) + #if obj.name == 'appleGold': + if conf.DEBUG: + print("result object: " + obj.__str__()) + print('- - - - - -') + + ITEMS.append(obj) + +if conf.SAVE: + for item in ITEMS: + try: + obj = Achievement.objects.get( + internal_name=item.name, + internal_id=item.id + ) + except Achievement.DoesNotExist: + obj = Achievement( + internal_name=item.name, + internal_id=item.id + ) + obj.save() + + +# Print the miner summary and compile the new old data +new_old_data = {} +new_old_data['list'] = [] +[new_old_data['list'].append(x.name) for x in ITEMS] +new_items = len(new_old_data['list'])-len(OLD_ITEMS['list']) +utils.info('Found %d achievements (%d new)' % (len(new_old_data['list']), new_items)) +if new_items != 0: + utils.sub('Modifications:', end='\n') + for item in ITEMS: + if item.name not in OLD_ITEMS['list']: + utils.sub(' + %s' % item.name, end='\n', color=utils.colors.GREEN) + + for item in OLD_ITEMS['list']: + if item not in new_old_data['list']: + utils.sub(' - %s' % item, end='\n', color=utils.colors.RED) + +olditems = open('achievements.json', 'w') +olditems.write(json.dumps(new_old_data)) diff --git a/utils/miner/conf.py b/utils/miner/conf.py index f96c802..da03cac 100644 --- a/utils/miner/conf.py +++ b/utils/miner/conf.py @@ -12,7 +12,6 @@ TEXTURES_EXTRA_SIZES_MULTIPLIER = [2, 4, 6, 8] ### # ITEMS ### - ITEMS_FILES = [] ITEMS_JAVA_KEYWORDS = ['flintAndSteel', 'axeStone', 'swordDiamond'] ITEMS_PATTERN = "new (?P[a-z]{2}\((?P[1-9]{1,3}).*\"(?P\w+)\"\))" @@ -20,7 +19,6 @@ ITEMS_PATTERN = "new (?P[a-z]{2}\((?P[1-9]{1,3}).*\"(?P\w+)\"\)) ### # BLOCKS ### - BLOCKS_FILES = [] BLOCKS_JAVA_KEYWORDS = ['stonebrick'] BLOCKS_PATTERN = "new (?P[a-z]{1,3}\((?P[1-9]{1,3}).*\"(?P\w+)\"\))" @@ -36,10 +34,17 @@ LANGUAGES_MASTER_KEYS = [ ] +### +# ACHIEVEMENTS +### +ACHIEVEMENTS_FILES = [] +ACHIEVEMENTS_JAVA_KEYWORDS = ['onARail', 'flyPig'] +ACHIEVEMENTS_PATTERN = "new (?P[a-z]{1,2}\((?P[1-9]{1,3})\, \"(?P\w+)\".*\))" + + ### # BLACKLIST ### - CLASS_BLACKLIST = [ 'and', 'abs', 'all', 'any', 'bin', 'chr' ] diff --git a/utils/miner/objects.py b/utils/miner/objects.py index 6c2e576..ea1bf7d 100644 --- a/utils/miner/objects.py +++ b/utils/miner/objects.py @@ -80,3 +80,23 @@ class GameLanguage(object): def add_string(self, key, value): if key not in self.strings: self.strings[key] = value + +### +# ACHIEVEMENTS +### +class GameAchievement(object): + def __init__(self, internal_id, name, *args): + self.id = int(internal_id) + self.name = name + + def method(self, *args): + return self + + def __getattr__(self, *args): + return self.method + + def __str__(self): + return "" % ( + self.id, + self.name + ) diff --git a/utils/miner/run.sh b/utils/miner/run.sh index 21a7425..7858590 100755 --- a/utils/miner/run.sh +++ b/utils/miner/run.sh @@ -9,3 +9,5 @@ python items.py python blocks.py python languages.py + +python achievements.py