Skip to content

Commit

Permalink
增加跨平台条件编译
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Jan 25, 2022
1 parent 864ff10 commit 7e00fb3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 34 deletions.
7 changes: 6 additions & 1 deletion data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"cfw-updater/platform"
"fmt"
"github.com/shirou/gopsutil/v3/process"
"os"
Expand Down Expand Up @@ -53,8 +54,12 @@ func checkCfw() *cfwInfo {
if !IsExists(info) {
exit("无法获取cfw信息, 请以管理员身份运行此程序")
}
ci.version = getExeVersion(info)
ci.process = item
if v, err := platform.FileVersion(info); err == nil {
ci.version = v
} else {
exit(err.Error())
}
if IsExists(fmt.Sprintf("%s/Uninstall Clash for Windows.exe", ci.rootPath)) {
if f, err := os.Create(path.Join(ci.rootPath, "test")); err != nil {
exit(fmt.Sprintf("%s目录无权限写入, 请以管理员身份运行此程序", ci.rootPath))
Expand Down
17 changes: 17 additions & 0 deletions platform/darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build darwin

package platform

import "syscall"

// NewSysProcAttr 进程属性
func NewSysProcAttr() *syscall.SysProcAttr {
return &syscall.SysProcAttr{
Setsid: true,
}
}

// FileVersion 获取文件版本号
func FileVersion(filePath string) (string, error) {
return "", nil
}
43 changes: 43 additions & 0 deletions platform/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build windows

package platform

import (
"errors"
"fmt"
"github.com/gonutz/w32/v2"
"syscall"
)

// NewSysProcAttr 进程属性
func NewSysProcAttr() *syscall.SysProcAttr {
return &syscall.SysProcAttr{
HideWindow: true,
}
}

// FileVersion 获取文件版本号
func FileVersion(filePath string) (string, error) {
size := w32.GetFileVersionInfoSize(filePath)
if size <= 0 {
return "", errors.New("获取cfw版本号失败: GetFileVersionInfoSize failed")
}

info := make([]byte, size)
ok := w32.GetFileVersionInfo(filePath, info)
if !ok {
return "", errors.New("获取cfw版本号失败: GetFileVersionInfo failed")
}

fixed, ok := w32.VerQueryValueRoot(info)
if !ok {
return "", errors.New("获取cfw版本号失败: VerQueryValueRoot failed")
}
version := fixed.FileVersion()
return fmt.Sprintf(
"%d.%d.%d",
version&0xFFFF000000000000>>48,
version&0x0000FFFF00000000>>32,
version&0x00000000FFFF0000>>16,
), nil
}
38 changes: 5 additions & 33 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package main

import (
"bufio"
"cfw-updater/platform"
"fmt"
"github.com/cheggaaa/pb/v3"
"github.com/eiannone/keyboard"
"github.com/gen2brain/go-unarr"
"github.com/gonutz/w32/v2"
"io"
"net/http"
"os"
Expand All @@ -15,7 +15,6 @@ import (
"regexp"
"strconv"
"strings"
"syscall"
"time"
)

Expand Down Expand Up @@ -199,31 +198,6 @@ func extract7z(name string) {
closeChan()
}

func getExeVersion(exePath string) string {
size := w32.GetFileVersionInfoSize(exePath)
if size <= 0 {
exit("获取cfw版本号失败: GetFileVersionInfoSize failed")
}

info := make([]byte, size)
ok := w32.GetFileVersionInfo(exePath, info)
if !ok {
exit("获取cfw版本号失败: GetFileVersionInfo failed")
}

fixed, ok := w32.VerQueryValueRoot(info)
if !ok {
exit("获取cfw版本号失败: VerQueryValueRoot failed")
}
version := fixed.FileVersion()
return fmt.Sprintf(
"%d.%d.%d",
version&0xFFFF000000000000>>48,
version&0x0000FFFF00000000>>32,
version&0x00000000FFFF0000>>16,
)
}

func getChar(str string) string {
err := keyboard.Open()
if err != nil {
Expand Down Expand Up @@ -331,12 +305,10 @@ func startBackground() {
}
//启动子进程
cmd := &exec.Cmd{
Path: os.Args[0],
Args: os.Args,
Env: append(os.Environ(), fmt.Sprintf("%s=%d", "CFW_DAEMON_IDX", runIdx)),
SysProcAttr: &syscall.SysProcAttr{
HideWindow: true,
},
Path: os.Args[0],
Args: os.Args,
Env: append(os.Environ(), fmt.Sprintf("%s=%d", "CFW_DAEMON_IDX", runIdx)),
SysProcAttr: platform.NewSysProcAttr(),
}
if err := cmd.Start(); err != nil {
exit(err.Error())
Expand Down

0 comments on commit 7e00fb3

Please sign in to comment.