mirror of
https://github.com/pret/pokered.git
synced 2024-10-22 22:55:31 +00:00
Remove independent tools and link to pokemon-asm-tools
This commit is contained in:
parent
835577193e
commit
b52b44f883
9 changed files with 2 additions and 2849 deletions
|
@ -17,6 +17,7 @@ To set up the repository, see [**INSTALL.md**](INSTALL.md).
|
|||
|
||||
- [**Wiki**][wiki] (includes [tutorials][tutorials])
|
||||
- [**Symbols**][symbols]
|
||||
- [**Tools**][tools]
|
||||
|
||||
You can find us on [Discord (pret, #pokered)](https://discord.gg/d5dubZ3).
|
||||
|
||||
|
@ -25,5 +26,6 @@ For other pret projects, see [pret.github.io](https://pret.github.io/).
|
|||
[wiki]: https://github.com/pret/pokered/wiki
|
||||
[tutorials]: https://github.com/pret/pokered/wiki/Tutorials
|
||||
[symbols]: https://github.com/pret/pokered/tree/symbols
|
||||
[tools]: https://github.com/pret/pokemon-asm-tools
|
||||
[ci]: https://github.com/pret/pokered/actions
|
||||
[ci-badge]: https://github.com/pret/pokered/actions/workflows/main.yml/badge.svg
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Usage: python consts.py constants/some_constants.asm
|
||||
|
||||
View numeric values of `const`ants.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import re
|
||||
|
||||
const_value = 0
|
||||
const_inc = 1
|
||||
|
||||
def asm_int(s):
|
||||
base = {'$': 16, '&': 8, '%': 2}.get(s[0], 10)
|
||||
return int(s if base == 10 else s[1:], base)
|
||||
|
||||
def print_const(s, v):
|
||||
print(f'{s} == {v} == ${v:x}')
|
||||
|
||||
def parse_for_constants(line):
|
||||
global const_value, const_inc
|
||||
if not (m := re.match(r'^\s+([A-Za-z_][A-Za-z0-9_@#]*)(?:\s+([^;\\n]+))?', line)):
|
||||
return
|
||||
macro, rest = m.groups()
|
||||
args = [arg.strip() for arg in rest.split(',')] if rest else []
|
||||
if args and not args[-1]:
|
||||
args = args[:-1]
|
||||
if macro == 'const_def':
|
||||
const_value = asm_int(args[0]) if len(args) >= 1 else 0
|
||||
const_inc = asm_int(args[1]) if len(args) >= 2 else 1
|
||||
elif macro == 'const':
|
||||
print_const(args[0], const_value)
|
||||
const_value += const_inc
|
||||
elif macro == 'shift_const':
|
||||
print_const(args[0], 1 << const_value)
|
||||
print_const(args[0] + '_F', const_value)
|
||||
const_value += const_inc
|
||||
elif macro == 'const_skip':
|
||||
const_value += const_inc * (asm_int(args[0]) if len(args) >= 1 else 1)
|
||||
elif macro == 'const_next':
|
||||
const_value = asm_int(args[0])
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print(f'Usage: {sys.argv[0]} constants/some_constants.asm', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
for filename in sys.argv[1:]:
|
||||
with open(filename, 'r', encoding='utf-8') as file:
|
||||
for line in file:
|
||||
parse_for_constants(line)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -1,78 +0,0 @@
|
|||
#!/usr/bin/gawk -f
|
||||
|
||||
# Usage: tools/free_space.awk [BANK=<bank_spec>] pokered.map
|
||||
|
||||
# The BANK argument allows printing free space in one, all, or none of the ROM's banks.
|
||||
# Valid arguments are numbers (in decimal "42" or hexadecimal "0x2a"), "all" or "none".
|
||||
# If not specified, defaults to "none".
|
||||
# The `BANK` argument MUST be before the map file name, otherwise it has no effect!
|
||||
# Yes: tools/free_space.awk BANK=all pokered.map
|
||||
# No: tools/free_space.awk pokered.map BANK=42
|
||||
|
||||
# Copyright (c) 2020, Eldred Habert.
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
BEGIN {
|
||||
nb_banks = 0
|
||||
free = 0
|
||||
rom_bank = 0 # Safety net for malformed files
|
||||
|
||||
# Default settings
|
||||
# Variables assigned via the command-line (except through `-v`) are *after* `BEGIN`
|
||||
BANK="none"
|
||||
}
|
||||
|
||||
# Only accept ROM banks, ignore everything else
|
||||
toupper($0) ~ /^[ \t]*ROM[0X][ \t]+BANK[ \t]+#/ {
|
||||
nb_banks++
|
||||
rom_bank = 1
|
||||
split($0, fields, /[ \t]/)
|
||||
bank_num = strtonum(substr(fields[3], 2))
|
||||
}
|
||||
|
||||
function register_bank(amount) {
|
||||
free += amount
|
||||
rom_bank = 0 # Reject upcoming banks by default
|
||||
|
||||
if (BANK ~ /all/ || BANK == bank_num) {
|
||||
printf "Bank %3d: %5d/16384 (%.2f%%)\n", bank_num, amount, amount * 100 / 16384
|
||||
}
|
||||
}
|
||||
function register_bank_str(str) {
|
||||
if (str ~ /\$[0-9A-F]+/) {
|
||||
register_bank(strtonum("0x" substr(str, 2)))
|
||||
} else {
|
||||
printf "Malformed number? \"%s\" does not start with '$'\n", str
|
||||
}
|
||||
}
|
||||
|
||||
rom_bank && toupper($0) ~ /^[ \t]*EMPTY$/ {
|
||||
# Empty bank
|
||||
register_bank(16384)
|
||||
}
|
||||
rom_bank && toupper($0) ~ /^[ \t]*SLACK:[ \t]/ {
|
||||
# Old (rgbds <=0.6.0) end-of-bank free space
|
||||
register_bank_str($2)
|
||||
}
|
||||
rom_bank && toupper($0) ~ /^[ \t]*TOTAL EMPTY:[ \t]/ {
|
||||
# New (rgbds >=0.6.1) total free space
|
||||
register_bank_str($3)
|
||||
}
|
||||
|
||||
END {
|
||||
# Determine number of banks, by rounding to upper power of 2
|
||||
total_banks = 2 # Smallest size is 2 banks
|
||||
while (total_banks < nb_banks) {
|
||||
total_banks *= 2
|
||||
}
|
||||
|
||||
# RGBLINK omits "trailing" ROM banks, so fake them
|
||||
bank_num = nb_banks
|
||||
while (bank_num < total_banks) {
|
||||
register_bank(16384)
|
||||
bank_num++
|
||||
}
|
||||
|
||||
total = total_banks * 16384
|
||||
printf "Free space: %5d/%5d (%.2f%%)\n", free, total, free * 100 / total
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Usage: python palfix.py image.png
|
||||
|
||||
Fix the palette format of the input image to two-bit grayscale.
|
||||
Colored images will have their palette sorted {white, light
|
||||
color, dark color, black}.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
import png
|
||||
|
||||
def rgb8_to_rgb5(c):
|
||||
r, g, b = c
|
||||
return (r // 8, g // 8, b // 8)
|
||||
|
||||
def invert(c):
|
||||
r, g, b = c
|
||||
return (31 - r, 31 - g, 31 - b)
|
||||
|
||||
def luminance(c):
|
||||
r, g, b = c
|
||||
return 0.299 * r**2 + 0.587 * g**2 + 0.114 * b**2
|
||||
|
||||
def rgb5_pixels(row):
|
||||
yield from (rgb8_to_rgb5(row[x:x+3]) for x in range(0, len(row), 4))
|
||||
|
||||
def fix_pal(filename):
|
||||
with open(filename, 'rb') as file:
|
||||
width, height, rows = png.Reader(file).asRGBA8()[:3]
|
||||
rows = list(rows)
|
||||
b_and_w = {(0, 0, 0), (31, 31, 31)}
|
||||
colors = {c for row in rows for c in rgb5_pixels(row)} - b_and_w
|
||||
if not colors:
|
||||
colors = {(21, 21, 21), (10, 10, 10)}
|
||||
elif len(colors) == 1:
|
||||
c = colors.pop()
|
||||
colors = {c, invert(c)}
|
||||
elif len(colors) != 2:
|
||||
return False
|
||||
palette = tuple(sorted(colors | b_and_w, key=luminance, reverse=True))
|
||||
assert len(palette) == 4
|
||||
rows = [[3 - palette.index(c) for c in rgb5_pixels(row)] for row in rows]
|
||||
writer = png.Writer(width, height, greyscale=True, bitdepth=2, compression=9)
|
||||
with open(filename, 'wb') as file:
|
||||
writer.write(file, rows)
|
||||
return True
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print(f'Usage: {sys.argv[0]} pic.png', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
for filename in sys.argv[1:]:
|
||||
if not filename.lower().endswith('.png'):
|
||||
print(f'{filename} is not a .png file!', file=sys.stderr)
|
||||
elif not fix_pal(filename):
|
||||
print(f'{filename} has too many colors!', file=sys.stderr)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
2357
tools/png.py
2357
tools/png.py
File diff suppressed because it is too large
Load diff
|
@ -1,38 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Usage: python rgb555.py image.png
|
||||
|
||||
Round all colors of the input image to RGB555.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
import png
|
||||
|
||||
def rgb8_to_rgb5(c):
|
||||
return (c & 0b11111000) | (c >> 5)
|
||||
|
||||
def round_pal(filename):
|
||||
with open(filename, 'rb') as file:
|
||||
width, height, rows = png.Reader(file).asRGBA8()[:3]
|
||||
rows = list(rows)
|
||||
for row in rows:
|
||||
del row[3::4]
|
||||
rows = [[rgb8_to_rgb5(c) for c in row] for row in rows]
|
||||
writer = png.Writer(width, height, greyscale=False, bitdepth=8, compression=9)
|
||||
with open(filename, 'wb') as file:
|
||||
writer.write(file, rows)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print(f'Usage: {sys.argv[0]} pic.png', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
for filename in sys.argv[1:]:
|
||||
if not filename.lower().endswith('.png'):
|
||||
print(f'{filename} is not a .png file!', file=sys.stderr)
|
||||
round_pal(filename)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -1,52 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Usage: python sym_comments.py file.asm [pokered.sym] > file_commented.asm
|
||||
|
||||
Comments each label in file.asm with its bank:address from the sym file.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import re
|
||||
|
||||
def main():
|
||||
if len(sys.argv) not in {2, 3}:
|
||||
print(f'Usage: {sys.argv[0]} file.asm [pokered.sym] > file_commented.asm', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
wram_name = sys.argv[1]
|
||||
sym_name = sys.argv[2] if len(sys.argv) == 3 else 'pokered.sym'
|
||||
|
||||
sym_def_rx = re.compile(r'(^{sym})(:.*)|(^\.{sym})(.*)'.format(sym=r'[A-Za-z_][A-Za-z0-9_#@]*'))
|
||||
|
||||
sym_addrs = {}
|
||||
with open(sym_name, 'r', encoding='utf-8') as file:
|
||||
for line in file:
|
||||
line = line.split(';', 1)[0].rstrip()
|
||||
parts = line.split(' ', 1)
|
||||
if len(parts) != 2:
|
||||
continue
|
||||
addr, sym = parts
|
||||
sym_addrs[sym] = addr
|
||||
|
||||
with open(wram_name, 'r', encoding='utf-8') as file:
|
||||
cur_label = None
|
||||
for line in file:
|
||||
line = line.rstrip()
|
||||
if (m = re.match(sym_def_rx, line)):
|
||||
sym, rest = m.group(1), m.group(2)
|
||||
if sym is None and rest is None:
|
||||
sym, rest = m.group(3), m.group(4)
|
||||
key = sym
|
||||
if not sym.startswith('.'):
|
||||
cur_label = sym
|
||||
elif cur_label:
|
||||
key = cur_label + sym
|
||||
if key in sym_addrs:
|
||||
addr = sym_addrs[key]
|
||||
line = sym + rest + ' ; ' + addr
|
||||
print(line)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
99
tools/toc.py
99
tools/toc.py
|
@ -1,99 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Usage: python toc.py file.md
|
||||
|
||||
Replace a "## TOC" heading in a Markdown file with a table of contents,
|
||||
generated from the other headings in the file. Supports multiple files.
|
||||
Headings must start with "##" signs to be detected.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import re
|
||||
from collections import namedtuple
|
||||
from urllib.parse import quote
|
||||
|
||||
toc_name = 'Contents'
|
||||
valid_toc_headings = {'## TOC', '##TOC'}
|
||||
|
||||
TocItem = namedtuple('TocItem', ['name', 'anchor', 'level'])
|
||||
punctuation_rx = re.compile(r'[^\w\- ]+')
|
||||
numbered_heading_rx = re.compile(r'^[0-9]+\. ')
|
||||
specialchar_rx = re.compile(r'[⅔]+')
|
||||
|
||||
def name_to_anchor(name):
|
||||
# GitHub's algorithm for generating anchors from headings
|
||||
# https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb
|
||||
anchor = name.strip().lower() # lowercase
|
||||
anchor = re.sub(punctuation_rx, '', anchor) # remove punctuation
|
||||
anchor = anchor.replace(' ', '-') # replace spaces with dash
|
||||
anchor = re.sub(specialchar_rx, '', anchor) # remove misc special chars
|
||||
anchor = quote(anchor) # url encode
|
||||
return anchor
|
||||
|
||||
def get_toc_index(lines):
|
||||
toc_index = None
|
||||
for i, line in enumerate(lines):
|
||||
if line.rstrip() in valid_toc_headings:
|
||||
toc_index = i
|
||||
break
|
||||
return toc_index
|
||||
|
||||
def get_toc_items(lines, toc_index):
|
||||
for i, line in enumerate(lines):
|
||||
if i <= toc_index:
|
||||
continue
|
||||
if line.startswith('##'):
|
||||
name = line.lstrip('#')
|
||||
level = len(line) - len(name) - len('##')
|
||||
name = name.strip()
|
||||
anchor = name_to_anchor(name)
|
||||
yield TocItem(name, anchor, level)
|
||||
|
||||
def toc_string(toc_items):
|
||||
lines = [f'## {toc_name}', '']
|
||||
for name, anchor, level in toc_items:
|
||||
padding = ' ' * level
|
||||
if re.match(numbered_heading_rx, name):
|
||||
bullet, name = name.split('.', 1)
|
||||
bullet += '.'
|
||||
name = name.lstrip()
|
||||
else:
|
||||
bullet = '-'
|
||||
lines.append(f'{padding}{bullet} [{name}](#{anchor})')
|
||||
return '\n'.join(lines) + '\n'
|
||||
|
||||
def add_toc(filename):
|
||||
with open(filename, 'r', encoding='utf-8') as file:
|
||||
lines = file.readlines()
|
||||
toc_index = get_toc_index(lines)
|
||||
if toc_index is None:
|
||||
return None # no TOC heading
|
||||
toc_items = list(get_toc_items(lines, toc_index))
|
||||
if not toc_items:
|
||||
return False # no content headings
|
||||
with open(filename, 'w', encoding='utf-8') as file:
|
||||
for i, line in enumerate(lines):
|
||||
if i == toc_index:
|
||||
file.write(toc_string(toc_items))
|
||||
else:
|
||||
file.write(line)
|
||||
return True # OK
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print(f'Usage: {sys.argv[0]} file.md', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
for filename in sys.argv[1:]:
|
||||
print(filename)
|
||||
result = add_toc(filename)
|
||||
if result is None:
|
||||
print('Warning: No "## TOC" heading found', file=sys.stderr)
|
||||
elif result is False:
|
||||
print('Warning: No content headings found', file=sys.stderr)
|
||||
else:
|
||||
print('OK')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
106
tools/unique.py
106
tools/unique.py
|
@ -1,106 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Usage: python unique.py [-f|--flip] [-x|--cross] image.png
|
||||
|
||||
Erase duplicate tiles from an input image.
|
||||
-f or --flip counts X/Y mirrored tiles as duplicates.
|
||||
-x or --cross erases with a cross instead of a blank tile.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
import png
|
||||
|
||||
def rgb5_pixels(row):
|
||||
yield from (tuple(c // 8 for c in row[x:x+3]) for x in range(0, len(row), 4))
|
||||
|
||||
def rgb8_pixels(row):
|
||||
yield from (c * 8 + c // 4 for pixel in row for c in pixel)
|
||||
|
||||
def gray_pixels(row):
|
||||
yield from (pixel[0] // 10 for pixel in row)
|
||||
|
||||
def rows_to_tiles(rows, width, height):
|
||||
assert len(rows) == height and len(rows[0]) == width
|
||||
yield from (tuple(tuple(row[x:x+8]) for row in rows[y:y+8])
|
||||
for y in range(0, height, 8) for x in range(0, width, 8))
|
||||
|
||||
def tiles_to_rows(tiles, width, height):
|
||||
assert width % 8 == 0 and height % 8 == 0
|
||||
width, height = width // 8, height // 8
|
||||
tiles = list(tiles)
|
||||
assert len(tiles) == width * height
|
||||
tile_rows = (tiles[y:y+width] for y in range(0, width * height, width))
|
||||
yield from ([tile[y][x] for tile in tile_row for x in range(8)]
|
||||
for tile_row in tile_rows for y in range(8))
|
||||
|
||||
def tile_variants(tile, flip):
|
||||
yield tile
|
||||
if flip:
|
||||
yield tile[::-1]
|
||||
yield tuple(row[::-1] for row in tile)
|
||||
yield tuple(row[::-1] for row in tile[::-1])
|
||||
|
||||
def unique_tiles(tiles, flip, cross):
|
||||
if cross:
|
||||
blank = [[(0, 0, 0)] * 8 for _ in range(8)]
|
||||
for y in range(8):
|
||||
blank[y][y] = blank[y][7 - y] = (31, 31, 31)
|
||||
blank = tuple(tuple(row) for row in blank)
|
||||
else:
|
||||
blank = tuple(tuple([(31, 31, 31)] * 8) for _ in range(8))
|
||||
seen = set()
|
||||
for tile in tiles:
|
||||
if any(variant in seen for variant in tile_variants(tile, flip)):
|
||||
yield blank
|
||||
else:
|
||||
yield tile
|
||||
seen.add(tile)
|
||||
|
||||
def is_grayscale(colors):
|
||||
return (colors.issubset({(31, 31, 31), (21, 21, 21), (10, 10, 10), (0, 0, 0)}) or
|
||||
colors.issubset({(31, 31, 31), (20, 20, 20), (10, 10, 10), (0, 0, 0)}))
|
||||
|
||||
def erase_duplicates(filename, flip, cross):
|
||||
with open(filename, 'rb') as file:
|
||||
width, height, rows = png.Reader(file).asRGBA8()[:3]
|
||||
rows = [list(rgb5_pixels(row)) for row in rows]
|
||||
if width % 8 or height % 8:
|
||||
return False
|
||||
tiles = unique_tiles(rows_to_tiles(rows, width, height), flip, cross)
|
||||
rows = list(tiles_to_rows(tiles, width, height))
|
||||
if is_grayscale({c for row in rows for c in row}):
|
||||
rows = [list(gray_pixels(row)) for row in rows]
|
||||
writer = png.Writer(width, height, greyscale=True, bitdepth=2, compression=9)
|
||||
else:
|
||||
rows = [list(rgb8_pixels(row)) for row in rows]
|
||||
writer = png.Writer(width, height, greyscale=False, bitdepth=8, compression=9)
|
||||
with open(filename, 'wb') as file:
|
||||
writer.write(file, rows)
|
||||
return True
|
||||
|
||||
def main():
|
||||
flip = cross = False
|
||||
while True:
|
||||
if len(sys.argv) < 2:
|
||||
print(f'Usage: {sys.argv[0]} [-f|--flip] [-x|--cross] tileset.png', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
elif sys.argv[1] in {'-f', '--flip'}:
|
||||
flip = True
|
||||
elif sys.argv[1] in {'-x', '--cross'}:
|
||||
cross = True
|
||||
elif sys.argv[1] in {'-fx', '-xf'}:
|
||||
flip = cross = True
|
||||
else:
|
||||
break
|
||||
sys.argv.pop(1)
|
||||
for filename in sys.argv[1:]:
|
||||
if not filename.lower().endswith('.png'):
|
||||
print(f'{filename} is not a .png file!', file=sys.stderr)
|
||||
elif not erase_duplicates(filename, flip, cross):
|
||||
print(f'{filename} is not divisible into 8x8 tiles!', file=sys.stderr)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in a new issue