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

Commit d397779

Browse files
committed
refactor: 代码重构
1 parent 55bc119 commit d397779

21 files changed

+377
-136
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.vscode/
33
*.code-workspace
44
dist/
5+
*.exe

.goreleaser.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ builds:
1111
goarch:
1212
- amd64
1313
ldflags:
14-
- -X main.Version={{.Tag}} -X main.BuildDate={{time "20060102"}}
14+
- -w -s
1515
hooks:
16+
pre:
17+
- go run build/build.go
1618
post:
1719
- upx "{{ .Path }}"
1820
archives:

app/app.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/project-vrcat/VRChatConfigurationEditor/pkg/utils"
7+
"github.com/project-vrcat/VRChatConfigurationEditor/pkg/win32"
8+
"github.com/zserge/lorca"
9+
)
10+
11+
var ui lorca.UI
12+
13+
func init() {
14+
utils.HideConsoleWindow()
15+
}
16+
17+
func bind() {
18+
_ = ui.Bind("vrchatPath", BindVRChatPath)
19+
_ = ui.Bind("readTextFile", BindReadTextFile)
20+
_ = ui.Bind("writeTextFile", BindWriteTextFile)
21+
_ = ui.Bind("selectDirectory", BindSelectDirectory)
22+
_ = ui.Bind("removeAll", BindRemoveAll)
23+
_ = ui.Bind("appVersion", BindAppVersion)
24+
_ = ui.Bind("open", BindOpen)
25+
_ = ui.Bind("checkUpdate", BindCheckUpdate)
26+
}
27+
28+
// Run 运行App
29+
func Run() {
30+
port := server()
31+
if lorca.ChromeExecutable() == "" {
32+
PromptDownload()
33+
return
34+
}
35+
var err error
36+
ui, err = lorca.New("", "", 800, 500)
37+
if err != nil {
38+
_ = win32.MessageBox(win32.HWND(0), err.Error(), "Error", win32.MB_OK|win32.MB_ICONERROR)
39+
return
40+
}
41+
defer ui.Close()
42+
43+
bind()
44+
_ = ui.Load(fmt.Sprintf("http://127.0.0.1:%d", port))
45+
<-ui.Done()
46+
}

bind.go app/bind.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package app
22

33
import (
44
"encoding/json"
@@ -13,7 +13,7 @@ import (
1313
"syscall"
1414
"time"
1515

16-
"github.com/TheTitanrain/w32"
16+
"github.com/project-vrcat/VRChatConfigurationEditor/pkg/win32"
1717
)
1818

1919
// BindVRChatPath 获取VRChat配置目录
@@ -22,10 +22,10 @@ func BindVRChatPath() (_path string, err error) {
2222
appdata := os.Getenv("LOCALAPPDATA")
2323
if appdata == "" {
2424
appdata = os.Getenv("APPDATA")
25-
if appdata == "" {
26-
err = errors.New("The AppData environment variable must be set for app to run correctly.")
27-
return
28-
}
25+
}
26+
if appdata == "" {
27+
err = errors.New("the AppData environment variable must be set for app to run correctly")
28+
return
2929
}
3030
localLow = filepath.Join(appdata, "../LocalLow")
3131
_path = filepath.Join(localLow, "VRChat/VRChat")
@@ -51,18 +51,18 @@ func BindWriteTextFile(filename, content string) error {
5151
// BindSelectDirectory 弹出目录选择框
5252
func BindSelectDirectory(title string) (string, error) {
5353
pid := ui.Getpid()
54-
owner := FindWindowByProcessId(pid)
54+
owner := win32.FindWindowByProcessId(pid)
5555
_title, _ := syscall.UTF16PtrFromString(title)
5656

57-
res := w32.SHBrowseForFolder(&w32.BROWSEINFO{
57+
res := win32.SHBrowseForFolder(&win32.BROWSEINFO{
5858
Owner: owner,
59-
Flags: w32.BIF_RETURNONLYFSDIRS | w32.BIF_NEWDIALOGSTYLE,
59+
Flags: win32.BIF_RETURNONLYFSDIRS | win32.BIF_NEWDIALOGSTYLE,
6060
Title: _title,
6161
})
6262
if res == 0 {
63-
return "", errors.New("Cancelled")
63+
return "", errors.New("cancelled")
6464
}
65-
return w32.SHGetPathFromIDList(res), nil
65+
return win32.SHGetPathFromIDList(res), nil
6666
}
6767

6868
// BindRemoveAll 清空指定目录
@@ -72,7 +72,7 @@ func BindRemoveAll(path string) error {
7272

7373
// BindAppVersion 获取应用版本号及编译日期
7474
func BindAppVersion() string {
75-
return fmt.Sprintf("%s build-%s", Version, BuildDate)
75+
return fmt.Sprintf("%s build-%s", Version, GitHash)
7676
}
7777

7878
// BindOpen 通过系统默认浏览器打开指定url

chrome.go app/chrome.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package main
1+
package app
22

33
import (
44
"os/exec"
55

6-
"github.com/TheTitanrain/w32"
6+
"github.com/project-vrcat/VRChatConfigurationEditor/pkg/utils"
7+
"github.com/project-vrcat/VRChatConfigurationEditor/pkg/win32"
78
)
89

910
// PromptDownload 弹出询问是否下载Chrome对话框
@@ -13,10 +14,10 @@ func PromptDownload() {
1314
downloadUrl := "https://www.google.com/chrome/"
1415
downloadUrlChina := "https://www.google.cn/chrome/"
1516

16-
r := w32.MessageBox(w32.HWND(0), message, title, w32.MB_YESNO|w32.MB_ICONQUESTION)
17-
if r == w32.IDYES {
17+
r := win32.MessageBox(0, message, title, win32.MB_YESNO|win32.MB_ICONQUESTION)
18+
if r == win32.IDYES {
1819
// 如果系统语言为简体中文, 使用中国专用Chrome下载链接
19-
if IsChineseSimplified() {
20+
if utils.IsChineseSimplified() {
2021
downloadUrl = downloadUrlChina
2122
}
2223
_ = exec.Command("cmd", "/c", "start", downloadUrl).Start()

server.go app/server.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
package main
1+
package app
22

33
import (
44
"embed"
55
"fmt"
66
"io/fs"
7+
"mime"
78
"net"
89
"net/http"
910
"os"
1011
)
1112

13+
// PublicFiles 嵌入资源
14+
var PublicFiles embed.FS
15+
16+
func init() {
17+
// Good job Microsoft :)
18+
// https://github.com/golang/go/issues/32350
19+
_ = mime.AddExtensionType(".js", "application/javascript; charset=utf-8")
20+
}
21+
1222
func server() int {
1323
port := pickPort()
1424
http.Handle("/", http.StripPrefix("/", http.FileServer(getFileSystem())))
@@ -29,13 +39,10 @@ func pickPort() int {
2939
return addr.Port
3040
}
3141

32-
//go:embed public
33-
var publicFiles embed.FS
34-
3542
func getFileSystem() http.FileSystem {
3643
if len(os.Args) > 1 && (os.Args[1] == "live" || os.Args[1] == "--live") {
3744
return http.Dir("./public")
3845
}
39-
fSys, _ := fs.Sub(publicFiles, "public")
46+
fSys, _ := fs.Sub(PublicFiles, "public")
4047
return http.FS(fSys)
4148
}

app/version.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app
2+
3+
var (
4+
// Version 应用版本号
5+
Version = "v1.0.9"
6+
// GitHash Git Commit Hash
7+
GitHash = "55bc119"
8+
)

build/build.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//+build script
2+
package main
3+
4+
import (
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"os"
9+
"os/exec"
10+
"path/filepath"
11+
"strings"
12+
)
13+
14+
func init() {
15+
log.SetFlags(log.Lshortfile)
16+
}
17+
18+
func main() {
19+
pwd, _ := os.Getwd()
20+
filename := filepath.Join(pwd, "app/version.go")
21+
code := fmt.Sprintf(`package app
22+
23+
var (
24+
// Version 应用版本号
25+
Version = "%s"
26+
// GitHash Git Commit Hash
27+
GitHash = "%s"
28+
)
29+
`, getGitTag(), getGitHash())
30+
_ = ioutil.WriteFile(filename, []byte(code), 0666)
31+
}
32+
33+
func getGitTag() string {
34+
cmd := exec.Command("git", "describe", "--abbrev=0", "--tags")
35+
out, err := cmd.CombinedOutput()
36+
if err != nil {
37+
log.Println(err)
38+
return "v1.0.0"
39+
}
40+
return strings.Replace(string(out), "\n", "", -1)
41+
}
42+
43+
func getGitHash() string {
44+
cmd := exec.Command("git", "rev-parse", "--short", "HEAD")
45+
out, err := cmd.CombinedOutput()
46+
if err != nil {
47+
log.Println(err)
48+
return ""
49+
}
50+
return strings.Replace(string(out), "\n", "", -1)
51+
}

config.go

-8
This file was deleted.

go.mod

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ module github.com/project-vrcat/VRChatConfigurationEditor
22

33
go 1.16
44

5-
require (
6-
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d
7-
github.com/zserge/lorca v0.1.10
8-
)
5+
require github.com/zserge/lorca v0.1.10
96

107
replace github.com/zserge/lorca v0.1.10 => github.com/project-vrcat/lorca v0.1.11-0.20210603092944-66a39a5d8b01

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d h1:2xp1BQbqcDDaikHnASWpVZRjibOxu7y9LhAv04whugI=
2-
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d/go.mod h1:peYoMncQljjNS6tZwI9WVyQB3qZS6u79/N3mBOcnd3I=
31
github.com/project-vrcat/lorca v0.1.11-0.20210603092944-66a39a5d8b01 h1:TtRBMvGOPxncsARmEkanHyX5I/pKPHW6PHYs89YJlgs=
42
github.com/project-vrcat/lorca v0.1.11-0.20210603092944-66a39a5d8b01/go.mod h1:TUOtEogaMwy0b+p9e4NxZC1jbPRUaNaEw5DnjQF//F8=
53
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

main.go

+7-37
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,20 @@
22
package main
33

44
import (
5-
"fmt"
5+
"embed"
66
"log"
7-
"mime"
87

9-
"github.com/TheTitanrain/w32"
10-
"github.com/zserge/lorca"
8+
"github.com/project-vrcat/VRChatConfigurationEditor/app"
119
)
1210

13-
var ui lorca.UI
11+
//go:embed public
12+
var publicFiles embed.FS
1413

1514
func init() {
16-
log.SetFlags(log.Lshortfile)
17-
18-
// Good job Microsoft :)
19-
// https://github.com/golang/go/issues/32350
20-
_ = mime.AddExtensionType(".js", "application/javascript; charset=utf-8")
21-
22-
HideConsoleWindow()
15+
log.SetFlags(log.Lshortfile | log.LstdFlags)
2316
}
2417

2518
func main() {
26-
port := server()
27-
if lorca.ChromeExecutable() == "" {
28-
PromptDownload()
29-
return
30-
}
31-
var err error
32-
ui, err = lorca.New("", "", 800, 500)
33-
if err != nil {
34-
w32.MessageBox(w32.HWND(0), err.Error(), "Error", w32.MB_OK|w32.MB_ICONERROR)
35-
return
36-
}
37-
defer ui.Close()
38-
39-
_ = ui.Bind("vrchatPath", BindVRChatPath)
40-
_ = ui.Bind("readTextFile", BindReadTextFile)
41-
_ = ui.Bind("writeTextFile", BindWriteTextFile)
42-
_ = ui.Bind("selectDirectory", BindSelectDirectory)
43-
_ = ui.Bind("removeAll", BindRemoveAll)
44-
_ = ui.Bind("appVersion", BindAppVersion)
45-
_ = ui.Bind("open", BindOpen)
46-
_ = ui.Bind("checkUpdate", BindCheckUpdate)
47-
48-
_ = ui.Load(fmt.Sprintf("http://127.0.0.1:%d", port))
49-
50-
<-ui.Done()
19+
app.PublicFiles = publicFiles
20+
app.Run()
5121
}

pkg/utils/utils.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package utils
2+
3+
import (
4+
"os"
5+
6+
"github.com/project-vrcat/VRChatConfigurationEditor/pkg/win32"
7+
)
8+
9+
// IsChineseSimplified 当前操作系统语言是否为简体中文
10+
func IsChineseSimplified() bool {
11+
switch win32.GetUserDefaultLCID() {
12+
case 0x0804:
13+
return true
14+
}
15+
return false
16+
}
17+
18+
// HideConsoleWindow 隐藏控制台窗口
19+
func HideConsoleWindow() {
20+
hwnd := win32.GetConsoleWindow()
21+
if hwnd <= 0 {
22+
return
23+
}
24+
_, pid := win32.GetWindowThreadProcessId(hwnd)
25+
if pid == os.Getpid() {
26+
win32.ShowWindow(hwnd, 0)
27+
}
28+
}

pkg/win32/kernel32.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package win32
2+
3+
import (
4+
"syscall"
5+
)
6+
7+
var (
8+
kernel32 = syscall.NewLazyDLL("kernel32.dll")
9+
procGetUserDefaultLCID = kernel32.NewProc("GetUserDefaultLCID")
10+
procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow")
11+
)
12+
13+
func GetUserDefaultLCID() uint32 {
14+
ret, _, _ := procGetUserDefaultLCID.Call()
15+
return uint32(ret)
16+
}
17+
18+
func GetConsoleWindow() HWND {
19+
ret, _, _ := procGetConsoleWindow.Call()
20+
return HWND(ret)
21+
}

0 commit comments

Comments
 (0)