-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.go
150 lines (121 loc) · 3.18 KB
/
Main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"errors"
"github.com/ncruces/zenity"
"io"
"io/fs"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
)
const url = "https://github.com/mircokroon/minecraft-world-downloader/releases/latest/download/world-downloader.jar"
const jarFile = "world-downloader.jar"
const jarFileDownloading = jarFile + ".downloading"
const gameRuntimeDir = "C:\\Program Files (x86)\\Minecraft Launcher\\runtime\\"
func main() {
if !exists(jarFile) {
notify("Downloading " + jarFile)
download()
rename()
}
run()
}
func exists(path string) bool {
_, err := os.Stat(path)
return !errors.Is(err, os.ErrNotExist)
}
func download() {
file, err := os.Create(jarFileDownloading)
defer file.Close()
checkError(err)
response, err := http.Get(url)
if response.StatusCode != 200 {
checkError(errors.New("Got status code " + strconv.Itoa(response.StatusCode) + " while downloading <" + url + ">"))
}
defer response.Body.Close()
checkError(err)
_, err = io.Copy(file, response.Body)
checkError(err)
}
func rename() {
err := os.Rename(jarFileDownloading, jarFile)
checkError(err)
}
func run() {
success := runWithJavaPath("java") || runEach(findJavaHomeSiblings()) || runEach(findJavaExecutables(gameRuntimeDir))
if !success {
writeLog()
checkError(errors.New("cannot find suitable Java version. Make sure one is correctly installed"))
}
}
func runWithJavaPath(java string) bool {
log("Trying to run " + java)
// running the command through cmd and not directly lets us hide the command prompt without hiding the gui as well
cmd := exec.Command("cmd.exe", "/C", java, "-jar", jarFile)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
out, err := cmd.CombinedOutput()
if err == nil {
cmd.Wait()
return true
}
log("\t" + err.Error())
log("\t" + string(out))
return false
}
// find JAVA_HOME and go up one folder to find all versions of Java (ideally)
func findJavaHomeSiblings() []string {
env := os.Getenv("JAVA_HOME")
if env == "" {
return nil
}
return findJavaExecutables(filepath.Dir(env))
}
// find java executables in a given path
func findJavaExecutables(dir string) []string {
options := make([]string, 0)
filepath.WalkDir(dir, func(path string, di fs.DirEntry, err error) error {
if strings.HasSuffix(path, "java.exe") {
options = append(options, path)
}
return err
})
return options
}
func runEach(options []string) bool {
for _, option := range options {
wasSuccess := runWithJavaPath(option)
if wasSuccess {
return true
}
}
return false
}
func checkErrorWithMessage(err error, info string) {
if err != nil {
zenity.Info("An error has occurred: \n\n"+err.Error()+"\n\n"+info,
zenity.Title("Error"),
zenity.ErrorIcon)
os.Exit(1)
}
}
func checkError(err error) {
checkErrorWithMessage(err, "")
}
func notify(msg string) {
zenity.Notify(msg, zenity.Title("Minecraft World Downloader"), zenity.InfoIcon)
}
// log file if unable to start
var logArr = make([]string, 0)
func writeLog() {
f, _ := os.OpenFile("world-downloader-launcher.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
for _, line := range logArr {
f.Write([]byte(line + "\n"))
}
}
func log(str string) {
logArr = append(logArr, str)
}