-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
257 lines (211 loc) · 5.5 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
package main
import (
"coh3-replay-manager-go/modules/game"
"coh3-replay-manager-go/modules/replay"
"coh3-replay-manager-go/modules/utils"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/Teages/go-autostart"
"github.com/fynelabs/selfupdate"
"github.com/gen2brain/beeep"
"github.com/getlantern/systray"
)
const CurrentVersion = "v0.1.4"
func main() {
if os.Getenv("DEV_MODE") != "true" {
autoUpdate()
// Run auto update every 24 hours
go func() {
for {
time.Sleep(24 * time.Hour)
autoUpdate()
}
}()
}
// Check if the app was opened with a command line argument
if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "coh3-replay-manager-go://") {
// Get the string containing the replay id and game version from the command line argument (without the protocol)
input := strings.TrimPrefix(os.Args[1], "coh3-replay-manager-go://")
parseUrlInput(input)
// Exit the application
return
}
systray.Run(onReady, onExit)
}
func onReady() {
appPath, err := os.Executable()
if err != nil {
fmt.Println("Error getting app path:", err)
return
}
app := &autostart.App{
Name: "coh3-replay-manager-go",
Exec: []string{appPath},
}
appIcon := getIconData("icon.ico")
systray.SetIcon(appIcon)
systray.SetTitle("Company of Heroes 3 Replay Manager")
systray.SetTooltip("Company of Heroes 3 Replay Manager")
mSetStartup := systray.AddMenuItem("Launch on startup", "Start this app when your computer starts")
systray.AddSeparator()
mAbout := systray.AddMenuItem(fmt.Sprintf("About (%s)", CurrentVersion), "")
mQuit := systray.AddMenuItem("Exit", "")
if app.IsEnabled() {
mSetStartup.Check()
} else {
mSetStartup.Uncheck()
}
go func() {
for range mSetStartup.ClickedCh {
if app.IsEnabled() {
mSetStartup.Uncheck()
if err := app.Disable(); err != nil {
fmt.Println(err)
}
} else {
mSetStartup.Check()
if err := app.Enable(); err != nil {
fmt.Println(err)
}
}
}
}()
go func() {
for range mAbout.ClickedCh {
utils.OpenUrl("https://github.com/SavageCore/coh3-replay-manager-go")
}
}()
go func() {
for range mQuit.ClickedCh {
systray.Quit()
}
}()
utils.RegisterUrlProtocol()
go replay.InitialiseFolderWatcher()
}
func onExit() {
os.Exit(0)
}
func parseUrlInput(input string) {
re := regexp.MustCompile(`^([A-Za-z]+)/(\d+)/v/(\d.+)$`)
if re.MatchString(input) {
action := re.FindStringSubmatch(input)[1]
// Get the ID from the URL
id := re.FindStringSubmatch(input)[2]
// Get the game version from the URL
replayGameVersion := re.FindStringSubmatch(input)[3]
gameVersion := game.GetGameVersion()
// Check if the game versions match
if gameVersion != replayGameVersion {
title := "⛔ Replay cannot be played"
message := fmt.Sprintf("⚠️ Game version: %s does not equal Replay's version: %s", gameVersion, replayGameVersion)
err := beeep.Notify(title, message, "")
if err != nil {
fmt.Println(err)
}
return
}
// If the action is "play", play the replay
if action == "play" {
filename := replay.Download(id)
replay.Play(filename)
}
if action == "download" {
replay.Download(id)
}
} else {
// Invalid URL
fmt.Println("Invalid URL")
os.Exit(1)
}
}
func getIconData(file string) []byte {
if file == "" {
file = "icon.ico"
}
iconData, err := Asset("assets/icons/" + file)
if err != nil {
fmt.Println(err)
}
return iconData
}
func autoUpdate() bool {
release, err := utils.GetLatestRelease()
if err != nil {
fmt.Println(err)
}
if release.TagName != CurrentVersion {
// New version available
title := "New version available"
message := fmt.Sprintf("Version %s is available. Downloading and restarting the app.", release.TagName)
err := beeep.Notify(title, message, "")
if err != nil {
fmt.Println(err)
}
for _, asset := range release.Assets {
if asset.Name == "coh3-replay-manager-go_Windows_x86_64.zip" {
// Set file path to tmp folder
downloadPath := filepath.Join(os.TempDir(), asset.Name)
utils.DownloadFile(asset.DownloadURL, downloadPath)
fmt.Println("Downloaded file to", downloadPath)
// Unzip the file
utils.ExtractZip(downloadPath, os.TempDir())
// Delete the zip file
err := os.Remove(downloadPath)
if err != nil {
fmt.Println("Failed to delete file:", err)
return false
}
extractedFilePath := filepath.Join(os.TempDir(), "coh3-replay-manager-go.exe")
// Read the extracted file coh3-replay-manager-go.exe
file, err := os.Open(extractedFilePath)
if err != nil {
fmt.Println("Failed to open file:", err)
return false
}
defer file.Close()
reader := io.Reader(file)
// Update the app
err = selfupdate.Apply(reader, selfupdate.Options{})
if err != nil {
// error handling
fmt.Println("Failed to update:", err)
}
err = file.Close()
if err != nil {
fmt.Println("Failed to close file:", err)
return false
}
// Get the path to the current executable
exePath, err := os.Executable()
if err != nil {
fmt.Println("Failed to get executable path:", err)
return false
}
// Delete the extracted file
err = os.Remove(extractedFilePath)
if err != nil {
fmt.Println("Failed to delete file:", err)
return false
}
// Restart the app
cmd := exec.Command(exePath)
_, err = cmd.Output()
if err != nil {
fmt.Println("Failed to restart:", err)
return false
}
os.Exit(0)
}
}
return true
} else {
return false
}
}