parent
9306f3275f
commit
161deba5f6
@ -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 |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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, "" |
||||||
|
} |
||||||
|
} |
@ -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 |
||||||
|
} |
@ -0,0 +1,7 @@ |
|||||||
|
package config |
||||||
|
|
||||||
|
type LauncherConfig struct { |
||||||
|
ConfigVersion int |
||||||
|
DogenameLocation string |
||||||
|
RuntimeLocation string |
||||||
|
} |
Binary file not shown.
@ -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 |
@ -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= |
@ -0,0 +1,3 @@ |
|||||||
|
ConfigVersion = 1 |
||||||
|
DogenameLocation = <@DEFAULT> |
||||||
|
RuntimeLocation = <@DEFAULT> |
@ -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() |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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 |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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 |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue