1
0
mirror of synced 2025-02-02 20:57:24 +01:00

Fold utils.py into parsers.py and writers.py

This commit is contained in:
Viv 2023-07-21 23:13:27 -04:00
parent 356bb7b036
commit e318d0a8a3
3 changed files with 38 additions and 38 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)