Skip to content

Commit

Permalink
Merge pull request #107 from lhearachel/j2b_optional_modes
Browse files Browse the repository at this point in the history
Add json2bin support for different processing modes for optional fields
  • Loading branch information
lhearachel authored Nov 4, 2023
2 parents b31e95b + 771e19e commit 04363ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
20 changes: 15 additions & 5 deletions tools/json2bin/json2bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess

from argparse import ArgumentParser
from enum import Enum, Flag
from enum import Enum, Flag, auto
from types import FunctionType, LambdaType


Expand All @@ -21,6 +21,12 @@
help='Output directory where generated files will be written')


class OptionalBehavior(Enum):
DISALLOW = 0
SKIP = auto()
PAD = auto()


class Parser():
__slots__ = ('registry', 'padding_index', 'field_index', 'alignment_index')

Expand Down Expand Up @@ -52,7 +58,7 @@ def register(self,
size: int | tuple[int,int],
func: FunctionType | LambdaType,
const_type: type[Enum] | None = None,
optional: bool = False) -> 'Parser':
optional: OptionalBehavior = OptionalBehavior.DISALLOW) -> 'Parser':
'''
Register a function for processing a given key within the JSON
structure, along with a size of the field in bytes and any
Expand Down Expand Up @@ -114,10 +120,14 @@ def parse(self, data: dict) -> bytes:
data_key = key[5:] # first 4 characters are a key-prefix
data_val = self._walk(data, data_key.split('.'))
if data_val == {} or data_val is None:
if optional:
if optional == OptionalBehavior.DISALLOW:
print(json.dumps(data, indent=4))
raise KeyError(data_key)
elif optional == OptionalBehavior.SKIP:
continue
elif optional == OptionalBehavior.PAD:
binary.extend((0).to_bytes(size, 'little'))
continue
print(json.dumps(data, indent=4))
raise KeyError(data_key)

binary.extend(parse_func(data_val, size, const_type))
return binary
Expand Down
6 changes: 5 additions & 1 deletion tools/json2bin/pokemon_wotbl_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
species
)


def table_line(move_id: int, level: int) -> bytes:
return ((move_id & 0x01FF) | ((level & 0x7F) << 9)).to_bytes(2, 'little', signed=False)


def parse_level_up_moves(table: dict, _size: int, _enum: None):
out = []
for key, value in table.items():
Expand All @@ -31,7 +33,7 @@ def parse_level_up_moves(table: dict, _size: int, _enum: None):

SCHEMA = j2b.Parser() \
.register_name(lambda s: s) \
.register('learnset.level_up', 0, parse_level_up_moves, optional=True) \
.register('learnset.level_up', 0, parse_level_up_moves, optional=j2b.OptionalBehavior.SKIP) \
.pad(2, 0xff) \
.align(4)

Expand Down Expand Up @@ -60,6 +62,8 @@ def parse_level_up_moves(table: dict, _size: int, _enum: None):
'MOW': 507,
},
}


def indexer(file_path: pathlib.Path) -> int:
name = file_path.parent.stem.upper()
if name == '000': return 0
Expand Down

0 comments on commit 04363ae

Please sign in to comment.