2023-06-28 16:22:45 +00:00
import sys , argparse
2023-06-28 15:36:32 +00:00
from bs4 import BeautifulSoup
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 ) :
# Load tag names first:
id_dict = { }
filename = " Reduced Ids 2.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)
item_tracking = { }
2023-06-28 16:22:45 +00:00
print ( ' ----- ' )
storage_space_counter = 1
2023-06-28 15:36:32 +00:00
for inv_tag in soup . find_all ( ' inv ' ) :
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 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 ]
print ( ' {:04} - {} - {} ' . format ( item_code , item_name , item_quantity ) )
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 ' )
parser . add_argument ( ' --add_item ' , required = False , metavar = ' N ' , type = int , nargs = 2 , help = " Add more of an item to storage by CODE - refer to accompanying data file reference " )
parser . add_argument ( ' --buff_chars ' , required = False , action = ' store_true ' )
args = parser . parse_args ( )
filename = args . filename
2023-06-28 15:36:32 +00:00
print ( filename )
full_text = " "
for line in open ( filename ) :
full_text + = line
# print(full_text)
soup = BeautifulSoup ( full_text , " xml " )
# print(soup.prettify())
# characters(soup)
inventory ( soup )
text = soup . prettify ( )
# Delete XML header
sansfirstline = ' \n ' . join ( text . split ( ' \n ' ) [ 1 : ] )
f = open ( ' game.xml ' , ' w ' )
f . write ( sansfirstline )
f . close ( )
if __name__ == " __main__ " :
main ( )