Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
KiameV committed Dec 21, 2022
1 parent 3cd3086 commit 32a7273
Show file tree
Hide file tree
Showing 23 changed files with 166 additions and 158 deletions.
38 changes: 14 additions & 24 deletions actions/installAction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/kiamev/moogle-mod-manager/config"
"github.com/kiamev/moogle-mod-manager/mods"
"github.com/kiamev/moogle-mod-manager/ui/util"
"github.com/kiamev/moogle-mod-manager/ui/util/working"
"sync"
"time"
)
Expand All @@ -17,19 +18,9 @@ type (
Run() error
}
action struct {
done Done
state *steps.State
steps []steps.Step
workingDialog WorkingDialog
}
WorkingDialog struct {
Show func()
Hide func()
}
Params struct {
Game config.GameDef
Mod mods.TrackedMod
WorkingDialog WorkingDialog
done Done
state *steps.State
steps []steps.Step
}
ActionKind byte
)
Expand Down Expand Up @@ -79,7 +70,7 @@ var (
mutex = sync.Mutex{}
)

func New(kind ActionKind, params Params, done Done) (Action, error) {
func New(kind ActionKind, game config.GameDef, mod mods.TrackedMod, done Done) (Action, error) {
mutex.Lock()
defer mutex.Unlock()
if running {
Expand All @@ -92,17 +83,16 @@ func New(kind ActionKind, params Params, done Done) (Action, error) {
)
switch kind {
case Install:
s, err = createInstallSteps(params.Game, params.Mod)
s, err = createInstallSteps(game, mod)
case Uninstall:
s, err = createUninstallSteps(params.Game, params.Mod)
s, err = createUninstallSteps(game, mod)
case Update:
s, err = createUpdateSteps(params.Game, params.Mod)
s, err = createUpdateSteps(game, mod)
}
return &action{
done: done,
state: steps.NewState(params.Game, params.Mod),
steps: s,
workingDialog: params.WorkingDialog,
done: done,
state: steps.NewState(game, mod),
steps: s,
}, err
}

Expand Down Expand Up @@ -164,7 +154,7 @@ func (a action) Run() (err error) {

func (a action) run() {
defer func() {
a.workingDialog.Hide()
working.HideDialog()
mutex.Lock()
running = false
mutex.Unlock()
Expand All @@ -181,13 +171,13 @@ func (a action) run() {
)
for _, step := range a.steps {
if result, err = step(a.state); err != nil {
a.workingDialog.Hide()
working.HideDialog()
util.ShowErrorLong(err)
return
} else if result == mods.Cancel {
break
} else if result == mods.Working {
a.workingDialog.Show()
working.ShowDialog()
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions actions/steps/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/kiamev/moogle-mod-manager/ui/confirm"
uic "github.com/kiamev/moogle-mod-manager/ui/conflicts"
ui "github.com/kiamev/moogle-mod-manager/ui/state"
"github.com/kiamev/moogle-mod-manager/util"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -106,7 +107,7 @@ func PreDownload(state *State) (result mods.Result, err error) {
modPath := filepath.Join(config.Get().GetModsFullPath(state.Game), mod.ID().AsDir())
if err = ui.GetScreen(ui.ConfigInstaller).(ci.ConfigInstaller).Setup(mod, modPath, func(r mods.Result, ti []*mods.ToInstall) error {
result = r
if len(ti) > 0 {
if r == mods.Ok && len(ti) > 0 {
state.ToInstall = append(state.ToInstall, ti...)
}
wg.Done()
Expand All @@ -118,9 +119,11 @@ func PreDownload(state *State) (result mods.Result, err error) {
ui.ShowScreen(ui.ConfigInstaller)
wg.Wait()
time.Sleep(100 * time.Millisecond)

}

if result == mods.Cancel || result == mods.Error {
return
}
if len(state.ToInstall) == 0 {
return mods.Error, errors.New("no files to install")
}
Expand Down Expand Up @@ -281,14 +284,14 @@ func Install(state *State) (mods.Result, error) {
if err = os.MkdirAll(filepath.Dir(absBackup), 0755); err != nil {
return mods.Error, err
}
if err = os.Rename(ti.AbsoluteTo, absBackup); err != nil {
if err = util.MoveFile(ti.AbsoluteTo, absBackup); err != nil {
return mods.Error, err
}
}
}

// Install the file
if err = os.Rename(ti.AbsoluteFrom, ti.AbsoluteTo); err != nil {
if err = util.MoveFile(ti.AbsoluteFrom, ti.AbsoluteTo); err != nil {
return mods.Error, err
}
files.SetFiles(state.Game, state.Mod.ID(), ti.AbsoluteTo)
Expand Down Expand Up @@ -330,7 +333,7 @@ func UninstallMove(state *State) (mods.Result, error) {

absBackup := filepath.Join(backupDir, rel)
if _, err = os.Stat(absBackup); err == nil {
if err = os.Rename(absBackup, f); err != nil {
if err = util.MoveFile(absBackup, f); err != nil {
return mods.Error, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion browser/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
Version = "v0.6.5"
Version = "v0.7.0"

tagUrl = `https://api.github.com/repos/KiameV/ffprModManager/tags`
relUrl = `https://github.com/KiameV/ffprModManager/releases/%s`
Expand Down
3 changes: 3 additions & 0 deletions discover/discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func GetModsAsLookup(game config.GameDef) (lookup mods.ModLookup[*mods.Mod], err
found.Mod().Merge(*m)
}
}
lookup.RemoveConditionally(func(m *mods.Mod) bool {
return m.Mod().Hide
})
if game == nil {
utilLookup = lookup
} else {
Expand Down
14 changes: 7 additions & 7 deletions discover/remote/nexus/nexus.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ func (c *client) GetFromUrl(url string) (found bool, mod *mods.Mod, err error) {

func (c *client) GetNewestMods(game config.GameDef, lastID int) (result []*mods.Mod, err error) {
var (
b []byte
path = game.Remote().Nexus.Path
nDls fileParent
mod *mods.Mod
found bool
b []byte
path = game.Remote().Nexus.Path
nDls fileParent
mod *mods.Mod
include bool
)
if b, err = sendRequest(fmt.Sprintf(nexusApiNewestModsUrl, path)); err != nil {
return
Expand All @@ -127,9 +127,9 @@ func (c *client) GetNewestMods(game config.GameDef, lastID int) (result []*mods.
if nDls, err = getDownloads(path, fmt.Sprintf("%d", nMod.ModID)); err != nil {
return
}
if found, mod, err = toMod(nMod, nDls.Files); err != nil {
if include, mod, err = toMod(nMod, nDls.Files); err != nil {
return
} else if found {
} else if !include {
continue
}
result = append(result, mod)
Expand Down
2 changes: 1 addition & 1 deletion discover/repo/committer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *repoClient) Submit() (url string, err error) {
err = errors.New("multi-game mods must be hosted")
return
}
file = util.CreateFileName(string(c.mod.ModID))
file = util.CreateFileName(c.mod.ModID.AsDir())
file = rd.removeFilePrefixes(file)
file = filepath.Join(rd.repoDir(Author), "utilities", file)
} else {
Expand Down
5 changes: 3 additions & 2 deletions files/movers/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package movers

import (
"fmt"
"github.com/kiamev/moogle-mod-manager/util"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func (*directMover) moveFiles(files []string, from, destination string) (moved [
return
}
// Move the file to the destination directory
if err = os.Rename(f, filepath.Join(destination, relPath)); err != nil {
if err = util.MoveFile(f, filepath.Join(destination, relPath)); err != nil {
return
}
moved = append(moved, f)
Expand Down Expand Up @@ -82,7 +83,7 @@ func (*directMover) backupFiles(files []string, from, destination, backupDir str
orig = filepath.Join(destination, relPath)
if _, err = os.Stat(orig); err == nil {
// Move the file to the backup directory
if err = os.Rename(orig, filepath.Join(backupDir, relPath)); err != nil {
if err = util.MoveFile(orig, filepath.Join(backupDir, relPath)); err != nil {
return
}
backedUp = append(backedUp, relPath)
Expand Down
12 changes: 12 additions & 0 deletions mods/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type (
Has(m T) bool
Len() int
Remove(m T)
RemoveConditionally(f func(t T) bool)
Set(m T)
}
// ModLookupConc is a public for serialization purposes.
Expand Down Expand Up @@ -66,6 +67,17 @@ func (l *ModLookupConc[T]) GetByID(modID ModID) (found T, ok bool) {
func (l *ModLookupConc[T]) Remove(m T) {
delete(l.Lookup, l.newLookupID(m))
}
func (l *ModLookupConc[T]) RemoveConditionally(f func(m T) bool) {
var toRemove []lookupID
for k, m := range l.Lookup {
if f(m) {
toRemove = append(toRemove, k)
}
}
for _, k := range toRemove {
delete(l.Lookup, k)
}
}

func (l *ModLookupConc[T]) Len() int {
return len(l.Lookup)
Expand Down
14 changes: 12 additions & 2 deletions mods/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (m *Mod) BranchName() string {
}

func (m *Mod) Save(to string) error {
return util.SaveToFile(to, m.ModDef)
return util.SaveToFile(to, m.ModDef, '\n')
}

type Preview struct {
Expand Down Expand Up @@ -251,6 +251,14 @@ func (m *Mod) Validate() string {
if m.Name == "" {
sb.WriteString("Name is required\n")
}

if m.Hide {
if m.ModKind.Kind == Hosted {
sb.WriteString("Cannot Hide hosted mods\n")
}
return sb.String()
}

if m.Version == "" {
sb.WriteString("Version is required\n")
}
Expand Down Expand Up @@ -289,7 +297,9 @@ func (m *Mod) Validate() string {
// sb.WriteString(fmt.Sprintf("Downloadables [%s]'s name cannot contain spaces\n"))
//}
if kind == Hosted {
if d.Hosted == nil {
if m.ModKind.SubKind == nil || m.ModKind.SubKind.Is(HostedBlank) {
sb.WriteString("Kind is required\n")
} else if d.Hosted == nil {
sb.WriteString(fmt.Sprintf("Downloadables [%s]'s Hosted is required\n", d.Name))
} else {
if len(d.Hosted.Sources) == 0 {
Expand Down
35 changes: 10 additions & 25 deletions ui/local/enableBinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@ import (
type (
enableBind struct {
binding.Bool
parent *localUI
tm mods.TrackedMod
start func() bool
showWorking func()
hideWorking func()
parent *localUI
tm mods.TrackedMod
start func() bool
//done mods.DoneCallback
}
)

func newEnableBind(parent *localUI, tm mods.TrackedMod, start func() bool, showWorking func(), hideWorking func() /*, done mods.DoneCallback*/) *enableBind {
func newEnableBind(parent *localUI, tm mods.TrackedMod, start func() bool /*, done mods.DoneCallback*/) *enableBind {
var (
b = &enableBind{
parent: parent,
Bool: binding.NewBool(),
tm: tm,
start: start,
showWorking: showWorking,
hideWorking: hideWorking,
parent: parent,
Bool: binding.NewBool(),
tm: tm,
start: start,
}
)
_ = b.Set(tm.Enabled())
Expand All @@ -49,7 +45,7 @@ func (b *enableBind) DataChanged() {
}
if isChecked {
// Enable
if action, err = actions.New(actions.Install, b.newActionParams(), b.ActionDone); err != nil {
if action, err = actions.New(actions.Install, state.CurrentGame, b.tm, b.ActionDone); err != nil {
util.ShowErrorLong(err)
_ = b.Set(false)
} else if err = action.Run(); err != nil {
Expand All @@ -58,7 +54,7 @@ func (b *enableBind) DataChanged() {
}
} else {
// Disable
if action, err = actions.New(actions.Uninstall, b.newActionParams(), b.ActionDone); err != nil {
if action, err = actions.New(actions.Uninstall, state.CurrentGame, b.tm, b.ActionDone); err != nil {
util.ShowErrorLong(err)
_ = b.Set(true)
} else if err = action.Run(); err != nil {
Expand All @@ -69,17 +65,6 @@ func (b *enableBind) DataChanged() {
}
}

func (b *enableBind) newActionParams() actions.Params {
return actions.Params{
Game: state.CurrentGame,
Mod: b.tm,
WorkingDialog: actions.WorkingDialog{
Show: b.showWorking,
Hide: b.hideWorking,
},
}
}

func (b *enableBind) ActionDone() {
b.parent.ModList.Refresh()
}
Loading

0 comments on commit 32a7273

Please sign in to comment.