This commit is contained in:
Trit0 2025-06-29 17:27:12 -04:00
parent 2acc9b0133
commit fdb414df59

View File

@ -1,15 +1,18 @@
package provider
import (
"archive/zip"
"conjure-os/lib/config"
"conjure-os/lib/models"
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
@ -71,17 +74,43 @@ func GetConjureGameInfo() []models.Game {
gamePath := config.GetDefaultConjureGamesDirectory()
entries, err := os.ReadDir(gamePath)
entrie, err := os.ReadDir(gamePath)
if err != nil {
log.Fatal(err)
}
var games []models.Game
for _, e := range entries {
for _, e := range entrie {
if e.IsDir() {
newGames := readFolder(e, gamePath)
games = append(newGames, games...)
} else if filepath.Ext(e.Name()) == ".conj" {
zipPath := filepath.Join(gamePath, e.Name())
r, err := zip.OpenReader(zipPath)
if err != nil {
log.Fatal(err)
}
defer r.Close()
fmt.Println("Contents of", zipPath)
for _, f := range r.File {
printIndentedPath(f.Name)
if f.Name == "metadata.txt" {
rc, err := f.Open()
if err != nil {
log.Fatal(err)
}
defer rc.Close()
fmt.Println("Contents of metadata.txt:")
metadata, err := io.ReadAll(rc)
parseGameInfo(metadata)
if err != nil {
log.Fatal(err)
}
}
}
}
}
@ -93,6 +122,18 @@ func GetConjureGameInfo() []models.Game {
return games
}
// Helper to print tree-like structure
func printIndentedPath(path string) {
parts := strings.Split(path, "/")
for i, part := range parts {
if part == "" {
continue
}
fmt.Print(strings.Repeat(" ", i))
fmt.Println(part)
}
}
func readFolder(entry fs.DirEntry, path string) []models.Game {
newPath := path + "/" + entry.Name()
entries, err := os.ReadDir(newPath)
@ -118,8 +159,12 @@ func readFolder(entry fs.DirEntry, path string) []models.Game {
func parseGameInfoFromFile(path string) models.Game {
data, err := os.ReadFile(path)
check(err)
return parseGameInfo(data)
}
func parseGameInfo(data []byte) models.Game {
game := models.Game{}
err = yaml.Unmarshal(data, &game)
err := yaml.Unmarshal(data, &game)
check(err)
return game
}