added basic support for downloading (not installing) modpack files

This commit is contained in:
Samuel Walker 2024-10-25 22:56:22 -06:00
parent 29bf715025
commit 3c8fb9a89f
10 changed files with 79 additions and 17 deletions

View File

@ -5,10 +5,12 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"context" "context"
"encoding/json"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"time"
"github.com/zhyee/zipstream" "github.com/zhyee/zipstream"
) )
@ -25,6 +27,12 @@ type Instance struct {
JavaVer string JavaVer string
} }
type Version struct {
Version string
Data time.Time
File string
}
func (Prism) CheckInstalled() bool { func (Prism) CheckInstalled() bool {
path, _ := os.UserConfigDir() path, _ := os.UserConfigDir()
_, err := os.Stat(filepath.Join(path, "FCLauncher", "prism")) _, err := os.Stat(filepath.Join(path, "FCLauncher", "prism"))
@ -114,3 +122,17 @@ func (p *Prism) Install() {
} }
} }
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)
}

View File

@ -19,7 +19,7 @@ type App struct {
type Modpack struct { type Modpack struct {
Name string Name string
Nd string Id string
Last_updated string Last_updated string
} }
@ -39,7 +39,7 @@ func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name) return fmt.Sprintf("Hello %s, It's show time!", name)
} }
func (a *App) GetModpacks() []string { func (a *App) GetModpacks() []Modpack {
buff := new(bytes.Buffer) buff := new(bytes.Buffer)
err := HttpDownload("modpacks.json", buff, nil) err := HttpDownload("modpacks.json", buff, nil)
if err != nil { if err != nil {
@ -52,11 +52,7 @@ func (a *App) GetModpacks() []string {
fmt.Println(err.Error()) fmt.Println(err.Error())
return nil return nil
} }
var names []string return modpacks
for _, pack := range modpacks {
names = append(names, pack.Name)
}
return names
} }
func (a *App) CheckPrerequisites() { func (a *App) CheckPrerequisites() {
@ -84,6 +80,11 @@ func (a *App) CheckPrerequisites() {
} }
} }
func (a *App) InstallModpack(pack Modpack) {
a.status(fmt.Sprintf("Installing %s\n", pack.Name))
a.prism.InstallModpack(pack)
}
func (a *App) status(status string) { func (a *App) status(status string) {
fmt.Printf("LOG: %s\n", status) fmt.Printf("LOG: %s\n", status)
runtime.EventsEmit(a.ctx, "status", status) runtime.EventsEmit(a.ctx, "status", status)

View File

@ -4,12 +4,12 @@
import Loading from './Loading.svelte' import Loading from './Loading.svelte'
import {CheckPrerequisites} from '../wailsjs/go/main/App.js' import {CheckPrerequisites} from '../wailsjs/go/main/App.js'
import { onMount } from 'svelte' import { onMount } from 'svelte'
import { loading } from './global.ts'
let loading: boolean = true
let width: int = 10 let width: int = 10
onMount(() => { onMount(() => {
CheckPrerequisites().then(() => loading = false) CheckPrerequisites().then(() => $loading = false)
}) })
@ -17,7 +17,7 @@
<main> <main>
<img alt="Wails logo" id="logo" src="{logo}"> <img alt="Wails logo" id="logo" src="{logo}">
{#if loading} {#if $loading}
<Loading /> <Loading />
{:else} {:else}
<Client /> <Client />

View File

@ -1,22 +1,31 @@
<script lang="ts"> <script lang="ts">
import {GetModpacks} from '../wailsjs/go/main/App.js' import {GetModpacks, InstallModpack} from '../wailsjs/go/main/App.js'
import {onMount} from 'svelte' import {onMount} from 'svelte'
import {loading} from './global.ts'
let modpacks: string[] = [] let modpacks: string[] = []
let pack: Modpack
onMount(() => { onMount(() => {
GetModpacks().then((result) => { GetModpacks().then((result) => {
modpacks = result modpacks = result
pack = modpacks[0]
}) })
}) })
function onclick(event) {
$loading = true
InstallModpack(pack).then(() => {$loading = false})
}
</script> </script>
<main> <main>
<select name="pack">Select a Modpack: <select bind:value={pack} name="pack">Select a Modpack:
{#each modpacks as pack} {#each modpacks as pack}
<option value={pack}>{pack}</option> <option value={pack}>{pack.Name}</option>
{/each} {/each}
</select> </select>
<button on:click={onclick}>Launch</button>
</main> </main>
<style> <style>

View File

@ -11,8 +11,8 @@
}) })
EventsOn("download", (Completed, Total) => { EventsOn("download", (Completed, Total) => {
completed = Completed / (1024*1024) completed = (Completed / (1024*1024)).toFixed(2)
total = Total / (1024*1024) total = (Total / (1024*1024)).toFixed(2)
downloading = true downloading = true
}) })

View File

@ -0,0 +1,2 @@
import { writable } from "svelte/store"
export const loading = writable(true)

View File

@ -1,8 +1,11 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
import {main} from '../models';
export function CheckPrerequisites():Promise<void>; export function CheckPrerequisites():Promise<void>;
export function GetModpacks():Promise<Array<string>>; export function GetModpacks():Promise<Array<main.Modpack>>;
export function Greet(arg1:string):Promise<string>; export function Greet(arg1:string):Promise<string>;
export function InstallModpack(arg1:main.Modpack):Promise<void>;

View File

@ -13,3 +13,7 @@ export function GetModpacks() {
export function Greet(arg1) { export function Greet(arg1) {
return window['go']['main']['App']['Greet'](arg1); return window['go']['main']['App']['Greet'](arg1);
} }
export function InstallModpack(arg1) {
return window['go']['main']['App']['InstallModpack'](arg1);
}

View File

@ -0,0 +1,21 @@
export namespace main {
export class Modpack {
Name: string;
Id: string;
Last_updated: string;
static createFrom(source: any = {}) {
return new Modpack(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.Name = source["Name"];
this.Id = source["Id"];
this.Last_updated = source["Last_updated"];
}
}
}

View File

@ -8,7 +8,7 @@ import (
"github.com/wailsapp/wails/v2/pkg/runtime" "github.com/wailsapp/wails/v2/pkg/runtime"
) )
const BlockSize = 1024 const BlockSize = 1024 * 64
func HttpDownload(path string, out io.Writer, ctx context.Context) error { func HttpDownload(path string, out io.Writer, ctx context.Context) error {
res, err := http.Get("https://gitea.piwalker.net/fclauncher/" + path) res, err := http.Get("https://gitea.piwalker.net/fclauncher/" + path)