-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
154 lines (137 loc) Β· 4.31 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
//go:generate go install -v github.com/kevinburke/go-bindata/v4/go-bindata
//go:generate go-bindata -prefix res/ -pkg assets -o assets/assets.go res/DiscordPTB.lnk res/pinned_update.json
//go:generate go install -v github.com/josephspurrier/goversioninfo/cmd/goversioninfo
//go:generate goversioninfo -icon=res/papp.ico -manifest=res/papp.manifest
package main
import (
"encoding/json"
"io"
"os"
"path"
"strings"
"github.com/portapps/discord-ptb-portable/assets"
"github.com/portapps/portapps/v3"
"github.com/portapps/portapps/v3/pkg/log"
"github.com/portapps/portapps/v3/pkg/registry"
"github.com/portapps/portapps/v3/pkg/shortcut"
"github.com/portapps/portapps/v3/pkg/utl"
)
type config struct {
Cleanup bool `yaml:"cleanup" mapstructure:"cleanup"`
}
var (
app *portapps.App
cfg *config
)
func init() {
var err error
// Default config
cfg = &config{
Cleanup: false,
}
// Init app
if app, err = portapps.NewWithCfg("discord-ptb-portable", "DiscordPTB", cfg); err != nil {
log.Fatal().Err(err).Msg("Cannot initialize application. See log file for more info.")
}
}
func main() {
utl.CreateFolder(app.DataPath)
electronAppPath := app.ElectronAppPath()
app.Process = utl.PathJoin(electronAppPath, "DiscordPTB.exe")
app.Args = []string{
"--user-data-dir=" + app.DataPath,
}
app.WorkingDir = electronAppPath
// Cleanup on exit
if cfg.Cleanup {
defer func() {
regKey := registry.Key{
Key: `HKCU\SOFTWARE\DiscordPTB`,
Arch: "32",
}
if regKey.Exists() {
if err := regKey.Delete(true); err != nil {
log.Error().Err(err).Msg("Cannot remove registry key")
}
}
utl.Cleanup([]string{
path.Join(os.Getenv("APPDATA"), "discordptb"),
path.Join(os.Getenv("TEMP"), "Discord Crashes"),
})
}()
}
// Update settings
settingsPath := utl.PathJoin(app.DataPath, "settings.json")
if _, err := os.Stat(settingsPath); err == nil {
log.Info().Msg("Update settings...")
rawSettings, err := os.ReadFile(settingsPath)
if err == nil {
jsonMapSettings := make(map[string]interface{})
if err = json.Unmarshal(rawSettings, &jsonMapSettings); err != nil {
log.Error().Err(err).Msg("Settings unmarshal")
}
log.Info().Interface("settings", jsonMapSettings).Msg("Current settings")
jsonMapSettings["SKIP_HOST_UPDATE"] = true
jsonMapSettings["USE_PINNED_UPDATE_MANIFEST"] = true
log.Info().Interface("settings", jsonMapSettings).Msg("New settings")
jsonSettings, err := json.Marshal(jsonMapSettings)
if err != nil {
log.Error().Err(err).Msg("Settings marshal")
}
err = os.WriteFile(settingsPath, jsonSettings, 0644)
if err != nil {
log.Error().Err(err).Msg("Write settings")
}
}
} else {
fo, err := os.Create(settingsPath)
if err != nil {
log.Error().Err(err).Msg("Cannot create settings.json")
}
defer fo.Close()
if _, err = io.Copy(fo, strings.NewReader(`{
"SKIP_HOST_UPDATE": true,
"USE_PINNED_UPDATE_MANIFEST": true
}`)); err != nil {
log.Error().Err(err).Msg("Cannot write to settings.json")
}
}
// Copy pinned_update.json
pinnedUpdate, err := assets.Asset("pinned_update.json")
if err != nil {
log.Error().Err(err).Msg("Cannot load asset pinned_update.json")
}
err = os.WriteFile(utl.PathJoin(app.DataPath, "pinned_update.json"), pinnedUpdate, 0644)
if err != nil {
log.Error().Err(err).Msg("Cannot write pinned_update.json")
}
// Copy default shortcut
shortcutPath := path.Join(utl.StartMenuPath(), "Discord PTB Portable.lnk")
defaultShortcut, err := assets.Asset("DiscordPTB.lnk")
if err != nil {
log.Error().Err(err).Msg("Cannot load asset DiscordPTB.lnk")
}
err = os.WriteFile(shortcutPath, defaultShortcut, 0644)
if err != nil {
log.Error().Err(err).Msg("Cannot write default shortcut")
}
// Update default shortcut
err = shortcut.Create(shortcut.Shortcut{
ShortcutPath: shortcutPath,
TargetPath: app.Process,
Arguments: shortcut.Property{Clear: true},
Description: shortcut.Property{Value: "Discord PTB Portable by Portapps"},
IconLocation: shortcut.Property{Value: app.Process},
WorkingDirectory: shortcut.Property{Value: app.AppPath},
})
if err != nil {
log.Error().Err(err).Msg("Cannot create shortcut")
}
defer func() {
if err := os.Remove(shortcutPath); err != nil {
log.Error().Err(err).Msg("Cannot remove shortcut")
}
}()
defer app.Close()
app.Launch(os.Args[1:])
}