start game perchance?
This commit is contained in:
parent
73e68bf2bb
commit
938e8e3195
44
app.go
44
app.go
@ -1,16 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"conjure-os/lib/inputs"
|
||||||
"conjure-os/lib/models"
|
"conjure-os/lib/models"
|
||||||
|
"conjure-os/lib/provider"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"conjure-os/lib/inputs"
|
"os/exec"
|
||||||
"conjure-os/lib/provider"
|
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var games []models.Game
|
||||||
|
|
||||||
// App struct
|
// App struct
|
||||||
type App struct {
|
type App struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
@ -29,15 +30,36 @@ func (a *App) startup(ctx context.Context) {
|
|||||||
provider.Update()
|
provider.Update()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Greet returns a greeting for the given name
|
func (a *App) StartGame(id string) {
|
||||||
func (a *App) Greet(name string) string {
|
for _, game := range games {
|
||||||
runtime.LogInfo(a.ctx, "Test : "+name)
|
if game.Id == id {
|
||||||
return fmt.Sprintf("Hello %s, It's show time!", name)
|
gamePath := provider.ExtractGame(game)
|
||||||
|
cmd := exec.Command(gamePath)
|
||||||
|
|
||||||
|
// Optional: attach current terminal's std streams
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
|
|
||||||
|
// Start the process
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
fmt.Println("Failed to start:", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) SelectGame(id string) {
|
fmt.Printf("Process started with PID %d\n", cmd.Process.Pid)
|
||||||
|
|
||||||
|
// Optional: wait for it to finish
|
||||||
|
if err := cmd.Wait(); err != nil {
|
||||||
|
fmt.Println("Process exited with error:", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Process exited successfully.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) LoadGames() []models.Game {
|
func (a *App) LoadGames() []models.Game {
|
||||||
return provider.GetConjureGameInfo()
|
games = provider.GetConjureGameInfo()
|
||||||
|
return games
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import {models} from "../../wailsjs/go/models";
|
import {models} from "../../wailsjs/go/models";
|
||||||
import Game = models.Game;
|
import Game = models.Game;
|
||||||
|
import { StartGame } from "../../wailsjs/go/main/App";
|
||||||
|
|
||||||
export const useAppStore = defineStore('app', {
|
export const useAppStore = defineStore('app', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
@ -48,6 +49,14 @@ export const useAppStore = defineStore('app', {
|
|||||||
},
|
},
|
||||||
showQr(link: string) {
|
showQr(link: string) {
|
||||||
this.qrLink = link;
|
this.qrLink = link;
|
||||||
|
},
|
||||||
|
startSelectedGame() {
|
||||||
|
if (this.selectedGame) {
|
||||||
|
StartGame(this.selectedGame.Id).then()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log("No game selected")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -35,5 +35,6 @@ export class CarouselKeyContext extends KeyContext {
|
|||||||
|
|
||||||
protected onEnter() {
|
protected onEnter() {
|
||||||
super.onEnter();
|
super.onEnter();
|
||||||
|
this.store.startSelectedGame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
4
frontend/wailsjs/go/main/App.d.ts
vendored
4
frontend/wailsjs/go/main/App.d.ts
vendored
@ -2,8 +2,6 @@
|
|||||||
// This file is automatically generated. DO NOT EDIT
|
// This file is automatically generated. DO NOT EDIT
|
||||||
import {models} from '../models';
|
import {models} from '../models';
|
||||||
|
|
||||||
export function Greet(arg1:string):Promise<string>;
|
|
||||||
|
|
||||||
export function LoadGames():Promise<Array<models.Game>>;
|
export function LoadGames():Promise<Array<models.Game>>;
|
||||||
|
|
||||||
export function SelectGame(arg1:string):Promise<void>;
|
export function StartGame(arg1:string):Promise<void>;
|
||||||
|
|||||||
@ -2,14 +2,10 @@
|
|||||||
// 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
|
||||||
|
|
||||||
export function Greet(arg1) {
|
|
||||||
return window['go']['main']['App']['Greet'](arg1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function LoadGames() {
|
export function LoadGames() {
|
||||||
return window['go']['main']['App']['LoadGames']();
|
return window['go']['main']['App']['LoadGames']();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SelectGame(arg1) {
|
export function StartGame(arg1) {
|
||||||
return window['go']['main']['App']['SelectGame'](arg1);
|
return window['go']['main']['App']['StartGame'](arg1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -70,6 +70,106 @@ func GetOrSetEnvKey(key string, defaultValue string) string {
|
|||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractZipToSiblingFolder(zipPath string) error {
|
||||||
|
// Determine destination folder name (same name as zip file, without .zip)
|
||||||
|
zipBase := strings.TrimSuffix(filepath.Base(zipPath), ".conj")
|
||||||
|
destDir := filepath.Join(filepath.Dir(zipPath), zipBase)
|
||||||
|
|
||||||
|
// Delete destination folder if it exists
|
||||||
|
if _, err := os.Stat(destDir); err == nil {
|
||||||
|
err = os.RemoveAll(destDir)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to remove existing folder: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open the zip archive
|
||||||
|
r, err := zip.OpenReader(zipPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
// Create the destination directory
|
||||||
|
err = os.MkdirAll(destDir, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range r.File {
|
||||||
|
destPath := filepath.Join(destDir, f.Name)
|
||||||
|
|
||||||
|
// ZipSlip protection
|
||||||
|
if !strings.HasPrefix(destPath, filepath.Clean(destDir)+string(os.PathSeparator)) {
|
||||||
|
return fmt.Errorf("illegal file path: %s", destPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.FileInfo().IsDir() {
|
||||||
|
if err := os.MkdirAll(destPath, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure parent directories exist
|
||||||
|
if err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create and copy the file
|
||||||
|
srcFile, err := f.Open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer srcFile.Close()
|
||||||
|
|
||||||
|
dstFile, err := os.OpenFile(destPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer dstFile.Close()
|
||||||
|
|
||||||
|
if _, err := io.Copy(dstFile, srcFile); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractZipsInFolder(folder string) error {
|
||||||
|
entries, err := os.ReadDir(folder)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("reading directory failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
if entry.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(strings.ToLower(entry.Name()), ".conj") {
|
||||||
|
zipPath := filepath.Join(folder, entry.Name())
|
||||||
|
fmt.Println("Extracting:", zipPath)
|
||||||
|
if err := extractZipToSiblingFolder(zipPath); err != nil {
|
||||||
|
return fmt.Errorf("failed to extract %s: %w", zipPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExtractGame(game models.Game) string {
|
||||||
|
gamePath := filepath.Join(config.GetDefaultConjureGamesDirectory(), fmt.Sprintf("%s.conj", game.Id))
|
||||||
|
err := extractZipToSiblingFolder(gamePath)
|
||||||
|
check(err)
|
||||||
|
gamePath = filepath.Join(config.GetDefaultConjureGamesDirectory(), game.Id)
|
||||||
|
err = extractZipsInFolder(gamePath)
|
||||||
|
check(err)
|
||||||
|
gamePath = filepath.Join(gamePath, game.Files)
|
||||||
|
return gamePath
|
||||||
|
}
|
||||||
|
|
||||||
func GetConjureGameInfo() []models.Game {
|
func GetConjureGameInfo() []models.Game {
|
||||||
|
|
||||||
gamePath := config.GetDefaultConjureGamesDirectory()
|
gamePath := config.GetDefaultConjureGamesDirectory()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user