fix :P
[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 textures_used=
12 # $1 = shader
13 # $2 = texture
14 # $3 = self | map | animmap | editorimage
15 use_texture()
16 {
17         # does texture exist?
18         if \
19                 [ -f "../$2.tga" ] || \
20                 [ -f "../$2.jpg" ] || \
21                 [ -f "../$2.png" ]; then
22                 :
23         else
24                 if [ "$3" = "self" ]; then
25                         return
26                 else
27                         echo "(EE) shader $1 uses non-existing texture $2"
28                 fi
29         fi
30         textures_used="$textures_used$LF$2"
31
32         # TODO verify shader -> texture name
33         case "$1" in
34                 textures/*x/*-*)
35                         pre=${1%%x/*}x
36                         suf=${1#*x/}
37                         # rule: in suffix part, change each - to /
38                         suf="`echo "$suf" | sed 's,-,/,g'`"
39                         case "$2" in
40                                 "$pre"/*/*)
41                                         ;;
42                                 *)
43                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $pre/$suf"
44                                         ;;
45                         esac
46                         ;;
47                 textures/*x/*)
48                         pre=${1%%x/*}x
49                         suf=${1#*x/}
50                         case "$2" in
51                                 "$pre"/*/*)
52                                         ;;
53                                 *)
54                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $pre/base/$suf"
55                                         ;;
56                         esac
57                         ;;
58                 textures/map_*/*)
59                         pre=${1%%/map_*}
60                         suf=${1#*/map_}
61                         case "$2" in
62                                 "$pre"/map_*)
63                                         ;;
64                                 *)
65                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $pre/map_$suf"
66                                         ;;
67                         esac
68                         ;;
69                 textures/common/*)
70                         case "$2" in
71                                 "$1")
72                                         ;;
73                                 textures/common/*/*)
74                                         ;;
75                                 *)
76                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1 or textures/common/*/*"
77                                         ;;
78                         esac
79                         ;;
80                 textures/decals/*)
81                         case "$2" in
82                                 "$1")
83                                         # I _suppose_ this is fine, as tZork committed this pack
84                                         ;;
85                                 *)
86                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1 or textures/common/*/*"
87                                         ;;
88                         esac
89                         ;;
90                 *)
91                         echo "(EE) no shader name pattern for $1"
92                         ;;
93         esac
94 }
95
96 parsing_shader=
97 parse_shaderstage()
98 {
99         while read L A1 A2 Aother; do
100                 case "$L" in
101                         map)
102                                 case "$A1" in
103                                         '$lightmap')
104                                                 ;;
105                                         *)
106                                                 use_texture "$parsing_shader" "`normalize "$A1"`" map
107                                                 ;;
108                                 esac
109                                 ;;
110                         animmap)
111                                 for X in $A2 $Aother; do
112                                         use_texture "$parsing_shader" "`normalize "$X"`" animmap
113                                 done
114                                 ;;
115                         '}')
116                                 break
117                                 ;;
118                         *)
119                                 ;;
120                 esac
121         done
122 }
123
124 parse_shader()
125 {
126         use_texture "$parsing_shader" "$parsing_shader" self
127         while read L A1; do
128                 case "$L" in
129                         qer_editorimage\ *)
130                                 use_texture "$parsing_shader" "$A1" editorimage
131                                 ;;
132                         '{')
133                                 parse_shaderstage
134                                 ;;
135                         '}')
136                                 break
137                                 ;;
138                         *)
139                                 ;;
140                 esac
141         done
142 }
143
144 parse_shaderfile()
145 {
146         while read L; do
147                 case "$L" in
148                         textures/*)
149                                 parsing_shader="`normalize "$L"`"
150                                 if [ x"$L" != x"$parsing_shader" ]; then
151                                         echo "(WW) normalized shader name $L to $parsing_shader"
152                                 fi
153                                 ;;
154                         '{')
155                                 parse_shader
156                                 ;;
157                         *)
158                                 ;;
159                 esac
160         done
161 }
162
163 strip_comments()
164 {
165         sed 's,//.*,,g; s,\r, ,g; s,\t, ,g; s,  *, ,g; s, $,,; s,^ ,,; /^$/ d'
166 }
167
168 t=`mktemp`
169 for X in *.shader; do
170         strip_comments < "$X" > "$t"
171         parse_shaderfile < "$t"
172 done
173 rm -f "$t"
174
175 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`
176 textures_used=`echo "${textures_used#$LF}" | sort -u`
177
178 echo "$textures_used$LF$textures_used$LF$textures_avail" | sort | uniq -u | while IFS= read -r L; do
179         case "$L" in
180                 textures/radiant/*)
181                         ;;
182                 textures/map_*/*)
183                         ;;
184                 *)
185                         echo "(EE) texture $L is not referenced by any shader"
186                         ;;
187         esac
188 done