""" Script to extract the total amount of MJ of the different fluids of BuildCraft 8+. """ import re import argparse import urllib.request DEFAULT_URL = 'https://raw.githubusercontent.com/BuildCraft/BuildCraft/8.0.x-1.12.2/common/buildcraft/energy/BCEnergyRecipes.java' def _parse_total_power(code): # read the constants in the file constants = {} for x in re.findall(r'final int (.*?) = (\d*)', code): constant, value = x if constant == 'TIME_BASE': value = int(value) * 1000 constants[constant] = int(value) # read the assigned values in the function amount_diffs = {} for x in re.findall(r'add(Dirty)?Fuel\((.*?), (.*?), (\d*?)\, (\d*?)\)', code): _, fluid, amountDiff, multiplier, boostOver4 = x if amountDiff not in constants: raise ValueError('The source code can\'t be read. It should have changed') amount_diffs[fluid] = { 'value': constants[amountDiff], 'boostOver4': int(boostOver4), } # print the total power of each fluid: # total power = totalTime * powerPerCycle = multiplier * MjAPI.MJ * (TIME_BASE * (boostOver4 / 4) / multiplier / amountDiff) <=> # total power = MjAPI.MJ * TIME_BASE * (boostOver4 / 4) / amountDiff total_power = {} for fluid, item in amount_diffs.items(): total_power[fluid] = constants['TIME_BASE'] * (item['boostOver4'] / 4) // item['value'] return total_power def main(): parser = argparse.ArgumentParser() parser.add_argument("--url", help="The url of the file that contains the BC energy recipes, " "BCEnergyRecipes.java. Something starting with https://raw.githubusercontent", type=str) parser.add_argument("--file", help="The location of the file that contains the BC energy recipes, " "BCEnergyRecipes.java.", type=argparse.FileType('r', encoding='UTF-8')) args = parser.parse_args() if args.file is None: # read the content of the relevant source if args.url is None: url = DEFAULT_URL else: url = args.url with urllib.request.urlopen(url) as f: code = f.read().decode('utf-8') else: code = args.file.read() total_power = _parse_total_power(code) print('Total power (MJ/B) of each of the BuildCraft fluids:') for fluid in total_power: print('%s = %d' % (fluid, total_power[fluid])) if __name__ == '__main__': main()