2023-06-28 16:22:45 +00:00
import sys , argparse
2023-06-28 15:36:32 +00:00
from bs4 import BeautifulSoup
2023-06-28 23:58:33 +00:00
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??
2023-06-28 15:36:32 +00:00
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 )
2023-06-28 23:58:33 +00:00
def inventory ( soup , add_code , add_quantity ) :
2023-06-28 15:36:32 +00:00
# Load tag names first:
id_dict = { }
2023-06-28 23:58:33 +00:00
filename = " item_ids.txt "
2023-06-28 15:36:32 +00:00
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)
2023-06-28 23:58:33 +00:00
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 ] ) )
2023-06-28 15:36:32 +00:00
item_tracking = { }
2023-06-28 16:22:45 +00:00
print ( ' ----- ' )
storage_space_counter = 1
2023-06-28 23:58:33 +00:00
# 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 ' ) :
2023-06-28 15:36:32 +00:00
if inv_tag . parent . name != ' feat ' :
continue
2023-06-28 16:22:45 +00:00
print ( ' Storage space {} ' . format ( storage_space_counter ) )
print ( )
2023-06-28 15:36:32 +00:00
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 ]
2023-06-28 16:22:45 +00:00
print ( " {:4} : {} - {} " . format ( item_code , item_name , item_quantity ) )
2023-06-28 23:58:33 +00:00
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 ' ] ) )
2023-06-28 15:36:32 +00:00
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
2023-06-28 16:22:45 +00:00
print ( )
print ( ' Total use of storage space {} : {} ' . format ( storage_space_counter , quantity_total ) )
storage_space_counter + = 1
2023-06-28 15:36:32 +00:00
print ( ' ----- ' )
2023-06-28 15:51:05 +00:00
print ( ' Item total summary: ' )
2023-06-28 16:22:45 +00:00
print ( )
2023-06-28 15:51:05 +00:00
for item in item_tracking . items ( ) :
item_code = item [ 0 ]
item_name = id_dict [ item_code ]
item_quantity = item [ 1 ]
2023-06-28 23:58:33 +00:00
print ( ' {:4} - {} - {} ' . format ( item_code , item_name , item_quantity ) )
2023-06-28 15:51:05 +00:00
2023-06-28 23:58:33 +00:00
def give_money ( soup , amount ) :
bank_tag = soup . find ( ' playerBank ' )
bank_tag [ ' ca ' ] = amount
2023-06-28 15:36:32 +00:00
def main ( ) :
2023-06-28 16:22:45 +00:00
parser = argparse . ArgumentParser ( prog = " Space Haven Saved Game Inspector " , description = " As above. " )
parser . add_argument ( ' filename ' )
2023-06-28 23:58:33 +00:00
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 " )
2023-06-28 16:22:45 +00:00
args = parser . parse_args ( )
2023-06-28 23:58:33 +00:00
# print(args)
print ( " --- Space Haven Saved Game Inspector --- " )
print ( )
2023-06-28 16:22:45 +00:00
filename = args . filename
2023-06-28 23:58:33 +00:00
# print(filename)
2023-06-28 15:36:32 +00:00
full_text = " "
for line in open ( filename ) :
full_text + = line
# print(full_text)
soup = BeautifulSoup ( full_text , " xml " )
# print(soup.prettify())
2023-06-28 23:58:33 +00:00
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 ] )
2023-06-28 15:36:32 +00:00
text = soup . prettify ( )
2023-06-28 23:58:33 +00:00
# Delete XML header - game doesn't have it
2023-06-28 15:36:32 +00:00
sansfirstline = ' \n ' . join ( text . split ( ' \n ' ) [ 1 : ] )
f = open ( ' game.xml ' , ' w ' )
f . write ( sansfirstline )
f . close ( )
if __name__ == " __main__ " :
main ( )