Debian Codegraph @

tools/delibdb

raw
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 dbf;
30
31 if (@ARGV < 4) {
32 print "Usage: $0 ptr.dbf file.res filename_field ptr_field <file_ext>\n";
33 print "Extracts files from res that are enumerated and offset in the dbf file. This is the LIBDB or ResourceDb style format.\n";
34 print "If file extension is not specified, then .ICN is used.\n";
35 exit 0;
36 }
37 my ($dbf_file, $res_file, $filename_field, $ptr_field, $ext) = @ARGV;
38 if (!defined($ext)) {
39 $ext = '.ICN';
40 }
41 my $res_fh;
42
43 my $dbf = dbf->read_file($dbf_file);
44 if (!$dbf) {
45 print "Error: Unable to read $dbf_file\n";
46 exit 0;
47 }
48 my $filename_idx = $dbf->get_field($filename_field);
49 if ($filename_idx < 0) {
50 print "Error: Invalid field '$filename_field'\n";
51 exit 1;
52 }
53 my $ptr_idx = $dbf->get_field($ptr_field);
54 if ($ptr_idx < 0) {
55 print "Error: Invalid field '$ptr_field'\n";
56 exit 1;
57 }
58 if (!open($res_fh, '<', $res_file)) {
59 print "Error: Unable to open $res_file\n";
60 exit 1;
61 }
62 my $records = $dbf->get_records();
63 for (my $i = 0; $i < $records; $i++) {
64 my $buf;
65 my $bytes;
66 my $len;
67 my $fh;
68 my $filename = dbf::trim($dbf->get_value($i, $filename_idx));
69 if (!defined($filename)) {
70 print "Error while reading record $i for $filename_field\n";
71 exit 1;
72 }
73 if ($filename eq '') {
74 print "No bitmap for record $i\n";
75 next;
76 }
77 my $packed_ptr = $dbf->get_value($i, $ptr_idx);
78 if (!defined($packed_ptr)) {
79 print "Error while reading record $i for $ptr_field\n";
80 exit 1;
81 }
82 my $ptr = unpack('L', $packed_ptr);
83 if (!defined($ptr)) {
84 print "Can't extract unsigned int (LE 4-bytes) $ptr_field value for record $i ($packed_ptr)\n";
85 next;
86 }
87 print "Extracting record $i for $filename (field $filename_idx) at $ptr_field=$ptr (field $ptr_idx)\n";
88 if (!seek($res_fh, $ptr, 0)) {
89 print "Error: Corrupt RES file: cannot seek to $ptr\n";
90 exit 1;
91 }
92 $bytes = read($res_fh, $buf, 4);
93 if ($bytes != 4) {
94 print "Error: Corrupt RES file: $bytes != 4\n";
95 exit 1;
96 }
97 $len = unpack('L', $buf);
98 $bytes = read($res_fh, $buf, $len);
99 if ($bytes != $len) {
100 print "Error: Corrupt RES file: $bytes != $len\n";
101 exit 1;
102 }
103 if (!open($fh, '>', "$filename$ext")) {
104 print "Unable to open file $filename$ext\n";
105 exit 1;
106 }
107 print $fh $buf;
108 close($fh);
109 }
110 close($res_fh);
111
112 exit 0;