Skip to content

Commit

Permalink
Add generate missing
Browse files Browse the repository at this point in the history
Set modified = true when regenerating a single game
Clone the thumbnails & reset them in the paged entries
  • Loading branch information
g026r committed Sep 14, 2024
1 parent 8745e56 commit c25c770
Showing 1 changed file with 66 additions and 7 deletions.
73 changes: 66 additions & 7 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ func (a *Application) libraryMenu() error {

func (a *Application) thumbnailMenu() error {
menu := gocliselect.NewMenu("Edit Thumbnails", false)
menu.AddItem("Generate Missing Thumbnails", "missing")
menu.AddItem("Regenerate Game Thumbnail", "single")
menu.AddItem("Regenerate User Library", "library")
menu.AddItem("Regenerate Complete Library", "library")
//menu.AddItem("Remove Thumbnail", "rm") // TODO: Maybe? Maybe not? Has some issues around mapping the thumbnail CRC to a name
menu.AddItem("Prune Thumbnails", "prune")
menu.AddItem("Generate Complete System Thumbnails", "all")
Expand All @@ -112,6 +113,10 @@ func (a *Application) thumbnailMenu() error {
for {
util.ClearScreen()
switch menu.Display() {
case "missing":
if err := a.regenMissing(); err != nil {
return err
}
case "single":
if err := a.regenSingle(); err != nil {
return err
Expand Down Expand Up @@ -177,7 +182,8 @@ func x(setting bool) string {
}

func (a *Application) pagedEntries(title string, f func(i int) error) error {
clone := slices.Clone(a.Entries) // For cancel
entriesClone := slices.Clone(a.Entries) // For cancel
thumbsClone := maps.Clone(a.Thumbs)
var start, pos int
var x string
for {
Expand All @@ -201,15 +207,19 @@ func (a *Application) pagedEntries(title string, f func(i int) error) error {
case "done":
return nil
case "":
a.Entries = clone // Restore the entries to the original copy
// Restore the original
a.Entries = entriesClone
a.Thumbs = thumbsClone
return nil
default:
i, err := strconv.Atoi(x)
if err != nil {
return err
}
if err := f(i); err != nil {
a.Entries = clone // Restore the original
// Restore the original
a.Entries = entriesClone
a.Thumbs = thumbsClone
return err
}
}
Expand Down Expand Up @@ -456,23 +466,55 @@ func (a *Application) regenSingle() error {
return err
}

t := a.Thumbs[sys]
// Thumbnails aren't stored in the same order as entries
found := false
for j, old := range a.Thumbs[sys].Images {
for j, old := range t.Images {
if old.Crc32 == img.Crc32 {
a.Thumbs[sys].Images[j] = img
t.Images[j] = img
found = true
break
}
}
if !found { // Shouldn't happen. But append if it does
t := a.Thumbs[sys]
t.Images = append(t.Images, img)
}
t.Modified = true
a.Thumbs[sys] = t

return nil
})
}

func (a *Application) regenMissing() error {
clone := maps.Clone(a.Thumbs)

util.ClearScreen()
fmt.Println(goterm.Bold("Regenerating thumbnails. This may take a while..."))

for _, e := range a.Entries {
sys := e.System.ThumbFile()

if slices.ContainsFunc(a.Thumbs[sys].Images, func(image model.Image) bool {
return image.Crc32 == e.Crc32
}) {
continue // Already exists. Just continue
}

img, err := model.GenerateThumbnail(a.RootDir, sys, e.Crc32)
if err != nil {
a.Thumbs = clone
return err
}
i := a.Thumbs[sys]
i.Images = append(i.Images, img)
i.Modified = true
a.Thumbs[sys] = i
}

return nil
}

func (a *Application) regenerate() error {
clone := maps.Clone(a.Thumbs)

Expand Down Expand Up @@ -575,6 +617,23 @@ func (a *Application) generateAll() error {
return nil
}

// TODO: How to deal with this
//func (a *Application) removeThumb() error {
// clone := maps.Clone(a.Thumbs)
//
// sys := gocliselect.NewMenu("System:", false)
// sys.AddItem("Game Boy / Game Boy Color", "GB")
// sys.AddItem("Game Boy Advance", "GBA")
// sys.AddItem("Game Gear / Sega Master System", "GG")
// sys.AddItem("Neo Geo Pocket / Neo Geo Pocket Color", "NGP")
// sys.AddItem("TurboGrafx 16", "PCE")
// sys.AddItem("Atari Lynx", "Lynx")
// system := sys.Display()
// if system == "" { // ESC or Ctrl-C pressed
// return nil
// }
//}

// prune removes entries from the thumbnails files that are no longer associated with any library entry
// If you have a very large library or very large thumbnail files, this may take a while.
func (a *Application) prune() {
Expand Down

0 comments on commit c25c770

Please sign in to comment.