detect skyparms
[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/*)
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                         case "$2" in
141                                 "$1")
142                                         # typical place for preview image
143                                         ;;
144                                 env/"${1#textures/skies/}")
145                                         # typical place for skybox
146                                         ;;
147                                 *)
148                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1"
149                                         ;;
150                         esac
151                         ;;
152                 models/*)
153                         case "$2" in
154                                 models/*)
155                                         ;;
156                                 *)
157                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1 or models/*"
158                                         ;;
159                         esac
160                         ;;
161                 *)
162                         echo "(EE) no shader name pattern for $1"
163                         ;;
164         esac
165 }
166
167 parsing_shader=
168 parse_shaderstage()
169 {
170         while read L A1 A2 Aother; do
171                 case "$L" in
172                         map)
173                                 case "$A1" in
174                                         '$lightmap')
175                                                 ;;
176                                         *)
177                                                 use_texture "$parsing_shader" "`normalize "$A1"`" map
178                                                 ;;
179                                 esac
180                                 ;;
181                         animmap)
182                                 for X in $A2 $Aother; do
183                                         use_texture "$parsing_shader" "`normalize "$X"`" animmap
184                                 done
185                                 ;;
186                         '}')
187                                 break
188                                 ;;
189                         *)
190                                 ;;
191                 esac
192         done
193 }
194
195 parse_shader()
196 {
197         use_texture "$parsing_shader" "$parsing_shader" shader
198         while read L A1; do
199                 case "$L" in
200                         qer_editorimage\ *)
201                                 use_texture "$parsing_shader" "$A1" editorimage
202                                 ;;
203                         skyparms\ *)
204                                 use_texture "$parsing_shader" "$A1" sky
205                                 ;;
206                         '{')
207                                 parse_shaderstage
208                                 ;;
209                         '}')
210                                 break
211                                 ;;
212                         *)
213                                 ;;
214                 esac
215         done
216 }
217
218 parse_shaderfile()
219 {
220         case "$1" in
221                 map_*)
222                         allowed_prefixes="textures/map_`echo "$1" | cut -d _ -f 2`_ textures/map_`echo "$1" | cut -d - -f 2`/"
223                         forbidden_prefixes=
224                         ;;
225                 skies_*)
226                         allowed_prefixes="textures/skies/`echo "$1" | cut -d _ -f 2`: textures/skies/`echo "$1" | cut -d _ -f 2`_"
227                         forbidden_prefixes=
228                         ;;
229                 *)
230                         allowed_prefixes=
231                         forbidden_prefixes="textures/skies/ textures/map_"
232                         ;;
233         esac
234         while read L; do
235                 case "$L" in
236                         */*)
237                                 parsing_shader="`normalize "$L"`"
238                                 if [ x"$L" != x"$parsing_shader" ]; then
239                                         echo "(WW) normalized shader name $L to $parsing_shader"
240                                 fi
241                                 ;;
242                         '{')
243                                 parse_shader
244                                 ;;
245                         *)
246                                 ;;
247                 esac
248         done
249 }
250
251 strip_comments()
252 {
253         sed 's,//.*,,g; s,\r, ,g; s,\t, ,g; s,  *, ,g; s, $,,; s,^ ,,; /^$/ d'
254 }
255
256 t=`mktemp`
257 for X in *.shader; do
258         strip_comments < "$X" > "$t"
259         parse_shaderfile "${X%.shader}" < "$t"
260 done
261 rm -f "$t"
262
263 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`
264 textures_used=`echo "${textures_used#$LF}" | sort -u`
265
266 echo "$textures_used$LF$textures_used$LF$textures_avail" | sort | uniq -u | while IFS= read -r L; do
267         case "$L" in
268                 textures/radiant/*)
269                         ;;
270                 textures/map_*/*)
271                         ;;
272                 *)
273                         echo "(EE) texture $L is not referenced by any shader"
274                         ;;
275         esac
276 done