diff --git a/.gitignore b/.gitignore index 6445b33..2e1b65d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,8 @@ target log logs .idea -process \ No newline at end of file +process +pkg +*.exe +*.jar +*.log \ No newline at end of file diff --git a/DogenameLauncher/src/checkers/dogeChecker.go b/DogenameLauncher/src/checkers/dogeChecker.go new file mode 100644 index 0000000..32171ab --- /dev/null +++ b/DogenameLauncher/src/checkers/dogeChecker.go @@ -0,0 +1,25 @@ +package checkers + +import ( + "os" + "path/filepath" +) + +func SearchDoge(dogeLocation string) (bool, string) { + + var err error + dogeLocation, err = filepath.Abs(dogeLocation) + + if err != nil { + return false, "" + } + + _, err1 := os.Stat(dogeLocation) + + if err1 != nil { + return false, "" + } else { + return true, dogeLocation + } + +} diff --git a/DogenameLauncher/src/checkers/runtimeChecker.go b/DogenameLauncher/src/checkers/runtimeChecker.go new file mode 100644 index 0000000..a4ce8ad --- /dev/null +++ b/DogenameLauncher/src/checkers/runtimeChecker.go @@ -0,0 +1,65 @@ +package checkers + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" +) + +func SearchRuntime(runtimeLocation string) (bool, string) { + + var err error + runtimeLocation, err = filepath.Abs(runtimeLocation) + + if err != nil { + return false, "" + } + + //try to find javaw.exe + javawExists, javawLocation := FindJava(runtimeLocation, "javaw") + + if javawExists == true { + fmt.Println("[RuntimeChecker]javawExists:", javawExists) + fmt.Println("[RuntimeChecker]javawLocation:", javawLocation) + return true, javawLocation + } + + //try to find java.exe + javaExists, javaLocation := FindJava(runtimeLocation, "java") + + if javaExists == true { + fmt.Println("[RuntimeChecker]javaExists:", javaExists) + fmt.Println("[RuntimeChecker]javaLocation:", javaLocation) + return true, javaLocation + } else { + return false, "" + } +} + +func FindJava(runtimeLocation string, runtimeName string) (bool, string) { + fmt.Println("[RuntimeChecker]Finding:", runtimeLocation+"/"+runtimeName+".exe") + _, err := os.Stat(runtimeLocation + "/" + runtimeName + ".exe") + + if err == nil { + return true, runtimeLocation + "/" + runtimeName + } + + installedJavaExists, installedJavaLocation := FindInstalledRuntime(runtimeName) + + if installedJavaExists == true { + return true, installedJavaLocation + } else { + return false, "" + } +} + +func FindInstalledRuntime(program string) (bool, string) { + findResult, err := exec.LookPath(program) + + if err == nil { + return true, findResult + } else { + return false, "" + } +} diff --git a/DogenameLauncher/src/config/configLoader.go b/DogenameLauncher/src/config/configLoader.go new file mode 100644 index 0000000..131db9e --- /dev/null +++ b/DogenameLauncher/src/config/configLoader.go @@ -0,0 +1,38 @@ +package config + +import ( + "fmt" + "os" + + "gopkg.in/ini.v1" +) + +const DEFAULT_MAIN_PROGRAM_LOCATION = "./dogename_program.jar" +const DEFAULT_RUNTIME_LOCATION = "./runtime/java8/bin" + +func GetEffectiveConfig() LauncherConfig { + + launcherConfig := &LauncherConfig{ConfigVersion: 1, DogenameLocation: DEFAULT_MAIN_PROGRAM_LOCATION, RuntimeLocation: DEFAULT_RUNTIME_LOCATION} + + _, err := os.Stat("./launcher.conf") + + if err != nil { + fmt.Println("[ConfigLoader]Config file doesn't exist,use default value.") + return *launcherConfig + } + + config, _ := ini.Load("./launcher.conf") + config.MapTo(launcherConfig) + + if launcherConfig.DogenameLocation == "<@DEFAULT>" { + fmt.Println("[ConfigLoader]Use default program location.") + launcherConfig.DogenameLocation = DEFAULT_MAIN_PROGRAM_LOCATION + } + + if launcherConfig.RuntimeLocation == "<@DEFAULT>" { + fmt.Println("[ConfigLoader]Use default runtime location.") + launcherConfig.RuntimeLocation = DEFAULT_RUNTIME_LOCATION + } + + return *launcherConfig +} diff --git a/DogenameLauncher/src/config/launcherConfig.go b/DogenameLauncher/src/config/launcherConfig.go new file mode 100644 index 0000000..4328eef --- /dev/null +++ b/DogenameLauncher/src/config/launcherConfig.go @@ -0,0 +1,7 @@ +package config + +type LauncherConfig struct { + ConfigVersion int + DogenameLocation string + RuntimeLocation string +} diff --git a/DogenameLauncher/src/dogename.syso b/DogenameLauncher/src/dogename.syso new file mode 100644 index 0000000..082cfad Binary files /dev/null and b/DogenameLauncher/src/dogename.syso differ diff --git a/DogenameLauncher/src/go.mod b/DogenameLauncher/src/go.mod new file mode 100644 index 0000000..7dba5b7 --- /dev/null +++ b/DogenameLauncher/src/go.mod @@ -0,0 +1,8 @@ +module launcher + +require ( + github.com/sqweek/dialog v0.0.0-20200911184034-8a3d98e8211d + gopkg.in/ini.v1 v1.62.0 +) + +go 1.16 diff --git a/DogenameLauncher/src/go.sum b/DogenameLauncher/src/go.sum new file mode 100644 index 0000000..7ee7685 --- /dev/null +++ b/DogenameLauncher/src/go.sum @@ -0,0 +1,6 @@ +github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf h1:FPsprx82rdrX2jiKyS17BH6IrTmUBYqZa/CXT4uvb+I= +github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf/go.mod h1:peYoMncQljjNS6tZwI9WVyQB3qZS6u79/N3mBOcnd3I= +github.com/sqweek/dialog v0.0.0-20200911184034-8a3d98e8211d h1:Chay1rwJnXxI27H+pzu7P81BKf647un9GOoRPTdXN18= +github.com/sqweek/dialog v0.0.0-20200911184034-8a3d98e8211d/go.mod h1:/qNPSY91qTz/8TgHEMioAUc6q7+3SOybeKczHMXFcXw= +gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU= +gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/DogenameLauncher/src/launcher.conf b/DogenameLauncher/src/launcher.conf new file mode 100644 index 0000000..3e73461 --- /dev/null +++ b/DogenameLauncher/src/launcher.conf @@ -0,0 +1,3 @@ +ConfigVersion = 1 +DogenameLocation = <@DEFAULT> +RuntimeLocation = <@DEFAULT> \ No newline at end of file diff --git a/DogenameLauncher/src/launcher.go b/DogenameLauncher/src/launcher.go index e69de29..a005c66 100644 --- a/DogenameLauncher/src/launcher.go +++ b/DogenameLauncher/src/launcher.go @@ -0,0 +1,84 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + + "launcher/checkers" + "launcher/config" + "launcher/utils" + + "github.com/sqweek/dialog" +) + +var workDir string + +func main() { + + var err error = nil + workDir, err = utils.GetDir(os.Args[0]) + + if err != nil { + os.Exit(1) + } + + var launcherConfig config.LauncherConfig + + launcherConfig = config.GetEffectiveConfig() + + dogenameDir, _ := utils.GetDir(launcherConfig.DogenameLocation) + + // solve existent dogename process + pidFilesExist, pids := utils.FindDogeProcess(dogenameDir) + if pidFilesExist == true { + killDogeProcess := dialog.Message("发现可能有至少一个dogename在运行,要把它关掉吗?").Title("DogenameLauncher - 诶嘿!").YesNo() + if killDogeProcess == false { + os.Exit(0) + } else { + for _, pid := range pids { + fmt.Println("killing:", pid) + exec.Command("taskkill", "/PID", pid).Start() + os.Remove(workDir + "/process/" + pid) + } + } + } + + // check necessary files + + // check main program + programExists, programLocation := checkers.SearchDoge(launcherConfig.DogenameLocation) + fmt.Println("programLocation: ", programLocation) + fmt.Println("programExists: ", programExists) + if programExists != true { + + dialog.Message("主程序文件“dogename.jar”不见惹,没办法运行啦。\n请检查主程序文件是否存在或访问,若不存在请前往https://github.com/lensferno/dogename下载主程序文件,并放置在" + dogenameDir + "下。").Title("DogenameLauncher - 诶嘿!").Error() + + exec.Command("rundll32", "url.dll,FileProtocolHandler", "https://github.com/lensferno/dogename").Start() + + os.Exit(1) + } + + // check runtime + runtimeExists, runtimeLocation := checkers.SearchRuntime(launcherConfig.RuntimeLocation) + + fmt.Println("RuntimeLocation: ", runtimeLocation) + fmt.Println("RuntimeExists: ", runtimeExists) + if runtimeExists != true { + dialog.Message("Java运行环境不见惹,没法运行啦!请确认是否安装Java。\n若您知道哪里有正确的Java,请编辑" + workDir + "下的“launcher.config”文件:)").Title("DogenameLauncher - 诶嘿!").Error() + os.Exit(1) + } + + // try to run program + cmd := exec.Command(runtimeLocation, "-jar", programLocation) + cmd.Dir = dogenameDir + + runError := cmd.Run() + + fmt.Println("runError:", runError) + + if runError != nil { + dialog.Message("程序已经找到并且试图运行,但好像出了点错误,他托我告诉你一下(嗯?)\n大概是这样的:" + runError.Error() + "\n不过我把具体信息给忘了(懒得说了)(嗯?)\n这边建议您去https://github.com/lensferno/dogename那里重新下载一个呢:)").Title("DogenameLauncher - 诶嘿!").Info() + } + +} diff --git a/DogenameLauncher/src/utils/fileUtil.go b/DogenameLauncher/src/utils/fileUtil.go new file mode 100644 index 0000000..7bd6274 --- /dev/null +++ b/DogenameLauncher/src/utils/fileUtil.go @@ -0,0 +1,15 @@ +package utils + +import ( + "path/filepath" +) + +func GetDir(filePath string) (string, error) { + absResult, err := filepath.Abs(filepath.Dir(filePath)) + if err != nil { + return "", err + } else { + return filepath.ToSlash(absResult), err + } + +} diff --git a/DogenameLauncher/src/utils/process.go b/DogenameLauncher/src/utils/process.go new file mode 100644 index 0000000..d5cabed --- /dev/null +++ b/DogenameLauncher/src/utils/process.go @@ -0,0 +1,21 @@ +package utils + +import ( + "fmt" + "path/filepath" +) + +func FindDogeProcess(dogePath string) (bool, []string) { + pids, _ := filepath.Glob(dogePath + "/process/*") + + if pids == nil { + return false, nil + } else { + for i := 0; i < len(pids); i++ { + pids[i] = filepath.Base(pids[i]) + } + fmt.Println("[Process]found pids:", pids) + return true, pids + } + +}