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