Debian Codegraph @

tools/libresx

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 File::Basename;
27 use File::Spec;
28
29 if (@ARGV != 2) {
30 print "Usage: $0 FILE.RES src_dir\n";
31 print "Puts all files in src_dir into the res archive\n";
32 exit 1;
33 }
34
35 my ($out_file, $src_dir) = @ARGV;
36
37 my $dir;
38 if (!opendir($dir, $src_dir)) {
39 print "Error: Unable to read $src_dir\n";
40 exit 1;
41 }
42 my @files = sort grep {-f File::Spec->catfile($src_dir, $_)} readdir($dir);
43 close($dir);
44
45 my @sizes = map {-s File::Spec->catfile($src_dir, $_)} @files;
46
47 my $rec_count = scalar(@files);
48
49 # RESX Header
50 # ----------------------------------------------------------------------------
51 # short (2 bytes): Number of records
52 #
53 # char[9] (9 bytes): Record name
54 # int (4 bytes): offset to record
55 # Repeat for each record
56 #
57 # char[9] (9 bytes): Dummy record (all zeros)
58 # int (4 bytes): final offset for size calculation of last record
59
60 my $fh;
61 if (!open($fh, '>', $out_file)) {
62 print "Error: Cannot open $out_file\n";
63 exit 1;
64 }
65
66 my $header_size = 2 + 13*$rec_count + 13;
67 print $fh pack('s', $rec_count);
68
69 my $offset = $header_size;
70 for (my $i = 0; $i < $rec_count; $i++) {
71 my $record_name = uc $files[$i];
72 $record_name =~ s/\.\w+$//;
73 $record_name =~ s/^\d+-//;
74 print $fh pack('Z9l', $record_name, $offset);
75 $offset += $sizes[$i];
76 }
77 print $fh "\0\0\0\0\0\0\0\0\0";
78 print $fh pack('l', $offset); # final offset for size calculation of last rec
79
80 for (my $i = 0; $i < $rec_count; $i++) {
81 my $fh2;
82 if (!open($fh2, '<', File::Spec->catfile($src_dir, $files[$i]))) {
83 print "Error: Cannot open ", File::Spec->catfile($src_dir, $files[$i]), "\n";
84 exit 1;
85 }
86 my $data = do { local $/; <$fh2> };
87 close($fh2);
88 print $fh $data;
89 }
90 close($fh);
91
92 exit 0;