Skip to content

Commit

Permalink
fixed txt2 offset calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gyy committed Oct 14, 2024
1 parent 776acfc commit df0ef4e
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pymsbt/msbt_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, msbt_file, filepath=None):
"""
self.msbt = msbt_file
self.filepath = filepath or self.msbt.filepath
self.stream = open(filepath, 'wb')
self.stream = open(self.filepath, 'wb')

self.label_index = 0
self.sec_offset = 0
Expand Down Expand Up @@ -71,8 +71,11 @@ def _calculate_table_size(self, sec_start_offset, end_offset):

if (byte_remainder <= 3):
return (table_size - byte_remainder), byte_remainder
elif (byte_remainder >= 13):
byte_remainder_sub = (16 - byte_remainder)
return (table_size) - byte_remainder_sub, 0
else:
return (table_size), 0
return table_size, 0

def _write_sections(self):
"""Writes the MSBT sections to the file"""
Expand Down Expand Up @@ -163,9 +166,9 @@ def _write_text_section(self, section_offset):
offset_count = self.msbt.TXT2.offset_count
txt_offsets = []

offset += 4 * offset_count
offset += 4 * offset_count # move past offsets to write texts
for i in range(offset_count):
txt_offsets.append(offset - 240) # for some reason this works...
txt_offsets.append(offset - (section_offset + 16))
self._write_text_string(offset, i)
offset = self.sec_offset + 2

Expand All @@ -175,6 +178,9 @@ def _write_text_section(self, section_offset):
self._pack_into_stream("<I", offset, offset_count)
offset += 4

print(self.msbt.TXT2.offset_table)
print(txt_offsets)

# write each string in the text section
for i in range(offset_count):
text_offset = txt_offsets[i]
Expand All @@ -183,6 +189,7 @@ def _write_text_section(self, section_offset):

#calculate table size and create header
table_size, r = self._calculate_table_size(section_offset, self.sec_offset)
print(table_size, r)
self._pack_into_stream("<4sI", section_offset, b'TXT2', table_size)

self._fill_bytes(self.sec_offset, r)
Expand Down

0 comments on commit df0ef4e

Please sign in to comment.