# charset="UTF-8" # encoding="UTF-8" # fwNES <-> iNES for *.fds #!/usr/bin/perl require 5.002; use MacPerl; use strict; # use diagnostics; $/ = "\1".'*NINTENDO-HVC*'; sub _ { "FDS\x1A", pack('C', $#_), "\0" x 11, @_; } while (defined(my $arg = shift @ARGV)) { open FDS, "< $arg" or die; binmode FDS;# DOS/WIN read FDS, my $header, 16; if ($header =~ /^FDS\x1A[\x01-\x04]/o) { (my $ines = $arg) =~ s/ ?(?:\(fw\))?(?:\.fds)?$/(i).fds/i; open INES, "> $ines" or die; binmode INES; print INES ; close INES; eval { MacPerl::SetFileInfo('NeRB', 'NeSd', $ines) }; # Nestopia # eval { MacPerl::SetFileInfo('iNES', 'Disk', $ines) }; # iNES } else { (my $fwnes = $arg) =~ s/ ?(?:\(i\))?(\.fds)?$/(fw).fds/i; open FWNES, "> $fwnes" or die; binmode FWNES; seek FDS, 0, 0; print FWNES _ ; close FWNES; eval { MacPerl::SetFileInfo('mcFC', 'FDS ', $fwnes) }; # MacFC } close FDS; } __END__ #!/usr/bin/env python # File Creator, File Type fwnes = ('mcFC', 'FDS ') # MacFC #ines = ('iNES', 'Disk') # iNes ines = ('NeRB', 'NeSd') # Nestopia import getopt import os import re import string import sys mac = os.name == 'mac' or sys.platform == 'darwin' if mac: import MacOS import macostools fds = re.compile(r'^FDS\x1A[\x01-\x04]') ext = re.compile(r' ?(\((?:i|fw)\))?(\.fds)?$', re.I) def stage(flop): return 'FDS\x1A' + chr(string.count(flop, '\1*NINTENDO-HVC*')) + ('\0' * 11) + flop def eachfile(x, o='-f', f=None): i = open(x, 'rb') if o == '-i' and fds.match(i.read(16)): c, t = ines f = ext.sub(r'(i).fds', x) o = open(f, 'wb') o.write(i.read()) o.close() elif o == '-f' and not fds.match(i.read(16)): c, t = fwnes i.seek(0) f = ext.sub(r'(fw).fds', x) o = open(f, 'wb') o.write(stage(i.read())) o.close() i.close() if mac and f: MacOS.SetCreatorAndType(f, c, t) macostools.touched(f) def usage(): print "Usage:", sys.argv[0], """-[fi] files... -f: Insert header (fwNES) -i: Delete header (iNES) """ sys.exit() def optget(): opts, args = getopt.getopt(sys.argv[1:], 'fi') if not opts or not opts[0][0]: usage() return opts[0][0], args if __name__ == '__main__': o, args = optget() for x in args: if os.path.isfile(x): eachfile(x, o)