Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Feb 15, 2022
1 parent f076739 commit dd36efb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 54 deletions.
94 changes: 44 additions & 50 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ func readCfwPort(path string) string {
return port
}

func parseProcessInfo(ci *cfwInfo, item *process.Process) {
info, _ := item.Cmdline()
if runtime.GOOS == "darwin" {
ci.installType = MacDmg
ci.rootPath = strings.TrimRight(info, "/Contents/MacOS/Clash for Windows")
info = fmt.Sprintf("%s/Contents/Info.plist", ci.rootPath)
} else {
ci.rootPath = strings.Trim(path.Dir(strings.Replace(info, "\\", "/", -1)), "\"")
info = strings.Replace(info, "\"", "", -1)
if !IsExists(info) {
exit("无法获取cfw信息, 请以管理员身份运行此程序")
}
}
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))
} else {
f.Close()
os.Remove(path.Join(ci.rootPath, "test"))
}
ci.installType = WinExe
}
}

func checkCfw() *cfwInfo {
ci := &cfwInfo{}
processList, _ := process.Processes()
Expand All @@ -58,33 +88,7 @@ func checkCfw() *cfwInfo {
if strings.Contains(name, "Clash for Windows") {
child, _ := item.Children()
if len(child) > 1 {
info, _ := item.Cmdline()
if runtime.GOOS == "darwin" {
ci.installType = MacDmg
ci.rootPath = strings.TrimRight(info, "/Contents/MacOS/Clash for Windows")
info = fmt.Sprintf("%s/Contents/Info.plist", ci.rootPath)
} else {
ci.rootPath = strings.Trim(path.Dir(strings.Replace(info, "\\", "/", -1)), "\"")
info = strings.Replace(info, "\"", "", -1)
if !IsExists(info) {
exit("无法获取cfw信息, 请以管理员身份运行此程序")
}
}
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))
} else {
f.Close()
os.Remove(path.Join(ci.rootPath, "test"))
}
ci.installType = WinExe
}
parseProcessInfo(ci, item)
break
}
}
Expand All @@ -110,36 +114,26 @@ func checkCfw() *cfwInfo {
}

func transDownloadUrl() string {
var url string
var url, dTag string
if transWay == "" {
return ""
} else if cfwVersion == cfwVersionList[0] {
fmt.Println(fmt.Sprintf("正在获取%s汉化包最新版本号...", transWay))
searchText := webSearch(fmt.Sprintf("https://github.com/%s/tags", transWay), cfwVersion)
if searchText == "" {
fmt.Println(fmt.Sprintf("%s的汉化补丁尚未发布, 若要汉化等后续补丁发布后重新运行工具来更新即可\n", cfwVersion))
return ""
}
} else {
fmt.Println(fmt.Sprintf("正在获取%s的%s版本汉化包...", transWay, cfwVersion))
}
var dTag string
if transWay == "BoyceLig/Clash_Chinese_Patch" {
} else if transWay == "BoyceLig/Clash_Chinese_Patch" {
dTag = cfwVersion
} else if transWay == "ender-zhao/Clash-for-Windows_Chinese" {
dTag = fmt.Sprintf("CFW-V%s_CN", cfwVersion)
}
fileName := "app.7z"
searchText := webSearch(fmt.Sprintf("https://github.com/%s/releases/tag/%s", transWay, dTag), fileName)
if searchText == "" {
fileName = "app.asar"
searchText = webSearch(fmt.Sprintf("https://github.com/%s/releases/tag/%s", transWay, dTag), fileName)
if searchText == "" {
fmt.Println(fmt.Sprintf("%s的翻译包不存在\n", transWay))
return ""
}
fmt.Println(fmt.Sprintf("正在获取%s的%s版本汉化包...", transWay, cfwVersion))
if cfwVersion == cfwVersionList[0] && webSearch(fmt.Sprintf("https://github.com/%s/tags", transWay), cfwVersion) == "" {
fmt.Println(fmt.Sprintf("%s的汉化补丁尚未发布, 若要汉化等后续补丁发布后重新运行工具来更新即可\n", cfwVersion))
return ""
}
keyName := webFirstMatchKey(fmt.Sprintf("https://github.com/%s/releases/tag/%s", transWay, dTag),
path.Join(dTag, "app.7z"), path.Join(dTag, "app.asar"))
if keyName == "" {
fmt.Println(fmt.Sprintf("%s的翻译包不存在\n", transWay))
return ""
}
url = fmt.Sprintf("https://github.com/%s/releases/download/%s/%s", transWay, dTag, fileName)
url = fmt.Sprintf("https://github.com/%s/releases/download/%s", transWay, keyName)
updateTrans = true
return url
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func getCfw() *downloadInfo {
downloadFile(cfwUrl, "")
di := newDI(cfwUrl)
if ci.installType == Win7z {
extract7z(di.fileFullName)
extractFile(di.fileFullName)
}
fmt.Println()
return di
Expand All @@ -62,7 +62,7 @@ func getTrans(url string) *downloadInfo {
di := newDI(url)
downloadFile(url, "")
if strings.Contains(di.fileFullName, "7z") {
extract7z(di.fileFullName)
extractFile(di.fileFullName)
}
fmt.Println()
return di
Expand Down
27 changes: 25 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/eiannone/keyboard"
"github.com/gen2brain/go-unarr"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -137,7 +138,7 @@ func searchText(r io.Reader, key string) string {
return findStr.String()
}

func webSearch(url, key string) string {
func httpGet(url string) *http.Response {
var (
count int
err error
Expand All @@ -155,10 +156,32 @@ func webSearch(url, key string) string {
count++
}
}
return resp
}

func webSearch(url, key string) string {
resp := httpGet(url)
defer resp.Body.Close()
return searchText(resp.Body, key)
}

func webFirstMatchKey(url string, keys ...string) string {
var content string
resp := httpGet(url)
defer resp.Body.Close()
if c, err := ioutil.ReadAll(resp.Body); err == nil {
content = string(c)
} else {
return ""
}
for _, v := range keys {
if strings.Contains(content, v) {
return v
}
}
return ""
}

func recentlyTag(url string) []string {
var tagStr string
searchText := webSearch(url, "archive/refs")
Expand All @@ -177,7 +200,7 @@ func recentlyTag(url string) []string {
return strings.Split(strings.TrimPrefix(tagStr, " "), " ")
}

func extract7z(name string) {
func extractFile(name string) {
stopCh := make(chan struct{})
closeChan := func() {
close(stopCh)
Expand Down

0 comments on commit dd36efb

Please sign in to comment.