7a114e5223f045686a15a695e5fbdee2f064bbf6
[xonotic/xonotic.git] / misc / tools / xonotic-map-compiler
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use POSIX;
6 use File::Temp;
7
8 # change these to match your system, or define them in ~/.xonotic-map-compiler
9 # (just copy paste this part to the file ~/.xonotic-map-compiler)
10
11         # Path to Xonotic (where the data directory is in)
12         our $XONOTICDIR   = '/home/rpolzer/Games/Xonotic';
13
14         # Path to your q3map2 program. You find it in your GtkRadiant/install
15         # directory.
16         our $Q3MAP2      = '/home/rpolzer/Games/Xonotic/netradiant/install/q3map2.x86';
17
18         # General flags for q3map2 (for example -threads 4)
19         our $Q3MAP2FLAGS = '';
20
21         # Default flags for the -bsp stage
22         our $BSPFLAGS    = '-meta -samplesize 8 -minsamplesize 4 -mv 1000000 -mi 6000000';
23
24         # Default flags for the -vis stage
25         our $VISFLAGS    = '';
26
27         # Default flags for the -light stage
28         our $LIGHTFLAGS  = '-deluxe -patchshadows -samples 3 -lightmapsize 512';
29
30         # Default flags for the -minimap stage
31         our $MINIMAPFLAGS = '';
32
33         # Default order of commands
34         our $ORDER = 'light,vis,minimap';
35
36 # end of user changable part
37
38 do "$ENV{HOME}/.xonotic-map-compiler";
39
40 sub Usage()
41 {
42         print <<EOF;
43 Usage:
44 $0 mapname [-bsp bspflags...] [-vis visflags...] [-light lightflags...] [-minimap minimapflags]
45 EOF
46         exit 1;
47 }
48
49 my $options =
50 {
51         bsp => [split /\s+/, $BSPFLAGS],
52         vis => [split /\s+/, $VISFLAGS],
53         light => [split /\s+/, $LIGHTFLAGS],
54         minimap => [split /\s+/, $MINIMAPFLAGS],
55         scale => [], # can't have defaults atm
56         order => [split /\s*,\s*/, $ORDER],
57         maps => [],
58         scale => 1,
59         bsp_timeout => 0,
60         vis_timeout => 0,
61         light_timeout => 0,
62         minimap_timeout => 0,
63         scale_timeout => 0
64 };
65
66 my $curmode = 'maps';
67
68 while(@ARGV)
69 {
70         $_ = shift @ARGV;
71         my $enterflags = undef;
72         if($_ eq '-bsp')
73         {
74                 $enterflags = 'bsp';
75         }
76         elsif($_ eq '-vis')
77         {
78                 $enterflags = 'vis';
79         }
80         elsif($_ eq '-light')
81         {
82                 $enterflags = 'light';
83         }
84         elsif($_ eq '-minimap')
85         {
86                 $enterflags = 'minimap';
87         }
88         elsif($_ eq '-map')
89         {
90                 $curmode = 'maps';
91         }
92         elsif($_ eq '-scale')
93         {
94                 $options->{scale} = (shift @ARGV) || 1;
95                 $enterflags = 'scale';
96         }
97         elsif($_ eq '-novis')
98         {
99                 $options->{vis} = undef;
100         }
101         elsif($_ eq '-nolight')
102         {
103                 $options->{light} = undef;
104         }
105         elsif($_ eq '-nominimap')
106         {
107                 $options->{minimap} = undef;
108         }
109         elsif($_ eq '-noshaderlist')
110         {
111                 $options->{noshaderlist} = 1;
112         }
113         elsif($_ eq '-bsp_timeout')
114         {
115                 $options->{bsp_timeout} = shift @ARGV;
116         }
117         elsif($_ eq '-vis_timeout')
118         {
119                 $options->{vis_timeout} = shift @ARGV;
120         }
121         elsif($_ eq '-light_timeout')
122         {
123                 $options->{light_timeout} = shift @ARGV;
124         }
125         elsif($_ eq '-minimap_timeout')
126         {
127                 $options->{minimap_timeout} = shift @ARGV;
128         }
129         elsif($_ eq '-scale_timeout')
130         {
131                 $options->{minimap_timeout} = shift @ARGV;
132         }
133         elsif($_ eq '-order')
134         {
135                 $options->{order} = [split /\s*,\s*/, shift @ARGV];
136         }
137         elsif($_ =~ /^-(-.*)/)
138         {
139                 if($curmode eq 'maps')
140                 {
141                         $curmode = 'bsp';
142                 }
143                 push @{$options->{$curmode}}, $1;
144         }
145         elsif($_ =~ /^-/ and $curmode eq 'maps')
146         {
147                 $curmode = 'bsp';
148                 push @{$options->{$curmode}}, $_;
149         }
150         else
151         {
152                 push @{$options->{$curmode}}, $_;
153         }
154         if(defined $enterflags)
155         {
156                 $curmode = $enterflags;
157                 if($ARGV[0] eq '+')
158                 {
159                         shift @ARGV;
160                 }
161                 else
162                 {
163                         $options->{$curmode} = [];
164                 }
165         }
166 }
167
168 my $linkdir = File::Temp::tempdir("xonotic-map-compiler.XXXXXX", TMPDIR => 1, CLEANUP => 1);
169
170 sub q3map2(@)
171 {
172         my $mode = $_[0];
173         my $timeout = undef;
174         $timeout = $options->{bsp_timeout} if $mode eq '-bsp';
175         $timeout = $options->{vis_timeout} if $mode eq '-vis';
176         $timeout = $options->{light_timeout} if $mode eq '-light';
177         $timeout = $options->{minimap_timeout} if $mode eq '-minimap';
178         $timeout = $options->{scale_timeout} if $mode eq '-scale';
179         die "Invalid call: not a standard q3map2 stage" if not defined $timeout;
180         my @args = ($Q3MAP2, split(/\s+/, $Q3MAP2FLAGS), '-game', 'xonotic', '-fs_basepath', $XONOTICDIR, '-fs_basepath', $linkdir, '-v', @_);
181         print "\$ @args\n";
182         defined(my $pid = fork())
183                 or die "fork: $!";
184         if($pid) # parent
185         {
186                 local $SIG{ALRM} = sub { warn "SIGALRM caught\n"; kill $pid; };
187                 alarm $timeout
188                         if $timeout;
189                 if(waitpid($pid, 0) != $pid)
190                 {
191                         die "waitpid: did not return our child process $pid: $!";
192                 }
193                 alarm 0;
194                 return ($? == 0);
195         }
196         else # child
197         {
198                 exec @args
199                         or die "exec: $!";
200         }
201 }
202
203 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
204 $mapdir = "/" if $mapdir eq "";
205 symlink "$mapdir", "$linkdir/data";
206
207 my ($prescale, $postscale) = ($options->{scale} =~ /^([0-9.]+)(?::([0-9.]+))?$/);
208 $postscale = 1 if not defined $postscale;
209
210 for my $m(@{$options->{maps}})
211 {
212         $m =~ s/\.(?:map|bsp)$//;
213         if($prescale != 1)
214         {
215                 open my $checkfh, "<", "$m.map"
216                         or die "open $m.map: $!";
217                 my $keeplights = 0;
218                 while(<$checkfh>)
219                 {
220                         /^\s*"_keeplights"\s+"1"\s*$/
221                                 or next;
222                         $keeplights = 1;
223                 }
224                 close $checkfh;
225                 die "$m does not define _keeplights to 1"
226                         unless $keeplights;
227         }
228
229         my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
230
231         my $restore_shaderlist = sub { };
232         if(!$options->{noshaderlist})
233         {
234                 my $previous_shaderlist = undef;
235                 my $shaderlist = "";
236                 if(open my $fh, "<", "$XONOTICDIR/data/scripts/shaderlist.txt")
237                 {
238                         while(<$fh>)
239                         {
240                                 $shaderlist .= $_;
241                         }
242
243                         # we may have to restore the file on exit
244                         $previous_shaderlist = $shaderlist
245                                 if "$XONOTICDIR/data" eq $mapdir;
246                 }
247                 else
248                 {
249                         # possibly extract the shader list from a pk3?
250                         local $ENV{N} = $XONOTICDIR;
251                         $shaderlist = `cd "\$N" && for X in "\$N"/data/data*.pk3; do Y=\$X; done; unzip -p "\$Y" scripts/shaderlist.txt`;
252                 }
253
254                 my $shaderlist_new = "";
255                 for(split /\r?\n|\r/, $shaderlist)
256                 {
257                         delete $shaders{$_};
258                         $shaderlist_new .= "$_\n";
259                 }
260                 if(%shaders)
261                 {
262                         for(sort keys %shaders)
263                         {
264                                 $shaderlist_new .= "$_\n";
265                         }
266                 }
267                 else
268                 {
269                         $shaderlist_new = undef;
270                 }
271
272                 $restore_shaderlist = sub
273                 {
274                         if(defined $shaderlist_new)
275                         {
276                                 if(defined $previous_shaderlist)
277                                 {
278                                         open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
279                                         print $fh $previous_shaderlist;
280                                         close $fh;
281                                 }
282                                 else
283                                 {
284                                         unlink "$mapdir/scripts/shaderlist.txt";
285                                 }
286                         }
287                 };
288
289                 if(defined $shaderlist_new)
290                 {
291                         mkdir "$mapdir/scripts";
292                         open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
293                         print $fh $shaderlist_new;
294                         close $fh;
295                 }
296         }
297
298         local $SIG{INT} = sub
299         {
300                 print "SIGINT caught, cleaning up...\n";
301                 $restore_shaderlist->();
302                 exit 0;
303         };
304
305         eval
306         {
307                 unlink <$m/lm_*>; # delete old external lightmaps
308                 q3map2 '-bsp', @{$options->{bsp}},   "$m.map"
309                         or die "-bsp: $?";
310                 if($prescale != 1)
311                 {
312                         q3map2 '-scale', @{$options->{scale}}, $prescale, "$m.bsp"
313                                 or die "-scale: $?";
314                         rename "${m}_s.bsp", "$m.bsp"
315                                 or die "rename ${m}_s.bsp $m.bsp: $!";
316                 }
317                 my @o = @{$options->{order}};
318                 push @o, qw/light vis minimap/;
319                 my %o = ();
320
321                 for(@o)
322                 {
323                         next if $o{$_}++;
324                         if($_ eq 'light')
325                         {
326                                 if(defined $options->{light})
327                                 {
328                                         q3map2 '-light',        @{$options->{light}}, "$m.map"
329                                                 or die "-light: $?";
330                                 }
331                         }
332                         if($_ eq 'vis')
333                         {
334                                 if(defined $options->{vis})
335                                 {
336                                         q3map2 '-vis',          @{$options->{vis}},   "$m.map"
337                                                 or die "-vis: $?";
338                                 }
339                         }
340                         if($_ eq 'minimap')
341                         {
342                                 if(defined $options->{minimap})
343                                 {
344                                         q3map2 '-minimap',      @{$options->{minimap}}, "$m.map"
345                                                 or die "-minimap: $?";
346                                 }
347                         }
348                 }
349
350                 if($postscale != 1)
351                 {
352                         q3map2 '-scale', @{$options->{scale}}, $postscale, "$m.bsp"
353                                 or die "-scale: $?";
354                         rename "${m}_s.bsp", "$m.bsp"
355                                 or die "rename ${m}_s.bsp $m.bsp: $!";
356                 }
357
358                 unlink "$m.srf";
359                 unlink "$m.prt";
360
361                 $restore_shaderlist->();
362                 1;
363         }
364         or do
365         {
366                 $restore_shaderlist->();
367                 die $@;
368         };
369 }