Skip to content

Commit

Permalink
Bugfixes for site implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lhearachel committed Jun 6, 2024
1 parent 4e60cde commit 9614e26
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 17 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ fake_libc_include/
# Output / testing directories
tst/
out/
data/

# pyinstaller
build/
Expand Down
46 changes: 39 additions & 7 deletions porydex.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
from porydex.parse.moves import parse_moves
from porydex.parse.species import parse_species

def prepend_file(f, s: str):
f_data = f.read()
f.seek(0, 0)
f.write(s + f_data)

def config_show(_):
porydex.config.load()
print(f'compiler command: {str(porydex.config.compiler)}')
Expand All @@ -32,6 +37,9 @@ def config_set(args):
if args.output:
porydex.config.output = args.output.resolve()

if args.format:
porydex.config.format = args.format

porydex.config.save()

def config_clear(_):
Expand All @@ -58,7 +66,7 @@ def extract(args):
lvlup_learnsets = parse_level_up_learnsets(custom_headers / 'level_up_learnsets.h', move_names)
teach_learnsets = parse_teachable_learnsets(expansion_data / 'pokemon' / 'teachable_learnsets.h', move_names)

species = parse_species(
species, learnsets = parse_species(
expansion_data / 'pokemon' / 'species_info.h',
abilities,
items,
Expand All @@ -71,14 +79,34 @@ def extract(args):
species_names = [mon['name'] for mon in sorted(species.values(), key=lambda m: m['num'])]
encounters = parse_encounters(expansion_data / 'wild_encounters.h', species_names)

with open(porydex.config.output / 'moves.json', 'w', encoding='utf-8') as outf:
json.dump(moves, outf, indent=4)
if porydex.config.format == porydex.config.OutputFormat.json:
with open(porydex.config.output / 'moves.json', 'w', encoding='utf-8') as outf:
json.dump(moves, outf, indent=4)

with open(porydex.config.output / 'species.json', 'w', encoding='utf-8') as outf:
json.dump(species, outf, indent=4)

with open(porydex.config.output / 'learnsets.json', 'w', encoding='utf-8') as outf:
json.dump(learnsets, outf, indent=4)

with open(porydex.config.output / 'encounters.json', 'w', encoding='utf-8') as outf:
json.dump(encounters, outf, indent=4)
else: # jsobj
with open(porydex.config.output / 'moves.js', 'w+', encoding='utf-8') as outf:
outf.write('BattleMovedex = ')
json.dump(moves, outf, indent=4)

with open(porydex.config.output / 'species.js', 'w+', encoding='utf-8') as outf:
outf.write('BattlePokedex = ')
json.dump(species, outf, indent=4)

with open(porydex.config.output / 'species.json', 'w', encoding='utf-8') as outf:
json.dump(species, outf, indent=4)
with open(porydex.config.output / 'learnsets.js', 'w+', encoding='utf-8') as outf:
outf.write('BattleLearnsets = ')
json.dump(learnsets, outf, indent=4)

with open(porydex.config.output / 'encounters.json', 'w', encoding='utf-8') as outf:
json.dump(encounters, outf, indent=4)
with open(porydex.config.output / 'encounters.js', 'w+', encoding='utf-8') as outf:
outf.write('Encounters = ')
json.dump(encounters, outf, indent=4)

def main():
argp = argparse.ArgumentParser(prog='porydex',
Expand All @@ -101,6 +129,10 @@ def main():
config_set_p.add_argument('-o', '--output', action='store',
help='path to output directory for extracted data files; default: ./out',
type=pathlib.Path)
config_set_p.add_argument('-f', '--format',
help='format for output files',
type=porydex.config.OutputFormat.argparse,
choices=list(porydex.config.OutputFormat))
config_set_p.set_defaults(func=config_set)

config_clear_p = config_subp.add_parser('clear', help='clear configured options')
Expand Down
24 changes: 23 additions & 1 deletion porydex/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import configparser
import enum
import pathlib
import os

class OutputFormat(enum.Enum):
json = enum.auto()
jsobj = enum.auto()

def __str__(self) -> str:
return self.name.lower()

def __repr__(self) -> str:
return str(self)

@staticmethod
def argparse(s):
try:
return OutputFormat[s.lower()]
except KeyError:
return s

compiler: pathlib.Path = pathlib.Path('gcc')
expansion: pathlib.Path = pathlib.Path('../pokeemerald-expansion').resolve()
output: pathlib.Path = pathlib.Path('./out').resolve()
output: pathlib.Path = pathlib.Path('./site/data').resolve()
format: OutputFormat = OutputFormat.jsobj

_CONFIG_FILE: pathlib.Path = pathlib.Path('porydex.ini')

Expand All @@ -14,6 +33,7 @@ def save():
'compiler': str(compiler),
'expansion': str(expansion),
'output': str(output),
'format': str(format),
}
with open(_CONFIG_FILE, 'w', encoding='utf-8') as cfgfile:
config.write(cfgfile)
Expand All @@ -22,6 +42,7 @@ def load():
global compiler
global expansion
global output
global format

# if no config exists, ensure it exists with defaults for the next load
if not _CONFIG_FILE.exists():
Expand All @@ -34,6 +55,7 @@ def load():
compiler = pathlib.Path(config['default']['compiler'])
expansion = pathlib.Path(config['default']['expansion'])
output = pathlib.Path(config['default']['output'])
format = OutputFormat[config['default']['format']]

def clear():
os.remove(_CONFIG_FILE)
Expand Down
2 changes: 1 addition & 1 deletion porydex/parse/encounters.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def load_json(fname: pathlib.Path) -> dict:
def parse_encounters(fname: pathlib.Path,
species_names: list[str]) -> dict[str, dict[str, EncounterRate] | dict[str, dict]]:
encounters: ExprList
with yaspin(text=f'Loading encouner tables: {fname}', color='cyan') as spinner:
with yaspin(text=f'Loading encounter tables: {fname}', color='cyan') as spinner:
encounters = load_data(fname)
spinner.ok("✅")

Expand Down
24 changes: 17 additions & 7 deletions porydex/parse/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def parse_mon(struct_init: NamedInitializer,
mon['baseStats']['spe'] = extract_int(field_expr)
case 'baseSpAttack':
mon['baseStats']['spa'] = extract_int(field_expr)
case 'baseDefense':
case 'baseSpDefense':
mon['baseStats']['spd'] = extract_int(field_expr)
case 'types':
types = [DAMAGE_TYPE[extract_int(t)] for t in field_expr.exprs]
Expand Down Expand Up @@ -113,13 +113,15 @@ def parse_mon(struct_init: NamedInitializer,
mon['color'] = BODY_COLOR[extract_int(field_expr)]
case 'speciesName':
name = extract_u8_str(field_expr).replace('♂', '-M').replace('♀', '-F')
if name == '??????????':
name = 'MissingNo.'
mon['name'] = name
case 'height':
# Stored in expansion as M * 10
mon['height'] = extract_int(field_expr) / 10
mon['heightm'] = extract_int(field_expr) / 10
case 'weight':
# Stored in expansion as KG * 10
mon['weight'] = extract_int(field_expr) / 10
mon['weightkg'] = extract_int(field_expr) / 10
case 'itemRare':
mon['items']['R'] = item_names[extract_int(field_expr)]
case 'itemUncommon':
Expand Down Expand Up @@ -295,16 +297,24 @@ def parse_species_data(species_data: ExprList,
forms: dict[str, dict[int, str]],
map_sections: list[str],
level_up_learnsets: dict[str, dict[str, list[int]]],
teachable_learnsets: dict[str, dict[str, list[str]]]) -> dict:
teachable_learnsets: dict[str, dict[str, list[str]]]) -> tuple[dict, dict]:
# first pass: raw AST parse, build evolutions table
all_species_data = {}
all_learnsets = {}
key: str
for species_init in species_data:
try:
mon, evos, lvlup_learnset, teach_learnset = parse_mon(species_init, abilities, items, forms, level_up_learnsets, teachable_learnsets)
all_species_data[mon['num']] = (mon, evos)

if 'name' not in mon or not mon['name']: # egg has no name nor learnset; don't try
continue
key = name_key(mon['name'])
all_learnsets[key] = {}
all_learnsets[key]['learnset'] = {}

if lvlup_learnset or teach_learnset:
mon['learnset'] = zip_learnsets(lvlup_learnset, teach_learnset)
all_learnsets[key]['learnset'] = zip_learnsets(lvlup_learnset, teach_learnset)
except Exception as err:
print('error parsing species info')
print(species_init.show())
Expand All @@ -320,7 +330,7 @@ def parse_species_data(species_data: ExprList,
continue
final_species[name_key(mon['name'])] = mon

return final_species
return final_species, all_learnsets

def parse_species(fname: pathlib.Path,
abilities: list[str],
Expand All @@ -329,7 +339,7 @@ def parse_species(fname: pathlib.Path,
forms: dict[str, dict[int, str]],
map_sections: list[str],
level_up_learnsets: dict[str, dict[str, list[int]]],
teachable_learnsets: dict[str, dict[str, list[str]]]) -> dict:
teachable_learnsets: dict[str, dict[str, list[str]]]) -> tuple[dict, dict]:
species_data: ExprList
with yaspin(text=f'Loading species data: {fname}', color='cyan') as spinner:
species_data = load_truncated(fname, extra_includes=[
Expand Down

0 comments on commit 9614e26

Please sign in to comment.