]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/xonotic-map-compiler
Merge branch 'master' of git://de.git.xonotic.org/xonotic/xonotic
[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 = '-fs_forbiddenpath xonotic*-data*.pk3* -fs_forbiddenpath xonotic*-nexcompat*.pk3*';
20
21         # Default flags for the -bsp stage
22         our $BSPFLAGS    = '-meta -maxarea -samplesize 8 -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  = '-lightmapsearchpower 3 -deluxe -patchshadows -randomsamples -samples 4 -lightmapsize 512 -fast -fastbounce -dirty -bouncegrid -fill';
29
30         # Default flags for the -minimap stage
31         our $MINIMAPFLAGS = '';
32
33         # Default order of commands
34         our $ORDER = 'vis,light';
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         scalefactor => 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->{scalefactor} = @ARGV ? 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->{scale_timeout} = shift @ARGV;
132         }
133         elsif($_ eq '-order')
134         {
135                 $options->{order} = [split /\s*,\s*/, shift @ARGV];
136         }
137         elsif($_ eq '-sRGB')
138         {
139                 push @{$options->{bsp}}, "-sRGBtex", "-sRGBcolor";
140                 push @{$options->{light}}, "-sRGBtex", "-sRGBcolor", "-sRGBlight"
141                         if defined $options->{light};
142         }
143         elsif($_ eq '-nosRGB')
144         {
145                 push @{$options->{bsp}}, "-nosRGBtex", "-nosRGBcolor";
146                 push @{$options->{light}}, "-nosRGBtex", "-nosRGBcolor", "-nosRGBlight"
147                         if defined $options->{light};
148         }
149         elsif($_ =~ /^--no(-.*)/)
150         {
151                 if($curmode eq 'maps')
152                 {
153                         $curmode = 'bsp';
154                 }
155                 my $flag = $1;
156                 @{$options->{$curmode}} = grep { (($_ eq $flag) ... /^-/) !~ /^[0-9]+$/ } @{$options->{$curmode}};
157                         # so, e.g. --no-samplesize removes "-samplesize" and a following "3"
158         }
159         elsif($_ =~ /^-(-.*)/)
160         {
161                 if($curmode eq 'maps')
162                 {
163                         $curmode = 'bsp';
164                 }
165                 push @{$options->{$curmode}}, $1;
166         }
167         elsif($_ =~ /^-/ and $curmode eq 'maps')
168         {
169                 $curmode = 'bsp';
170                 push @{$options->{$curmode}}, $_;
171         }
172         else
173         {
174                 push @{$options->{$curmode}}, $_;
175         }
176         if(defined $enterflags)
177         {
178                 $curmode = $enterflags;
179                 if($ARGV[0] eq '+')
180                 {
181                         shift @ARGV;
182                 }
183                 else
184                 {
185                         $options->{$curmode} = [];
186                 }
187         }
188 }
189
190 my $linkdir = File::Temp::tempdir("xonotic-map-compiler.XXXXXX", TMPDIR => 1, CLEANUP => 1);
191
192 sub q3map2(@)
193 {
194         my $mode = $_[0];
195         my $timeout = undef;
196         $timeout = $options->{bsp_timeout} if $mode eq '-bsp';
197         $timeout = $options->{vis_timeout} if $mode eq '-vis';
198         $timeout = $options->{light_timeout} if $mode eq '-light';
199         $timeout = $options->{minimap_timeout} if $mode eq '-minimap';
200         $timeout = $options->{scale_timeout} if $mode eq '-scale';
201         die "Invalid call: not a standard q3map2 stage" if not defined $timeout;
202         my @args = ($Q3MAP2, split(/\s+/, $Q3MAP2FLAGS), '-game', 'xonotic', '-fs_basepath', $XONOTICDIR, '-fs_basepath', $linkdir, '-v', @_);
203         print "\$ @args\n";
204         defined(my $pid = fork())
205                 or die "fork: $!";
206         if($pid) # parent
207         {
208                 local $SIG{ALRM} = sub { warn "SIGALRM caught\n"; kill TERM => $pid; };
209                 alarm $timeout
210                         if $timeout;
211                 if(waitpid($pid, 0) != $pid)
212                 {
213                         die "waitpid: did not return our child process $pid: $!";
214                 }
215                 alarm 0;
216                 return ($? == 0);
217         }
218         else # child
219         {
220                 exec @args
221                         or die "exec: $!";
222         }
223 }
224
225 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
226 $mapdir = "/" if $mapdir eq "";
227 symlink "$mapdir", "$linkdir/data";
228
229 my ($prescale, $postscale) = ($options->{scalefactor} =~ /^([0-9.]+)(?::([0-9.]+))?$/);
230 $prescale = 1 if not defined $prescale;
231 $postscale = 1 if not defined $postscale;
232
233 for my $m(@{$options->{maps}})
234 {
235         $m =~ s/\.(?:map|bsp)$//;
236
237         if($prescale != 1)
238         {
239                 unshift @{$options->{bsp}}, "-keeplights";
240         }
241
242         my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
243
244         my $restore_shaderlist = sub { };
245         if(!$options->{noshaderlist})
246         {
247                 my $previous_shaderlist = undef;
248                 my $shaderlist = "";
249                 if(open my $fh, "<", "$XONOTICDIR/data/scripts/shaderlist.txt")
250                 {
251                         while(<$fh>)
252                         {
253                                 $shaderlist .= $_;
254                         }
255
256                         # we may have to restore the file on exit
257                         $previous_shaderlist = $shaderlist
258                                 if "$XONOTICDIR/data" eq $mapdir;
259                 }
260                 else
261                 {
262                         # possibly extract the shader list from a pk3?
263                         local $ENV{N} = $XONOTICDIR;
264                         $shaderlist = `cd "\$N" && for X in "\$N"/data/data*.pk3; do Y=\$X; done; unzip -p "\$Y" scripts/shaderlist.txt`;
265                 }
266
267                 my $shaderlist_new = "";
268                 for(split /\r?\n|\r/, $shaderlist)
269                 {
270                         delete $shaders{$_};
271                         $shaderlist_new .= "$_\n";
272                 }
273                 if(%shaders)
274                 {
275                         for(sort keys %shaders)
276                         {
277                                 $shaderlist_new .= "$_\n";
278                         }
279                 }
280                 else
281                 {
282                         $shaderlist_new = undef;
283                 }
284
285                 $restore_shaderlist = sub
286                 {
287                         if(defined $shaderlist_new)
288                         {
289                                 if(defined $previous_shaderlist)
290                                 {
291                                         open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
292                                         print $fh $previous_shaderlist;
293                                         close $fh;
294                                 }
295                                 else
296                                 {
297                                         unlink "$mapdir/scripts/shaderlist.txt";
298                                 }
299                         }
300                 };
301
302                 if(defined $shaderlist_new)
303                 {
304                         mkdir "$mapdir/scripts";
305                         open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
306                         print $fh $shaderlist_new;
307                         close $fh;
308                 }
309         }
310
311         local $SIG{INT} = sub
312         {
313                 print "SIGINT caught, cleaning up...\n";
314                 $restore_shaderlist->();
315                 exit 0;
316         };
317
318         eval
319         {
320                 unlink <$m/lm_*>; # delete old external lightmaps
321                 q3map2 '-bsp', @{$options->{bsp}},   "$m.map"
322                         or die "-bsp: $?";
323                 if($prescale != 1)
324                 {
325                         q3map2 '-scale', @{$options->{scale}}, $prescale, "$m.bsp"
326                                 or die "-scale: $?";
327                         rename "${m}_s.bsp", "$m.bsp"
328                                 or die "rename ${m}_s.bsp $m.bsp: $!";
329                 }
330                 my @o = @{$options->{order}};
331                 push @o, qw/light vis/;
332                 my %o = ();
333
334                 for(@o)
335                 {
336                         next if $o{$_}++;
337                         if($_ eq 'light')
338                         {
339                                 if(defined $options->{light})
340                                 {
341                                         q3map2 '-light',        @{$options->{light}}, "$m.map"
342                                                 or die "-light: $?";
343                                 }
344                         }
345                         if($_ eq 'vis')
346                         {
347                                 if(defined $options->{vis})
348                                 {
349                                         q3map2 '-vis',          @{$options->{vis}},   "$m.map"
350                                                 or die "-vis: $?";
351                                 }
352                         }
353                 }
354
355                 if($postscale != 1)
356                 {
357                         q3map2 '-scale', @{$options->{scale}}, $postscale, "$m.bsp"
358                                 or die "-scale: $?";
359                         rename "${m}_s.bsp", "$m.bsp"
360                                 or die "rename ${m}_s.bsp $m.bsp: $!";
361                 }
362
363                 if(defined $options->{minimap})
364                 {
365                         q3map2 '-minimap',      @{$options->{minimap}}, "$m.map"
366                                 or die "-minimap: $?";
367                 }
368
369                 unlink "$m.srf";
370                 unlink "$m.prt";
371
372                 $restore_shaderlist->();
373                 1;
374         }
375         or do
376         {
377                 $restore_shaderlist->();
378                 die $@;
379         };
380 }