8076dea9eeee87014ab2e605d9530944bfb019e1
[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/common/*)
59                         case "$2" in
60                                 "$1")
61                                         ;;
62                                 textures/common/*/*)
63                                         ;;
64                                 *)
65                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1 or textures/common/*/*"
66                                         ;;
67                         esac
68                         ;;
69                 textures/decals/*)
70                         case "$2" in
71                                 "$1")
72                                         # I _suppose_ this is fine, as tZork committed this pack
73                                         ;;
74                                 *)
75                                         echo "(EE) texture $2 of shader $1 is out of place, recommended file name is $1 or textures/common/*/*"
76                                         ;;
77                         esac
78                         ;;
79                 *)
80                         echo "(EE) no shader name pattern for $1"
81                         ;;
82         esac
83 }
84
85 parsing_shader=
86 parse_shaderstage()
87 {
88         while read L A1 A2 Aother; do
89                 case "$L" in
90                         map)
91                                 case "$A1" in
92                                         '$lightmap')
93                                                 ;;
94                                         *)
95                                                 use_texture "$parsing_shader" "`normalize "$A1"`" map
96                                                 ;;
97                                 esac
98                                 ;;
99                         animmap)
100                                 for X in $A2 $Aother; do
101                                         use_texture "$parsing_shader" "`normalize "$X"`" animmap
102                                 done
103                                 ;;
104                         '}')
105                                 break
106                                 ;;
107                         *)
108                                 ;;
109                 esac
110         done
111 }
112
113 parse_shader()
114 {
115         use_texture "$parsing_shader" "$parsing_shader" self
116         while read L A1; do
117                 case "$L" in
118                         qer_editorimage\ *)
119                                 use_texture "$parsing_shader" "$A1" editorimage
120                                 ;;
121                         '{')
122                                 parse_shaderstage
123                                 ;;
124                         '}')
125                                 break
126                                 ;;
127                         *)
128                                 ;;
129                 esac
130         done
131 }
132
133 parse_shaderfile()
134 {
135         while read L; do
136                 case "$L" in
137                         textures/*)
138                                 parsing_shader="`normalize "$L"`"
139                                 if [ x"$L" != x"$parsing_shader" ]; then
140                                         echo "(WW) normalized shader name $L to $parsing_shader"
141                                 fi
142                                 ;;
143                         '{')
144                                 parse_shader
145                                 ;;
146                         *)
147                                 ;;
148                 esac
149         done
150 }
151
152 strip_comments()
153 {
154         sed 's,//.*,,g; s,\r, ,g; s,\t, ,g; s,  *, ,g; s, $,,; s,^ ,,; /^$/ d'
155 }
156
157 t=`mktemp`
158 for X in *.shader; do
159         strip_comments < "$X" > "$t"
160         parse_shaderfile < "$t"
161 done
162 rm -f "$t"
163
164 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`
165 textures_used=`echo "${textures_used#$LF}" | sort -u`
166
167 echo "$textures_used$LF$textures_used$LF$textures_avail" | sort | uniq -u | while IFS= read -r L; do
168         echo "(EE) texture $L is not referenced by any shader"
169 done