FCLauncher/fclauncher/app.go

97 lines
2.2 KiB
Go
Raw Normal View History

2024-10-24 13:31:01 -06:00
package main
import (
"context"
2024-10-30 16:41:03 -06:00
"encoding/json"
2024-10-24 13:31:01 -06:00
"fmt"
2024-10-30 16:41:03 -06:00
"io"
"os"
"path/filepath"
"github.com/wailsapp/wails/v2/pkg/runtime"
2024-10-24 13:31:01 -06:00
)
const client_id string = "9305aeb8-5ecb-4e7a-b28f-c33aefcfbd8d"
2024-10-24 13:31:01 -06:00
// App struct
type App struct {
Ctx context.Context
PrismLauncher Prism
Java JavaManager
Instance InstanceManager
Modpacks ModpackManager
2024-10-30 16:41:03 -06:00
Auth authenticationResp
2024-10-24 13:31:01 -06:00
}
// NewApp creates a new App application struct
func NewApp() *App {
a := &App{}
a.Java = JavaManager{app: a}
a.Instance = InstanceManager{app: a}
a.Modpacks = ModpackManager{app: a}
return a
2024-10-24 13:31:01 -06:00
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.Ctx = ctx
2024-10-24 13:31:01 -06:00
}
// Greet returns a greeting for the given name
2024-10-24 15:46:17 -06:00
func (a *App) CheckPrerequisites() {
a.Status("Querrying Existing Instances")
a.Instance.SearchInstances()
a.Status("Pulling Modpacks")
a.Modpacks.QuerryModpacks()
2024-10-30 17:16:10 -06:00
a.Status("Logging in with Microsoft")
2024-10-30 16:41:03 -06:00
dir, _ := os.UserConfigDir()
authenticated := false
if _, err := os.Stat(filepath.Join(dir, "FCLauncher", "authentication.json")); err == nil {
f, _ := os.OpenFile(filepath.Join(dir, "FCLauncher", "authentication.json"), os.O_RDONLY, 0755)
defer f.Close()
data, _ := io.ReadAll(f)
json.Unmarshal(data, &a.Auth)
a.Auth, err = TokenRefresh(*a, a.Auth)
if err == nil {
authenticated = true
}
}
if !authenticated {
var err error
a.Auth, err = AuthCode(*a)
if err != nil {
fmt.Printf("Authentication Error: %s\n", err)
return
}
}
os.MkdirAll(filepath.Join(dir, "FCLauncher"), 0755)
f, _ := os.OpenFile(filepath.Join(dir, "FCLauncher", "authentication.json"), os.O_CREATE|os.O_RDWR, 0755)
defer f.Close()
data, _ := json.Marshal(a.Auth)
f.Write(data)
2024-10-29 21:48:59 -06:00
}
func (App) GetVersions() ([]string, error) {
manifest, err := GetVersionManifest()
if err != nil {
fmt.Printf("Manifest Error: %s\n", err)
return []string{}, err
}
versions := []string{}
for _, version := range manifest.Versions {
versions = append(versions, version.Id)
}
return versions, nil
2024-10-24 15:46:17 -06:00
}
func (a *App) Status(status string) {
fmt.Printf("LOG: %s\n", status)
runtime.EventsEmit(a.Ctx, "status", status)
}