shader-audit: use the parsed shader data instead of a stupid grep
[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 [ "$3" != "self" ]; then
19                 if \
20                         [ -f "../$2.tga" ] || \
21                         [ -f "../$2.jpg" ] || \
22                         [ -f "../$2.png" ]; then
23                         :
24                 else
25                         echo "(EE) shader $1 uses non-existing texture $2"
26                 fi
27         fi
28         textures_used="$textures_used$LF$2"
29
30         # TODO verify shader -> texture name
31 }
32
33 parsing_shader=
34 parse_shaderstage()
35 {
36         while read L A1 A2 Aother; do
37                 case "$L" in
38                         map)
39                                 case "$A1" in
40                                         '$lightmap')
41                                                 ;;
42                                         *)
43                                                 use_texture "$parsing_shader" "`normalize "$A1"`" map
44                                                 ;;
45                                 esac
46                                 ;;
47                         animmap)
48                                 for X in $A2 $Aother; do
49                                         use_texture "$parsing_shader" "`normalize "$X"`" animmap
50                                 done
51                                 ;;
52                         '}')
53                                 break
54                                 ;;
55                         *)
56                                 ;;
57                 esac
58         done
59 }
60
61 parse_shader()
62 {
63         use_texture "$parsing_shader" "$parsing_shader" self
64         while read L A1; do
65                 case "$L" in
66                         qer_editorimage\ *)
67                                 use_texture "$parsing_shader" "$A1" editorimage
68                                 ;;
69                         '{')
70                                 parse_shaderstage
71                                 ;;
72                         '}')
73                                 break
74                                 ;;
75                         *)
76                                 ;;
77                 esac
78         done
79 }
80
81 parse_shaderfile()
82 {
83         while read L; do
84                 case "$L" in
85                         textures/*)
86                                 parsing_shader="`normalize "$L"`"
87                                 if [ x"$L" != x"$parsing_shader" ]; then
88                                         echo "(WW) normalized shader name $L to $parsing_shader"
89                                 fi
90                                 ;;
91                         '{')
92                                 parse_shader
93                                 ;;
94                         *)
95                                 ;;
96                 esac
97         done
98 }
99
100 strip_comments()
101 {
102         sed 's,//.*,,g; s,\r, ,g; s,\t, ,g; s,  *, ,g; s, $,,; s,^ ,,; /^$/ d'
103 }
104
105 t=`mktemp`
106 for X in *.shader; do
107         strip_comments < "$X" > "$t"
108         parse_shaderfile < "$t"
109 done
110 rm -f "$t"
111
112 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`
113 textures_used=`echo "${textures_used#$LF}" | sort -u`
114
115 echo "$textures_used$LF$textures_used$LF$textures_avail" | sort | uniq -u | while IFS= read -r L; do
116         echo "(EE) texture $L is not referenced by any shader"
117 done