139 lines
2.7 KiB
Go
139 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/zhyee/zipstream"
|
|
)
|
|
|
|
type Prism struct {
|
|
Instances []Instance
|
|
ctx context.Context
|
|
}
|
|
|
|
type Instance struct {
|
|
Path string
|
|
Name string
|
|
MCVer string
|
|
JavaVer string
|
|
}
|
|
|
|
type Version struct {
|
|
Version string
|
|
Data time.Time
|
|
File string
|
|
}
|
|
|
|
func (Prism) CheckInstalled() bool {
|
|
path, _ := os.UserConfigDir()
|
|
_, err := os.Stat(filepath.Join(path, "FCLauncher", "prism"))
|
|
if err == nil {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (p *Prism) Install() {
|
|
suffix := "lin.tar.gz"
|
|
if runtime.GOOS == "windows" {
|
|
suffix = "win.zip"
|
|
}
|
|
buff := new(bytes.Buffer)
|
|
HttpDownload("prism/prism-"+suffix, buff, p.ctx)
|
|
path, _ := os.UserConfigDir()
|
|
os.MkdirAll(filepath.Join(path, "FCLauncher", "prism"), 0755)
|
|
if runtime.GOOS == "windows" {
|
|
zr := zipstream.NewReader(buff)
|
|
for {
|
|
entry, err := zr.GetNextEntry()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return
|
|
}
|
|
target := filepath.Join(path, "FCLauncher", "prism", entry.Name)
|
|
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, "FCLauncher", "prism", header.Name)
|
|
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()
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
func (p *Prism)InstallModpack(modpack Modpack){
|
|
buff := new(bytes.Buffer)
|
|
HttpDownload(modpack.Id + "/versions.json", buff, nil)
|
|
var versions []Version
|
|
json.Unmarshal(buff.Bytes(), &versions)
|
|
version := versions[len(versions)-1]
|
|
dname, _ := os.MkdirTemp("", "fclauncher-*")
|
|
f, _ := os.OpenFile(filepath.Join(dname, modpack.Name), os.O_CREATE|os.O_RDWR, 0755)
|
|
defer f.Close()
|
|
HttpDownload(modpack.Id + "/" + version.File, f, p.ctx)
|
|
|
|
}
|