-
Notifications
You must be signed in to change notification settings - Fork 3
/
patch.sh
executable file
·120 lines (106 loc) · 2.33 KB
/
patch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
if [[ $# -lt 2 ]]; then
echo "usage: $0 output.bsp target.gba [target.gba ...]" >&2
exit 4
fi
# patch in 32KB-sized fragments. Adjust for better patch compression.
fragsize=0x8000
output="$1"
shift
targets="$@"
function checkfail {
$@
local result=$?
if [[ $result -eq 0 ]]; then
return 0
fi
echo "[$1] exit status $result" >&2
exit 3
}
function updaterepo {
# $1: repo, $2: URL
if [[ ! ( -d $1 ) ]]; then
checkfail git clone --recursive $2 $1
fi
pushd $1
git pull
popd
}
function checkhash {
# $1: file, $2: expected hash
# returns a Bool: https://thedailywtf.com/articles/What_Is_Truth_0x3f_
if [[ ! ( -f $1 ) ]]; then
return 2
fi
[ `sha1sum -b $1 | cut -c 1-40` = $2 ]
if [[ $? -ne 0 ]]; then
return 1
fi
return 0
}
function buildemerald {
updaterepo agbcc https://github.com/pret/agbcc.git
updaterepo pokeemerald https://github.com/pret/pokeemerald.git
pushd agbcc
checkfail ./build.sh
checkfail ./install.sh ../pokeemerald
cd ../pokeemerald
checkfail make
checkfail cp pokeemerald.gba ..
popd
}
# make sure the patch directory exists
if [[ ! ( -d build/patch ) ]]; then
checkfail mkdir build/patch
fi
pushd build/patch
# make sure we have a copy of bspbuild
if [[ ! ( -x bspbuild ) ]]; then
updaterepo bspbuildrepo https://github.com/aaaaaa123456789/bspbuild.git
pushd bspbuildrepo
checkfail make
checkfail cp bspbuild ..
popd
fi
# rebuild the ROM if we don't have a good base
checkhash pokeemerald.gba f3ae088181bf583e55daf962a92bb46f4f1d07b7
if [[ $? -ne 0 ]]; then
buildemerald
checkhash pokeemerald.gba f3ae088181bf583e55daf962a92bb46f4f1d07b7
case $? in
0)
;;
1)
echo "[check] base ROM hash mismatch" >&2
exit 1
;;
2)
echo "[check] base ROM not found" >&2
exit 2
;;
*)
echo "[check] unknown error" >&2
exit 3
;;
esac
fi
# retrieve the target file(s)
popd
checkfail cp -t build/patch/ -- $targets
pushd build/patch
# build the patch
./bspbuild -s pokeemerald.gba -m xor-rle -st $targets -o $output -f $fragsize -pb 0 --check-fragment-swap --titles-from-stdin <<-END
// base ROM
pokeemerald.gba=Pokémon Emerald
// targets
trihardemerald.gba=TPP TriHard Emerald
END
result=$?
if [[ $result -ne 0 ]]; then
echo "[bspbuild] exit status $result" >&2
exit 3
fi
# copy the file to the parent directory and we're done
popd
checkfail cp build/patch/$output .
exit 0