Skip to content

Instantly share code, notes, and snippets.

@camthesaxman
Last active August 27, 2017 03:18
Show Gist options
  • Save camthesaxman/b892eac8cf7c6e548c6a72c33633f22b to your computer and use it in GitHub Desktop.
Save camthesaxman/b892eac8cf7c6e548c6a72c33633f22b to your computer and use it in GitHub Desktop.
#!/bin/perl
# This script converts pokeruby data from assembly to C
# Usage: ./pokeruby-data.pl [file] [type] [line]
use strict;
use warnings;
use Switch;
my $file;
sub handle_AnimCmd {
my $line = <$file>;
$line =~ /^(\w+):/;
print "const union AnimCmd $1\[] =\n{\n";
while (defined($line = <$file>) && ($line !~ /^$/)) {
my @tokens = split ' ', $line;
foreach my $i (0..$#tokens) {$tokens[$i] =~ s/,//}
print ' ';
switch ($tokens[0]) {
case /frame/ {
my @macroArgs = ($tokens[1], $tokens[2]);
if ($line =~ /H_FLIP/) {push @macroArgs, '.hFlip = TRUE'}
if ($line =~ /V_FLIP/) {push @macroArgs, '.vFlip = TRUE'}
print "ANIMCMD_FRAME(", join(', ', @macroArgs), "),\n";
}
case /loop/ {
print "ANIMCMD_LOOP($tokens[1]),\n";
}
case /jump/ {
print "ANIMCMD_JUMP($tokens[1]),\n";
}
case /end/ {
print "ANIMCMD_END,\n";
}
}
}
print "};\n";
}
sub handle_AffineAnimCmd {
my $line = <$file>;
$line =~ /^(\w+):/;
print "const union AffineAnimCmd $1\[] =\n{\n";
while (defined($line = <$file>) && ($line !~ /^$/)) {
my @tokens = split ' ', $line;
foreach my $i (0..$#tokens) {$tokens[$i] =~ s/,//}
print ' ';
switch ($tokens[0]) {
case /frame/ {
print "AFFINEANIMCMD_FRAME($tokens[1], $tokens[2], $tokens[3], $tokens[4]),\n";
}
case /loop/ {
print "AFFINEANIMCMD_LOOP($tokens[1]),\n";
}
case /jump/ {
print "AFFINEANIMCMD_JUMP($tokens[1]),\n";
}
case /end/ {
print "AFFINEANIMCMD_END,\n";
}
}
}
print "};\n";
}
sub handle_SpriteTemplate {
my $line = <$file>;
$line =~ /^(\w+):/;
print "const struct SpriteTemplate $1 =\n{\n";
$line = <$file>;
my @tokens = split ' ', $line;
foreach my $i (0..$#tokens) {$tokens[$i] =~ s/,//}
print " .tileTag = $tokens[1],\n",
" .paletteTag = $tokens[2],\n",
" .oam = &$tokens[3],\n",
" .anims = $tokens[4],\n",
" .images = $tokens[5],\n",
" .affineAnims = $tokens[6],\n",
" .callback = $tokens[7],\n",
"};\n";
}
sub handle_OamData {
my $line = <$file>;
$line =~ /^(\w+):/;
print "const struct OamData $1 =\n{\n";
my @halfwords = ();
while (defined($line = <$file>) && ($line !~ /^$/)) {
$line =~ /2byte (\w+)$/;
push @halfwords, hex $1;
}
if (@halfwords < 4) {$halfwords[3] = 0}
print " .y = ", $halfwords[0] & 0xFF, ",\n",
" .affineMode = ", ($halfwords[0] >> 8) & 3, ",\n",
" .objMode = ", ($halfwords[0] >> 10) & 3, ",\n",
" .mosaic = ", ($halfwords[0] >> 12) & 1, ",\n",
" .bpp = ", ($halfwords[0] >> 13) & 1, ",\n",
" .shape = ", ($halfwords[0] >> 14) & 3, ",\n",
" .x = ", $halfwords[1] & 0x1FF, ",\n",
" .matrixNum = ", ($halfwords[1] >> 9) & 0x1F, ",\n",
" .size = ", ($halfwords[1] >> 14) & 3, ",\n",
" .tileNum = ", $halfwords[2] & 0x3FF, ",\n",
" .priority = ", ($halfwords[2] >> 10) & 3, ",\n",
" .paletteNum = ", ($halfwords[2] >> 12) & 0xF, ",\n",
" .affineParam = ", $halfwords[3], ",\n",
"};\n";
}
my %handlers = (
"AnimCmd" => \&handle_AnimCmd,
"AffineAnimCmd" => \&handle_AffineAnimCmd,
"SpriteTemplate" => \&handle_SpriteTemplate,
"OamData" => \&handle_OamData,
);
open($file, $ARGV[0])
or die "Could not open file $ARGV[0]\n";
$#ARGV == 2
or die 'Usage: ./pokeruby-data.pl [file] [type] [line number]';
exists $handlers{$ARGV[1]}
or die "Don't know how to dump $ARGV[1]\n";
# skip to line number
for (my $i = 1; $i < $ARGV[2]; $i++) {<$file>}
# call appropriate handler
&{$handlers{$ARGV[1]}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment