-
Notifications
You must be signed in to change notification settings - Fork 344
/
game_library.go
301 lines (261 loc) · 6.34 KB
/
game_library.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package games
import (
"crypto/md5"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
)
// Config is an external configuration
type Config struct {
// some directory which is going to be
// the root folder for the library
BasePath string
// a list of supported file extensions
Supported []string
// a list of ignored words in the files
Ignored []string
// print some additional info
Verbose bool
// enable directory changes watch
WatchMode bool
}
// libConf is an optimized internal library configuration
type libConf struct {
path string
supported map[string]bool
ignored map[string]bool
verbose bool
watchMode bool
}
type library struct {
config libConf
// indicates repo source existence
hasSource bool
// scan time
lastScanDuration time.Duration
// library entries
// !should be a tree-based structure
// game name -> game meta
// games with duplicate names are merged
games map[string]GameMetadata
// to restrict parallel execution
// or throttling
// !CAS would be better
mu sync.Mutex
isScanning bool
isScanningDelayed bool
}
type GameLibrary interface {
GetAll() []GameMetadata
FindGameByName(name string) GameMetadata
Scan()
}
type FileExtensionWhitelist interface {
GetSupportedExtensions() []string
}
type GameMetadata struct {
uid string
// the display name of the game
Name string
// the game file extension (e.g. nes, n64)
Type string
Base string
// the game path relative to the library base path
Path string
}
func (c Config) GetSupportedExtensions() []string { return c.Supported }
func NewLib(conf Config) GameLibrary { return NewLibWhitelisted(conf, conf) }
func NewLibWhitelisted(conf Config, filter FileExtensionWhitelist) GameLibrary {
hasSource := true
dir, err := filepath.Abs(conf.BasePath)
if err != nil {
hasSource = false
log.Printf("[lib] invalid source: %v (%v)\n", conf.BasePath, err)
}
if len(conf.Supported) == 0 {
conf.Supported = filter.GetSupportedExtensions()
}
library := &library{
config: libConf{
path: dir,
supported: toMap(conf.Supported),
ignored: toMap(conf.Ignored),
verbose: conf.Verbose,
watchMode: conf.WatchMode,
},
mu: sync.Mutex{},
games: map[string]GameMetadata{},
hasSource: hasSource,
}
if conf.WatchMode && hasSource {
go library.watch()
}
return library
}
func (lib *library) GetAll() []GameMetadata {
var res []GameMetadata
for _, value := range lib.games {
res = append(res, value)
}
return res
}
// FindGameByName returns some game info with its full filepath
func (lib *library) FindGameByName(name string) GameMetadata {
var game GameMetadata
if val, ok := lib.games[name]; ok {
val.Base = lib.config.path
return val
}
return game
}
func (lib *library) Scan() {
if !lib.hasSource {
log.Printf("[lib] scan... skipped (no source)\n")
return
}
// scan throttling
lib.mu.Lock()
if lib.isScanning {
defer lib.mu.Unlock()
lib.isScanningDelayed = true
log.Printf("[lib] scan... delayed\n")
return
}
lib.isScanning = true
lib.mu.Unlock()
log.Printf("[lib] scan... started\n")
start := time.Now()
var games []GameMetadata
dir := lib.config.path
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info != nil && !info.IsDir() && lib.isFileExtensionSupported(path) {
meta := getMetadata(path, dir)
meta.uid = hash(path)
if !lib.config.ignored[meta.Name] {
games = append(games, meta)
}
}
return nil
})
if err != nil {
log.Printf("[lib] scan error with %q: %v\n", dir, err)
}
if len(games) > 0 {
lib.set(games)
}
lib.lastScanDuration = time.Since(start)
if lib.config.verbose {
lib.dumpLibrary()
}
// run scan again if delayed
lib.mu.Lock()
defer lib.mu.Unlock()
lib.isScanning = false
if lib.isScanningDelayed {
lib.isScanningDelayed = false
go lib.Scan()
}
log.Printf("[lib] scan... completed\n")
}
// watch adds the ability to rescan the entire library
// during filesystem changes in a watched directory.
// !to add incremental library change
func (lib *library) watch() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("[lib] watcher has failed: %v", err)
return
}
done := make(chan bool)
go func(repo *library) {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op == fsnotify.Create || event.Op == fsnotify.Remove {
// !to try to add the proper file/dir add/remove scan logic
// which is tricky
repo.Scan()
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}(lib)
if err = watcher.Add(lib.config.path); err != nil {
log.Printf("[lib] watch error %v", err)
}
<-done
_ = watcher.Close()
log.Printf("[lib] the watch has ended\n")
}
func (lib *library) set(games []GameMetadata) {
res := make(map[string]GameMetadata)
for _, value := range games {
res[value.Name] = value
}
lib.games = res
}
func (lib *library) isFileExtensionSupported(path string) bool {
ext := filepath.Ext(path)
if ext == "" {
return false
}
return lib.config.supported[ext[1:]]
}
// getMetadata returns game info from a path
func getMetadata(path string, basePath string) GameMetadata {
name := filepath.Base(path)
ext := filepath.Ext(name)
relPath, _ := filepath.Rel(basePath, path)
return GameMetadata{
Name: strings.TrimSuffix(name, ext),
Type: ext[1:],
Path: relPath,
}
}
// dumpLibrary printouts the current library snapshot of games
func (lib *library) dumpLibrary() {
var gameList strings.Builder
for _, game := range lib.games {
gameList.WriteString(" " + game.Name + " (" + game.Path + ")" + "\n")
}
log.Printf("\n"+
"--------------------------------------------\n"+
"--- The Library of ROMs ---\n"+
"--------------------------------------------\n"+
"%v"+
"--------------------------------------------\n"+
"--- ROMs: %03d %26s ---\n"+
"--------------------------------------------\n",
gameList.String(), len(lib.games), lib.lastScanDuration)
}
// hash makes an MD5 hash of the string
func hash(str string) string {
h := md5.New()
_, err := io.WriteString(h, str)
if err != nil {
return ""
}
return fmt.Sprintf("%x", h.Sum(nil))
}
func toMap(list []string) map[string]bool {
res := make(map[string]bool)
for _, s := range list {
res[s] = true
}
return res
}