This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
267 lines (241 loc) · 7.75 KB
/
main.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"github.com/gen2brain/go-unarr"
"github.com/xybydy/go-mega"
)
const (
UpdateURL = "https://clonehero.cameronct.com/ingame/update.php"
)
type Update struct {
Version string `json:"version"` //The latest available version
Download string `json:"download"` //The Mega.nz folder containing builds of the latest available version
Required bool `json:"required"` //Whether or not the update is required
}
func main() {
fmt.Println("CHUpdater © JoshuaDoes: 2018.")
fmt.Println("Detected operating system: " + runtime.GOOS + "/" + runtime.GOARCH)
fmt.Println("")
fmt.Println("> Initializing Mega...")
m := mega.New()
fmt.Println("> Fetching update data...")
update := &Update{}
updateResult, err := http.Get(UpdateURL)
if err != nil {
defer runCloneHero()
panic(err)
}
err = unmarshal(updateResult, update)
if err != nil {
defer runCloneHero()
panic(err)
}
latestVersion, err := strconv.ParseFloat(update.Version, 32)
if err != nil {
defer runCloneHero()
panic(err)
}
installUpdated := false
fmt.Println("> Looking for Clone Hero data...")
if _, err := os.Stat("Clone Hero_Data/data.unity3d"); err == nil {
fmt.Println("> Reading Clone Hero data...")
data, err := ioutil.ReadFile("Clone Hero_Data/data.unity3d")
if err != nil {
runCloneHero()
panic(err)
}
fmt.Println("> Checking if Clone Hero is latest version...")
if bytes.Contains(data, float32ToBytes(float32(latestVersion))) {
installUpdated = true
}
}
if installUpdated {
fmt.Println("> Clone Hero is already up-to-date")
runCloneHero()
os.Exit(0)
} else {
fmt.Println("> An update is available!")
fmt.Println("> Updating...")
}
fmt.Println("> Fetching Mega URL...")
//Make a function that tells http to not run down the redirect line
checkRedirect := func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
//Create an HTTP client that uses our custom checkRedirect function
client := &http.Client{CheckRedirect: checkRedirect}
//Create the request to the Bitly URL
request, err := client.Head(update.Download)
if err != nil {
defer runCloneHero()
panic(err)
}
//Finally, get the Mega URL from the redirect
megaURL := request.Header.Get("Location")
fmt.Println("> Setting MegaFS to Clone Hero folder...")
_, _ = m.ReturnPublicNode(megaURL)
fmt.Println("> Fetching MegaFS...")
megaFS := m.FS
fmt.Println("> Fetching MegaFS files...")
megaFSNodes := megaFS.GetAllNodes()
fmt.Println("> Looking for Clone Hero " + runtime.GOOS + "/" + runtime.GOARCH + "...")
downloadFound := false
downloadType := ""
for _, v := range megaFSNodes {
nodeName := v.GetName()
switch nodeName {
//RAR
case "Windows (64).rar":
if runtime.GOOS == "windows" && runtime.GOARCH == "amd64" {
downloadFound = true
downloadType = "rar"
fmt.Println("> Downloading Clone Hero for " + runtime.GOOS + "/" + runtime.GOARCH + "...")
m.DownloadFile(v, "clonehero.rar", nil, true)
break
}
case "Windows (32).rar":
if runtime.GOOS == "windows" && runtime.GOARCH == "386" {
downloadFound = true
downloadType = "rar"
fmt.Println("> Downloading Clone Hero for " + runtime.GOOS + "/" + runtime.GOARCH + "...")
m.DownloadFile(v, "clonehero.rar", nil, true)
break
}
case "Linux.rar":
if runtime.GOOS == "linux" {
downloadFound = true
downloadType = "rar"
fmt.Println("> Downloading Clone Hero for " + runtime.GOOS + "/" + runtime.GOARCH + "...")
m.DownloadFile(v, "clonehero.rar", nil, true)
break
}
//7z
case "Windows (64).7z":
if runtime.GOOS == "windows" && runtime.GOARCH == "amd64" {
downloadFound = true
downloadType = "7z"
fmt.Println("> Downloading Clone Hero for " + runtime.GOOS + "/" + runtime.GOARCH + "...")
m.DownloadFile(v, "clonehero.7z", nil, true)
break
}
case "Windows (32).7z":
if runtime.GOOS == "windows" && runtime.GOARCH == "386" {
downloadFound = true
downloadType = "7z"
fmt.Println("> Downloading Clone Hero for " + runtime.GOOS + "/" + runtime.GOARCH + "...")
m.DownloadFile(v, "clonehero.7z", nil, true)
break
}
case "Linux.7z":
if runtime.GOOS == "linux" {
downloadFound = true
downloadType = "7z"
fmt.Println("> Downloading Clone Hero for " + runtime.GOOS + "/" + runtime.GOARCH + "...")
m.DownloadFile(v, "clonehero.7z", nil, true)
break
}
}
}
if !downloadFound {
defer runCloneHero()
panic(errors.New("Error finding download for " + runtime.GOOS + "/" + runtime.GOARCH))
}
fmt.Println("> Removing previous Clone Hero game files...")
removeCloneHero()
fmt.Println("> Loading Clone Hero archive into memory...")
archive, err := unarr.NewArchive("clonehero." + downloadType)
if err != nil {
os.Remove("clonehero." + downloadType)
panic(err)
}
defer archive.Close()
fmt.Println("> Extracting Clone Hero...")
err = archive.Extract("")
if err != nil {
os.Remove("clonehero." + downloadType)
removeCloneHero()
panic(err)
}
fmt.Println("> Removing Clone Hero archive...")
os.Remove("clonehero." + downloadType)
if runtime.GOOS == "linux" {
fmt.Println("> Moving game files to current working directory...")
_ = os.Rename("Linux/Clone Hero_Data", "Clone Hero_Data")
_ = os.Rename("Linux/Clone Hero.x86_64", "Clone Hero.x86_64")
_ = os.Rename("Linux/README.txt", "README.txt")
os.RemoveAll("Linux")
} else if runtime.GOOS == "windows" {
if runtime.GOARCH == "386" {
fmt.Println("> Moving game files to current working directory...")
os.Rename("Windows (32)/Clone Hero_Data", "Clone Hero_Data")
os.Rename("Windows (32)/MonoBleedingEdge", "MonoBleedingEdge")
os.Rename("Windows (32)/Songs", "Songs")
os.Rename("Windows (32)/Clone Hero.exe", "Clone Hero.exe")
os.Rename("Windows (32)/README.txt", "README.txt")
os.Rename("Windows (32)/UnityCrashHandler32.exe", "UnityCrashHandler32.exe")
os.Rename("Windows (32)/UnityPlayer.dll", "UnityPlayer.dll")
os.RemoveAll("Windows (32)")
} else if runtime.GOARCH == "amd64" {
fmt.Println("> Moving game files to current working directory...")
os.Rename("Windows (64)/Clone Hero_Data", "Clone Hero_Data")
os.Rename("Windows (64)/MonoBleedingEdge", "MonoBleedingEdge")
os.Rename("Windows (64)/Songs", "Songs")
os.Rename("Windows (64)/Clone Hero.exe", "Clone Hero.exe")
os.Rename("Windows (64)/README.txt", "README.txt")
os.Rename("Windows (64)/UnityCrashHandler32.exe", "UnityCrashHandler32.exe")
os.Rename("Windows (64)/UnityPlayer.dll", "UnityPlayer.dll")
os.RemoveAll("Windows (64)")
}
}
runCloneHero()
}
func runCloneHero() {
fmt.Println("> Running Clone Hero...")
switch runtime.GOOS {
case "windows":
cmd := exec.Command("./Clone Hero.exe")
err := cmd.Start()
if err != nil {
panic(err)
}
case "linux":
cmd := exec.Command("./Clone Hero.x86_64")
err := cmd.Run()
if err != nil {
panic(err)
}
}
}
func removeCloneHero() {
switch runtime.GOOS {
case "windows":
os.RemoveAll("Clone Hero_Data")
os.Remove("Clone Hero.exe")
os.Remove("UnityPlayer.dll")
case "linux":
os.RemoveAll("Clone Hero_Data")
os.Remove("Clone Hero.x86_64")
}
}
func unmarshal(body *http.Response, target interface{}) error {
defer body.Body.Close()
return json.NewDecoder(body.Body).Decode(target)
}
func float32ToBytes(f float32) []byte {
var buf bytes.Buffer
err := binary.Write(&buf, binary.LittleEndian, f)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
return buf.Bytes()
}