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