Browse Source

rely on external server for download

master
Merith-TK 10 months ago
parent
commit
999c24ccb6
5 changed files with 111 additions and 6 deletions
  1. +5
    -1
      Readme.md
  2. +53
    -0
      archive.go
  3. +8
    -2
      exec.go
  4. +42
    -2
      file.go
  5. +3
    -1
      main.go

+ 5
- 1
Readme.md View File

@ -22,4 +22,8 @@ your `config.portable.json` file would look like `{"launcher":"MultiMC/MultiMC.e
When setting up MultiMC with the setup in this example, it is IMPORTANT to use this path to javaw.exe
`../runtime/jre-x64/bin/javaw.exe`, otherwise Java may not work
If you wanted to use a `jar` version of minecraft, like `minecraft.jar` (can be found [here](https://launcher.mojang.com/mc/launcher/jar/fa896bd4c79d4e9f0d18df43151b549f865a3db6/launcher.jar.lzma), you will need winrar or 7zip to open the `lzma` archive)
If you wanted to use a `jar` version of minecraft, like `minecraft.jar` (can be found [here](https://launcher.mojang.com/mc/launcher/jar/fa896bd4c79d4e9f0d18df43151b549f865a3db6/launcher.jar.lzma), you will need winrar or 7zip to open the `lzma` archive)
and change your `config.portable.json` to match this
`{"launcher":"minecraft.jar","java":true}`

+ 53
- 0
archive.go View File

@ -2,6 +2,7 @@ package main
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"io"
"os"
@ -99,3 +100,55 @@ func unlzma(source, target string) error {
_, err = io.Copy(writer, archive)
return err
}
func unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
os.MkdirAll(dest, 0755)
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
path := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}
return nil
}

+ 8
- 2
exec.go View File

@ -4,6 +4,8 @@ import (
"fmt"
"os"
"os/exec"
"github.com/gen2brain/dlgs"
)
/*
@ -20,7 +22,7 @@ var (
)
func minecraftexe() {
filecheck("MinecraftData/minecraft.exe", exeMinecraft)
filecheck("minecraft.exe")
cmd := exec.Command("MinecraftData/minecraft.exe", "--workDir", ".minecraft")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
@ -31,11 +33,15 @@ func minecraftexe() {
}
func unknownexe(execute string) {
filecheck(execute)
if _, err := os.Stat("MinecraftData/" + execute); err != nil {
dlgs.Error("[MineCraftPortable]: ERROR", execute+" not found, did you edit config.portable.json?")
}
cmd := exec.Command("MinecraftData/" + execute)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
cmd.Stdin = os.Stdin
fmt.Println("[MineCraftPortable] Running Launcher")
fmt.Println("[MineCraftPortable] Running " + execute)
fmt.Println("[MineCraftPortable] Launcher will start Shortly")
cmd.Run()
}

+ 42
- 2
file.go View File

@ -3,7 +3,9 @@ package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
@ -15,9 +17,47 @@ import (
*/
func filecheck(fileName string, URL string) error {
//Get the response bytes from the url
var (
rel Release
URL = "https://cdn.merith.tk/_Releases/Other/minecraft.json"
)
type Release struct {
Official map[string]string `json:"official"`
Unofficial map[string]string `json:"unofficial"`
}
func filecheck(filename string) {
err := getjson(URL, &rel)
if err != nil {
fmt.Println("Failed to PARSE json\n", err)
fmt.Println(rel)
os.Exit(1)
}
for k, v := range rel.Official {
if filename == k {
if _, err := os.Stat("MinecraftData/" + k); err != nil {
download("MinecraftData/"+k, v)
}
}
}
}
func getjson(url string, target interface{}) error {
r, err := http.Get(url)
if err != nil {
return err
}
if r.StatusCode != 200 {
return errors.New("Received non 200 response code")
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
func download(fileName string, URL string) error {
//Get the response bytes from the url
fmt.Println(fileName, "\n", URL)
if _, err := os.Stat(fileName); os.IsNotExist(err) {
response, err := http.Get(URL)
if err != nil {

+ 3
- 1
main.go View File

@ -6,6 +6,8 @@ import (
"io"
"io/ioutil"
"os"
"github.com/gen2brain/dlgs"
)
var (
@ -40,6 +42,7 @@ func main() {
}
func createConfig() {
dlgs.Warning("[MineCraftPortable]", "This application takes a bit to work on the first run, please be patient")
file, _ := os.Create(configfile)
defer file.Close()
@ -54,7 +57,6 @@ func readjson() (string, bool) {
fmt.Println("Creating Config")
createConfig()
}
fmt.Println(str)
err = json.Unmarshal([]byte(str), &conf)
return conf.Launcher, conf.Java

Loading…
Cancel
Save