-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
135 lines (110 loc) · 3.34 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package bridge
import (
"encoding/json"
"github.com/nokka/slashdiablo-launcher/config"
"github.com/nokka/slashdiablo-launcher/log"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
)
// ConfigBridge is the connection between QML and the Go config.
type ConfigBridge struct {
core.QObject
configPath string
// Dependencies.
config config.Service
logger log.Logger
// Models.
GameModel *core.QAbstractListModel `property:"games"`
// Properties.
_ string `property:"buildVersion"`
_ []string `property:"availableHDMods"`
_ []string `property:"availableMaphackMods"`
_ bool `property:"prerequisitesLoaded"`
_ bool `property:"prerequisitesError"`
// Slots.
_ func() `slot:"addGame"`
_ func(body string) bool `slot:"upsertGame"`
_ func(id string) `slot:"deleteGame"`
_ func() bool `slot:"persistGameModel"`
_ func() `slot:"getPrerequisites"`
_ func() `slot:"openConfigPath"`
}
// Connect will connect the QML signals to functions in Go.
func (c *ConfigBridge) Connect() {
c.ConnectUpsertGame(c.upsertGame)
c.ConnectAddGame(c.addGame)
c.ConnectDeleteGame(c.deleteGame)
c.ConnectPersistGameModel(c.persistGameModel)
c.ConnectGetPrerequisites(c.getPrerequisites)
c.ConnectOpenConfigPath(c.openConfigPath)
}
// addGame will add a game to the game model.
func (c *ConfigBridge) addGame() {
c.config.AddGame()
}
// upsertGame will update the game model.
func (c *ConfigBridge) upsertGame(body string) bool {
var request config.UpdateGameRequest
if err := json.Unmarshal([]byte(body), &request); err != nil {
c.logger.Error(err)
return false
}
err := c.config.UpsertGame(request)
if err != nil {
c.logger.Error(err)
return false
}
return true
}
// deleteGame will delete the given id from the game model.
func (c *ConfigBridge) deleteGame(id string) {
err := c.config.DeleteGame(id)
if err != nil {
c.logger.Error(err)
}
}
// persistGameModel will persist the current game model to the config.
func (c *ConfigBridge) persistGameModel() bool {
if err := c.config.PersistGameModel(); err != nil {
c.logger.Error(err)
return false
}
return true
}
// getPrerequisites will fetch all required config data.
func (c *ConfigBridge) getPrerequisites() {
go func() {
// Tell the UI that we're fetching prerequisites.
c.SetPrerequisitesLoaded(false)
c.SetPrerequisitesError(false)
mods, err := c.config.GetAvailableMods()
if err != nil {
c.SetPrerequisitesError(true)
c.logger.Error(err)
return
}
// Default option for no mod at all.
defaultMods := []string{config.ModVersionNone}
c.SetAvailableHDMods(append(defaultMods, mods.HD...))
c.SetAvailableMaphackMods(append(defaultMods, mods.Maphack...))
c.SetPrerequisitesLoaded(true)
}()
}
// openConfigPath will open the config path in the file explorer.
func (c *ConfigBridge) openConfigPath() {
gui.QDesktopServices_OpenUrl(core.QUrl_FromLocalFile(c.configPath))
}
// NewConfig returns a new config bridge with all dependencies set up.
func NewConfig(cs config.Service, gm *config.GameModel, configPath string, logger log.Logger) *ConfigBridge {
b := NewConfigBridge(nil)
b.configPath = configPath
// Setup dependencies.
b.config = cs
b.logger = logger
// Setup model.
b.SetGames(gm)
// Set initial state.
b.SetPrerequisitesLoaded(false)
b.SetPrerequisitesError(false)
return b
}