mirror of
https://gitlab.com/xCrystal/pokecrystal-board.git
synced 2024-09-09 09:51:34 -07:00
Update and add more tool scripts
This commit is contained in:
parent
711f5c87b3
commit
6c26e2617c
@ -17,24 +17,23 @@ from mapreader import MapReader
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
print_bank = 'none'
|
print_bank = 'none'
|
||||||
mapfile = 'pokecrystal.map'
|
filename = 'pokecrystal.map'
|
||||||
|
|
||||||
for arg in sys.argv[1:]:
|
for arg in sys.argv[1:]:
|
||||||
if arg.startswith('BANK='):
|
if arg.startswith('BANK='):
|
||||||
print_bank = arg.split('=', 1)[-1]
|
print_bank = arg.split('=', 1)[-1]
|
||||||
else:
|
else:
|
||||||
mapfile = arg
|
filename = arg
|
||||||
|
|
||||||
if print_bank not in {'all', 'none'}:
|
if print_bank not in {'all', 'none'}:
|
||||||
try:
|
try:
|
||||||
if print_bank.startswith('0x') or print_bank.startswith('0X'):
|
print_bank = (int(print_bank[2:], 16)
|
||||||
print_bank = int(print_bank[2:], 16)
|
if print_bank.startswith('0x') or print_bank.startswith('0X')
|
||||||
else:
|
else int(print_bank))
|
||||||
print_bank = int(print_bank)
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
error = 'Error: invalid BANK: %s' % print_bank
|
error = f'Error: invalid BANK: {print_bank}'
|
||||||
if print_bank.isalnum():
|
if print_bank.isalnum():
|
||||||
error += ' (did you mean: 0x%s?)' % print_bank
|
error += f' (did you mean: 0x{print_bank}?)'
|
||||||
print(error, file=sys.stderr)
|
print(error, file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
@ -42,30 +41,29 @@ def main():
|
|||||||
bank_size = 0x4000 # bytes
|
bank_size = 0x4000 # bytes
|
||||||
total_size = num_banks * bank_size
|
total_size = num_banks * bank_size
|
||||||
|
|
||||||
r = MapReader()
|
reader = MapReader()
|
||||||
with open(mapfile, 'r', encoding='utf-8') as f:
|
with open(filename, 'r', encoding='utf-8') as file:
|
||||||
l = f.readlines()
|
reader.read_map_data(file.readlines())
|
||||||
r.read_map_data(l)
|
|
||||||
|
|
||||||
free_space = 0
|
free_space = 0
|
||||||
per_bank = []
|
per_bank = []
|
||||||
default_bank_data = {'sections': [], 'used': 0, 'slack': bank_size}
|
default_bank_data = {'sections': [], 'used': 0, 'slack': bank_size}
|
||||||
for bank in range(num_banks):
|
for bank in range(num_banks):
|
||||||
bank_data = r.bank_data['ROM0 bank'] if bank == 0 else r.bank_data['ROMX bank']
|
bank_data = reader.bank_data['ROM0 bank' if bank == 0 else 'ROMX bank']
|
||||||
data = bank_data.get(bank, default_bank_data)
|
data = bank_data.get(bank, default_bank_data)
|
||||||
used = data['used']
|
used, slack = data['used'], data['slack']
|
||||||
slack = data['slack']
|
|
||||||
per_bank.append((used, slack))
|
per_bank.append((used, slack))
|
||||||
free_space += slack
|
free_space += slack
|
||||||
|
|
||||||
print('Free space: %d/%d (%.2f%%)' % (free_space, total_size, free_space * 100.0 / total_size))
|
free_percent = 100 * free_space / total_size
|
||||||
|
print(f'Free space: {free_space}/{total_size} ({free_percent:.2f}%)')
|
||||||
if print_bank != 'none':
|
if print_bank != 'none':
|
||||||
print()
|
print()
|
||||||
print('bank, used, free')
|
print('bank, used, free')
|
||||||
for bank in range(num_banks):
|
for bank in range(num_banks):
|
||||||
used, slack = per_bank[bank]
|
used, slack = per_bank[bank]
|
||||||
if print_bank in {'all', bank}:
|
if print_bank in {'all', bank}:
|
||||||
print('$%02X, %d, %d' % (bank, used, slack))
|
print(f'${bank:02X}, {used}, {slack}')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# A library for parsing the pokecrystal.map file output by rgbds.
|
# A library for parsing the pokecrystal.map file output by rgbds.
|
||||||
|
@ -30,15 +30,18 @@ def luminance(c):
|
|||||||
return 0.299 * r**2 + 0.587 * g**2 + 0.114 * b**2
|
return 0.299 * r**2 + 0.587 * g**2 + 0.114 * b**2
|
||||||
|
|
||||||
def rgb5_pixels(row):
|
def rgb5_pixels(row):
|
||||||
for x in range(0, len(row), 4):
|
yield from (rgb8_to_rgb5(row[x:x+3]) for x in range(0, len(row), 4))
|
||||||
yield rgb8_to_rgb5(row[x:x+3])
|
|
||||||
|
def is_grayscale(palette):
|
||||||
|
return (palette == ((31, 31, 31), (21, 21, 21), (10, 10, 10), (0, 0, 0)) or
|
||||||
|
palette == ((31, 31, 31), (20, 20, 20), (10, 10, 10), (0, 0, 0)))
|
||||||
|
|
||||||
def fix_pal(filename):
|
def fix_pal(filename):
|
||||||
with open(filename, 'rb') as file:
|
with open(filename, 'rb') as file:
|
||||||
width, height, data = png.Reader(file).asRGBA8()[:3]
|
width, height, rows = png.Reader(file).asRGBA8()[:3]
|
||||||
data = list(data)
|
rows = list(rows)
|
||||||
b_and_w = {(0, 0, 0), (31, 31, 31)}
|
b_and_w = {(0, 0, 0), (31, 31, 31)}
|
||||||
colors = set(c for row in data for c in rgb5_pixels(row)) - b_and_w
|
colors = {c for row in rows for c in rgb5_pixels(row)} - b_and_w
|
||||||
if not colors:
|
if not colors:
|
||||||
colors = {(21, 21, 21), (10, 10, 10)}
|
colors = {(21, 21, 21), (10, 10, 10)}
|
||||||
elif len(colors) == 1:
|
elif len(colors) == 1:
|
||||||
@ -48,26 +51,26 @@ def fix_pal(filename):
|
|||||||
return False
|
return False
|
||||||
palette = tuple(sorted(colors | b_and_w, key=luminance, reverse=True))
|
palette = tuple(sorted(colors | b_and_w, key=luminance, reverse=True))
|
||||||
assert len(palette) == 4
|
assert len(palette) == 4
|
||||||
data = [list(map(palette.index, rgb5_pixels(row))) for row in data]
|
rows = [list(map(palette.index, rgb5_pixels(row))) for row in rows]
|
||||||
if palette == ((31, 31, 31), (21, 21, 21), (10, 10, 10), (0, 0, 0)):
|
if is_grayscale(palette):
|
||||||
data = [[3 - c for c in row] for row in data]
|
rows = [[3 - c for c in row] for row in rows]
|
||||||
writer = png.Writer(width, height, greyscale=True, bitdepth=2, compression=9)
|
writer = png.Writer(width, height, greyscale=True, bitdepth=2, compression=9)
|
||||||
else:
|
else:
|
||||||
palette = tuple(map(rgb5_to_rgb8, palette))
|
palette = tuple(map(rgb5_to_rgb8, palette))
|
||||||
writer = png.Writer(width, height, palette=palette, bitdepth=8, compression=9)
|
writer = png.Writer(width, height, palette=palette, bitdepth=8, compression=9)
|
||||||
with open(filename, 'wb') as file:
|
with open(filename, 'wb') as file:
|
||||||
writer.write(file, data)
|
writer.write(file, rows)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
print('Usage:', sys.argv[0], 'pic.png', file=sys.stderr)
|
print(f'Usage: {sys.argv[0]} pic.png', file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
for filename in sys.argv[1:]:
|
for filename in sys.argv[1:]:
|
||||||
if not filename.lower().endswith('.png'):
|
if not filename.lower().endswith('.png'):
|
||||||
print(filename, 'is not a .png file!', file=sys.stderr)
|
print(f'{filename} is not a .png file!', file=sys.stderr)
|
||||||
elif not fix_pal(filename):
|
elif not fix_pal(filename):
|
||||||
print(filename, 'has too many colors!', file=sys.stderr)
|
print(f'{filename} has too many colors!', file=sys.stderr)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
38
tools/rgb555.py
Executable file
38
tools/rgb555.py
Executable file
@ -0,0 +1,38 @@
|
|||||||
|
#!/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()
|
@ -2,7 +2,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Usage: python3 sym_comments.py file.asm [pokecrystal.sym] > file_commented.asm
|
Usage: python sym_comments.py file.asm [pokecrystal.sym] > file_commented.asm
|
||||||
|
|
||||||
Comments each label in file.asm with its bank:address from the sym file.
|
Comments each label in file.asm with its bank:address from the sym file.
|
||||||
"""
|
"""
|
||||||
@ -10,18 +10,19 @@ Comments each label in file.asm with its bank:address from the sym file.
|
|||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
|
||||||
if len(sys.argv) != 2 and len(sys.argv) != 3:
|
def main():
|
||||||
print('Usage: %s file.asm [pokecrystal.sym] > file_commented.asm' % sys.argv[0], file=sys.stderr)
|
if len(sys.argv) not in {2, 3}:
|
||||||
|
print(f'Usage: {sys.argv[0]} file.asm [pokecrystal.sym] > file_commented.asm', file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
wram_name = sys.argv[1]
|
wram_name = sys.argv[1]
|
||||||
sym_name = sys.argv[2] if len(sys.argv) == 3 else 'pokecrystal.sym'
|
sym_name = sys.argv[2] if len(sys.argv) == 3 else 'pokecrystal.sym'
|
||||||
|
|
||||||
sym_format = r'[A-Za-z_][A-Za-z0-9_#@]*'
|
sym_def_rx = re.compile(r'(^{sym})(:.*)|(^\.{sym})(.*)'.format(sym=r'[A-Za-z_][A-Za-z0-9_#@]*'))
|
||||||
sym_def_rx = re.compile(r'(^%s)(:.*)|(^\.%s)(.*)' % (sym_format, sym_format))
|
|
||||||
|
|
||||||
sym_addrs = {}
|
sym_addrs = {}
|
||||||
with open(sym_name, 'r', encoding='utf-8') as f:
|
with open(sym_name, 'r', encoding='utf-8') as file:
|
||||||
for line in f:
|
for line in file:
|
||||||
line = line.split(';', 1)[0].rstrip()
|
line = line.split(';', 1)[0].rstrip()
|
||||||
parts = line.split(' ', 1)
|
parts = line.split(' ', 1)
|
||||||
if len(parts) != 2:
|
if len(parts) != 2:
|
||||||
@ -29,12 +30,11 @@ with open(sym_name, 'r', encoding='utf-8') as f:
|
|||||||
addr, sym = parts
|
addr, sym = parts
|
||||||
sym_addrs[sym] = addr
|
sym_addrs[sym] = addr
|
||||||
|
|
||||||
with open(wram_name, 'r', encoding='utf-8') as f:
|
with open(wram_name, 'r', encoding='utf-8') as file:
|
||||||
cur_label = None
|
cur_label = None
|
||||||
for line in f:
|
for line in file:
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
m = re.match(sym_def_rx, line)
|
if (m = re.match(sym_def_rx, line)):
|
||||||
if m:
|
|
||||||
sym, rest = m.group(1), m.group(2)
|
sym, rest = m.group(1), m.group(2)
|
||||||
if sym is None and rest is None:
|
if sym is None and rest is None:
|
||||||
sym, rest = m.group(3), m.group(4)
|
sym, rest = m.group(3), m.group(4)
|
||||||
@ -47,3 +47,6 @@ with open(wram_name, 'r', encoding='utf-8') as f:
|
|||||||
addr = sym_addrs[key]
|
addr = sym_addrs[key]
|
||||||
line = sym + rest + ' ; ' + addr
|
line = sym + rest + ' ; ' + addr
|
||||||
print(line)
|
print(line)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
41
tools/toc.py
41
tools/toc.py
@ -2,7 +2,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Usage: python3 toc.py [-n] files.md...
|
Usage: python toc.py file.md
|
||||||
|
|
||||||
Replace a "## TOC" heading in a Markdown file with a table of contents,
|
Replace a "## TOC" heading in a Markdown file with a table of contents,
|
||||||
generated from the other headings in the file. Supports multiple files.
|
generated from the other headings in the file. Supports multiple files.
|
||||||
@ -18,16 +18,17 @@ toc_name = 'Contents'
|
|||||||
valid_toc_headings = {'## TOC', '##TOC'}
|
valid_toc_headings = {'## TOC', '##TOC'}
|
||||||
|
|
||||||
TocItem = namedtuple('TocItem', ['name', 'anchor', 'level'])
|
TocItem = namedtuple('TocItem', ['name', 'anchor', 'level'])
|
||||||
punctuation_regexp = re.compile(r'[^\w\- ]+')
|
punctuation_rx = re.compile(r'[^\w\- ]+')
|
||||||
specialchar_regexp = re.compile(r'[⅔]+')
|
numbered_heading_rx = re.compile(r'^[0-9]+\. ')
|
||||||
|
specialchar_rx = re.compile(r'[⅔]+')
|
||||||
|
|
||||||
def name_to_anchor(name):
|
def name_to_anchor(name):
|
||||||
# GitHub's algorithm for generating anchors from headings
|
# GitHub's algorithm for generating anchors from headings
|
||||||
# https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb
|
# https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb
|
||||||
anchor = name.strip().lower() # lowercase
|
anchor = name.strip().lower() # lowercase
|
||||||
anchor = re.sub(punctuation_regexp, '', anchor) # remove punctuation
|
anchor = re.sub(punctuation_rx, '', anchor) # remove punctuation
|
||||||
anchor = anchor.replace(' ', '-') # replace spaces with dash
|
anchor = anchor.replace(' ', '-') # replace spaces with dash
|
||||||
anchor = re.sub(specialchar_regexp, '', anchor) # remove misc special chars
|
anchor = re.sub(specialchar_rx, '', anchor) # remove misc special chars
|
||||||
anchor = quote(anchor) # url encode
|
anchor = quote(anchor) # url encode
|
||||||
return anchor
|
return anchor
|
||||||
|
|
||||||
@ -51,42 +52,46 @@ def get_toc_items(lines, toc_index):
|
|||||||
yield TocItem(name, anchor, level)
|
yield TocItem(name, anchor, level)
|
||||||
|
|
||||||
def toc_string(toc_items):
|
def toc_string(toc_items):
|
||||||
lines = ['## %s' % toc_name, '']
|
lines = [f'## {toc_name}', '']
|
||||||
for name, anchor, level in toc_items:
|
for name, anchor, level in toc_items:
|
||||||
padding = ' ' * level
|
padding = ' ' * level
|
||||||
line = '%s- [%s](#%s)' % (padding, name, anchor)
|
if re.match(numbered_heading_rx, name):
|
||||||
lines.append(line)
|
bullet, name = name.split('.', 1)
|
||||||
|
bullet += '.'
|
||||||
|
name = name.lstrip()
|
||||||
|
else:
|
||||||
|
bullet = '-'
|
||||||
|
lines.append(f'{padding}{bullet} [{name}](#{anchor})')
|
||||||
return '\n'.join(lines) + '\n'
|
return '\n'.join(lines) + '\n'
|
||||||
|
|
||||||
def add_toc(filename):
|
def add_toc(filename):
|
||||||
with open(filename, 'r', encoding='utf-8') as f:
|
with open(filename, 'r', encoding='utf-8') as file:
|
||||||
lines = f.readlines()
|
lines = file.readlines()
|
||||||
toc_index = get_toc_index(lines)
|
toc_index = get_toc_index(lines)
|
||||||
if toc_index is None:
|
if toc_index is None:
|
||||||
return None # no TOC heading
|
return None # no TOC heading
|
||||||
toc_items = list(get_toc_items(lines, toc_index))
|
toc_items = list(get_toc_items(lines, toc_index))
|
||||||
if not toc_items:
|
if not toc_items:
|
||||||
return False # no content headings
|
return False # no content headings
|
||||||
with open(filename, 'w', encoding='utf-8') as f:
|
with open(filename, 'w', encoding='utf-8') as file:
|
||||||
for i, line in enumerate(lines):
|
for i, line in enumerate(lines):
|
||||||
if i == toc_index:
|
if i == toc_index:
|
||||||
f.write(toc_string(toc_items))
|
file.write(toc_string(toc_items))
|
||||||
else:
|
else:
|
||||||
f.write(line)
|
file.write(line)
|
||||||
return True # OK
|
return True # OK
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
print('*** ERROR: No filenames specified')
|
print(f'Usage: {sys.argv[0]} file.md', file=sys.stderr)
|
||||||
print(__doc__)
|
sys.exit(1)
|
||||||
exit(1)
|
|
||||||
for filename in sys.argv[1:]:
|
for filename in sys.argv[1:]:
|
||||||
print(filename)
|
print(filename)
|
||||||
result = add_toc(filename)
|
result = add_toc(filename)
|
||||||
if result is None:
|
if result is None:
|
||||||
print('*** WARNING: No "## TOC" heading found')
|
print('Warning: No "## TOC" heading found', file=sys.stderr)
|
||||||
elif result is False:
|
elif result is False:
|
||||||
print('*** WARNING: No content headings found')
|
print('Warning: No content headings found', file=sys.stderr)
|
||||||
else:
|
else:
|
||||||
print('OK')
|
print('OK')
|
||||||
|
|
||||||
|
106
tools/unique.py
Executable file
106
tools/unique.py
Executable file
@ -0,0 +1,106 @@
|
|||||||
|
#!/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()
|
120
tools/unnamed.py
120
tools/unnamed.py
@ -1,19 +1,27 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from sys import stderr, exit
|
"""
|
||||||
from subprocess import Popen, PIPE
|
Usage: unnamed.py [-h] [-r rootdir] [-l count] pokecrystal.sym
|
||||||
from struct import unpack, calcsize
|
|
||||||
from enum import Enum
|
|
||||||
|
|
||||||
class symtype(Enum):
|
Parse the symfile to find unnamed symbols.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
import subprocess
|
||||||
|
import struct
|
||||||
|
import enum
|
||||||
|
import signal
|
||||||
|
|
||||||
|
class symtype(enum.Enum):
|
||||||
LOCAL = 0
|
LOCAL = 0
|
||||||
IMPORT = 1
|
IMPORT = 1
|
||||||
EXPORT = 2
|
EXPORT = 2
|
||||||
|
|
||||||
def unpack_file(fmt, file):
|
def unpack_from(fmt, file):
|
||||||
size = calcsize(fmt)
|
size = struct.calcsize(fmt)
|
||||||
return unpack(fmt, file.read(size))
|
return struct.unpack(fmt, file.read(size))
|
||||||
|
|
||||||
def read_string(file):
|
def read_string(file):
|
||||||
buf = bytearray()
|
buf = bytearray()
|
||||||
@ -21,111 +29,109 @@ def read_string(file):
|
|||||||
b = file.read(1)
|
b = file.read(1)
|
||||||
if b is None or b == b'\0':
|
if b is None or b == b'\0':
|
||||||
return buf.decode()
|
return buf.decode()
|
||||||
else:
|
|
||||||
buf += b
|
buf += b
|
||||||
|
|
||||||
|
|
||||||
# Fix broken pipe when using `head`
|
# Fix broken pipe when using `head`
|
||||||
from signal import signal, SIGPIPE, SIG_DFL
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
||||||
signal(SIGPIPE,SIG_DFL)
|
|
||||||
|
|
||||||
import argparse
|
parser = argparse.ArgumentParser(description='Parse the symfile to find unnamed symbols')
|
||||||
parser = argparse.ArgumentParser(description="Parse the symfile to find unnamed symbols")
|
parser.add_argument('symfile', type=argparse.FileType('r'),
|
||||||
parser.add_argument('symfile', type=argparse.FileType('r'), help="the list of symbols")
|
help='the list of symbols')
|
||||||
parser.add_argument('-r', '--rootdir', type=str, help="scan the output files to obtain a list of files with unnamed symbols (NOTE: will rebuild objects as necessary)")
|
parser.add_argument('-r', '--rootdir', type=str,
|
||||||
parser.add_argument('-l', '--list', type=int, default=0, help="output this many of each file's unnamed symbols (NOTE: requires -r)")
|
help='scan the output files to obtain a list of files with unnamed symbols (note: will rebuild objects as necessary)')
|
||||||
|
parser.add_argument('-l', '--list', type=int, default=0,
|
||||||
|
help="output this many of each file's unnamed symbols (note: requires -r)")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Get list of object files
|
# Get list of object files
|
||||||
objects = None
|
objects = None
|
||||||
if args.rootdir:
|
if args.rootdir:
|
||||||
for line in Popen(["make", "-C", args.rootdir, "-s", "-p", "DEBUG=1"],
|
for line in subprocess.Popen(['make', '-C', args.rootdir, '-s', '-p', 'DEBUG=1'],
|
||||||
stdout=PIPE).stdout.read().decode().split("\n"):
|
stdout=subprocess.PIPE).stdout.read().decode().split('\n'):
|
||||||
if line.startswith("pokecrystal_obj := "):
|
if line.startswith('pokecrystal_obj := '):
|
||||||
objects = line[19:].strip().split()
|
objects = line[19:].strip().split()
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print("Error: Object files not found!", file=stderr)
|
print('Error: Object files not found!', file=sys.stderr)
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Scan all unnamed symbols from the symfile
|
# Scan all unnamed symbols from the symfile
|
||||||
symbols_total = 0
|
symbols_total = 0
|
||||||
symbols = set()
|
symbols = set()
|
||||||
for line in args.symfile:
|
for line in args.symfile:
|
||||||
line = line.split(";")[0].strip()
|
line = line.split(';')[0].strip()
|
||||||
split = line.split(" ")
|
split = line.split(' ')
|
||||||
if len(split) < 2:
|
if len(split) < 2:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
symbols_total += 1
|
symbols_total += 1
|
||||||
|
|
||||||
symbol = " ".join(split[1:]).strip()
|
symbol = ' '.join(split[1:]).strip()
|
||||||
if symbol[-3:].lower() == split[0][-3:].lower():
|
if symbol[-3:].lower() == split[0][-3:].lower():
|
||||||
symbols.add(symbol)
|
symbols.add(symbol)
|
||||||
|
|
||||||
# If no object files were provided, just print what we know and exit
|
# If no object files were provided, just print what we know and exit
|
||||||
print("Unnamed pokecrystal symbols: %d (%.2f%% complete)" % (len(symbols),
|
unnamed_percent = 100 * (symbols_total - len(symbols)) / symbols_total
|
||||||
(symbols_total - len(symbols)) / symbols_total * 100))
|
print(f'Unnamed pokecrystal symbols: {len(symbols)} ({unnamed_percent:.2f}% complete)')
|
||||||
if not objects:
|
if not objects:
|
||||||
for sym in symbols:
|
for sym in symbols:
|
||||||
print(sym)
|
print(sym)
|
||||||
exit()
|
sys.exit()
|
||||||
|
|
||||||
# Count the amount of symbols in each file
|
# Count the amount of symbols in each file
|
||||||
files = {}
|
file_symbols = {}
|
||||||
for objfile in objects:
|
for objfile in objects:
|
||||||
f = open(objfile, "rb")
|
with open(objfile, 'rb') as file:
|
||||||
obj_ver = None
|
obj_ver = None
|
||||||
|
|
||||||
magic = unpack_file("4s", f)[0]
|
magic = unpack_from('4s', file)[0]
|
||||||
if magic == b'RGB6':
|
if magic == b'RGB6':
|
||||||
obj_ver = 6
|
obj_ver = 6
|
||||||
elif magic == b'RGB9':
|
elif magic == b'RGB9':
|
||||||
obj_ver = 10 + unpack_file("<I", f)[0]
|
obj_ver = 10 + unpack_from('<I', file)[0]
|
||||||
|
|
||||||
if obj_ver not in [6, 10, 11, 12, 13, 15, 16, 17, 18]:
|
if obj_ver not in [6, 10, 11, 12, 13, 15, 16, 17, 18]:
|
||||||
print("Error: File '%s' is of an unknown format." % objfile, file=stderr)
|
print(f"Error: File '{objfile}' is of an unknown format.", file=sys.stderr)
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
num_symbols = unpack_file("<I", f)[0]
|
num_symbols = unpack_from('<I', file)[0]
|
||||||
unpack_file("<I", f) # skip num sections
|
unpack_from('<I', file) # skip num sections
|
||||||
|
|
||||||
if obj_ver in [16, 17, 18]:
|
if obj_ver in [16, 17, 18]:
|
||||||
node_filenames = []
|
node_filenames = []
|
||||||
num_nodes = unpack_file("<I", f)[0]
|
num_nodes = unpack_from('<I', file)[0]
|
||||||
for x in range(num_nodes):
|
for x in range(num_nodes):
|
||||||
unpack_file("<II", f) # parent id, parent line no
|
unpack_from('<II', file) # parent id, parent line no
|
||||||
node_type = unpack_file("<B", f)[0]
|
node_type = unpack_from('<B', file)[0]
|
||||||
if node_type:
|
if node_type:
|
||||||
node_filenames.append(read_string(f))
|
node_filenames.append(read_string(file))
|
||||||
else:
|
else:
|
||||||
node_filenames.append("rept")
|
node_filenames.append('rept')
|
||||||
depth = unpack_file("<I", f)[0]
|
depth = unpack_from('<I', file)[0]
|
||||||
for i in range(depth):
|
for i in range(depth):
|
||||||
unpack_file("<I", f) # rept iterations
|
unpack_from('<I', file) # rept iterations
|
||||||
node_filenames.reverse()
|
node_filenames.reverse()
|
||||||
|
|
||||||
for x in range(num_symbols):
|
for _ in range(num_symbols):
|
||||||
sym_name = read_string(f)
|
sym_name = read_string(file)
|
||||||
sym_type = symtype(unpack_file("<B", f)[0] & 0x7f)
|
sym_type = symtype(unpack_from('<B', file)[0] & 0x7f)
|
||||||
if sym_type == symtype.IMPORT:
|
if sym_type == symtype.IMPORT:
|
||||||
continue
|
continue
|
||||||
if obj_ver in [16, 17, 18]:
|
if obj_ver in [16, 17, 18]:
|
||||||
sym_fileno = unpack_file("<I", f)[0]
|
sym_fileno = unpack_from('<I', file)[0]
|
||||||
sym_filename = node_filenames[sym_fileno]
|
sym_filename = node_filenames[sym_fileno]
|
||||||
else:
|
else:
|
||||||
sym_filename = read_string(f)
|
sym_filename = read_string(file)
|
||||||
unpack_file("<III", f)
|
unpack_from('<III', file)
|
||||||
if sym_name not in symbols:
|
if sym_name not in symbols:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if sym_filename not in files:
|
if sym_filename not in file_symbols:
|
||||||
files[sym_filename] = []
|
file_symbols[sym_filename] = []
|
||||||
files[sym_filename].append(sym_name)
|
file_symbols[sym_filename].append(sym_name)
|
||||||
|
|
||||||
# Sort the files, the one with the most amount of symbols first
|
# Sort the files, the one with the most amount of symbols first
|
||||||
files = sorted(((f, files[f]) for f in files), key=lambda x: len(x[1]), reverse=True)
|
file_symbols = sorted(file_symbols.items(), key=lambda item: len(item[1]), reverse=True)
|
||||||
for f in files:
|
for filename, unnamed_syms in file_symbols:
|
||||||
filename, unnamed = f
|
sym_list = ', '.join(unnamed_syms[:args.list])
|
||||||
sym_list = ', '.join(unnamed[:args.list])
|
print(f'{filename}: {len(unnamed_syms)}{": " + sym_list if sym_list else ""}')
|
||||||
print("%s: %d%s" % (filename, len(unnamed), ': ' + sym_list if sym_list else ''))
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Usage: python3 used_space.py [pokecrystal.map] [used_space.png]
|
Usage: python used_space.py [pokecrystal.map] [used_space.png]
|
||||||
|
|
||||||
Generate a PNG visualizing the space used by each bank in the ROM.
|
Generate a PNG visualizing the space used by each bank in the ROM.
|
||||||
"""
|
"""
|
||||||
@ -15,7 +15,7 @@ from mapreader import MapReader
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
mapfile = sys.argv[1] if len(sys.argv) >= 2 else 'pokecrystal.map'
|
mapfile = sys.argv[1] if len(sys.argv) >= 2 else 'pokecrystal.map'
|
||||||
filename = sys.argv[2] if len(sys.argv) >= 3 else 'used_space.png'
|
outfile = sys.argv[2] if len(sys.argv) >= 3 else 'used_space.png'
|
||||||
|
|
||||||
num_banks = 0x80
|
num_banks = 0x80
|
||||||
bank_mask = 0x3FFF
|
bank_mask = 0x3FFF
|
||||||
@ -29,16 +29,15 @@ def main():
|
|||||||
bank_width = pixels_per_bank // height # 8 pixels
|
bank_width = pixels_per_bank // height # 8 pixels
|
||||||
width = bank_width * num_banks # 1024 pixels
|
width = bank_width * num_banks # 1024 pixels
|
||||||
|
|
||||||
r = MapReader()
|
reader = MapReader()
|
||||||
with open(mapfile, 'r', encoding='utf-8') as f:
|
with open(mapfile, 'r', encoding='utf-8') as file:
|
||||||
l = f.readlines()
|
reader.read_map_data(file.readlines())
|
||||||
r.read_map_data(l)
|
|
||||||
|
|
||||||
hit_data = []
|
hit_data = []
|
||||||
default_bank_data = {'sections': [], 'used': 0, 'slack': bank_size}
|
default_bank_data = {'sections': [], 'used': 0, 'slack': bank_size}
|
||||||
for bank in range(num_banks):
|
for bank in range(num_banks):
|
||||||
hits = [0] * pixels_per_bank
|
hits = [0] * pixels_per_bank
|
||||||
bank_data = r.bank_data['ROM0 bank'] if bank == 0 else r.bank_data['ROMX bank']
|
bank_data = reader.bank_data['ROM0 bank' if bank == 0 else 'ROMX bank']
|
||||||
data = bank_data.get(bank, default_bank_data)
|
data = bank_data.get(bank, default_bank_data)
|
||||||
for s in data['sections']:
|
for s in data['sections']:
|
||||||
beg = s['beg'] & bank_mask
|
beg = s['beg'] & bank_mask
|
||||||
@ -49,18 +48,17 @@ def main():
|
|||||||
|
|
||||||
pixels = [[(0xFF, 0xFF, 0xFF)] * width for _ in range(height)]
|
pixels = [[(0xFF, 0xFF, 0xFF)] * width for _ in range(height)]
|
||||||
for bank, hits in enumerate(hit_data):
|
for bank, hits in enumerate(hit_data):
|
||||||
hue = 0 if not bank else 210 if bank % 2 else 270
|
hue = 0 if bank == 0 else 210 if bank % 2 else 270
|
||||||
for i, h in enumerate(hits):
|
for i, hit in enumerate(hits):
|
||||||
y = i // bank_width
|
x, y = i % bank_width + bank * bank_width, i // bank_width
|
||||||
x = i % bank_width + bank * bank_width
|
hls = (hue / 360, 1 - (85 * hit / bpp) / 100, 1)
|
||||||
hls = (hue / 360.0, 1.0 - (h / bpp * (100 - 15)) / 100.0, 1.0)
|
rgb = tuple(int(c * 0xFF) for c in hls_to_rgb(*hls))
|
||||||
rgb = tuple(c * 255 for c in hls_to_rgb(*hls))
|
|
||||||
pixels[y][x] = rgb
|
pixels[y][x] = rgb
|
||||||
|
|
||||||
png_data = [tuple(c for pixel in row for c in pixel) for row in pixels]
|
png_data = [tuple(c for pixel in row for c in pixel) for row in pixels]
|
||||||
with open(filename, 'wb') as f:
|
with open(outfile, 'wb') as file:
|
||||||
w = png.Writer(width, height)
|
writer = png.Writer(width, height, greyscale=False, bitdepth=8, compression=9)
|
||||||
w.write(f, png_data)
|
writer.write(file, png_data)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user