]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/tools/sv_game-hashtest.sh
Merge branch 'terencehill/menu_registries' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / tools / sv_game-hashtest.sh
1 #!/bin/bash
2
3 # abort on error(non-0 return code) and abort on unset variable
4 set -eu
5
6 # refuse to run on Windows' MINGW due to symlinking issues
7 # `ln -s "$PWD" data/xonotic-data.pk3dir` seems to not work in any form
8 # even with fully relative paths or `ln -s ../ xonotic-data.pk3dir`
9 # FIXME?
10 case "$(uname)" in
11         MINGW*) printf "%s\n%s\n" "This file is not suitable for Windows' MINGW" \
12                 "without modifications due to the lack of proper symlink support"; exit 1 ;;
13 esac
14
15 printf "Testing for dependencies\n"
16 # command -q / --quiet one day? :(
17 command -V awk       > /dev/null
18 command -V chmod     > /dev/null
19 command -V git       > /dev/null
20 command -V grep      > /dev/null
21 command -V make      > /dev/null
22 command -V md5sum    > /dev/null
23 command -V mkdir     > /dev/null
24 command -V mktemp    > /dev/null
25 command -V rm        > /dev/null
26 command -V rmdir     > /dev/null
27 command -V sed       > /dev/null
28 command -V tee       > /dev/null
29 command -V test      > /dev/null
30 command -V true      > /dev/null
31 command -V printf    > /dev/null
32 command -V wget      > /dev/null
33 printf "All dependencies found\n"
34
35 createdtoday() {
36         # check if a file's creation date is today
37         if [ "$(stat -c '%y' "$1" | cut -d ' ' -f 1)" = "$(date -I)" ]
38         then
39                 #echo "$1 was created today"
40                 return 0
41         else
42                 #echo "$1 was not created today"
43                 return 1
44         fi
45 }
46
47 hashtestcleanup() {
48         # allow for error return codes in this function as this may be ran on interrupt
49         # right after starting where all files to clean up don't exist yet
50         set +e
51
52         # unset trap
53         trap - EXIT INT QUIT TERM
54
55         # Few files' removal has been disabled for file reuse
56         # It's possible to get rate limited with enough testing
57
58         rm lock
59         rm data/darkplaces_history.txt
60         rm data/xonotic-data.pk3dir
61         #rm data/stormkeep.pk3
62         rm data/data/defaultSVQC.cfg
63         rm data/data/hits---1.plot
64         rm data/data/hits---2.plot
65         rm data/data/hits---3.plot
66         rm data/data/notifications_dump.cfg
67         rm data/data/server.db
68         rmdir data/data/
69         #rm data/maps/_init.bsp
70         #rm data/maps/stormkeep.mapinfo
71         #rm data/maps/stormkeep.waypoints
72         #rm data/maps/stormkeep.waypoints.cache
73         #rmdir data/maps/
74
75         set -e
76 }
77 trap "hashtestcleanup" EXIT INT QUIT TERM
78
79 # cd xonotic-data.pk3dir
80 cd "$(dirname "$0")/../../"
81
82 WORKINGDIR="$PWD"
83
84 TMPDIR="$PWD/.tmp"
85
86 # if xonotic-data.pk3dir/data/xonotic-data.pk3dir isn't a symlink then link it
87 if [ -e data/xonotic-data.pk3dir ]
88 then # file exists
89         if ! [ -L data/xonotic-data.pk3dir ]
90         then # file exists but it's not a symlink, replace it
91                 if [ -d data/xonotic-data.pk3dir ]
92                 then # it's a dir
93                         rmdir data/xonotic-data.pk3dir
94                         ln -s "$PWD" data/xonotic-data.pk3dir
95                 else # it's not a dir
96                         rm data/xonotic-data.pk3dir
97                         ln -s "$PWD" data/xonotic-data.pk3dir
98                 fi
99         else # it is a symlink, verify where it points
100                 if [ "$(realpath data/xonotic-data.pk3dir)" != "$PWD" ]
101                 then # wrong place, recreate it
102                         rm data/xonotic-data.pk3dir
103                         ln -s "$PWD" data/xonotic-data.pk3dir
104                 fi
105         fi
106 else # no file exists there
107         ln -s "$PWD" data/xonotic-data.pk3dir
108 fi
109
110 MAKEFLAGS=-j$(nproc)
111 export MAKEFLAGS
112 printf "%s\n" "MAKEFLAGS=$MAKEFLAGS"
113 export CC="gcc -pipe"
114
115 # precompiled binary is executable and its creation date is today
116 if [ -x "$TMPDIR/gmqcc-bin" ] && createdtoday "$TMPDIR/gmqcc-bin"
117 then
118         export QCC="$TMPDIR/gmqcc-bin"
119 else # previously compiled is executable and its creation date is today
120         if [ -x "$TMPDIR/gmqcc/gmqcc" ] && createdtoday "$TMPDIR/gmqcc/gmqcc"
121         then
122                 export QCC="$TMPDIR/gmqcc/gmqcc"
123         else # nothing reusable exists
124                 # prefer a precompiled binary
125                 if wget -nv https://beta.xonotic.org/pipeline-bin/gmqcc -O "$TMPDIR/gmqcc-bin"
126                 then
127                         export QCC="$TMPDIR/gmqcc-bin"
128                         chmod +x "$QCC"
129                 else
130                         if [ -d "$TMPDIR/gmqcc" ]
131                         then
132                                 cd "$TMPDIR/gmqcc"
133                                 git checkout main
134                                 git pull
135                                 cd "$WORKINGDIR"
136                         else
137                                 git clone --depth=1 --branch=main https://gitlab.com/xonotic/gmqcc.git "$TMPDIR/gmqcc"
138                         fi
139                         make -C "$TMPDIR/gmqcc" || exit 1
140                         export QCC="$TMPDIR/gmqcc/gmqcc"
141                 fi
142         fi
143 fi
144
145 # Makefile: don't complain about lack of tags (fetching them is slow)
146 export QCCFLAGS_WATERMARK=gitlab_pipeline
147 # Makefile: don't compress anything or complain about lack of zip program
148 export ZIP=true
149
150
151 if [ "$(uname):$(uname -m)" = "Linux:x86_64" ] && [ -x "$TMPDIR/xonotic-linux64-dedicated" ] && createdtoday "$TMPDIR/xonotic-linux64-dedicated"
152 then # precompiled binary is executable and its creation date is today
153         export ENGINE="$TMPDIR/xonotic-linux64-dedicated"
154 else
155         if [ -x "$TMPDIR/darkplaces/darkplaces-dedicated" ] && createdtoday "$TMPDIR/darkplaces/darkplaces-dedicated"
156         then # previously compiled is executable and its creation date is today
157                 export ENGINE="$TMPDIR/darkplaces/darkplaces-dedicated -xonotic"
158         else # nothing reusable exists
159                 # prefer a precompiled binary
160                 if [ "$(uname):$(uname -m)" = "Linux:x86_64" ] && wget -nv https://beta.xonotic.org/pipeline-bin/xonotic-linux64-dedicated -O "$TMPDIR/xonotic-linux64-dedicated"
161                 then
162                         export ENGINE="$TMPDIR/xonotic-linux64-dedicated"
163                         chmod +x "$ENGINE"
164                 else
165                         if [ -d "$TMPDIR/darkplaces" ]
166                         then
167                                 cd "$TMPDIR/darkplaces"
168                                 #git checkout master
169                                 git pull --autostash
170                                 cd "$WORKINGDIR"
171                         else
172                                 git clone --depth=1 https://gitlab.com/xonotic/darkplaces.git "$TMPDIR/darkplaces"
173                         fi
174                         make -C "$TMPDIR/darkplaces" sv-release || exit 1
175                         export ENGINE="$TMPDIR/darkplaces/darkplaces-dedicated -xonotic"
176                 fi
177         fi
178 fi
179 export ENGINE="$ENGINE -noconfig -nohome"
180
181 make qc || exit 1
182
183 mkdir -p data/maps
184
185 createdtoday "data/maps/_init.bsp" \
186         || wget -nv -O data/maps/_init.bsp https://gitlab.com/xonotic/xonotic-maps.pk3dir/raw/master/maps/_init/_init.bsp
187
188 PASS=0
189 while read -r LINE
190 do
191         printf "%s\n" "$LINE"
192         printf "%s\n" "$LINE" | grep -q ".*All tests OK$" && PASS=1
193 done < <(${ENGINE} +developer 1 +map _init +sv_cmd runtest +wait +quit)
194 test "$PASS" = "1" || { printf "\033[1;31m%s\033[0m\n" "sv_cmd runtest failed!"; exit 1; }
195
196 ${ENGINE} +map _init +sv_cmd dumpnotifs +wait +quit
197 diff notifications.cfg data/data/notifications_dump.cfg ||
198         { printf "Please update notifications.cfg using \`dumpnotifs\`!"; exit 1; }
199
200 createdtoday "data/stormkeep.pk3" \
201         || wget -nv -O data/stormkeep.pk3 https://beta.xonotic.org/pipeline-bin/stormkeep.pk3
202 createdtoday "data/maps/stormkeep.mapinfo" \
203         || wget -nv -O data/maps/stormkeep.mapinfo https://gitlab.com/xonotic/xonotic-maps.pk3dir/raw/master/maps/stormkeep.mapinfo
204 createdtoday "data/maps/stormkeep.waypoints" \
205         || wget -nv -O data/maps/stormkeep.waypoints https://gitlab.com/xonotic/xonotic-maps.pk3dir/raw/master/maps/stormkeep.waypoints
206 createdtoday "data/maps/stormkeep.waypoints.cache" \
207         || wget -nv -O data/maps/stormkeep.waypoints.cache https://gitlab.com/xonotic/xonotic-maps.pk3dir/raw/master/maps/stormkeep.waypoints.cache
208
209 set +u
210 if [ -z "$EXPECT" ]
211 then
212         # find the line with expected hash from .gitlab-ci.yml, extract the hash and remove carriage return
213         EXPECT="$(grep 'EXPECT=' './.gitlab-ci.yml' | cut -d '=' -f 2 | tr -d $'\r')"
214 fi
215 set -u
216 HASH=$(${ENGINE} +exec serverbench.cfg \
217       | tee /dev/stderr \
218       | grep '^:' \
219       | grep -v '^:gamestart:' \
220       | grep -v '^:anticheat:' \
221       | md5sum | awk '{ print $1 }')
222
223 hashtestcleanup
224
225 if [ "$HASH" = "$EXPECT" ]
226 then # green ok print
227         printf "\033[32m%s\033[0m\n" "expected: $EXPECT"
228         printf "\033[32m%s\033[0m\n" "  actual: $HASH"
229         printf "\033[32m%s\033[0m\n" "hashes match"
230         exit 0
231 else # red error print
232         printf "\033[31m%s\033[0m\n" "expected: $EXPECT"
233         printf "\033[31m%s\033[0m\n" "  actual: $HASH"
234         printf "\033[1;31m%s\033[0m\n" "!!! ERROR: HASHES DO NOT MATCH !!!"
235         exit 1
236 fi