Started https download code

This commit is contained in:
Samuel Walker 2024-10-24 17:05:47 -06:00
parent 0a6b37ccab
commit 7ad769ff96
3 changed files with 75 additions and 7 deletions

31
fclauncher/Prism.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"os"
"path/filepath"
)
type Prism struct {
Instances []Instance
}
type Instance struct {
Path string
Name string
MCVer string
JavaVer 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 (Prism) Install() {
}

View File

@ -1,16 +1,17 @@
package main package main
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/http"
) )
// App struct // App struct
type App struct { type App struct {
ctx context.Context ctx context.Context
prism Prism
} }
type Modpack struct { type Modpack struct {
@ -36,12 +37,12 @@ func (a *App) Greet(name string) string {
} }
func (a *App) GetModpacks() []string { func (a *App) GetModpacks() []string {
res, err := http.Get("https://gitea.piwalker.net/fclauncher/modpacks.json") buff := new(bytes.Buffer)
err := HttpDownload("modpacks.json", buff)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err)
} }
defer res.Body.Close() body, err := io.ReadAll(buff)
body, err := io.ReadAll(res.Body)
var modpacks []Modpack var modpacks []Modpack
err = json.Unmarshal([]byte(body), &modpacks) err = json.Unmarshal([]byte(body), &modpacks)
if err != nil { if err != nil {
@ -56,5 +57,14 @@ func (a *App) GetModpacks() []string {
} }
func (a *App) CheckPrerequisites() { func (a *App) CheckPrerequisites() {
a.prism = Prism{}
res := a.prism.CheckInstalled()
fmt.Printf("Checking if prism is installed: ")
if res {
fmt.Println("Yes")
} else {
fmt.Println("No")
fmt.Println("Installing Prism")
a.prism.Install()
}
} }

27
fclauncher/https.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
"fmt"
"io"
"net/http"
)
const BlockSize = 1024
func HttpDownload(path string, out io.Writer) error {
res, err := http.Get("https://gitea.piwalker.net/fclauncher/" + path)
if err != nil {
return err
}
defer res.Body.Close()
var read int64 = 0
for read < res.ContentLength {
count, err := io.CopyN(out, res.Body, BlockSize)
read += count
fmt.Printf("Downloaded %dMB / %dMB\n", read/(1024*1024), res.ContentLength/(1024*1024))
if err != nil {
break
}
}
return nil
}