Created
February 18, 2014 16:36
-
-
Save LemonBoy/9074479 to your computer and use it in GitHub Desktop.
Let's play with G6_GRAPH.BIN!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Extract/import database entries from G6_GRAPH.BIN database | |
# The Lemon Man (C) 2014 | |
use strict; | |
use warnings; | |
use File::Slurp; | |
use JSON qw(from_json to_json); | |
sub json_to_db ($) { | |
my $text = read_file shift; | |
my %db = %{from_json ($text)}; | |
my @off_tab = (); | |
my $off_pos = 0; | |
my $thumb_buf = ""; | |
foreach my $code (keys %db) { | |
my $thumb = read_file($db{$code}{'thumb'}, { binmode => ':raw' }); | |
push @off_tab, ($off_pos, length $thumb); | |
$off_pos += length $thumb; | |
$thumb_buf .= $thumb; | |
} | |
my $out .= pack 'n', scalar keys %db; | |
foreach my $code (keys %db) { | |
$out .= pack 'A[4]A[12]NNn', | |
$code, $db{$code}{'title'}, | |
shift @off_tab, shift @off_tab, | |
$db{$code}{'unk18'}; | |
} | |
$out .= $thumb_buf; | |
} | |
sub db_to_json ($$) { | |
my ($path, $extract_thumbnail) = @_; | |
open (my $fd, $path) or die "open"; | |
binmode $fd; | |
my %db = (); | |
my $buf = ""; | |
read $fd, $buf, 2; | |
my $count = unpack 'n', $buf; | |
my $dupe = 0; | |
for my $i (0 .. $count - 1) { | |
read $fd, $buf, 26; | |
my ($code, $title, $offset, $size, $unk_18) = unpack 'A[4]A[12]NNn', $buf; | |
do { $dupe++; } if exists $db{$code}; | |
$db{$code} = { | |
'title' => $title, | |
'thumb' => "$code.bmp", | |
'unk18' => $unk_18 | |
}; | |
if ($extract_thumbnail) { | |
my $pos = tell $fd; | |
my $buf = ""; | |
seek $fd, $offset, 0; | |
read $fd, $buf, $size; | |
seek $fd, $pos, 0; | |
write_file "$code.bmp", { binmode => 'raw' }, $buf; | |
} | |
} | |
print "$dupe/$count entries were duplicated.\n"; | |
my $json_db = to_json \%db, { pretty => 1 }; | |
close $fd; | |
return $json_db; | |
} | |
die "Usage : $0 [-j | -d] <file>" if $#ARGV != 1; | |
db_to_json $ARGV[1], 1 if $ARGV[0] eq '-d'; | |
json_to_db $ARGV[1] if $ARGV[0] eq '-j'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment