From 1f02e78ea0ee70e83e1469145d1d2bafc62a2668 Mon Sep 17 00:00:00 2001 From: cdle Date: Wed, 7 Jun 2023 19:15:27 +0800 Subject: [PATCH] x --- core/function.go | 14 +++++++------- core/init.go | 20 ++++++++++++-------- main.go | 8 ++++++++ utils/init.go | 38 +++++++++++++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 16 deletions(-) diff --git a/core/function.go b/core/function.go index 3f161d4..33db44e 100644 --- a/core/function.go +++ b/core/function.go @@ -233,13 +233,13 @@ func initToHandleMessage() { } s.Reply("ok") case "reply": - if data := noReplyGroups.GetBytes(cid); len(data) != 0 { - info := &GroupInfo{} - if info.Enable { - info.Enable = !info.Enable - noReplyGroups.Set(cid, utils.JsonMarshal(info)) - } - } + // if data := noReplyGroups.GetBytes(cid); len(data) != 0 { + info := &GroupInfo{} + // if info.Enable { + // info.Enable = !info.Enable + noReplyGroups.Set(cid, utils.JsonMarshal(info)) + // } + // } s.Reply("ok") case "noreply", "unreply": if data := noReplyGroups.GetBytes(cid); len(data) == 0 { diff --git a/core/init.go b/core/init.go index 0334ac0..d25013d 100644 --- a/core/init.go +++ b/core/init.go @@ -5,7 +5,7 @@ import ( "os" "regexp" "runtime" - "syscall" + "strings" "time" "github.com/beego/beego/v2/adapter/httplib" @@ -65,8 +65,8 @@ func Init() { } console.Debug("正在创建编译文件...") filename := utils.ExecPath + "/" + utils.ProcessName - ready := filename + ".ready" - if f, err := os.OpenFile(ready, syscall.O_CREAT, 0777); err != nil { + ready := strings.Replace(filename, ".exe", ".ready.exe", -1) + if f, err := os.OpenFile(ready, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777); err != nil { console.Error("创建编译文件错误:%v", err) return &storage.Final{ Error: fmt.Errorf("创建编译文件错误:%v", err), @@ -84,11 +84,15 @@ func Init() { } } } - console.Debug("正在删除旧程序错误...") - if err = os.RemoveAll(filename); err != nil { - console.Error("删除旧程序错误:%v", err) - return &storage.Final{ - Error: fmt.Errorf("删除旧程序错误:%v", err), + if runtime.GOOS == "window" { + utils.Daemon("ready") + } else { + console.Debug("正在删除旧程序错误...") + if err = os.RemoveAll(filename); err != nil { + console.Error("删除旧程序错误:%v", err) + return &storage.Final{ + Error: fmt.Errorf("删除旧程序错误:%v", err), + } } } console.Debug("正在移动新程序错误...") diff --git a/main.go b/main.go index e61b726..1d9239f 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bufio" "os" + "strings" "time" _ "github.com/cdle/sillyplus/adapters/qq" @@ -26,6 +27,13 @@ func main() { if arg == "-d" { d = true } + if arg == "-r" && strings.Contains(os.Args[0], ".ready.exe") { //准备程序还原程序 + err := utils.CopyFile(utils.ProcessName, strings.Replace(utils.ProcessName, "ready.exe", ".exe", -1)) + if err == nil { + utils.Daemon("reset") + } + continue + } } if !d { t := false diff --git a/utils/init.go b/utils/init.go index 48fe6af..3fbe952 100644 --- a/utils/init.go +++ b/utils/init.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "io/ioutil" "os" "os/exec" @@ -169,8 +170,43 @@ func GetPidFromFile(pidFile string) (int, error) { return pid, nil } -func Daemon() { +func CopyFile(src string, dst string) error { + // 打开源文件 + srcFile, err := os.Open(src) + if err != nil { + return err + } + defer srcFile.Close() + + // 创建目标文件,如果目标文件已存在则覆盖 + dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + defer dstFile.Close() + + // 将源文件内容复制到目标文件 + _, err = io.Copy(dstFile, srcFile) + if err != nil { + return err + } + + return nil +} + +func Daemon(str ...string) { + first := "" + if len(str) > 0 { + first = str[0] + } args := os.Args[1:] + if first == "ready" { + os.Args[0] = strings.Replace(os.Args[0], ".exe", ".ready.exe", -1) + args = append(args, "-r") + } + if first == "reset" { + os.Args[0] = strings.Replace(os.Args[0], ".ready.exe", ".exe", -1) + } execArgs := make([]string, 0) l := len(args) for i := 0; i < l; i++ {