| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Seven Kingdoms: Ancient Adversaries |
| 4 |
# |
| 5 |
# Copyright 1997,1998 Enlight Software Ltd. |
| 6 |
# Copyright 2017 Jesse Allen |
| 7 |
# |
| 8 |
# This program is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation, either version 2 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
# |
| 21 |
# |
| 22 |
|
| 23 |
use warnings; |
| 24 |
use strict; |
| 25 |
|
| 26 |
use FindBin; |
| 27 |
use lib $FindBin::Bin; |
| 28 |
|
| 29 |
use File::Spec; |
| 30 |
|
| 31 |
use dbf; |
| 32 |
|
| 33 |
if (@ARGV < 5) { |
| 34 |
print <<EOF; |
| 35 |
Usage: $0 ptr.dbf file.res input_dir filename_field ptr_field file_ext |
| 36 |
Puts all files defined by dbf in res. Input_dir must have all the files. |
| 37 |
A repacked res file may change the offsets, so a ptr.dbf.new is written |
| 38 |
to sync these changes, which you will need to rename and repack std.set. |
| 39 |
EOF |
| 40 |
exit 0; |
| 41 |
} |
| 42 |
my ($dbf_file, $res_file, $input_dir, $filename_field, $ptr_field, $ext) = @ARGV; |
| 43 |
if (!defined($ext)) { |
| 44 |
$ext = '.ICN'; |
| 45 |
} |
| 46 |
my $res_fh; |
| 47 |
|
| 48 |
my $dbf = dbf->read_file($dbf_file); |
| 49 |
if (!$dbf) { |
| 50 |
print "Error: Unable to read $dbf_file\n"; |
| 51 |
exit 1; |
| 52 |
} |
| 53 |
my $filename_idx = $dbf->get_field($filename_field); |
| 54 |
if ($filename_idx < 0) { |
| 55 |
print "Error: Invalid field '$filename_field'\n"; |
| 56 |
exit 1; |
| 57 |
} |
| 58 |
my $ptr_idx = $dbf->get_field($ptr_field); |
| 59 |
if ($ptr_idx < 0) { |
| 60 |
print "Error: Invalid field '$ptr_field'\n"; |
| 61 |
exit 1; |
| 62 |
} |
| 63 |
my $records = $dbf->get_records(); |
| 64 |
my %file_order; |
| 65 |
for (my $i = 0; $i < $records; $i++) { |
| 66 |
my $buf; |
| 67 |
my $bytes; |
| 68 |
my $len; |
| 69 |
my $filename = dbf::trim($dbf->get_value($i, $filename_idx)); |
| 70 |
if (!defined($filename)) { |
| 71 |
print "Error: Can't read record $i for $filename_field\n"; |
| 72 |
next; |
| 73 |
} |
| 74 |
my $packed_ptr = $dbf->get_value($i, $ptr_idx); |
| 75 |
if (!defined($packed_ptr)) { |
| 76 |
print "Error: Can't read record $i for $ptr_field\n"; |
| 77 |
next; |
| 78 |
} |
| 79 |
my $ptr = unpack('L', $packed_ptr); |
| 80 |
if (!defined($ptr)) { |
| 81 |
print "Error: Can't extract $ptr_field pointer for record $i\n"; |
| 82 |
next; |
| 83 |
} |
| 84 |
$file_order{$ptr} = [$i, $filename]; |
| 85 |
} |
| 86 |
if (!open($res_fh, '>', $res_file)) { |
| 87 |
print "Error: Unable to open $res_file\n"; |
| 88 |
exit 1; |
| 89 |
} |
| 90 |
my @ptrs = sort {$a <=> $b} (keys(%file_order)); |
| 91 |
my $offset = 0; |
| 92 |
for (my $i = 0; $i < @ptrs; $i++) { |
| 93 |
my $record; |
| 94 |
my $infile; |
| 95 |
my $file; |
| 96 |
my $size; |
| 97 |
my $buf; |
| 98 |
$record = $file_order{$ptrs[$i]}; |
| 99 |
$infile = File::Spec->catfile($input_dir, $record->[1]) . $ext; |
| 100 |
if (! -f $infile) { |
| 101 |
print "Error: No such file $infile, found in record $record->[0]\n"; |
| 102 |
exit 1; |
| 103 |
} |
| 104 |
if (!open($file, '<', $infile)) { |
| 105 |
print "Error: Cannot open $infile\n"; |
| 106 |
exit 1; |
| 107 |
} |
| 108 |
$size = -s $infile; |
| 109 |
if (read($file, $buf, $size) != $size) { |
| 110 |
print "Error: Could not read $infile\n"; |
| 111 |
exit 1; |
| 112 |
} |
| 113 |
close($file); |
| 114 |
print $res_fh pack('L', $size); |
| 115 |
print $res_fh $buf; |
| 116 |
$dbf->set_value($record->[0], $ptr_idx, pack('L', $offset)); |
| 117 |
$offset += $size + 4; # includes ptr |
| 118 |
} |
| 119 |
close($res_fh); |
| 120 |
$dbf->write_file("$dbf_file.new"); |
| 121 |
|
| 122 |
exit 0; |