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