]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/autocvarize.pl
try tracking menu QC owned cvars
[divverent/nexuiz.git] / data / qcsrc / server / autocvarize.pl
1 #!/usr/bin/perl
2 # this tool generates JUST the autocvar declarations for cvars
3 use strict;
4 use warnings;
5
6 my @files = @ARGV;
7
8 my %cvars = ();
9 my %old = ();
10 my %menu = ();
11
12 sub found($$$)
13 {
14         my ($name, $type, $force) = @_;
15         $old{$name} = 1
16                 if $force;
17         $menu{$name} = 1
18                 if $force > 1;
19         if(exists $cvars{$name} and $type ne $cvars{$name})
20         {
21                 warn "cvar $name used with different types";
22                 if($force)
23                 {
24                         $cvars{$name} = $type;
25                 }
26                 else
27                 {
28                         undef $cvars{$name}
29                                 unless $old{$name};
30                 }
31                 next;
32         }
33         else
34         {
35                 $cvars{$name} = $type;
36         }
37 }
38
39 for my $f(@files)
40 {
41         print STDERR "In file $f\n";
42         open my $fh, "<", $f;
43         while(<$fh>)
44         {
45                 chomp;
46                 if(/^\/\/#NO AUTOCVARS START/ .. /^\/\/#NO AUTOCVARS END/)
47                 {
48                         next;
49                 }
50                 s/\/\/.*//;
51                 if(/^float autocvar_(.*);$/)
52                 {
53                         found $1, 'cvar', 1;
54                         next;
55                 }
56                 if(/^string autocvar_(.*);$/)
57                 {
58                         found $1, 'cvar_string', 1;
59                         next;
60                 }
61                 if(/^#define autocvar_(.*) cvar("\1")$/)
62                 {
63                         found $1, 'cvar', 2;
64                         next;
65                 }
66                 if(/^#define autocvar_(.*) cvar_string("\1")$/)
67                 {
68                         found $1, 'cvar_string', 2;
69                         next;
70                 }
71                 while(/\b(cvar|cvar_string)\s*\(\s*"([^"]+)"\s*\)/g)
72                 {
73                         found $2, $1, 0;
74                 }
75         }
76 }
77
78 for my $f(<../menu/nexuiz/*.c>)
79 {
80         print STDERR "In file $f\n";
81         open my $fh, "<", $f;
82         while(<$fh>)
83         {
84                 for(/"([^"]*)"/g)
85                 {
86                         $menu{$1} = 1;
87                 }
88         }
89 }
90
91 for my $f(<../../maps/campaign*.txt>)
92 {
93         print STDERR "In file $f\n";
94         open my $fh, "<", $f;
95         while(<$fh>)
96         {
97                 for(/\b(.+?)\b/g)
98                 {
99                         $menu{$1} = 1;
100                 }
101         }
102 }
103
104 for my $name(sort keys %cvars)
105 {
106         my $type = $cvars{$name};
107         my $menu = $menu{$name};
108         if(not defined $type)
109         {
110                 print "// cannot declare $name, it is used with different types\n";
111         }
112         if($type eq 'cvar' and not $menu)
113         {
114                 print "float autocvar_$name;\n";
115         }
116         if($type eq 'cvar_string' and not $menu)
117         {
118                 print "string autocvar_$name;\n";
119         }
120         if($type eq 'cvar' and $menu)
121         {
122                 print "#define autocvar_$name cvar(\"$name\")\n";
123         }
124         if($type eq 'cvar_string' and $menu)
125         {
126                 print "#define autocvar_$name cvar_string(\"$name\")\n";
127         }
128 }
129
130 for my $f(@files)
131 {
132         print STDERR "In file $f\n";
133         open my $fh, "<", $f;
134         my $out = "";
135         while(<$fh>)
136         {
137                 chomp;
138                 if(/^\/\/#NO AUTOCVARS START/ .. /^\/\/#NO AUTOCVARS END/)
139                 {
140                         $out .= "$_\n";
141                         next;
142                 }
143                 if(/^float autocvar_(.*);$/)
144                 {
145                         $out .= "$_\n";
146                         next;
147                 }
148                 if(/^string autocvar_(.*);$/)
149                 {
150                         $out .= "$_\n";
151                         next;
152                 }
153                 if(/^#define autocvar_(.*) cvar("\1")$/)
154                 {
155                         $out .= "$_\n";
156                         next;
157                 }
158                 if(/^#define autocvar_(.*) cvar_string("\1")$/)
159                 {
160                         $out .= "$_\n";
161                         next;
162                 }
163                 s{\b(cvar|cvar_string)\s*\(\s*"([^"]+)"\s*\)}{
164                         my ($type, $name) = ($1, $2);
165                         my $realtype = $cvars{$name};
166                         my $r = $&;
167                         if(defined $realtype)
168                         {
169                                 #$r = "$realtype(\"$name\")";
170                                 $r = "autocvar_$name";
171                                 if($type eq 'cvar' && $realtype eq 'cvar_string')
172                                 {
173                                         $r = "stof($r)";
174                                 }
175                                 if($type eq 'cvar_string' && $realtype eq 'cvar')
176                                 {
177                                         $r = "ftos($r)";
178                                 }
179                         }
180                         $r;
181                 }ge;
182                 $out .= "$_\n";
183         }
184         close $fh;
185         open $fh, ">", $f;
186         print $fh $out;
187         close $fh;
188 }