Debian Codegraph @

lib/adm/collect_images.pl

raw
1 #!/usr/bin/perl
2 #
3 # this script goes through and collects the various image sets into crossfire-x.png
4 # files, where x is the numbers from the image_info file. This
5 # collects all the images at once, whether or not they have
6 # been changed or not.
7
8 # This script collects all the images. If given a -archive option, it
9 # does some additional work - checksumming the images, making a bmaps.client
10 # file, and then tarring that data up. This data can then be used on the
11 # client as a fast way to 'bootstrap' the clients images.
12
13 use FileHandle;
14
15 die("No arch directory - will not rebuild $mode image file") if (! -e "arch");
16
17 $archive = 0;
18 $src_path = ".";
19 $TMPDIR="/tmp";
20
21 # What we will call the collection of images.
22 $ARCHNAME="crossfire-images";
23 $DESTDIR="$TMPDIR/$ARCHNAME";
24
25 # Maximum expected file
26 $MAXFILESIZE=100000;
27
28 if ($ARGV[0] eq "-src") {
29 $src_path = $ARGV[1];
30 print "Using $ARGV[1] as source directory\n";
31 die("$src_path doesn't exist.") if (! -d $src_path);
32 shift;
33 shift;
34 }
35
36 if ($ARGV[0] eq "-archive") {
37 $archive =1;
38 print "Will generate appropriate files for image archive\n";
39 die("$DESTDIR already exists - remove if you really want to remake the images") if (-d $DESTDIR);
40 die("$0: unable to mkdir $DESTDIR: $1\n") if (!mkdir($DESTDIR, 0755));
41 }
42
43 open(IMAGEINFO,"$src_path/arch/image_info") || die("Can't open image_info file: $!\n");
44 binmode(IMAGEINFO);
45 while (<IMAGEINFO>) {
46 # Ignore lines that start with comments or just empty lines
47 next if /^#/;
48 next if /^\s*$/;
49 ($setnum, $ext, @rest) = split /:/;
50 # We don't actually need that data in this script, but may as well do sanity
51 # checking.
52 if ($#rest != 4) {
53 print STDERR "image_info: line is corrupt:\n$_";
54 }
55 if ($extension[$setnum]) {
56 print STDERR "Warning: set $setnum is duplicated in image_info file\n";
57 }
58 $extension[$setnum] = $ext;
59 }
60 close(IMAGEINFO);
61
62
63 for ($count=0; $count<=$#extension; $count++) {
64 $ESRV[$count] = new FileHandle;
65 $fh = $ESRV[$count];
66 open($fh, ">crossfire.$count") ||
67 die("Can't open crossfire.$count for write: $!\n");
68 binmode( $fh );
69 }
70
71 open(BMAPS,"bmaps.paths") || die("Can't open bmaps.paths: $!\n");
72 binmode(BMAPS);
73 $_ = <BMAPS>;
74 while(<BMAPS>) {
75 chop;
76
77 # we need to insert the extension for the images between the name
78 # and the number (.171 or whatever) extension, so split on that.
79
80 die("Unknown line: '$_'\n") unless /^\\(\d{5})\s+(\S+)\.(\w\w\w)$/o;
81 $num = $1;
82 $file = $2;
83 $file1 = $3;
84
85 print "$num $file\n" if ($num % 500) == 0 ;
86 # This probably isn't the most efficient way to do this if a
87 # large number of images are added, as we try to open each
88 # instance.
89 # OTOH, we are doing one directory
90 # at a time, so we should be hitting the DNLC at a very high
91 # rate.
92
93 for ($count=0; $count<=$#extension; $count++) {
94 $filename = "$file.$extension[$count].$file1.png";
95 $fh = $ESRV[$count];
96
97 $length = -s "$filename";
98 if (open(FILE,"$filename")) {
99 binmode( FILE );
100 print $fh "IMAGE $length $file.$file1\n";
101 print "Error reading file $filename" if (!read(FILE, $buf, $length));
102 $position = tell $fh;
103 print $fh $buf;
104 close(FILE);
105
106 if ($archive) {
107 # Now figure out the checksum
108 # Same as what is used for the client/server - code basically
109 # taken write form that.
110 $sum = 0;
111 for ($i=0; $i<$length; $i++) {
112 if ($sum & 01) {
113 $sum = ($sum >> 1) | 0x80000000;
114 } else {
115 $sum >>= 1;
116 }
117 $sum += ord(substr $buf, $i, 1);
118 $sum &= 0xffffffff;
119 }
120 # Do some name translation to figure out our output file name.
121 @comps = split /\//, $file;
122 $destfile = $comps[$#comps];
123 push @csums, "$destfile.$file1 $sum crossfire.$extension[$count]\@$position:$length\n";
124 } # if archive
125 }
126 elsif ($count==0) {
127 # set 0 should have all the sets
128 print "Error: Image $filename not found for set 0!\n";
129 }
130
131 }
132 }
133 for ($count=0; $count<=$#extension; $count++) {
134 close($ESRV[$count]);
135 }
136 close(BMAPS);
137
138 if ($archive) {
139 open(OUT,">$DESTDIR/bmaps.client") || die("Can not open $DESTDIR/bmaps.client\n");
140 binmode(OUT);
141 print OUT sort @csums;
142 close(OUT);
143 open(OUT,">$DESTDIR/README") || die("Can not open $DESTDIR/README\n");
144 print OUT "These image files for the client should be located in\n";
145 print OUT "\$prefix/share/crossfire-client. \$prefix is the location given in the -prefix\n";
146 print OUT "option when configure is run to build the client. The default is /usr/local.\n";
147 print OUT "In that case these files should be put in /usr/local/share/crossfire-client\n";
148 print OUT "The client will print a messgae if it is unable to find the image information\n";
149 print OUT "with the location is looked for them.\n";
150 close(OUT);
151
152 for ($count=0; $count<=$#extension; $count++) {
153 system("cp crossfire.$count $DESTDIR/crossfire.$extension[$count]");
154 }
155 system("cd $DESTDIR; tar cf $TMPDIR/$ARCHNAME.tar .");
156 system("mv $TMPDIR/$ARCHNAME.tar ../");
157 system("rm -rf $TMPDIR/$ARCHNAME");
158 }