Skip to content

Commit

Permalink
big fancy new API
Browse files Browse the repository at this point in the history
  • Loading branch information
duncathan committed Aug 19, 2023
1 parent 9b3638f commit 9ac8907
Show file tree
Hide file tree
Showing 4 changed files with 636 additions and 138 deletions.
8 changes: 7 additions & 1 deletion src/mercury_engine_data_structures/file_tree_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,13 @@ def get_parsed_asset(self, name: str, *, in_pkg: Optional[str] = None,
elif type_hint != type_from_name:
raise ValueError(f"type_hint was {type_hint}, expected {type_from_name} from name")

return format_class.parse(data, target_game=self.target_game)
return format_class.parse(data, target_game=self.target_game, editor=self)

def get_file(self, path: str, type_hint: type[_T] = BaseResource) -> _T:
"""
Wrapper for `get_parsed_asset`. Override in children for additional functionality.
"""
return self.get_parsed_asset(path, type_hint=type_hint)

def add_new_asset(self, name: str, new_data: typing.Union[bytes, BaseResource],
in_pkgs: typing.Iterable[str]):
Expand Down
13 changes: 10 additions & 3 deletions src/mercury_engine_data_structures/formats/base_resource.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
from __future__ import annotations

import typing

from construct import Construct, Container

from mercury_engine_data_structures.game_check import Game

if typing.TYPE_CHECKING:
from mercury_engine_data_structures.file_tree_editor import FileTreeEditor


class BaseResource:
_raw: Container
target_game: Game
editor: FileTreeEditor | None

def __init__(self, raw: Container, target_game: Game):
def __init__(self, raw: Container, target_game: Game, editor: FileTreeEditor | None = None):
self._raw = raw
self.target_game = target_game
self.editor = editor

@classmethod
def construct_class(cls, target_game: Game) -> Construct:
raise NotImplementedError()

@classmethod
def parse(cls, data: bytes, target_game: Game) -> "BaseResource":
def parse(cls, data: bytes, target_game: Game, editor: FileTreeEditor | None = None) -> BaseResource:
return cls(cls.construct_class(target_game).parse(data, target_game=target_game),
target_game)
target_game, editor)

def build(self) -> bytes:
return self.construct_class(self.target_game).build(self._raw, target_game=self.target_game)
Expand Down
Loading

0 comments on commit 9ac8907

Please sign in to comment.