new allowed paths: liquids/water, liquids/slime, liquids/lava, warpzone
[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                 sky)
60                         case "$2" in
61                                 env/*)
62                                         ;;
63                                 *)
64                                         echo "(EE) texture $2 of shader $1 is out of place, $3 textures must be in env/"
65                                         ;;
66                         esac
67                         ;;
68                 *)
69                         case "$2" in
70                                 env/*)
71                                         echo "(EE) texture $2 of shader $1 is out of place, $3 textures must not be in env/"
72                                         ;;
73                                 *)
74                                         ;;
75                         esac
76                         ;;
77         esac
78
79         # verify shader -> texture name
80         case "$1" in
81                 textures/*x/*-*)
82                         pre=${1%%x/*}x
83                         suf=${1#*x/}
84                         # rule: in suffix part, change each - to /
85                         suf="`echo "$suf" | sed 's,-,/,g'`"
86                         case "$2" in
87                                 "$pre"/*/*)
88                                         ;;
89                                 *)
90                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $pre/$suf"
91                                         ;;
92                         esac
93                         ;;
94                 textures/*x/*)
95                         pre=${1%%x/*}x
96                         suf=${1#*x/}
97                         case "$2" in
98                                 "$pre"/*/*)
99                                         ;;
100                                 *)
101                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $pre/base/$suf"
102                                         ;;
103                         esac
104                         ;;
105                 textures/map_*/*)
106                         pre=${1%%/map_*}
107                         suf=${1#*/map_}
108                         map=${suf%%[_/]*}
109                         case "$2" in
110                                 "$pre"/map_$map[/_]*)
111                                         ;;
112                                 *)
113                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $pre/map_$map/*"
114                                         ;;
115                         esac
116                         ;;
117                 textures/common/*)
118                         case "$2" in
119                                 "$1")
120                                         ;;
121                                 textures/common/*/*)
122                                         ;;
123                                 *)
124                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1 or textures/common/*/*"
125                                         ;;
126                         esac
127                         ;;
128                 textures/decals/*|textures/liquids/water/*|textures/liquids/slime/*|textures/liquids/lava/*|textures/warpzone/*)
129                         case "$2" in
130                                 "$1")
131                                         # I _suppose_ this is fine, as tZork committed this pack
132                                         ;;
133                                 *)
134                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1"
135                                         ;;
136                         esac
137                         ;;
138                 textures/skies/*)
139                         sky=${1#textures/skies/}
140                         sky=${sky%%_*}
141                         case "$2" in
142                                 "$1")
143                                         # typical place for preview image
144                                         ;;
145                                 env/$sky[/_]*)
146                                         # typical place for skybox
147                                         ;;
148                                 *)
149                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1"
150                                         ;;
151                         esac
152                         ;;
153                 models/*)
154                         case "$2" in
155                                 models/*)
156                                         ;;
157                                 *)
158                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1 or models/*"
159                                         ;;
160                         esac
161                         ;;
162                 *)
163                         echo "(EE) no shader name pattern for $1"
164                         ;;
165         esac
166 }
167
168 parsing_shader=
169 parse_shaderstage()
170 {
171         while read L A1 A2 Aother; do
172                 case "$L" in
173                         map)
174                                 case "$A1" in
175                                         '$lightmap')
176                                                 ;;
177                                         *)
178                                                 use_texture "$parsing_shader" "`normalize "$A1"`" map
179                                                 ;;
180                                 esac
181                                 ;;
182                         animmap)
183                                 for X in $A2 $Aother; do
184                                         use_texture "$parsing_shader" "`normalize "$X"`" animmap
185                                 done
186                                 ;;
187                         '}')
188                                 break
189                                 ;;
190                         *)
191                                 ;;
192                 esac
193         done
194 }
195
196 parse_shader()
197 {
198         use_texture "$parsing_shader" "$parsing_shader" shader
199         while read L A1; do
200                 case "$L" in
201                         qer_editorimage\ *)
202                                 use_texture "$parsing_shader" "$A1" editorimage
203                                 ;;
204                         skyparms\ *)
205                                 use_texture "$parsing_shader" "$A1" sky
206                                 ;;
207                         '{')
208                                 parse_shaderstage
209                                 ;;
210                         '}')
211                                 break
212                                 ;;
213                         *)
214                                 ;;
215                 esac
216         done
217 }
218
219 parse_shaderfile()
220 {
221         case "$1" in
222                 map_*)
223                         allowed_prefixes="textures/map_`echo "$1" | cut -d _ -f 2`_ textures/map_`echo "$1" | cut -d - -f 2`/"
224                         forbidden_prefixes=
225                         ;;
226                 skies_*)
227                         allowed_prefixes="textures/skies/`echo "$1" | cut -d _ -f 2`: textures/skies/`echo "$1" | cut -d _ -f 2`_"
228                         forbidden_prefixes=
229                         ;;
230                 *)
231                         allowed_prefixes=
232                         forbidden_prefixes="textures/skies/ textures/map_"
233                         ;;
234         esac
235         while read L; do
236                 case "$L" in
237                         */*)
238                                 parsing_shader="`normalize "$L"`"
239                                 if [ x"$L" != x"$parsing_shader" ]; then
240                                         echo "(WW) normalized shader name $L to $parsing_shader"
241                                 fi
242                                 ;;
243                         '{')
244                                 parse_shader
245                                 ;;
246                         *)
247                                 ;;
248                 esac
249         done
250 }
251
252 strip_comments()
253 {
254         sed 's,//.*,,g; s,\r, ,g; s,\t, ,g; s,  *, ,g; s, $,,; s,^ ,,; /^$/ d'
255 }
256
257 t=`mktemp`
258 for X in *.shader; do
259         strip_comments < "$X" > "$t"
260         parse_shaderfile "${X%.shader}" < "$t"
261 done
262 rm -f "$t"
263
264 textures_avail=`( cd ..; find textures/ -type f -not -name '*_norm.*' -not -name '*_glow.*' -not -name '*_gloss.*' ) | while IFS= read -r T; do normalize "$T"; done | sort -u`
265 textures_used=`echo "${textures_used#$LF}" | sort -u`
266
267 echo "$textures_used$LF$textures_used$LF$textures_avail" | sort | uniq -u | while IFS= read -r L; do
268         case "$L" in
269                 textures/radiant/*)
270                         ;;
271                 textures/map_*/*)
272                         ;;
273                 *)
274                         echo "(EE) texture $L is not referenced by any shader"
275                         ;;
276         esac
277 done