diff --git a/lib/provider/provider.go b/lib/provider/provider.go index b7036da..c203830 100644 --- a/lib/provider/provider.go +++ b/lib/provider/provider.go @@ -73,6 +73,7 @@ func GetOrSetEnvKey(key string, defaultValue string) string { func extractZipToSiblingFolder(zipPath string) error { // Determine destination folder name (same name as zip file, without .zip) zipBase := strings.TrimSuffix(filepath.Base(zipPath), ".conj") + zipBase = strings.TrimSuffix(filepath.Base(zipBase), ".zip") destDir := filepath.Join(filepath.Dir(zipPath), zipBase) // Delete destination folder if it exists @@ -147,7 +148,7 @@ func extractZipsInFolder(folder string) error { if entry.IsDir() { continue } - if strings.HasSuffix(strings.ToLower(entry.Name()), ".conj") { + if strings.HasSuffix(strings.ToLower(entry.Name()), ".zip") { zipPath := filepath.Join(folder, entry.Name()) fmt.Println("Extracting:", zipPath) if err := extractZipToSiblingFolder(zipPath); err != nil { @@ -160,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) @@ -174,44 +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" { - 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) - } + conjPath := filepath.Join(gamePath, e.Name()) + 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 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) + 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) } } + } }