2024-10-25 20:46:06 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zhyee/zipstream"
|
|
|
|
)
|
|
|
|
|
2024-10-26 14:00:53 -06:00
|
|
|
type JavaManager struct {
|
|
|
|
app *App
|
2024-10-25 20:46:06 -06:00
|
|
|
}
|
|
|
|
|
2024-10-26 14:00:53 -06:00
|
|
|
func (JavaManager)CheckJavaVer(version int) bool{
|
2024-10-25 20:46:06 -06:00
|
|
|
suffix := "lin"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
suffix = "win"
|
|
|
|
}
|
|
|
|
path, _ := os.UserConfigDir()
|
|
|
|
path = filepath.Join(path, "FCLauncher", "java", fmt.Sprintf("java-%d-%s", version, suffix))
|
|
|
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-10-26 14:00:53 -06:00
|
|
|
func (j *JavaManager)InstallJavaVer(version int) {
|
2024-10-25 20:46:06 -06:00
|
|
|
suffix := "lin.tar.gz"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
suffix = "win.zip"
|
|
|
|
}
|
|
|
|
buff := new(bytes.Buffer)
|
2024-10-26 14:00:53 -06:00
|
|
|
HttpDownload("java/"+fmt.Sprintf("java-%d-%s", version, suffix), buff, j.app.Ctx)
|
2024-10-25 20:46:06 -06:00
|
|
|
path, _ := os.UserConfigDir()
|
|
|
|
suffix = "lin"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
suffix = "win"
|
|
|
|
}
|
|
|
|
|
|
|
|
path = filepath.Join(path, "FCLauncher", "java", fmt.Sprintf("java-%d-%s", version, suffix))
|
|
|
|
os.MkdirAll(path, 0755)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
zr := zipstream.NewReader(buff)
|
|
|
|
for {
|
|
|
|
entry, err := zr.GetNextEntry()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-10-26 19:27:39 -06:00
|
|
|
target := filepath.Join(path, strings.SplitN(entry.Name, "/", 2)[1])
|
2024-10-25 20:46:06 -06:00
|
|
|
if !entry.IsDir() {
|
|
|
|
rc, err := entry.Open()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, entry.FileInfo().Mode())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(f, rc); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
rc.Close()
|
|
|
|
} else {
|
|
|
|
if _, err := os.Stat(target); err != nil {
|
|
|
|
if err := os.MkdirAll(target, 0755); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gzip, _ := gzip.NewReader(buff)
|
|
|
|
defer gzip.Close()
|
|
|
|
tr := tar.NewReader(gzip)
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
header, err := tr.Next()
|
|
|
|
switch {
|
|
|
|
case err == io.EOF:
|
|
|
|
break out
|
|
|
|
case err != nil:
|
|
|
|
return
|
|
|
|
case header == nil:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
target := filepath.Join(path, strings.SplitN(header.Name, string(os.PathSeparator), 2)[1])
|
|
|
|
switch header.Typeflag {
|
|
|
|
case tar.TypeDir:
|
|
|
|
if _, err := os.Stat(target); err != nil {
|
|
|
|
if err := os.MkdirAll(target, 0755); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case tar.TypeReg:
|
|
|
|
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(f, tr); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|