-
-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathpaths.go
More file actions
190 lines (159 loc) · 4.76 KB
/
Copy pathpaths.go
File metadata and controls
190 lines (159 loc) · 4.76 KB
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
package utils
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
)
var Roaming string
var BetterDiscord string
var Data string
var Asar string
var Plugins string
var Themes string
func init() {
var configDir, err = os.UserConfigDir()
if err != nil {
return
}
Roaming = configDir
BetterDiscord = filepath.Join(configDir, "BetterDiscord")
Data = filepath.Join(BetterDiscord, "data")
Asar = filepath.Join(Data, "betterdiscord.asar")
Plugins = filepath.Join(BetterDiscord, "plugins")
Themes = filepath.Join(BetterDiscord, "themes")
}
func Exists(path string) bool {
var _, err = os.Stat(path)
return err == nil
}
func DiscordPath(channel string) string {
var channelName = GetChannelName(channel)
switch op := runtime.GOOS; op {
case "windows":
return ValidatePath(filepath.Join(os.Getenv("LOCALAPPDATA"), channelName))
case "darwin", "linux":
return ValidatePath(filepath.Join(Roaming, strings.ToLower(channelName)))
default:
return ""
}
}
// TODO: make this check and attempt to return the base path when full path doesn't exist
func BrowsePath(channel string) string {
var channelName = GetChannelName(channel)
var candidate = ""
switch op := runtime.GOOS; op {
case "windows":
candidate = filepath.Join(os.Getenv("LOCALAPPDATA"), channelName)
case "darwin", "linux":
candidate = filepath.Join(Roaming, strings.ToLower(channelName))
default:
candidate = ""
}
if Exists(candidate) {
return candidate
}
candidate, err := os.UserHomeDir()
if err != nil {
return ""
}
return candidate
}
func ValidatePath(proposed string) string {
switch op := runtime.GOOS; op {
case "windows":
return validateWindows(proposed)
case "darwin", "linux":
return validateMacLinux(proposed)
default:
return ""
}
}
func Filter[T any](source []T, filterFunc func(T) bool) (ret []T) {
var returnArray = []T{}
for _, s := range source {
if filterFunc(s) {
returnArray = append(ret, s)
}
}
return returnArray
}
func validateWindows(proposed string) string {
var finalPath = ""
var selected = filepath.Base(proposed)
fmt.Println("Selected " + selected)
if strings.HasPrefix(selected, "Discord") {
// Get version dir like 1.0.9002
var dFiles, err = os.ReadDir(proposed)
if err != nil {
return ""
}
var candidates = Filter(dFiles, func(file fs.DirEntry) bool { return file.IsDir() && len(strings.Split(file.Name(), ".")) == 3 })
sort.Slice(candidates, func(i, j int) bool { return candidates[i].Name() < candidates[j].Name() })
var versionDir = candidates[len(candidates)-1].Name()
// Get core wrap like discord_desktop_core-1
dFiles, err = os.ReadDir(filepath.Join(proposed, versionDir, "modules"))
if err != nil {
return ""
}
candidates = Filter(dFiles, func(file fs.DirEntry) bool {
return file.IsDir() && strings.HasPrefix(file.Name(), "discord_desktop_core")
})
var coreWrap = candidates[len(candidates)-1].Name()
finalPath = filepath.Join(proposed, versionDir, "modules", coreWrap, "discord_desktop_core")
}
// Use a separate if statement because forcing same-line } else if { is gross
if strings.HasPrefix(selected, "app-") {
var dFiles, err = os.ReadDir(filepath.Join(proposed, "modules"))
if err != nil {
return ""
}
var candidates = Filter(dFiles, func(file fs.DirEntry) bool {
return file.IsDir() && strings.HasPrefix(file.Name(), "discord_desktop_core")
})
var coreWrap = candidates[len(candidates)-1].Name()
finalPath = filepath.Join(proposed, "modules", coreWrap, "discord_desktop_core")
}
if selected == "discord_desktop_core" {
finalPath = proposed
}
// If the path and the asar exist, all good
if Exists(finalPath) && Exists(filepath.Join(finalPath, "core.asar")) {
return finalPath
}
return ""
}
func validateMacLinux(proposed string) string {
if strings.Contains(proposed, "/snap") {
return ""
}
var finalPath = ""
var selected = filepath.Base(proposed)
if strings.HasPrefix(selected, "discord") {
// Get version dir like 1.0.9002
var dFiles, err = os.ReadDir(proposed)
if err != nil {
return ""
}
var candidates = Filter(dFiles, func(file fs.DirEntry) bool { return file.IsDir() && len(strings.Split(file.Name(), ".")) == 3 })
sort.Slice(candidates, func(i, j int) bool { return candidates[i].Name() < candidates[j].Name() })
var versionDir = candidates[len(candidates)-1].Name()
finalPath = filepath.Join(proposed, versionDir, "modules", "discord_desktop_core")
}
if len(strings.Split(selected, ".")) == 3 {
finalPath = filepath.Join(proposed, "modules", "discord_desktop_core")
}
if selected == "modules" {
finalPath = filepath.Join(proposed, "discord_desktop_core")
}
if selected == "discord_desktop_core" {
finalPath = proposed
}
if Exists(finalPath) && Exists(filepath.Join(finalPath, "core.asar")) {
return finalPath
}
return ""
}