Skip to content
This repository was archived by the owner on Nov 20, 2022. It is now read-only.

Commit 09faa8a

Browse files
authored
Merge pull request #5 from project-vrcat/dev
Dev
2 parents fe2ef22 + 6ee5379 commit 09faa8a

File tree

8 files changed

+86
-72
lines changed

8 files changed

+86
-72
lines changed

bind.go

+4-19
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,13 @@ import (
1212
"os"
1313
"os/exec"
1414
"path/filepath"
15-
"runtime"
1615
"strings"
1716
"syscall"
1817
"time"
1918

2019
"github.com/TheTitanrain/w32"
2120
)
2221

23-
var windowTitle string
24-
25-
// BindSetWindowTitle 设置当前窗口标题, 用于获取HWND
26-
func BindSetWindowTitle(title string) {
27-
windowTitle = title
28-
}
29-
3022
// BindVRChatPath 获取VRChat配置目录
3123
func BindVRChatPath() (_path string, err error) {
3224
appdata := os.Getenv("AppData")
@@ -59,11 +51,12 @@ func BindWriteTextFile(filename, content string) error {
5951

6052
// BindSelectDirectory 弹出目录选择框
6153
func BindSelectDirectory(title string) (string, error) {
62-
_windowTitle, _ := syscall.UTF16PtrFromString(windowTitle)
54+
pid := ui.Getpid()
55+
owner := FindWindowByProcessId(pid)
6356
_title, _ := syscall.UTF16PtrFromString(title)
6457

6558
res := w32.SHBrowseForFolder(&w32.BROWSEINFO{
66-
Owner: w32.FindWindowW(nil, _windowTitle),
59+
Owner: owner,
6760
Flags: w32.BIF_RETURNONLYFSDIRS | w32.BIF_NEWDIALOGSTYLE,
6861
Title: _title,
6962
})
@@ -85,15 +78,7 @@ func BindAppVersion() string {
8578

8679
// BindOpen 通过系统默认浏览器打开指定url
8780
func BindOpen(url string) error {
88-
switch runtime.GOOS {
89-
case "windows":
90-
return exec.Command("cmd", "/c", "start", url).Start()
91-
case "darwin":
92-
return exec.Command("open", url).Start()
93-
case "linux":
94-
return exec.Command("xdg-open", url).Start()
95-
}
96-
return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
81+
return exec.Command("cmd", "/c", "start", url).Start()
9782
}
9883

9984
// BindCheckUpdate 检查是否存在新版本

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ require (
66
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d
77
github.com/zserge/lorca v0.1.10
88
)
9+
10+
replace github.com/zserge/lorca v0.1.10 => github.com/project-vrcat/lorca v0.1.11-0.20210603092944-66a39a5d8b01

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d h1:2xp1BQbqcDDaikHnASWpVZRjibOxu7y9LhAv04whugI=
22
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d/go.mod h1:peYoMncQljjNS6tZwI9WVyQB3qZS6u79/N3mBOcnd3I=
3-
github.com/zserge/lorca v0.1.10 h1:f/xBJ3D3ipcVRCcvN8XqZnpoKcOXV8I4vwqlFyw7ruc=
4-
github.com/zserge/lorca v0.1.10/go.mod h1:bVmnIbIRlOcoV285KIRSe4bUABKi7R7384Ycuum6e4A=
3+
github.com/project-vrcat/lorca v0.1.11-0.20210603092944-66a39a5d8b01 h1:TtRBMvGOPxncsARmEkanHyX5I/pKPHW6PHYs89YJlgs=
4+
github.com/project-vrcat/lorca v0.1.11-0.20210603092944-66a39a5d8b01/go.mod h1:TUOtEogaMwy0b+p9e4NxZC1jbPRUaNaEw5DnjQF//F8=
55
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
66
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0 h1:MsuvTghUPjX762sGLnGsxC3HM0B5r83wEtYcYR8/vRs=
77
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

main.go

+6-36
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,30 @@
22
package main
33

44
import (
5-
"embed"
65
"fmt"
7-
"io/fs"
8-
"net"
9-
"net/http"
10-
"net/url"
6+
"log"
117

128
"github.com/TheTitanrain/w32"
139
"github.com/zserge/lorca"
1410
)
1511

12+
var ui lorca.UI
13+
1614
func main() {
15+
log.SetFlags(log.Lshortfile)
1716
port := server()
1817
if lorca.ChromeExecutable() == "" {
1918
PromptDownload()
2019
return
2120
}
22-
loadingFile, _ := publicFiles.ReadFile("public/loading.html")
23-
ui, err := lorca.New("data:text/html,"+url.PathEscape(string(loadingFile)), "", 800, 500)
21+
var err error
22+
ui, err = lorca.New("", "", 800, 500)
2423
if err != nil {
2524
w32.MessageBox(w32.HWND(0), err.Error(), "Error", w32.MB_OK|w32.MB_ICONERROR)
2625
return
2726
}
2827
defer ui.Close()
2928

30-
_ = ui.Bind("setWindowTitle", BindSetWindowTitle)
3129
_ = ui.Bind("vrchatPath", BindVRChatPath)
3230
_ = ui.Bind("readTextFile", BindReadTextFile)
3331
_ = ui.Bind("writeTextFile", BindWriteTextFile)
@@ -41,31 +39,3 @@ func main() {
4139

4240
<-ui.Done()
4341
}
44-
45-
func server() int {
46-
port := pickPort()
47-
http.Handle("/", http.StripPrefix("/", http.FileServer(getFileSystem())))
48-
go func() {
49-
_ = http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), nil)
50-
}()
51-
return port
52-
}
53-
54-
func pickPort() int {
55-
listener, err := net.Listen("tcp4", "127.0.0.1:0")
56-
if err != nil {
57-
return -1
58-
}
59-
defer listener.Close()
60-
61-
addr := listener.Addr().(*net.TCPAddr)
62-
return addr.Port
63-
}
64-
65-
//go:embed public
66-
var publicFiles embed.FS
67-
68-
func getFileSystem() http.FileSystem {
69-
fSys, _ := fs.Sub(publicFiles, "public")
70-
return http.FS(fSys)
71-
}

public/loading.html

-12
This file was deleted.

public/main.js

-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ new Vue({
1919
})
2020
appVersion().then(version => {
2121
document.title += " " + version
22-
setWindowTitle(document.title)
23-
}).catch(_ => {
24-
setWindowTitle(document.title)
2522
})
2623
vrchatPath().then(dir => {
2724
this.vrchat_dir = dir

server.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
"io/fs"
7+
"net"
8+
"net/http"
9+
)
10+
11+
func server() int {
12+
port := pickPort()
13+
http.Handle("/", http.StripPrefix("/", http.FileServer(getFileSystem())))
14+
go func() {
15+
_ = http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), nil)
16+
}()
17+
return port
18+
}
19+
20+
func pickPort() int {
21+
listener, err := net.Listen("tcp4", "127.0.0.1:0")
22+
if err != nil {
23+
return -1
24+
}
25+
defer listener.Close()
26+
27+
addr := listener.Addr().(*net.TCPAddr)
28+
return addr.Port
29+
}
30+
31+
//go:embed public
32+
var publicFiles embed.FS
33+
34+
func getFileSystem() http.FileSystem {
35+
fSys, _ := fs.Sub(publicFiles, "public")
36+
return http.FS(fSys)
37+
}

utils.go

+35
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package main
22

33
import (
4+
"syscall"
5+
46
"github.com/TheTitanrain/w32"
57
)
68

9+
var (
10+
user32 = syscall.MustLoadDLL("user32.dll")
11+
procEnumWindows = user32.MustFindProc("EnumWindows")
12+
)
13+
714
// IsChineseSimplified 当前操作系统语言是否为简体中文
815
func IsChineseSimplified() bool {
916
switch w32.GetUserDefaultLCID() {
@@ -12,3 +19,31 @@ func IsChineseSimplified() bool {
1219
}
1320
return false
1421
}
22+
23+
// FindWindowByProcessId 通过PID寻找窗口
24+
func FindWindowByProcessId(pid int) w32.HWND {
25+
_hwnd := w32.HWND(0)
26+
cb := syscall.NewCallback(func(hwnd syscall.Handle, lParam uintptr) uintptr {
27+
_, _pid := w32.GetWindowThreadProcessId(w32.HWND(hwnd))
28+
if _pid == pid && w32.IsWindowVisible(w32.HWND(hwnd)) {
29+
_hwnd = w32.HWND(hwnd)
30+
return 0
31+
}
32+
return 1
33+
})
34+
_ = EnumWindows(cb, 0)
35+
return _hwnd
36+
}
37+
38+
// EnumWindows 枚举窗口
39+
func EnumWindows(enumFunc uintptr, lParam uintptr) (err error) {
40+
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, enumFunc, lParam, 0)
41+
if r1 == 0 {
42+
if e1 != 0 {
43+
err = error(e1)
44+
} else {
45+
err = syscall.EINVAL
46+
}
47+
}
48+
return
49+
}

0 commit comments

Comments
 (0)