-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bfont format #220
base: main
Are you sure you want to change the base?
bfont format #220
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import math | ||
|
||
import construct | ||
from construct import Construct | ||
from construct.core import ( | ||
Const, | ||
Int16sl, | ||
Int16ul, | ||
Int32ul, | ||
Int64ul, | ||
Rebuild, | ||
Struct, | ||
Terminated, | ||
) | ||
|
||
from mercury_engine_data_structures.common_types import StrId, VersionAdapter | ||
from mercury_engine_data_structures.construct_extensions.alignment import AlignTo | ||
from mercury_engine_data_structures.formats.base_resource import BaseResource | ||
from mercury_engine_data_structures.game_check import Game, GameSpecificStruct | ||
|
||
|
||
# helper func to calculate padding for rebuild | ||
def calc_padding(pad_to, offset): | ||
return math.ceil(offset / pad_to) * pad_to | ||
|
||
|
||
Sprite = Struct("pos" / Int16sl[2], "width" / Int16sl, "height" / Int16sl, "unk1" / Int16sl, "unk2" / Int16sl[2]) | ||
|
||
BFONT_MSR = Struct( | ||
"magic" / Const(b"MFNT"), | ||
"version" / VersionAdapter("1.9.0"), | ||
Const(0x28, Int32ul), # pointer to name | ||
"width" / Int32ul, | ||
"height" / Int32ul, | ||
"unk1" / Int32ul, | ||
"unk2" / Int32ul, | ||
"glyph_count" / Rebuild(Int32ul, lambda ctx: len(ctx.glyph_data)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"_data_start" / Rebuild(Int32ul, lambda ctx: calc_padding(0x10, 0x28 + len(ctx.atlas_path))), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's gotta be a way to have this be calculated automatically somehow...... |
||
"_buct_name_offset" / Rebuild(Int32ul, lambda ctx: calc_padding(0x4, ctx._data_start + ctx.glyph_count * 14)), | ||
"atlas_path" / StrId, | ||
AlignTo(0x10), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does just adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. Aligned adds bytes until the subcon length is a multiple of the modulus, while this should be aligned to the file. |
||
"glyph_data" / Sprite[construct.this.glyph_count], | ||
AlignTo(0x4), | ||
"buct_path" / StrId, | ||
Terminated, | ||
) | ||
|
||
BFONT_DREAD = Struct( | ||
"magic" / Const(b"MFNT"), | ||
"version" / VersionAdapter("1.10.0"), | ||
Const(0x38, Int64ul), # pointer to name | ||
"width" / Int32ul, | ||
"height" / Int32ul, | ||
"unk1" / Int16ul, | ||
Const(b"\xff\xff"), | ||
"unk2" / Int32ul, | ||
"glyph_count" / Rebuild(Int32ul, lambda ctx: len(ctx.glyph_data)), | ||
Const(b"\xff\xff\xff\xff"), | ||
"_data_start" / Rebuild(Int64ul, lambda ctx: calc_padding(0x10, 0x38 + len(ctx.atlas_path))), | ||
"_buct_name_offset" / Rebuild(Int64ul, lambda ctx: ctx._data_start + ctx.glyph_count * 14), | ||
"atlas_path" / StrId, | ||
AlignTo(0x10, pattern=b"\xff"), | ||
"glyph_data" / Sprite[construct.this.glyph_count], | ||
"buct_path" / StrId, | ||
Terminated, | ||
) | ||
|
||
|
||
class Bfont(BaseResource): | ||
@classmethod | ||
def construct_class(cls, target_game: Game) -> Construct: | ||
return GameSpecificStruct({Game.SAMUS_RETURNS: BFONT_MSR, Game.DREAD: BFONT_DREAD}[target_game], target_game) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pytest | ||
from tests.test_lib import parse_build_compare_editor | ||
|
||
from mercury_engine_data_structures import dread_data, samus_returns_data | ||
from mercury_engine_data_structures.formats.bfont import Bfont | ||
|
||
|
||
@pytest.mark.parametrize("bfont_path", dread_data.all_files_ending_with(".bfont")) | ||
def test_buct_dread(dread_file_tree, bfont_path): | ||
parse_build_compare_editor(Bfont, dread_file_tree, bfont_path) | ||
|
||
|
||
@pytest.mark.parametrize("bfont_path", samus_returns_data.all_files_ending_with(".bfont")) | ||
def test_buct_sr(samus_returns_tree, bfont_path): | ||
parse_build_compare_editor(Bfont, samus_returns_tree, bfont_path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a
,
after the last argument so it ends up multi-line