]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/nexuiz-map-compiler
now restores shaderlist again and catches Ctrl-C
[divverent/nexuiz.git] / misc / nexuiz-map-compiler
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use POSIX;
6
7 # change these to match your system, or define them in ~/.nexuiz-map-compiler
8 # (just copy paste this part to the file ~/.nexuiz-map-compiler)
9
10         # Path to Nexuiz (where the data directory is in)
11         our $NEXUIZDIR   = '/home/polzer/Nexvn/nexuiz';
12
13         # Path to your q3map2 program. You find it in your GtkRadiant/install
14         # directory.
15         our $Q3MAP2      = '/home/users4/ommz/polzer/bin/q3map2.x86';
16
17         # General flags for q3map2 (for example -threads 4)
18         our $Q3MAP2FLAGS = '';
19
20         # Default flags for the -bsp stage
21         our $BSPFLAGS    = '-samplesize 8';
22
23         # Default flags for the -vis stage
24         our $VISFLAGS    = '';
25
26         # Default flags for the -light stage
27         our $LIGHTFLAGS  = '-deluxe -patchshadows -samples 3';
28
29 # end of user changable part
30
31 do "$ENV{HOME}/.nexuiz-map-compiler";
32
33 sub Usage()
34 {
35         print <<EOF;
36 Usage:
37 $0 mapname [-bsp bspflags...] [-vis visflags...] [-light lightflags...]
38 EOF
39         exit 1;
40 }
41
42 my $options =
43 {
44         bsp => [split /\s+/, $BSPFLAGS],
45         vis => [split /\s+/, $VISFLAGS],
46         light => [split /\s+/, $LIGHTFLAGS],
47         maps => [],
48         scale => 1
49 };
50
51 my $curmode = 'maps';
52
53 while(@ARGV)
54 {
55         $_ = shift @ARGV;
56         my $enterflags = undef;
57         if($_ eq '-bsp' or $_ eq '-meta')
58         {
59                 $enterflags = 'bsp';
60         }
61         elsif($_ eq '-vis')
62         {
63                 $enterflags = 'vis';
64         }
65         elsif($_ eq '-light')
66         {
67                 $enterflags = 'light';
68         }
69         elsif($_ eq '-map')
70         {
71                 $curmode = 'maps';
72         }
73         elsif($_ eq '-scale')
74         {
75                 $options->{scale} = (shift @ARGV) || 1;
76         }
77         elsif($_ =~ /^-/ and $curmode eq 'maps')
78         {
79                 $curmode = 'bsp';
80                 push @{$options->{$curmode}}, $_;
81         }
82         else
83         {
84                 push @{$options->{$curmode}}, $_;
85         }
86         if(defined $enterflags)
87         {
88                 $curmode = $enterflags;
89                 if($ARGV[0] eq '+')
90                 {
91                         shift @ARGV;
92                 }
93                 else
94                 {
95                         $options->{$curmode} = [];
96                 }
97         }
98 }
99
100 sub q3map2(@)
101 {
102         my @args = @_;
103         return !system $Q3MAP2, '-game', 'quake3', '-fs_basepath', $NEXUIZDIR, '-fs_game', 'data', '-v', @_;
104 }
105
106 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
107
108 unlink "$ENV{HOME}/.q3a/data";
109 mkdir "$ENV{HOME}/.q3a";
110 symlink "$mapdir", "$ENV{HOME}/.q3a/data"
111         or die "Setting up directory structure, $mapdir -> $ENV{HOME}/.q3a/data: $!";
112
113 for my $m(@{$options->{maps}})
114 {
115         if($options->{scale} != 1)
116         {
117                 open my $checkfh, "<", "$m.map"
118                         or die "open $m.map: $!";
119                 my $keeplights = 0;
120                 while(<$checkfh>)
121                 {
122                         /^\s*"_keeplights"\s+"1"\s*$/
123                                 or next;
124                         $keeplights = 1;
125                 }
126                 close $checkfh;
127                 die "$m does not define _keeplights to 1"
128                         unless $keeplights;
129         }
130
131         my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
132         open my $shaderlist, "+<", "$NEXUIZDIR/data/scripts/shaderlist.txt"
133                 or die "open $NEXUIZDIR/data/scripts/shaderlist.txt: $!";
134         my $previous_shaderlist = "";
135         while(<$shaderlist>)
136         {
137                 $previous_shaderlist .= $_;
138                 y/\r\n//d;
139                 delete $shaders{$_};
140         }
141         my $restore_shaderlist = sub
142         {
143                 open $shaderlist, ">", "$NEXUIZDIR/data/scripts/shaderlist.txt";
144                 print $shaderlist $previous_shaderlist;
145                 close $shaderlist;
146         };
147         local $SIG{INT} = sub
148         {
149                 print "SIGINT caught, cleaning up...\n";
150                 $restore_shaderlist->();
151                 exit 0;
152         };
153         eval
154         {
155                 for(keys %shaders)
156                 {
157                         print $shaderlist "$_\n";
158                 }
159                 close $shaderlist;
160
161                 q3map2 '-bsp', '-meta', @{$options->{bsp}},   "$m.map"
162                         or die "-bsp: $?";
163                 if($options->{scale} != 1)
164                 {
165                         q3map2 '-scale', $options->{scale}, "$m.bsp"
166                                 or die "-scale: $?";
167                         rename "${m}_s.bsp", "$m.bsp"
168                                 or die "rename ${m}_s.bsp $m.bsp: $!";
169                 }
170                 q3map2 '-vis',          @{$options->{vis}},   "$m.map"
171                         or die "-vis: $?";
172                 q3map2 '-light',        @{$options->{light}}, "$m.map"
173                         or die "-light: $?";
174
175                 unlink "$m.srf";
176
177                 $restore_shaderlist->();
178                 1;
179         }
180         or do
181         {
182                 $restore_shaderlist->();
183                 die $@;
184         };
185 }