Squashed commit of the following:
[xonotic/xonotic-maps.pk3dir.git] / scripts / shader-audit.sh
1 #!/bin/sh
2
3 case "$0" in
4         */*)
5                 cd "${0%/*}"
6                 ;;
7 esac
8
9 pid=$$
10 status=true
11 trap 'status=false' USR1
12 seterror()
13 {
14         kill -USR1 "$pid"
15 }
16
17 LF="
18 "
19
20 normalize()
21 {
22         echo "$1" | sed 's/\.\(tga\|jpg\|png\)$//'
23 }
24
25 allowed_prefixes=
26 forbidden_prefixes=
27
28 textures_used=
29 # $1 = shader
30 # $2 = texture
31 # $3 = shader | map | animmap | editorimage | sky
32 use_texture()
33 {
34         # does texture exist?
35         if \
36                 [ -f "../$2.tga" ] || \
37                 [ -f "../$2.jpg" ] || \
38                 [ -f "../$2.png" ]; then
39                 :
40         else
41                 if [ "$3" = "shader" ]; then
42                         return
43                 else
44                         echo "(EE) shader $1 uses non-existing texture $2"; seterror
45                 fi
46         fi
47         textures_used="$textures_used$LF$2"
48
49         if [ -n "$allowed_prefixes" ]; then
50                 ok=false
51                 for p in $allowed_prefixes; do
52                         case "$1:" in
53                                 "$p"*)
54                                         ok=true
55                                         ;;
56                         esac
57                 done
58         else
59                 ok=true
60         fi
61         for p in $forbidden_prefixes; do
62                 case "$1:" in
63                         "$p"*)
64                                 ok=false
65                                 ;;
66                 esac
67         done
68         if ! $ok; then
69                 echo "(EE) shader $1 is not allowed in this shader file (allowed: $allowed_prefixes, forbidden: $forbidden_prefixes)"; seterror
70         fi
71
72         case "$3" in
73                 ## RULE: skyboxes must lie in env/
74                 sky)
75                         case "$2" in
76                                 env/*)
77                                         ;;
78                                 *)
79                                         echo "(EE) texture $2 of shader $1 is out of place, $3 textures must be in env/"; seterror
80                                         ;;
81                         esac
82                         ;;
83                 ## RULE: non-skyboxes must not lie in env/
84                 *)
85                         case "$2" in
86                                 env/*)
87                                         echo "(EE) texture $2 of shader $1 is out of place, $3 textures must not be in env/"; seterror
88                                         ;;
89                                 *)
90                                         ;;
91                         esac
92                         ;;
93         esac
94
95         # verify shader -> texture name
96         case "$1" in
97                 ## RULE: textures/FOOx/BAR-BAZ must use textures/FOOx/*/*, recommended textures/FOOx/BAR/BAZ
98                 textures/*x/*-*)
99                         pre=${1%%x/*}x
100                         suf=${1#*x/}
101                         suf="`echo "$suf" | sed 's,-,/,g'`"
102                         case "$2" in
103                                 "$pre"/*/*)
104                                         ;;
105                                 *)
106                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $pre/$suf"; seterror
107                                         ;;
108                         esac
109                         ;;
110                 ## RULE: textures/FOOx/BAR must use textures/FOOx/*/*, recommended textures/FOOx/base/BAR
111                 textures/*x/*)
112                         pre=${1%%x/*}x
113                         suf=${1#*x/}
114                         case "$2" in
115                                 "$pre"/*/*)
116                                         ;;
117                                 *)
118                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $pre/base/$suf"; seterror
119                                         ;;
120                         esac
121                         ;;
122                 ## RULE: textures/map_FOO[_/]* must use textures/map_FOO[_/]*
123                 textures/map_*/*)
124                         pre=${1%%/map_*}
125                         suf=${1#*/map_}
126                         map=${suf%%[_/]*}
127                         case "$2" in
128                                 "$pre"/map_$map[/_]*)
129                                         ;;
130                                 textures/map_*)
131                                         # protect one map's textures from the evil of other maps :P
132                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $pre/map_$map/*"; seterror
133                                         ;;
134                                 *)
135                                         # using outside stuff is permitted
136                                         ;;
137                         esac
138                         ;;
139                 ## RULE: textures/common/FOO must use textures/common/FOO or textures/common/*/*
140                 textures/common/*)
141                         case "$2" in
142                                 "$1")
143                                         ;;
144                                 textures/common/*/*)
145                                         ;;
146                                 *)
147                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1 or textures/common/*/*"; seterror
148                                         ;;
149                         esac
150                         ;;
151                 ## RULE: textures/FOO/* must use textures/FOO/*, for FOO in decals, liquids_water, liquids_slime, liquids_lava
152                 textures/decals/*|textures/liquids_*/*|textures/effects_*/*|textures/screens/*|textures/logos/*)
153                         pre=`echo "$1" | cut -d / -f 1-2`
154                         case "$2" in
155                                 "$pre"/*)
156                                         # I _suppose_ this is fine, as tZork committed this pack
157                                         ;;
158                                 *)
159                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1"; seterror
160                                         ;;
161                         esac
162                         ;;
163                 ## RULE: textures/skies/FOO or textures/skies/FOO_BAR must use textures/skies/FOO respective textures/skies/FOO_BAR as preview image, and env/FOO[_/]* as skybox
164                 textures/skies/*)
165                         sky=${1#textures/skies/}
166                         sky=${sky%%_*}
167                         case "$2" in
168                                 textures/skies/$sky|textures/skies/$sky[_]*)
169                                         # typical place for preview image
170                                         ;;
171                                 env/$sky[/_]*)
172                                         # typical place for skybox
173                                         ;;
174                                 *)
175                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1"; seterror
176                                         ;;
177                         esac
178                         ;;
179                 ## RULE: models/* must use models/*
180                 models/*)
181                         case "$2" in
182                                 models/*)
183                                         ;;
184                                 *)
185                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1 or models/*"; seterror
186                                         ;;
187                         esac
188                         ;;
189                 *)
190                         echo "(EE) no shader name pattern for $1"; seterror
191                         ;;
192         esac
193 }
194
195 parsing_shader=
196 parse_shaderstage()
197 {
198         while read L A1 Aother; do
199                 case "`echo "$L" | tr A-Z a-z`" in
200                         map)
201                                 case "$A1" in
202                                         '$lightmap')
203                                                 ;;
204                                         *)
205                                                 use_texture "$parsing_shader" "`normalize "$A1"`" map
206                                                 ;;
207                                 esac
208                                 ;;
209                         animmap)
210                                 for X in $Aother; do
211                                         use_texture "$parsing_shader" "`normalize "$X"`" animmap
212                                 done
213                                 ;;
214                         '}')
215                                 break
216                                 ;;
217                         *)
218                                 ;;
219                 esac
220         done
221 }
222
223 parse_shader()
224 {
225         use_texture "$parsing_shader" "$parsing_shader" shader
226         while read L A1 Aother; do
227                 case "`echo "$L" | tr A-Z a-z`" in
228                         qer_editorimage)
229                                 use_texture "$parsing_shader" "`normalize "$A1"`" editorimage
230                                 ;;
231                         skyparms)
232                                 use_texture "$parsing_shader" "${A1}_lf" sky
233                                 use_texture "$parsing_shader" "${A1}_rt" sky
234                                 use_texture "$parsing_shader" "${A1}_up" sky
235                                 use_texture "$parsing_shader" "${A1}_dn" sky
236                                 use_texture "$parsing_shader" "${A1}_ft" sky
237                                 use_texture "$parsing_shader" "${A1}_bk" sky
238                                 ;;
239                         '{')
240                                 parse_shaderstage
241                                 ;;
242                         '}')
243                                 break
244                                 ;;
245                         *)
246                                 ;;
247                 esac
248         done
249 }
250
251 parse_shaderfile()
252 {
253         case "$1" in
254                 ## RULE: map_FOO.shader may define tetxures/map_FOO_* and textures/map_FOO/*
255                 map_*)
256                         allowed_prefixes="textures/map_`echo "$1" | cut -d _ -f 2`_ textures/map_`echo "$1" | cut -d _ -f 2`/"
257                         forbidden_prefixes=
258                         ;;
259                 ## RULE: skies_FOO.shader may define tetxures/skies/FOO and textures/skies/FOO_*
260                 skies_*)
261                         allowed_prefixes="textures/skies/`echo "$1" | cut -d _ -f 2`: textures/skies/`echo "$1" | cut -d _ -f 2`_"
262                         forbidden_prefixes=
263                         ;;
264                 ## RULE: model_*.shader may define models/*
265                 model_*)
266                         allowed_prefixes="models/"
267                         forbidden_prefixes=
268                         ;;
269                 ## RULE: any other FOO.shader may define textures/FOO/*
270                 *)
271                         allowed_prefixes="textures/$1/"
272                         forbidden_prefixes="textures/skies/ textures/map_ models/"
273                         ;;
274         esac
275         while read L; do
276                 case "$L" in
277                         */*)
278                                 parsing_shader="`normalize "$L"`"
279                                 if [ x"$L" != x"$parsing_shader" ]; then
280                                         echo "(WW) normalized shader name $L to $parsing_shader"
281                                 fi
282                                 ;;
283                         '{')
284                                 parse_shader
285                                 ;;
286                         *)
287                                 ;;
288                 esac
289         done
290 }
291
292 strip_comments()
293 {
294         sed 's,//.*,,g; s,\r, ,g; s,\t, ,g; s,  *, ,g; s, $,,; s,^ ,,; /^$/ d'
295 }
296
297 t=`mktemp || echo ".temp"`
298 for X in *.shader; do
299         strip_comments < "$X" > "$t"
300         parse_shaderfile "${X%.shader}" < "$t"
301 done
302 rm -f "$t"
303
304 textures_avail=`( cd ..; find textures/ -type f -not -name '*_norm.*' -not -name '*_glow.*' -not -name '*_gloss.*' -not -name '*_reflect.*' -not -name '*.xcf' ) | while IFS= read -r T; do normalize "$T"; done | sort -u`
305 textures_used=`echo "${textures_used#$LF}" | sort -u`
306
307 echo "$textures_used$LF$textures_used$LF$textures_avail" | sort | uniq -u | while IFS= read -r L; do
308         case "$L" in
309                 textures/radiant/*)
310                         ;;
311                 textures/map_*/*)
312                         ;;
313                 *)
314                         echo "(EE) texture $L is not referenced by any shader"; seterror
315                         ;;
316         esac
317 done
318
319 $status