Skip to content

Commit f815ab5

Browse files
committed
refactor: remove dead code
1 parent 32fcd80 commit f815ab5

File tree

24 files changed

+320
-358
lines changed

24 files changed

+320
-358
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/Masterminds/semver/v3 v3.4.0
77
github.com/Microsoft/go-winio v0.6.2
88
github.com/axrona/go-discordrpc v1.1.0-1
9-
github.com/corpix/uarand v0.2.0
9+
github.com/go-ole/go-ole v1.3.0
1010
github.com/goccy/go-json v0.10.5
1111
github.com/google/go-github/v30 v30.1.0
1212
github.com/joho/godotenv v1.5.1
@@ -29,7 +29,6 @@ require (
2929
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
3030
github.com/go-git/go-billy/v5 v5.7.0 // indirect
3131
github.com/go-git/go-git/v5 v5.16.4 // indirect
32-
github.com/go-ole/go-ole v1.3.0 // indirect
3332
github.com/godbus/dbus/v5 v5.2.2 // indirect
3433
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
3534
github.com/google/go-querystring v1.0.0 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg
2121
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
2222
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
2323
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
24-
github.com/corpix/uarand v0.2.0 h1:U98xXwud/AVuCpkpgfPF7J5TQgr7R5tqT8VZP5KWbzE=
25-
github.com/corpix/uarand v0.2.0/go.mod h1:/3Z1QIqWkDIhf6XWn/08/uMHoQ8JUoTIKc2iPchBOmM=
2624
github.com/cyphar/filepath-securejoin v0.6.1 h1:5CeZ1jPXEiYt3+Z6zqprSAgSWiggmpVyciv8syjIpVE=
2725
github.com/cyphar/filepath-securejoin v0.6.1/go.mod h1:A8hd4EnAeyujCJRrICiOWqjS1AX0a9kM5XL+NwKoYSc=
2826
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

internal/downloader/downloader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"sync"
1414
"time"
1515

16-
"github.com/corpix/uarand"
16+
"github.com/liteldev/LeviLauncher/internal/httpx"
1717
"github.com/wailsapp/wails/v3/pkg/application"
1818
)
1919

@@ -211,7 +211,7 @@ func (m *Manager) run(s *state) {
211211
m.finishRunning(local)
212212
return
213213
}
214-
req.Header.Set("User-Agent", uarand.GetRandom())
214+
httpx.ApplyDefaultHeaders(req)
215215
if m.opts.Resume && cur > 0 {
216216
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", cur))
217217
}

internal/explorer/explorer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func OpenPath(dir string) bool {
2222
return false
2323
}
2424
}
25-
cmd := exec.Command("powershell", "explorer \""+d+"\"")
25+
cmd := exec.Command("explorer.exe", d)
2626
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
2727
if err := cmd.Run(); err != nil {
2828
log.Println("explorer.OpenPath error:", err)

internal/gdk/wdapp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func UnregisterIfExists(isPreview bool) string {
4444
if info, err := registry.GetAppxInfo(pkg); err == nil && info != nil {
4545
pf := strings.TrimSpace(info.PackageFullName)
4646
if pf != "" {
47-
cmd := exec.Command("powershell", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", "Remove-AppxPackage -Package '"+pf+"' -PreserveRoamableApplicationData")
47+
cmd := exec.Command("powershell", "-NoProfile", "-NonInteractive", "-Command", "Remove-AppxPackage -Package '"+pf+"' -PreserveRoamableApplicationData")
4848
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
4949
if er := cmd.Run(); er != nil {
5050
return "ERR_UNREGISTER_FAILED"
@@ -77,7 +77,7 @@ func UnregisterVersionFolder(folder string) string {
7777
if pf == "" {
7878
return "ERR_NOT_REGISTERED_THIS_VERSION"
7979
}
80-
cmd := exec.Command("powershell", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", "Remove-AppxPackage -Package '"+pf+"' -PreserveRoamableApplicationData")
80+
cmd := exec.Command("powershell", "-NoProfile", "-NonInteractive", "-Command", "Remove-AppxPackage -Package '"+pf+"' -PreserveRoamableApplicationData")
8181
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
8282
if er := cmd.Run(); er != nil {
8383
return "ERR_UNREGISTER_FAILED"

internal/httpx/useragent.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package httpx
2+
3+
import (
4+
"net/http"
5+
"runtime"
6+
)
7+
8+
const appUserAgent = "LeviLauncher (" + runtime.GOOS + "; " + runtime.GOARCH + ")"
9+
10+
func UserAgent() string {
11+
return appUserAgent
12+
}
13+
14+
func ApplyDefaultHeaders(req *http.Request) {
15+
if req == nil {
16+
return
17+
}
18+
if req.Header.Get("User-Agent") == "" {
19+
req.Header.Set("User-Agent", appUserAgent)
20+
}
21+
}

internal/launch/launch.go

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package launch
22

33
import (
44
"context"
5-
"encoding/csv"
6-
"os/exec"
75
"path/filepath"
86
"strings"
97
"syscall"
@@ -14,6 +12,7 @@ import (
1412

1513
"github.com/liteldev/LeviLauncher/internal/discord"
1614
"github.com/liteldev/LeviLauncher/internal/registry"
15+
"golang.org/x/sys/windows"
1716
)
1817

1918
const (
@@ -52,39 +51,51 @@ func EnsureGamingServicesInstalled(ctx context.Context) bool {
5251
return true
5352
}
5453

54+
func normalizeProcessPath(p string) string {
55+
s := strings.ToLower(filepath.Clean(strings.TrimSpace(p)))
56+
s = strings.TrimPrefix(s, `\\?\`)
57+
s = strings.TrimPrefix(s, `\??\`)
58+
return s
59+
}
60+
5561
func isGameRunning(versionDir string) bool {
5662
if versionDir == "" {
5763
return false
5864
}
59-
cmd := exec.Command("powershell", "-NoProfile", "-Command", "Get-CimInstance Win32_Process -Filter \"Name='Minecraft.Windows.exe'\" | Select-Object ExecutablePath | ConvertTo-Csv -NoTypeInformation")
60-
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
61-
out, err := cmd.Output()
65+
66+
snapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
6267
if err != nil {
6368
return false
6469
}
65-
reader := csv.NewReader(strings.NewReader(string(out)))
66-
records, err := reader.ReadAll()
67-
if err != nil || len(records) < 2 {
68-
return false
69-
}
70-
idx := -1
71-
for i, col := range records[0] {
72-
if strings.EqualFold(col, "ExecutablePath") {
73-
idx = i
74-
break
75-
}
76-
}
77-
if idx == -1 {
70+
defer windows.CloseHandle(snapshot)
71+
72+
cleanVerDir := normalizeProcessPath(versionDir)
73+
74+
var entry windows.ProcessEntry32
75+
entry.Size = uint32(unsafe.Sizeof(entry))
76+
if err := windows.Process32First(snapshot, &entry); err != nil {
7877
return false
7978
}
80-
cleanVerDir := strings.ToLower(filepath.Clean(versionDir))
81-
for _, row := range records[1:] {
82-
if len(row) <= idx {
83-
continue
79+
80+
for {
81+
if strings.EqualFold(windows.UTF16ToString(entry.ExeFile[:]), "Minecraft.Windows.exe") {
82+
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, entry.ProcessID)
83+
if err == nil {
84+
buf := make([]uint16, 1024)
85+
size := uint32(len(buf))
86+
if err := windows.QueryFullProcessImageName(h, 0, &buf[0], &size); err == nil && size > 0 {
87+
p := normalizeProcessPath(windows.UTF16ToString(buf[:size]))
88+
_ = windows.CloseHandle(h)
89+
if strings.HasPrefix(p, cleanVerDir) {
90+
return true
91+
}
92+
} else {
93+
_ = windows.CloseHandle(h)
94+
}
95+
}
8496
}
85-
path := strings.ToLower(filepath.Clean(row[idx]))
86-
if strings.HasPrefix(path, cleanVerDir) {
87-
return true
97+
if err := windows.Process32Next(snapshot, &entry); err != nil {
98+
break
8899
}
89100
}
90101
return false
@@ -192,7 +203,7 @@ func MonitorGameProcess(ctx context.Context, versionDir string) {
192203
// 似乎会出现Bug,后续修复
193204
//w := application.Get().Window.Current()
194205
//if w != nil {
195-
//w.Restore()
206+
//w.Restore()
196207
//}
197208
return
198209
}
264 KB
Binary file not shown.

internal/lip/install.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"syscall"
1414
"time"
1515

16-
"github.com/corpix/uarand"
1716
json "github.com/goccy/go-json"
1817
"github.com/liteldev/LeviLauncher/internal/config"
18+
"github.com/liteldev/LeviLauncher/internal/httpx"
1919
"github.com/liteldev/LeviLauncher/internal/utils"
2020
"github.com/wailsapp/wails/v3/pkg/application"
2121
)
@@ -75,7 +75,7 @@ func GetLatestVersion() (string, error) {
7575
if err != nil {
7676
continue
7777
}
78-
req.Header.Set("User-Agent", uarand.GetRandom())
78+
httpx.ApplyDefaultHeaders(req)
7979
resp, err := http.DefaultClient.Do(req)
8080
if err != nil {
8181
continue
@@ -156,7 +156,7 @@ func downloadFile(url string, dest string) error {
156156
if err != nil {
157157
return err
158158
}
159-
req.Header.Set("User-Agent", uarand.GetRandom())
159+
httpx.ApplyDefaultHeaders(req)
160160
resp, err := http.DefaultClient.Do(req)
161161
if err != nil {
162162
return err
@@ -198,7 +198,7 @@ func (pw *progressWriter) Write(p []byte) (int, error) {
198198
n := len(p)
199199
pw.current += float64(n)
200200
now := time.Now().UnixMilli()
201-
if now-pw.lastUpdate > 100 {
201+
if now-pw.lastUpdate > 100 {
202202
if pw.total > 0 {
203203
pw.onProgress((pw.current/pw.total)*100, pw.current, pw.total)
204204
}

internal/mcservice/levilamina.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"syscall"
1212
"time"
1313

14-
"github.com/corpix/uarand"
1514
json "github.com/goccy/go-json"
1615
"github.com/liteldev/LeviLauncher/internal/apppath"
16+
"github.com/liteldev/LeviLauncher/internal/httpx"
1717
"github.com/liteldev/LeviLauncher/internal/lip"
1818
"github.com/liteldev/LeviLauncher/internal/utils"
1919
)
@@ -39,7 +39,7 @@ func FetchLeviLaminaVersionDB() (map[string][]string, error) {
3939
lastErr = err
4040
continue
4141
}
42-
req.Header.Set("User-Agent", uarand.GetRandom())
42+
httpx.ApplyDefaultHeaders(req)
4343

4444
resp, err := client.Do(req)
4545
if err != nil {

0 commit comments

Comments
 (0)