import sys, argparse from bs4 import BeautifulSoup class ItemCodeDatabase: def __init__(self, database_filename): pass def get_name_from_code(self, code): pass def validate_code(self, code): pass # A single item with its code, name, and quantity class Item: def __init__(self, code, name, quantity): self.code = code self.name = name self.quantity = quantity # A single storage area, with a list of contents (Items) class StorageArea: def __init__(self): pass def add_item(self, item): pass # Returns how full the storage area is based on its contents def total_occupancy(self): pass class Character: # Will need an internal sense of what the codes mean for string output purposes def __init__(self, name): self.name = name def set_skills(self, skill_array): pass def set_attributes(self, attribute_array): pass def maximize_skills(self): pass def maximize_attributes(self): pass # How will we get all this back into the XML file though?? def characters(soup): for character in soup.find_all('characters'): c_elems = character.find_all('c') if len(c_elems) > 0: print('Found some appropriate c-tags') for char_c in c_elems: # print(char_c['name']) if 'name' in char_c.attrs: print(char_c['name']) # We have found a character tag! # ---- SKILL UPRGRADING skill_tag = char_c.find('skills') print(skill_tag) # Experimental changing for sk_tag in skill_tag.find_all('s'): sk_tag['level'] = '5' sk_tag['mxn'] = '8' if 'mxp' in sk_tag.attrs: sk_tag['mxp'] = '8' print(skill_tag) # ---- ATTRIBUTE UPGRADING attribute_tag = char_c.find('attr') print(attribute_tag) for a_tag in attribute_tag.find_all('a'): a_tag['points'] = '6' print(attribute_tag) def inventory(soup, add_code, add_quantity): # Load tag names first: id_dict = {} filename = "item_ids.txt" for line in open(filename): result = line.split() code = int(result[0]) name = ' '.join(result[1:]) # print(code, name) id_dict[code] = name # print(id_dict) print("You have requested that {} unit(s) of {} be added to existing storage of this item (storage site will be selected at random)".format(add_quantity, id_dict[add_code])) item_tracking = {} print('-----') storage_space_counter = 1 # This line is a hack to prevent finding alien ship inventories # Unfortunately at the moment it will only likely work if the player has only one ship # I do not yet know how to fix this problem if the player has a fleet ship_tag = soup.find('ship') added_quantity = False for inv_tag in ship_tag.find_all('inv'): if inv_tag.parent.name != 'feat': continue print('Storage space {}'.format(storage_space_counter)) print() quantity_total = 0 for s_tag in inv_tag.find_all('s'): item_code = int(s_tag['elementaryId']) item_quantity = int(s_tag['inStorage']) item_name = id_dict[item_code] print("{:4}: {} - {}".format(item_code, item_name, item_quantity)) if item_code == add_code and not added_quantity: print(" Updating quantity with requested amount...") item_quantity += add_quantity s_tag['inStorage'] = item_quantity added_quantity = True print(" Item quantity is now {}".format(s_tag['inStorage'])) quantity_total += item_quantity if item_code not in item_tracking: item_tracking[item_code] = item_quantity else: item_tracking[item_code] = item_tracking[item_code] + item_quantity print() print('Total use of storage space {}: {}'.format(storage_space_counter, quantity_total)) storage_space_counter += 1 print('-----') print('Item total summary:') print() for item in item_tracking.items(): item_code = item[0] item_name = id_dict[item_code] item_quantity = item[1] print('{:4} - {} - {}'.format(item_code, item_name, item_quantity)) def give_money(soup, amount): bank_tag = soup.find('playerBank') bank_tag['ca'] = amount def main(): parser = argparse.ArgumentParser(prog="Space Haven Saved Game Inspector", description="As above.") parser.add_argument('filename') parser.add_argument('--add_item', required=False, metavar='N', type=int, nargs=2, help="Add more of an existing item to storage by CODE - refer to accompanying data file reference for codes. First number is the code, second is the desired quantity.") parser.add_argument('--buff_chars', required=False, action='store_true', help="For all characters, increases all skills and attributes to maximum. Use wisely.") parser.add_argument('--money', required=False, type=int, nargs=1, help="Give the player credits of the specified amount") args = parser.parse_args() # print(args) print("--- Space Haven Saved Game Inspector ---") print() filename = args.filename # print(filename) full_text = "" for line in open(filename): full_text += line # print(full_text) soup = BeautifulSoup(full_text, "xml") # print(soup.prettify()) if args.buff_chars: print('Buffing all characters...') characters(soup) if args.add_item: print('Adding items and listing storage contents...') add_code = args.add_item[0] add_quantity = args.add_item[1] inventory(soup, add_code, add_quantity) if args.money: # print(args.money[0]) print('Increasing money to the given amount...') give_money(soup, args.money[0]) text = soup.prettify() # Delete XML header - game doesn't have it sansfirstline = '\n'.join(text.split('\n')[1:]) f = open('game.xml', 'w') f.write(sansfirstline) f.close() if __name__ == "__main__": main()