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