- config: OS-specific paths, launcher.json load/save, DefaultSettings with MRIXSCRAFT_SERVER_URL env override - auth: Yggdrasil client (authenticate, refresh, validate), session persistence in session.json, EnsureValid flow - fetcher: HTTP download with SHA-1 verification, WorkerPool for concurrent downloads - java: JRE detection (IsInstalled/Find), platform-specific executable name - utils: SHA1File, SHA1Bytes, Unzip with zip-slip protection - cmd/launcher: wire config + auth into main, session restore on startup Co-Authored-By: OWL <noreply@anthropic.com>
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
// package java manages portable JRE downloads and detection.
|
|
package java
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/config"
|
|
)
|
|
|
|
// ExecutableName returns the platform-specific Java executable name.
|
|
func ExecutableName() string {
|
|
if runtime.GOOS == "windows" {
|
|
return "java.exe"
|
|
}
|
|
return "java"
|
|
}
|
|
|
|
// IsInstalled checks whether the given Java version is available locally.
|
|
func IsInstalled(version int) (string, error) {
|
|
dir, err := config.JavaDir(version)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
bin := filepath.Join(dir, "bin", ExecutableName())
|
|
if _, err := os.Stat(bin); err != nil {
|
|
if os.IsNotExist(err) {
|
|
return "", nil // not installed
|
|
}
|
|
return "", fmt.Errorf("checking %s: %w", bin, err)
|
|
}
|
|
return bin, nil
|
|
}
|
|
|
|
// Find searches for the required Java version, downloading it if necessary.
|
|
// Returns the path to the java binary.
|
|
func Find(version int) (string, error) {
|
|
bin, err := IsInstalled(version)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if bin != "" {
|
|
return bin, nil
|
|
}
|
|
|
|
// TODO: Download and extract JRE for the requested version.
|
|
// For now, return an error with instructions.
|
|
return "", fmt.Errorf(
|
|
"Java %d is not installed. Please wait for auto-download feature "+
|
|
"(planned) or manually place JRE in %s",
|
|
version,
|
|
must(config.JavaDir(version)),
|
|
)
|
|
}
|
|
|
|
// must panics on error — only used for error messages where the dir is already known valid.
|
|
func must(s string, err error) string {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return s
|
|
}
|