diff --git a/src/tja2fumen/parsers.py b/src/tja2fumen/parsers.py index 5cb3cdd..aeb0404 100644 --- a/src/tja2fumen/parsers.py +++ b/src/tja2fumen/parsers.py @@ -1,8 +1,8 @@ import os import re +import struct from copy import deepcopy -from tja2fumen.utils import read_struct from tja2fumen.types import (TJASong, TJAMeasure, TJAData, FumenCourse, FumenMeasure, FumenBranch, FumenNote, FumenHeader) from tja2fumen.constants import (NORMALIZE_COURSE, TJA_NOTE_TYPES, @@ -393,3 +393,31 @@ def read_fumen(fumen_file, exclude_empty_measures=False): or m.branches['master'].length] return song + + +def read_struct(file, order, format_string, seek=None): + """ + Interpret bytes as packed binary data. + + Arguments: + - file: The fumen's file object (presumably in 'rb' mode). + - order: '<' or '>' (little or big endian). + - format_string: String made up of format characters that describes + the data layout. Full list of available characters: + (https://docs.python.org/3/library/struct.html#format-characters) + - seek: The position of the read pointer to be used within the file. + + Return values: + - interpreted_string: A string containing interpreted byte values, + based on the specified 'fmt' format characters. + """ + if seek: + file.seek(seek) + expected_size = struct.calcsize(order + format_string) + byte_string = file.read(expected_size) + # One "official" fumen (AC11\deo\deo_n.bin) runs out of data early + # This workaround fixes the issue by appending 0's to get the size to match + if len(byte_string) != expected_size: + byte_string += (b'\x00' * (expected_size - len(byte_string))) + interpreted_string = struct.unpack(order + format_string, byte_string) + return interpreted_string diff --git a/src/tja2fumen/utils.py b/src/tja2fumen/utils.py deleted file mode 100644 index bcc7e91..0000000 --- a/src/tja2fumen/utils.py +++ /dev/null @@ -1,36 +0,0 @@ -import struct - - -def read_struct(file, order, format_string, seek=None): - """ - Interpret bytes as packed binary data. - - Arguments: - - file: The fumen's file object (presumably in 'rb' mode). - - order: '<' or '>' (little or big endian). - - format_string: String made up of format characters that describes - the data layout. Full list of available characters: - (https://docs.python.org/3/library/struct.html#format-characters) - - seek: The position of the read pointer to be used within the file. - - Return values: - - interpreted_string: A string containing interpreted byte values, - based on the specified 'fmt' format characters. - """ - if seek: - file.seek(seek) - expected_size = struct.calcsize(order + format_string) - byte_string = file.read(expected_size) - # One "official" fumen (AC11\deo\deo_n.bin) runs out of data early - # This workaround fixes the issue by appending 0's to get the size to match - if len(byte_string) != expected_size: - byte_string += (b'\x00' * (expected_size - len(byte_string))) - interpreted_string = struct.unpack(order + format_string, byte_string) - return interpreted_string - - -def write_struct(file, order, format_string, value_list, seek=None): - if seek: - file.seek(seek) - packed_bytes = struct.pack(order + format_string, *value_list) - file.write(packed_bytes) diff --git a/src/tja2fumen/writers.py b/src/tja2fumen/writers.py index 34bfeaa..080d209 100644 --- a/src/tja2fumen/writers.py +++ b/src/tja2fumen/writers.py @@ -1,4 +1,5 @@ -from tja2fumen.utils import write_struct +import struct + from tja2fumen.constants import BRANCH_NAMES, FUMEN_TYPE_NOTES @@ -38,3 +39,10 @@ def write_fumen(path_out, song): if note.type.lower() == "drumroll": file.write(note.drumroll_bytes) + + +def write_struct(file, order, format_string, value_list, seek=None): + if seek: + file.seek(seek) + packed_bytes = struct.pack(order + format_string, *value_list) + file.write(packed_bytes)