-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
111 lines (93 loc) · 2.53 KB
/
utils.go
File metadata and controls
111 lines (93 loc) · 2.53 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
func clearScreen() {
if runtime.GOOS == "windows" {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
} else {
fmt.Print("\033[H\033[2J")
}
}
func getPossibleGameDirs() []string {
var dirs []string
for _, drive := range "CDEFGHIJKLMNOPQRSTUVWXYZ" {
drivePath := string(drive) + ":\\"
if _, err := os.Stat(drivePath); err == nil {
dirs = append(dirs,
drivePath+"Program Files (x86)",
drivePath+"Program Files",
drivePath+"Games",
drivePath+"ACE",
drivePath+"Delta Force",
)
}
}
if home, err := os.UserHomeDir(); err == nil {
dirs = append(dirs,
filepath.Join(home, "Documents", "Games"),
filepath.Join(home, "Documents", "My Games"),
filepath.Join(home, "Games"),
)
}
return dirs
}
func autoDetectGamePath() string {
targetFile := "delta_force_launcher.exe"
searchDirs := getPossibleGameDirs()
fmt.Println(colorize("正在搜索游戏路径...", FgYellow))
for _, dir := range searchDirs {
fmt.Printf("\r%s", colorize("搜索目录: "+dir, FgCyan))
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return filepath.SkipDir
}
if info.IsDir() && (strings.Contains(strings.ToLower(info.Name()), "windows") ||
strings.Contains(strings.ToLower(info.Name()), "system") ||
strings.Contains(strings.ToLower(info.Name()), "$recycle.bin")) {
return filepath.SkipDir
}
if strings.EqualFold(info.Name(), targetFile) {
fmt.Printf("\n%s\n", colorize("找到游戏启动器: "+path, FgGreen))
if confirmAction("是否使用此路径?") {
return fmt.Errorf("FOUND:%s", path)
}
}
return nil
})
if err != nil && strings.HasPrefix(err.Error(), "FOUND:") {
return strings.TrimPrefix(err.Error(), "FOUND:")
}
}
fmt.Printf("\n%s\n", colorize("未找到游戏启动器", FgYellow))
return ""
}
func openFileDialog(title string) string {
var args = []string{
"powershell",
"-Command",
`Add-Type -AssemblyName System.Windows.Forms;
$f=New-Object System.Windows.Forms.OpenFileDialog;
$f.Filter='游戏启动器 (*.exe)|*.exe';
$f.Title='` + title + `';
$f.ShowDialog();
$f.FileName`,
}
cmd := exec.Command(args[0], args[1:]...)
output, err := cmd.Output()
if err != nil {
return ""
}
path := strings.TrimSpace(string(output))
if path == "" {
return ""
}
return path
}