diff --git a/lib/provider/provider.go b/lib/provider/provider.go index 9e09110..c203830 100644 --- a/lib/provider/provider.go +++ b/lib/provider/provider.go @@ -161,10 +161,13 @@ func extractZipsInFolder(folder string) error { } 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) + info, err := os.Stat(filepath.Join(config.GetDefaultConjureGamesDirectory(), game.Id)) + if err != nil || !info.IsDir() { + 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) @@ -175,47 +178,40 @@ func GetConjureGameInfo() []models.Game { gamePath := config.GetDefaultConjureGamesDirectory() - entrie, err := os.ReadDir(gamePath) + entries, err := os.ReadDir(gamePath) if err != nil { log.Fatal(err) } var games []models.Game - for _, e := range entrie { + for _, e := range entries { if e.IsDir() { - newGames := readFolder(e, gamePath) - games = append(newGames, games...) + continue } else if filepath.Ext(e.Name()) == ".conj" { conjPath := filepath.Join(gamePath, e.Name()) - r, err := zip.OpenReader(conjPath) - if err != nil { - log.Fatal(err) - } - defer r.Close() - + err = extractZipToSiblingFolder(conjPath) + check(err) + conjBase := strings.TrimSuffix(conjPath, ".conj") + entries, err := os.ReadDir(conjBase) + check(err) fmt.Println("Contents of", conjPath) - for _, f := range r.File { - printIndentedPath(f.Name) - if f.Name == "metadata.txt" { - rc, err := f.Open() - if err != nil { - log.Fatal(err) - } + for _, f := range entries { + if f.Name() == "metadata.txt" { + rc, err := os.Open(filepath.Join(conjBase, f.Name())) + check(err) defer rc.Close() fmt.Println("Contents of metadata.txt:") metadata, err := io.ReadAll(rc) game := parseGameInfo(metadata) - conjBase := strings.TrimSuffix(filepath.Base(conjPath), ".conj") game.ThumbnailPath = filepath.Join(conjBase, game.ThumbnailPath) game.ImagePath = filepath.Join(conjBase, game.ImagePath) games = append(games, game) - if err != nil { - log.Fatal(err) - } + check(err) } } + } }