This commit is contained in:
dannye 2023-04-19 20:04:38 -05:00
commit 772520c2c2
63 changed files with 4114 additions and 1216 deletions

View file

@ -19,6 +19,8 @@
#error Define USAGE_OPTS before including common.h!
#endif
#define COUNTOF(...) (sizeof(__VA_ARGS__) / sizeof(*(__VA_ARGS__)))
#define error_exit(...) exit((fprintf(stderr, PROGRAM_NAME ": " __VA_ARGS__), 1))
noreturn void usage_exit(int status) {

56
tools/consts.py Executable file
View file

@ -0,0 +1,56 @@
#!/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()

70
tools/free_space.awk Executable file
View file

@ -0,0 +1,70 @@
#!/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
}
}
rom_bank && toupper($0) ~ /^[ \t]*EMPTY$/ {
# Empty bank
register_bank(16384)
}
rom_bank && toupper($0) ~ /^[ \t]*SLACK:[ \t]/ {
if ($2 ~ /\$[0-9A-F]+/) {
register_bank(strtonum("0x" substr($2, 2)))
} else {
printf "Malformed slack line? \"%s\" does not start with '$'\n", $2
}
}
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
}

View file

@ -165,7 +165,7 @@ int strfind(const char *s, const char *list[], int count) {
return -1;
}
#define vstrfind(s, ...) strfind(s, (const char *[]){__VA_ARGS__}, sizeof (const char *[]){__VA_ARGS__} / sizeof(const char *))
#define vstrfind(s, ...) strfind(s, (const char *[]){__VA_ARGS__}, COUNTOF((const char *[]){__VA_ARGS__}))
int parse_arg_value(const char *arg, bool absolute, const struct Symbol *symbols, const char *patch_name) {
// Comparison operators for "ConditionValueB" evaluate to their particular values

77
tools/palfix.py Executable file
View file

@ -0,0 +1,77 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Usage: python palfix.py image.png
Fix the palette format of the input image. Colored images (Gen 2 Pokémon or
trainer sprites) will become indexed, with a palette sorted {white, light
color, dark color, black}. Grayscale images (all Gen 1 images) will become
two-bit grayscale.
"""
import sys
import png
def rgb8_to_rgb5(c):
r, g, b = c
return (r // 8, g // 8, b // 8)
def rgb5_to_rgb8(c):
r, g, b = c
return (r * 8 + r // 4, g * 8 + g // 4, b * 8 + b // 4)
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 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):
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 = [list(map(palette.index, rgb5_pixels(row))) for row in rows]
if is_grayscale(palette):
rows = [[3 - c for c in row] for row in rows]
writer = png.Writer(width, height, greyscale=True, bitdepth=2, compression=9)
else:
palette = tuple(map(rgb5_to_rgb8, palette))
writer = png.Writer(width, height, palette=palette, bitdepth=8, 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()

0
tools/pic.py Normal file → Executable file
View file

View file

@ -32,7 +32,7 @@ void write_bit(int bit) {
}
void compress_plane(uint8_t *plane, int width) {
static int nybble_lookup[2][0x10] = {
static int gray_codes[2][0x10] = {
{0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9, 0x8},
{0x8, 0x9, 0xB, 0xA, 0xE, 0xF, 0xD, 0xC, 0x4, 0x5, 0x7, 0x6, 0x2, 0x3, 0x1, 0x0},
};
@ -44,10 +44,10 @@ void compress_plane(uint8_t *plane, int width) {
}
int j = i / width + m * width * 8;
int nybble_hi = (plane[j] >> 4) & 0xF;
int code_1 = nybble_lookup[nybble_lo & 1][nybble_hi];
int code_hi = gray_codes[nybble_lo & 1][nybble_hi];
nybble_lo = plane[j] & 0xF;
int code_2 = nybble_lookup[nybble_hi & 1][nybble_lo];
plane[j] = (code_1 << 4) | code_2;
int code_lo = gray_codes[nybble_hi & 1][nybble_lo];
plane[j] = (code_hi << 4) | code_lo;
}
}
@ -105,7 +105,7 @@ int interpret_compress(uint8_t *plane1, uint8_t *plane2, int mode, int order, in
}
cur_bit = 7;
cur_byte = 0;
memset(compressed, 0, sizeof(compressed) / sizeof(*compressed));
memset(compressed, 0, COUNTOF(compressed));
compressed[0] = (width << 4) | width;
write_bit(order);
uint8_t bit_groups[0x1000] = {0};
@ -113,7 +113,7 @@ int interpret_compress(uint8_t *plane1, uint8_t *plane2, int mode, int order, in
for (int plane = 0; plane < 2; plane++) {
int type = 0;
int nums = 0;
memset(bit_groups, 0, sizeof(bit_groups) / sizeof(*bit_groups));
memset(bit_groups, 0, COUNTOF(bit_groups));
for (int x = 0; x < width; x++) {
for (int bit = 0; bit < 8; bit += 2) {
for (int y = 0, byte = x * width * 8; y < width * 8; y++, byte++) {
@ -129,7 +129,7 @@ int interpret_compress(uint8_t *plane1, uint8_t *plane2, int mode, int order, in
write_bit(0);
}
type = 1;
memset(bit_groups, 0, sizeof(bit_groups) / sizeof(*bit_groups));
memset(bit_groups, 0, COUNTOF(bit_groups));
index = 0;
} else {
if (!type) {
@ -171,7 +171,7 @@ int compress(uint8_t *data, int width) {
plane1[i] = data[i * 2];
plane2[i] = data[i * 2 + 1];
}
uint8_t current[sizeof(compressed) / sizeof(*compressed)] = {0};
uint8_t current[COUNTOF(compressed)] = {0};
int compressed_size = -1;
for (int mode = 1; mode < 4; mode++) {
for (int order = 0; order < 2; order++) {
@ -181,12 +181,12 @@ int compress(uint8_t *data, int width) {
int new_size = interpret_compress(plane1, plane2, mode, order, width);
if (compressed_size == -1 || new_size < compressed_size) {
compressed_size = new_size;
memset(current, 0, sizeof(current) / sizeof(*current));
memset(current, 0, COUNTOF(current));
memcpy(current, compressed, compressed_size / 8);
}
}
}
memset(compressed, 0, sizeof(compressed) / sizeof(*compressed));
memset(compressed, 0, COUNTOF(compressed));
memcpy(compressed, current, compressed_size / 8);
free(plane1);
free(plane2);

2357
tools/png.py Normal file

File diff suppressed because it is too large Load diff

38
tools/rgb555.py Executable file
View 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()

52
tools/sym_comments.py Executable file
View file

@ -0,0 +1,52 @@
#!/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 Executable file
View file

@ -0,0 +1,99 @@
#!/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 Executable file
View 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()

View file

@ -1,130 +1,137 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sys import stderr, exit
from subprocess import Popen, PIPE
from struct import unpack, calcsize
from enum import Enum
"""
Usage: unnamed.py [-h] [-r rootdir] [-l count] pokered.sym
class symtype(Enum):
LOCAL = 0
IMPORT = 1
EXPORT = 2
Parse the symfile to find unnamed symbols.
"""
def unpack_file(fmt, file):
size = calcsize(fmt)
return unpack(fmt, file.read(size))
import sys
import argparse
import subprocess
import struct
import enum
import signal
class symtype(enum.Enum):
LOCAL = 0
IMPORT = 1
EXPORT = 2
def unpack_from(fmt, file):
size = struct.calcsize(fmt)
return struct.unpack(fmt, file.read(size))
def read_string(file):
buf = bytearray()
while True:
b = file.read(1)
if b is None or b == b'\0':
return buf.decode()
else:
buf += b
buf = bytearray()
while True:
b = file.read(1)
if b is None or b == b'\0':
return buf.decode()
buf += b
# Fix broken pipe when using `head`
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
import argparse
parser = argparse.ArgumentParser(description="Parse the symfile to find unnamed symbols")
parser.add_argument('symfile', type=argparse.FileType('r'), 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('-l', '--list', type=int, default=0, help="output this many of each file's unnamed symbols (NOTE: requires -r)")
parser = argparse.ArgumentParser(description='Parse the symfile to find unnamed symbols')
parser.add_argument('symfile', type=argparse.FileType('r'),
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('-l', '--list', type=int, default=0,
help="output this many of each file's unnamed symbols (note: requires -r)")
args = parser.parse_args()
# Get list of object files
objects = None
if args.rootdir:
for line in Popen(["make", "-C", args.rootdir, "-s", "-p", "DEBUG=1"],
stdout=PIPE).stdout.read().decode().split("\n"):
if line.startswith("pokered_obj := "):
objects = line[15:].strip().split()
break
else:
print("Error: Object files not found!", file=stderr)
exit(1)
for line in subprocess.Popen(['make', '-C', args.rootdir, '-s', '-p', 'DEBUG=1'],
stdout=subprocess.PIPE).stdout.read().decode().split('\n'):
if line.startswith('pokered_obj :='):
objects = line[len('pokered_obj :='):].strip().split()
break
else:
print('Error: Object files not found!', file=sys.stderr)
sys.exit(1)
# Scan all unnamed symbols from the symfile
symbols_total = 0
symbols = set()
for line in args.symfile:
line = line.split(";")[0].strip()
split = line.split(" ")
if len(split) < 2:
continue
line = line.split(';')[0].strip()
split = line.split(' ')
if len(split) < 2:
continue
symbols_total += 1
symbols_total += 1
symbol = " ".join(split[1:]).strip()
if symbol[-3:].lower() == split[0][-3:].lower():
symbols.add(symbol)
symbol = ' '.join(split[1:]).strip()
if symbol[-3:].lower() == split[0][-3:].lower():
symbols.add(symbol)
# If no object files were provided, just print what we know and exit
print("Unnamed pokered symbols: %d (%.2f%% complete)" % (len(symbols),
(symbols_total - len(symbols)) / symbols_total * 100))
unnamed_percent = 100 * (symbols_total - len(symbols)) / symbols_total
print(f'Unnamed pokered symbols: {len(symbols)} ({unnamed_percent:.2f}% complete)')
if not objects:
for sym in symbols:
print(sym)
exit()
for sym in symbols:
print(sym)
sys.exit()
# Count the amount of symbols in each file
files = {}
file_symbols = {}
for objfile in objects:
f = open(objfile, "rb")
obj_ver = None
with open(objfile, 'rb') as file:
obj_ver = None
magic = unpack_file("4s", f)[0]
if magic == b'RGB6':
obj_ver = 6
elif magic == b'RGB9':
obj_ver = 10 + unpack_file("<I", f)[0]
magic = unpack_from('4s', file)[0]
if magic == b'RGB6':
obj_ver = 6
elif magic == b'RGB9':
obj_ver = 10 + unpack_from('<I', file)[0]
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)
exit(1)
if obj_ver not in [6, 10, 11, 12, 13, 15, 16, 17, 18, 19]:
print(f"Error: File '{objfile}' is of an unknown format.", file=sys.stderr)
sys.exit(1)
num_symbols = unpack_file("<I", f)[0]
unpack_file("<I", f) # skip num sections
num_symbols = unpack_from('<I', file)[0]
unpack_from('<I', file) # skip num sections
if obj_ver in [16, 17, 18]:
node_filenames = []
num_nodes = unpack_file("<I", f)[0]
for x in range(num_nodes):
unpack_file("<II", f) # parent id, parent line no
node_type = unpack_file("<B", f)[0]
if node_type:
node_filenames.append(read_string(f))
else:
node_filenames.append("rept")
depth = unpack_file("<I", f)[0]
for i in range(depth):
unpack_file("<I", f) # rept iterations
node_filenames.reverse()
if obj_ver in [16, 17, 18, 19]:
node_filenames = []
num_nodes = unpack_from('<I', file)[0]
for x in range(num_nodes):
unpack_from('<II', file) # parent id, parent line no
node_type = unpack_from('<B', file)[0]
if node_type:
node_filenames.append(read_string(file))
else:
node_filenames.append('rept')
depth = unpack_from('<I', file)[0]
for i in range(depth):
unpack_from('<I', file) # rept iterations
node_filenames.reverse()
for x in range(num_symbols):
sym_name = read_string(f)
sym_type = symtype(unpack_file("<B", f)[0] & 0x7f)
if sym_type == symtype.IMPORT:
continue
if obj_ver in [16, 17, 18]:
sym_fileno = unpack_file("<I", f)[0]
sym_filename = node_filenames[sym_fileno]
else:
sym_filename = read_string(f)
unpack_file("<III", f)
if sym_name not in symbols:
continue
for _ in range(num_symbols):
sym_name = read_string(file)
sym_type = symtype(unpack_from('<B', file)[0] & 0x7f)
if sym_type == symtype.IMPORT:
continue
if obj_ver in [16, 17, 18, 19]:
sym_fileno = unpack_from('<I', file)[0]
sym_filename = node_filenames[sym_fileno]
else:
sym_filename = read_string(file)
unpack_from('<III', file)
if sym_name not in symbols:
continue
if sym_filename not in files:
files[sym_filename] = []
files[sym_filename].append(sym_name)
if sym_filename not in file_symbols:
file_symbols[sym_filename] = []
file_symbols[sym_filename].append(sym_name)
# 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)
for f in files:
filename, unnamed = f
sym_list = ', '.join(unnamed[:args.list])
print("%s: %d%s" % (filename, len(unnamed), ': ' + sym_list if sym_list else ''))
file_symbols = sorted(file_symbols.items(), key=lambda item: len(item[1]), reverse=True)
for filename, unnamed_syms in file_symbols:
sym_list = ', '.join(unnamed_syms[:args.list])
print(f'{filename}: {len(unnamed_syms)}{": " + sym_list if sym_list else ""}')