-
Notifications
You must be signed in to change notification settings - Fork 2
/
wow-profile-copy.go
423 lines (355 loc) · 11.4 KB
/
wow-profile-copy.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main
import (
"fmt"
"os"
"io"
"log"
"runtime"
"regexp"
"path/filepath"
"github.com/pterm/pterm"
"strings"
// "github.com/pterm/pterm/putils"
)
type WowInstall struct {
availableVersions []string
installDirectory string
}
type Wtf struct {
account string
server string
character string
}
type CopyTarget struct {
wtf Wtf
version string
}
// smelly?
var _wowInstanceFolderNames = map[string]string{
"_classic_": "WoTLK Classic",
"_classic_ptr_": "WoTLK Classic PTR",
"_classic_beta_": "WoTLK Classic Beta",
"_retail_": "Retail",
}
var _probableWowInstallLocations = map[string]string{
"darwin": "/Applications/World of Warcraft",
"windows": "C:\\Program Files (x86)\\World of Warcraft",
}
//
//
// WoWInstall methods
//
//
// Finds all valid WTF configs (account, server, character) for a given WoW version
func (wow WowInstall) getWtfConfigurations(version string) []Wtf {
var configurations []Wtf
accountRegex := regexp.MustCompile(`[0-9]*#[0-9]*`)
wtfPath := filepath.Join(wow.installDirectory, version, "WTF", "Account") // a fitting name
// enumerate available accounts on this instance
wtfFiles, err := os.ReadDir(wtfPath)
if err != nil {
log.Fatal(err)
}
// this is O(n)^3 - consider lazy loading or refactoring?
for _, acct := range wtfFiles {
if acct.IsDir() && accountRegex.MatchString(acct.Name()) { // make sure that this directory matches the account number format
accountPath := filepath.Join(wtfPath, acct.Name())
serverFiles, err := os.ReadDir(accountPath) // enumerate available servers under each account
if err != nil {
log.Fatal(err)
}
for _, server := range serverFiles {
if server.IsDir() && server.Name() != "SavedVariables" { // assume that any folder that isn't SavedVariables here is a realm
serverPath := filepath.Join(accountPath, server.Name())
characterFiles, err := os.ReadDir(serverPath)
if err != nil {
log.Fatal(err)
}
for _, character := range characterFiles { // any subdirectories of the server directories are characters, they have arbitrary names
if character.IsDir() {
finalWtf := Wtf{
account: acct.Name(),
server: server.Name(),
character: character.Name(),
}
configurations = append(configurations, finalWtf)
}
}
}
}
}
}
return configurations
}
// determines which WoW versions are available in a given WoW install directory (classic, retail, SoM, etc..)
func (wow *WowInstall) findAvailableVersions(dir string) {
files, err := os.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
// if this directory contains a wow instance folder name, it's probably where WoW is installed
_, matchesInstanceName := _wowInstanceFolderNames[file.Name()]
if file.IsDir() && matchesInstanceName {
wow.availableVersions = append(wow.availableVersions, file.Name())
}
}
}
// prompts the user to select a WTF tuple to copy to/from
// isSource: whether we are selecting the source of the copy or the destination
func (wow WowInstall) selectWtf(isSource bool) CopyTarget {
var preposition = "to"
if isSource {
preposition = "from"
}
var versions []string
for _, version := range wow.availableVersions {
versions = append(versions, version)
}
wowVersion, _ := pterm.DefaultInteractiveSelect.
WithOptions(versions).
WithDefaultText(fmt.Sprintf("WoW Version to copy %s", preposition)).
Show()
pterm.Debug.Printfln("chose %s", wowVersion)
wtfConfigs := wow.getWtfConfigurations(wowVersion)
var accountOptions []string
for _, wtf := range wtfConfigs {
accountOptions = append(accountOptions, wtf.account)
}
accountOptions = deduplicateStringSlice(accountOptions)
chosenAccount, _ := pterm.DefaultInteractiveSelect.
WithOptions(accountOptions).
WithDefaultText(fmt.Sprintf("Account to copy %s", preposition)).
Show()
pterm.Debug.Printfln("chose %s", chosenAccount)
var serverOptions []string
for _, wtf := range wtfConfigs {
if wtf.account == chosenAccount {
serverOptions = append(serverOptions, wtf.server)
}
}
serverOptions = deduplicateStringSlice(serverOptions)
chosenServer, _ := pterm.DefaultInteractiveSelect.
WithOptions(serverOptions).
WithDefaultText(fmt.Sprintf("Server to copy %s", preposition)).
Show()
pterm.Debug.Printfln("chose %s", chosenServer)
var characterOptions []string
for _, wtf := range wtfConfigs {
if wtf.account == chosenAccount && wtf.server == chosenServer {
characterOptions = append(characterOptions, wtf.character)
}
}
chosenCharacter, _ := pterm.DefaultInteractiveSelect.
WithOptions(characterOptions).
WithDefaultText(fmt.Sprintf("Character to copy %s", preposition)).
Show()
pterm.Debug.Printfln("chose %s", chosenCharacter)
return CopyTarget{
wtf: Wtf{
account: chosenAccount,
server: chosenServer,
character: chosenCharacter,
},
version: wowVersion,
}
}
//
//
// helper functions
//
//
// determines if a given path appears to contain a WoW install
func isWowInstallDirectory(dir string) bool {
var isInstallDir = false
files, err := os.ReadDir(dir)
if err != nil {
// directory probably doesn't exist
return false
}
for _, file := range files {
// if this directory contains a wow instance folder name, it's probably where WoW is installed
_, matchesInstanceName := _wowInstanceFolderNames[file.Name()]
if file.IsDir() && matchesInstanceName {
isInstallDir = true
break
}
}
return isInstallDir
}
func promptForWowDirectory(dir string) (wowDir string, err error) {
files, err := os.ReadDir(dir)
if err != nil {
return "", err
}
var fileChoices = []string{".. (go back)"}
for _, file := range files {
fileChoices = append(fileChoices, file.Name())
}
selectedFile, _ := pterm.DefaultInteractiveSelect.
WithOptions(fileChoices).
WithDefaultText("Select a WoW Install directory").
WithMaxHeight(15).
Show()
var fullSelectedPath string
if selectedFile == ".. (go back)" {
fullSelectedPath = filepath.Clean(filepath.Join(dir, ".."))
} else {
fullSelectedPath = filepath.Join(dir, selectedFile)
}
isWowDir := isWowInstallDirectory(fullSelectedPath)
if !isWowDir {
return promptForWowDirectory(fullSelectedPath)
} else {
return fullSelectedPath, nil
}
}
// deduplicates slices by throwing them into a map
// not mine, credit to @kylewbanks
func deduplicateStringSlice(input []string) []string {
u := make([]string, 0, len(input))
m := make(map[string]bool)
for _, val := range input {
if _, ok := m[val]; !ok {
m[val] = true
u = append(u, val)
}
}
return u
}
// small wrapper around os and io to copy files from source to destination
func copyFile(src string, dest string) (bytes int64, err error) {
srcFileHandle, err := os.Open(src)
if err != nil {
return -1, err
}
defer srcFileHandle.Close()
dstFileHandle, err := os.Create(dest)
if err != nil {
return -1, err
}
defer dstFileHandle.Close()
bytes, err = io.Copy(dstFileHandle, srcFileHandle)
return bytes, err
}
func main() {
var wow WowInstall
userHomeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
_probableWowInstallLocations["linux"] = fmt.Sprintf("%s/.var/app/com.usebottles.bottles/data/bottles/bottles/WoW/drive_c/Program Files (x86)/World of Warcraft", userHomeDir)
// this will crash when not on linux, macOS, or windows
// if you're trying to run wow on BSD or plan9, you can probably fix this yourself
installLocation := _probableWowInstallLocations[runtime.GOOS]
dirOk := isWowInstallDirectory(installLocation);
if !dirOk {
base := "/"
if runtime.GOOS == "windows" {
baseInput, _ := pterm.DefaultInteractiveTextInput.
WithDefaultText("Which drive is WoW located on? e.g. C, D").
Show()
base = fmt.Sprintf("%s:\\", string(baseInput[0]))
}
installLocation, _ = promptForWowDirectory(base);
}
wow.installDirectory = installLocation
wow.findAvailableVersions(installLocation)
pterm.DefaultHeader.Printfln("WoW Install Directory: %s", wow.installDirectory)
srcConfig := wow.selectWtf(true)
dstConfig := wow.selectWtf(false)
pterm.Info.Printfln("Source: { Version: %s, Account: %s, Server: %s, Character: %s }", _wowInstanceFolderNames[srcConfig.version], srcConfig.wtf.account, srcConfig.wtf.server, srcConfig.wtf.character)
pterm.Info.Printfln("Destination: { Version: %s, Account :%s, Server: %s, Character: %s }", _wowInstanceFolderNames[dstConfig.version], dstConfig.wtf.account, dstConfig.wtf.server, dstConfig.wtf.character)
confirmation, _ := pterm.DefaultInteractiveConfirm.
WithDefaultText("Copy Keybindings, Macros, and SavedVariables? This can cause data loss - make a backup!").
Show()
if !confirmation {
os.Exit(1)
}
//
// account-level client configuration
//
srcWtfAccountPath := filepath.Join(wow.installDirectory, srcConfig.version, "WTF", "Account", srcConfig.wtf.account)
dstWtfAccountPath := filepath.Join(wow.installDirectory, dstConfig.version, "WTF", "Account", dstConfig.wtf.account)
accountFilesToCopy := [3]string{"bindings-cache.wtf", "config-cache.wtf", "macros-cache.txt"}
for _, file := range accountFilesToCopy {
src := filepath.Join(srcWtfAccountPath, file)
dst := filepath.Join(dstWtfAccountPath, file)
_, err := copyFile(src, dst)
if err != nil {
log.Fatal(err)
}
pterm.Info.Printfln("Copied %s", src)
}
//
// character-level client configuration
//
srcWtfCharacterPath := filepath.Join(srcWtfAccountPath, srcConfig.wtf.server, srcConfig.wtf.character)
dstWtfCharacterPath := filepath.Join(dstWtfAccountPath, dstConfig.wtf.server, dstConfig.wtf.character)
characterFilesToCopy := [4]string{"AddOns.txt", "config-cache.wtf", "layout-local.txt", "macros-cache.txt"}
for _, file := range characterFilesToCopy {
src := filepath.Join(srcWtfCharacterPath, file)
dst := filepath.Join(dstWtfCharacterPath, file)
_, err := copyFile(src, dst)
if err != nil {
log.Fatal(err)
}
pterm.Info.Printfln("Copied %s", src)
}
//
// account-level saved variables
//
svFileRegex := regexp.MustCompile(`.*\.lua$`)
accountSavedVariablesFiles, err := os.ReadDir(filepath.Join(srcWtfAccountPath, "SavedVariables"))
if err != nil {
log.Fatal(err)
}
for _, file := range accountSavedVariablesFiles {
if svFileRegex.MatchString(file.Name()) {
src := filepath.Join(srcWtfAccountPath, "SavedVariables", file.Name())
dst := filepath.Join(dstWtfAccountPath, "SavedVariables", file.Name())
_, err := copyFile(src, dst)
if err != nil {
log.Fatal(err)
}
pterm.Info.Printfln("Copied %s", src)
}
}
//
// character-level saved variables
//
charSavedVariablesFiles, err := os.ReadDir(filepath.Join(srcWtfCharacterPath, "SavedVariables"))
if err != nil {
log.Fatal(err)
}
for _, file := range charSavedVariablesFiles {
if svFileRegex.MatchString(file.Name()) {
src := filepath.Join(srcWtfCharacterPath, "SavedVariables", file.Name())
dst := filepath.Join(dstWtfCharacterPath, "SavedVariables", file.Name())
_, err := copyFile(src, dst)
if err != nil {
log.Fatal(err)
}
pterm.Info.Printfln("Copied %s", src)
}
}
//
// clean up
//
dstAccountCache := filepath.Join(dstWtfAccountPath, "cache.md5")
err = os.Remove(dstAccountCache)
if err != nil {
if !strings.Contains(err.Error(), "no such file or directory") {
log.Fatal(err)
}
}
pterm.Info.Printfln("Removed %s", dstAccountCache)
dstCharacterCache := filepath.Join(dstWtfCharacterPath, "cache.md5")
err = os.Remove(dstCharacterCache)
if err != nil {
if !strings.Contains(err.Error(), "no such file or directory") {
log.Fatal(err)
}
}
pterm.Info.Printfln("Removed %s", dstCharacterCache)
}