Skip to content

Commit

Permalink
Bugfix. (#19)
Browse files Browse the repository at this point in the history
* Bugfix.

* Update model.go
  • Loading branch information
g026r authored Sep 28, 2024
1 parent b5d9a38 commit d24cc3f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cmd/playfix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
}

complete := false
out, err := os.CreateTemp("", "playtimes_*.bin")
out, err := os.CreateTemp(root, "playtimes_*.bin")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func LoadConfig() (Config, error) {
ShowAdd: false,
GenerateNew: true,
SaveUnmodified: false,
Overwrite: false,
Overwrite: true,
}
// FIXME: When compiling, use the program's dir rather than the cwd
// FIXME: When testing, use the cwd & remember to comment out the filepath.Dir call
Expand Down
6 changes: 3 additions & 3 deletions pkg/ui/menus.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ var (
menuItem{"Fix played times", libFix},
menuItem{"Back", back}}
thumbOptions = []list.Item{
menuItem{"Generate tmMissing thumbnails", tmMissing},
menuItem{"Regenerate tmSingle game", tmSingle},
menuItem{"Generate Missing thumbnails", tmMissing},
menuItem{"Regenerate Single game", tmSingle},
menuItem{"Regenerate full library", tmGenlib},
menuItem{"Prune orphaned thumbnails", tmPrune},
menuItem{"Generate complete system thumbnails", tmAll},
menuItem{"Back", back}}
configOptions = []list.Item{
menuItem{"Remove thumbnail when removing game", cfgRmThumbs},
menuItem{"Generate new thumbnail when editing game", cfgGenNew},
menuItem{"Overwrite original files on save", cfgOverwrite},
//menuItem{"Overwrite original files on save", cfgOverwrite},
menuItem{"Always save _thumbs.bin files, even if unmodified", cfgUnmodified},
//menuItem{"Show advanced library editing fields " + italic.Render("(Experimental)"), cfgAdvEdit},
//menuItem{"Show 'Add to Library' " + italic.Render("(Experimental)"), cfgShowAdd},
Expand Down
85 changes: 43 additions & 42 deletions pkg/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,14 @@ func (m *Model) initSystem() tea.Msg {
}
m.playTimes = p

if len(m.entries) != len(m.playTimes) {
if len(m.entries) < len(m.playTimes) { // We can have more entries than play times, but not the other way around.
return errMsg{fmt.Errorf("entry count mismatch between list.bin [%d] & playtimes.bin [%d]", len(m.entries), len(m.playTimes)), true}
}
for _, e := range m.entries { // Check to make certain that every entry has a play time entry
if _, ok := m.playTimes[e.Sig]; !ok { // entry without a play time
return errMsg{fmt.Errorf("entry mismatch between list.bin & playtimes.bin"), true}
}
}

t, err := io.LoadThumbs(m.rootDir)
if err != nil {
Expand All @@ -259,19 +264,42 @@ func (m *Model) initSystem() tea.Msg {

// save is the opposite of init: save our data to disk
func (m *Model) save() tea.Msg {
var root string
var err error
if m.Overwrite {
root, err = util.GetRoot()
if err != nil {
return errMsg{err, true}
}
} else {
// If we're not overwriting in place, get the working dir & create all our directories if they don't exist
wd, err := os.Getwd()
if err != nil {
return errMsg{err, true}
}
root = fmt.Sprintf("%s/pocket-toolkit", wd)
_ = os.Mkdir(root, os.ModePerm)
_ = os.Mkdir(fmt.Sprintf("%s/System", root), os.ModePerm)
_ = os.Mkdir(fmt.Sprintf("%s/System/Library", root), os.ModePerm)
_ = os.Mkdir(fmt.Sprintf("%s/System/Library/Images", root), os.ModePerm)
if err := os.Mkdir(fmt.Sprintf("%s/System/Played Games", root), os.ModePerm); err != nil && !os.IsExist(err) {
return errMsg{err, true} // Only going to check this final one for errors on the basis of "if it failed, the others did as well"
}
}

success := false
tmpList, err := os.CreateTemp("", "list.bin_*")
tmpList, err := os.CreateTemp(root, "list.bin_*")
if err != nil {
return errMsg{err, true}
}
tmpPlaytimes, err := os.CreateTemp("", "playtimes.bin_*")
tmpPlaytimes, err := os.CreateTemp(root, "playtimes.bin_*")
if err != nil {
return errMsg{err, true}
}
tmpThumbs := make(map[models.System]*os.File)
for k, v := range m.thumbnails {
if v.Modified || m.SaveUnmodified {
tmpThumbs[k], err = os.CreateTemp("", fmt.Sprintf("%s_thumbs.bin_*", strings.ToLower(k.String())))
tmpThumbs[k], err = os.CreateTemp(root, fmt.Sprintf("%s_thumbs.bin_*", strings.ToLower(k.String())))
if err != nil {
return errMsg{err, true}
}
Expand All @@ -292,30 +320,6 @@ func (m *Model) save() tea.Msg {
_ = v.Close()
}
if success {
var root string
var err error
if m.Overwrite {
root, err = util.GetRoot()
if err != nil {
log.Fatal(errorStyle.Render(err.Error()))
}
} else {
// If we're not overwriting in place, get the working dir & create all our directories if they don't exist
wd, err := os.Getwd()
if err != nil {
log.Fatal(errorStyle.Render(err.Error()))
}
root = fmt.Sprintf("%s/pocket-toolkit", wd)
_ = os.Mkdir(root, os.ModePerm)
_ = os.Mkdir(fmt.Sprintf("%s/System", root), os.ModePerm)
if len(tmpThumbs) > 0 {
_ = os.Mkdir(fmt.Sprintf("%s/System/Library", root), os.ModePerm)
_ = os.Mkdir(fmt.Sprintf("%s/System/Library/Images", root), os.ModePerm)
}
if err := os.Mkdir(fmt.Sprintf("%s/System/Played Games", root), os.ModePerm); err != nil && !os.IsExist(err) {
log.Fatal(errorStyle.Render(err.Error())) // Only going to check this final one for errors on the basis of "if it failed, the others did as well"
}
}
if err := os.Rename(tmpList.Name(), fmt.Sprintf("%s/System/Played Games/list.bin", root)); err != nil {
log.Fatal(errorStyle.Render(err.Error()))
}
Expand All @@ -339,7 +343,6 @@ func (m *Model) save() tea.Msg {

ctr := 0.0
tick := make(chan any)
defer close(tick)
total := float64(len(m.entries))
for _, v := range m.thumbnails {
if v.Modified {
Expand Down Expand Up @@ -530,7 +533,7 @@ func (m *Model) regenLib() tea.Msg {
return updateMsg{}
}

// genSingle generates a tmSingle thumbnail entry & then either updates or inserts it into the list of thumbnails
// genSingle generates a Single thumbnail entry & then either updates or inserts it into the list of thumbnails
func (m *Model) genSingle(e models.Entry) tea.Cmd {
return func() tea.Msg {
m.percent = 0.0
Expand Down Expand Up @@ -754,16 +757,14 @@ func (m *Model) saveEntry() (tea.Model, tea.Cmd) {
e.Sig, _ = util.HexStringTransform(m.gameInput[sig].Value())
e.Magic, _ = util.HexStringTransform(m.gameInput[magic].Value())

p := models.PlayTime{}
if stored, ok := m.playTimes[e.Sig]; ok {
p = stored
// We only want to edit the played time *if* we're editing or we're adding & a played time doesn't yet exist
if p, ok := m.playTimes[e.Sig]; !ok || m.Peek() == EditScreen {
t, _ := parseDate(m.gameInput[added].Value())
p.Added = uint32(t.Unix())
p.Played = parsePlayTime(m.gameInput[play].Value())
p.System = e.System
m.playTimes[e.Sig] = p
}
t, _ := parseDate(m.gameInput[added].Value())
p.Added = uint32(t.Unix())
p.Played = parsePlayTime(m.gameInput[play].Value())
p.System = e.System

m.playTimes[e.Sig] = p

if m.Peek() == EditScreen {
m.entries[m.gameList.Index()] = e
Expand Down Expand Up @@ -843,15 +844,15 @@ func (m *Model) processMenuItem(key menuKey) (*Model, tea.Cmd) {
case tmMissing:
m.Push(Waiting)
m.percent = 0.0
m.wait = "Generating tmMissing thumbnails for library"
m.wait = "Generating Missing thumbnails for library"
return m, tea.Batch(m.genMissing, tickCmd())
case tmSingle:
m.gameList = generateGameList(m.gameList, m.entries, "Main > Library > Generate Thumbnail", m.mainMenu.Width(), m.mainMenu.Height())
m.Push(GenerateList)
case tmGenlib:
m.Push(Waiting)
m.percent = 0.0
m.wait = "Regenerating tmAll thumbnails for library"
m.wait = "Regenerating All thumbnails for library"
return m, tea.Batch(m.regenLib, tickCmd())
case tmPrune:
m.Push(Waiting)
Expand All @@ -861,7 +862,7 @@ func (m *Model) processMenuItem(key menuKey) (*Model, tea.Cmd) {
case tmAll:
m.Push(Waiting)
m.percent = 0.0
m.wait = "Generating thumbnails for tmAll games in the Images folder. This may take a while."
m.wait = "Generating thumbnails for All games in the Images folder. This may take a while."
return m, tea.Batch(m.genFull, tickCmd())
case cfgShowAdd, cfgAdvEdit, cfgRmThumbs, cfgGenNew, cfgOverwrite, cfgUnmodified:
return m.configChange(key)
Expand Down

0 comments on commit d24cc3f

Please sign in to comment.