Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Fix PEP warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
np-c0mp1ete committed Nov 21, 2021
1 parent ef5ec34 commit 1722841
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
28 changes: 14 additions & 14 deletions tools/gamesave-convert/gamesave_convert.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#!python

# Python 3.7: Dictionary iteration order is guaranteed to be in order of insertion
import sys

MIN_PYTHON = (3, 7)
if sys.version_info < MIN_PYTHON:
sys.exit("Python {'.'.join([str(n) for n in MIN_PYTHON])} or later is required.")

import argparse
import os
import struct
Expand All @@ -17,6 +11,11 @@
import str_db
import seasave

# Python 3.7: Dictionary iteration order is guaranteed to be in order of insertion
MIN_PYTHON = (3, 7)
if sys.version_info < MIN_PYTHON:
sys.exit("Python {'.'.join([str(n) for n in MIN_PYTHON])} or later is required.")

FILEINFO_CONFIG = {
'ver 1.0.7': {'str_encoding': 'cp1251', 'obj_id_format': 'QQQ'},
'ver 1.7.3': {'str_encoding': 'utf-8', 'obj_id_format': 'Q'}
Expand Down Expand Up @@ -91,8 +90,7 @@ def read_value(var_type, buffer, cur_ptr, s_db, str_encoding, obj_id_format):
elif var_type == VarType.String:
v, cur_ptr = read_string(buffer, cur_ptr, str_encoding)
elif var_type == VarType.Object:
v = {}
v['id'] = struct.unpack_from(obj_id_format, buffer, cur_ptr)
v = {'id': struct.unpack_from(obj_id_format, buffer, cur_ptr)}
cur_ptr += struct.calcsize(obj_id_format)
attributes = {}
attr, cur_ptr = read_attributes_data(buffer, cur_ptr, s_db, str_encoding)
Expand Down Expand Up @@ -178,8 +176,10 @@ def read_save(file_name):
for _ in range(num_strings):
s, cur_ptr = read_string(buffer, cur_ptr, str_encoding)
if s is not None:
#if not s.isascii():
#logging.warning(f'non ascii string: {s} (cp1251: {s.encode("cp1251").hex(" ")}, utf-8: {s.encode("utf-8").hex(" ")})')
# if not s.isascii():
# logging.warning(f'non ascii string: {s}
# (cp1251: {s.encode("cp1251").hex(" ")},
# utf-8: {s.encode("utf-8").hex(" ")})')
strings.append(s)

s_db = str_db.create_db(strings, str_encoding)
Expand Down Expand Up @@ -280,8 +280,8 @@ def write_value(var_type, value, buffer, fileinfo_config):
elif var_type == VarType.String:
buffer = write_string(value, buffer, fileinfo_config['str_encoding'])
elif var_type == VarType.Object:
id = value['id']
buffer += struct.pack(fileinfo_config['obj_id_format'], *id)
obj_id = value['id']
buffer += struct.pack(fileinfo_config['obj_id_format'], *obj_id)
root_attr = next(iter(value['attributes']))
buffer = write_attributes_data(value['attributes'][root_attr], buffer, fileinfo_config['str_encoding'])
elif var_type == VarType.Reference:
Expand Down Expand Up @@ -315,7 +315,6 @@ def write_save(save_data, filename):
file_info = save_data['file_info']
fileinfo_config = FILEINFO_CONFIG[file_info]
str_encoding = fileinfo_config['str_encoding']
obj_id_format = fileinfo_config['obj_id_format']

buffer = bytearray()
buffer = write_string(save_data['program_dir'], buffer, str_encoding)
Expand All @@ -337,7 +336,8 @@ def write_save(save_data, filename):
seasave_buf = bytearray()
seasave_buf = seasave.write_seasave(seasave_data, seasave_buf)
seasave_size = f'{len(seasave_buf):08x}' # as hexadecimal string without prefix 8 bytes long
variables['oSeaSave']['values'][0]['attributes']['skip']['attributes']['save']['value'] = seasave_size + seasave_buf.hex()
variables['oSeaSave']['values'][0]['attributes']['skip']['attributes']['save']['value'] = \
seasave_size + seasave_buf.hex()

for varname in variables:
buffer = write_string(varname, buffer, str_encoding)
Expand Down
2 changes: 1 addition & 1 deletion tools/gamesave-convert/seasave.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def read_aifort(buffer, cur_ptr):

num_mortars = 4 # hardcode
mortars = []
for _ in range(num_cannons):
for _ in range(num_mortars):
mortar, cur_ptr = read_aicannon(buffer, cur_ptr)
mortars.append(mortar)

Expand Down
15 changes: 8 additions & 7 deletions tools/gamesave-convert/str_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

HASH_TABLE_SIZE = 512

def hash(s, str_encoding):

def hash_str(s, str_encoding):
h = 0
bin = s.encode(str_encoding)
for b in bin:
if ord('A') <= b and b <= ord('Z'):
str_bytes = s.encode(str_encoding)
for b in str_bytes:
if ord('A') <= b <= ord('Z'):
b += ord('a') - ord('A')
h = (h << 4) + b
g = h & 0xf0000000
Expand All @@ -18,7 +19,7 @@ def hash(s, str_encoding):


def get_int(db, s, str_encoding):
h = hash(s, str_encoding)
h = hash_str(s, str_encoding)
row_id = h % HASH_TABLE_SIZE
if row_id >= HASH_TABLE_SIZE or s not in db[row_id]:
logging.warning(f"couldn't find string {s} in the string database")
Expand All @@ -43,7 +44,7 @@ def create_db(str_list, str_encoding):
db.append([])

for s in str_list:
h = hash(s, str_encoding)
h = hash_str(s, str_encoding)
row_id = h % HASH_TABLE_SIZE
db[row_id].append(s)

Expand All @@ -58,7 +59,7 @@ def remove_unused(db, used_str, str_encoding):
unused_str.append(elem)

for s in unused_str:
h = hash(s, str_encoding)
h = hash_str(s, str_encoding)
row_id = h % HASH_TABLE_SIZE
elem_id = db[row_id].index(s)
del db[row_id][elem_id]
Expand Down

0 comments on commit 1722841

Please sign in to comment.