Skip to content

Commit 65dbeff

Browse files
authored
Custom titledb (#5)
* first draft for customTitleDB * Fix lint issues * Add example in config * Add missing part in index for banned theme Co-authored-by: DblK <admin@dblk.org>
1 parent 7638137 commit 65dbeff

File tree

8 files changed

+136
-34
lines changed

8 files changed

+136
-34
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[![golangci-lint](https://github.com/DblK/tinshop/actions/workflows/golangci-lint.yml/badge.svg?event=release)](https://github.com/DblK/tinshop/actions/workflows/golangci-lint.yml)
1010
[![GitHub go.mod Go version of a Go module](https://img.shields.io/github/go-mod/go-version/dblk/tinshop.svg)](https://github.com/dblk/tinshop)
11-
[![GoDoc reference example](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/dblk/tinshop/v0.0.1)
11+
[![GoDoc reference example](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/dblk/tinshop/v0.0.3)
1212
[![GoReportCard example](https://goreportcard.com/badge/github.com/dblk/tinshop)](https://goreportcard.com/report/github.com/dblk/tinshop)
1313
[![GitHub release](https://img.shields.io/github/release/dblk/tinshop.svg)](https://GitHub.com/dblk/tinshop/releases/)
1414

@@ -43,6 +43,7 @@ Here is the list of all main features so far:
4343
- [X] Auto-refresh configuration on file change
4444
- [X] Add the possibility to whitelist or blacklist a switch
4545
- [X] Add the possibility to ban theme
46+
- [X] You can specify custom titledb to be merged with official one
4647

4748
# Dev or build from source
4849

config.example.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,16 @@ security:
5151
# Block access to all switch present in this list
5252
# You can find the uid of a switch in the log upon access
5353
backlist:
54-
- NOACCESSNOACCESSNOACCESSNOACCESSNOACCESSNOACCESSNOACCESSNOACCESS
54+
- NOACCESSNOACCESSNOACCESSNOACCESSNOACCESSNOACCESSNOACCESSNOACCESS
55+
56+
# This section describe all custom title db to show up properly in tinfoil
57+
customTitledb:
58+
# Id of the entry
59+
"060000BADDAD0000":
60+
id: "050000BADDAD0000"
61+
name: "Tinfoil"
62+
region: "US"
63+
releaseDate: 20180801
64+
description: "Nintendo Switch Title Manager"
65+
size: 14000000
66+
iconUrl: ""

config/config.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ type security struct {
2727
// File holds all config information
2828
type File struct {
2929
rootShop string
30-
ShopHost string `mapstructure:"host"`
31-
ShopProtocol string `mapstructure:"protocol"`
32-
ShopPort int `mapstructure:"port"`
33-
Debug debug `mapstructure:"debug"`
34-
AllSources repository.Sources `mapstructure:"sources"`
35-
Name string `mapstructure:"name"`
36-
Security security `mapstructure:"security"`
30+
ShopHost string `mapstructure:"host"`
31+
ShopProtocol string `mapstructure:"protocol"`
32+
ShopPort int `mapstructure:"port"`
33+
Debug debug `mapstructure:"debug"`
34+
AllSources repository.Sources `mapstructure:"sources"`
35+
Name string `mapstructure:"name"`
36+
Security security `mapstructure:"security"`
37+
CustomTitleDB map[string]repository.CustomDBEntry `mapstructure:"customTitledb"`
3738
shopTemplateData repository.ShopTemplate
3839
}
3940

4041
var serverConfig File
41-
var allHooks []func(repository.Sources)
42+
var allHooks []func(repository.Config)
4243

4344
// LoadConfig handles viper under the hood
4445
func LoadConfig() {
@@ -71,7 +72,7 @@ func configChange() {
7172

7273
// Call all hooks
7374
for _, hook := range allHooks {
74-
hook(serverConfig.AllSources)
75+
hook(&serverConfig)
7576
}
7677
}
7778

@@ -134,8 +135,8 @@ func ComputeDefaultValues(config repository.Config) repository.Config {
134135
return config
135136
}
136137

137-
// HookOnSource Add hook function on change sources
138-
func HookOnSource(f func(repository.Sources)) {
138+
// AddHook Add hook function on change config
139+
func AddHook(f func(repository.Config)) {
139140
allHooks = append(allHooks, f)
140141
}
141142

@@ -179,6 +180,11 @@ func (cfg *File) Directories() []string {
179180
return cfg.AllSources.Directories
180181
}
181182

183+
// CustomDB returns the list of custom title db
184+
func (cfg *File) CustomDB() map[string]repository.CustomDBEntry {
185+
return cfg.CustomTitleDB
186+
}
187+
182188
// NfsShares returns the list of nfs sources
183189
func (cfg *File) NfsShares() []string {
184190
return cfg.AllSources.Nfs
@@ -235,10 +241,13 @@ func (cfg *File) isInWhiteList(uid string) bool {
235241

236242
// IsBannedTheme tells if the theme is banned or not
237243
func (cfg *File) IsBannedTheme(theme string) bool {
238-
fmt.Println(theme)
239-
fmt.Println(cfg.Security.BannedTheme)
240244
idxBannedTheme := utils.Search(len(cfg.Security.BannedTheme), func(index int) bool {
241245
return cfg.Security.BannedTheme[index] == theme
242246
})
243247
return idxBannedTheme != -1
244248
}
249+
250+
// BannedTheme returns all banned theme
251+
func (cfg *File) BannedTheme() []string {
252+
return cfg.Security.BannedTheme
253+
}

gamescollection/collection.go

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"io"
66
"log"
77
"os"
8+
"reflect"
9+
"strings"
810

911
"github.com/dblk/tinshop/config"
1012
"github.com/dblk/tinshop/repository"
1113
"github.com/dblk/tinshop/utils"
1214
)
1315

1416
var library map[string]map[string]interface{}
17+
var mergedLibrary map[string]interface{}
1518
var games repository.GameType
1619

1720
// Load ensure that necessary data is loaded
@@ -58,28 +61,56 @@ func loadTitlesLibrary() {
5861
func initGamesCollection() {
5962
// Build games object
6063
games.Success = "Welcome to your own shop!"
61-
games.Titledb = make(map[string]map[string]interface{})
64+
games.Titledb = make(map[string]interface{})
6265
games.Files = make([]interface{}, 0)
66+
games.ThemeBlackList = nil
6367
}
6468

65-
// Reset the collection of files
66-
func Reset(src repository.Sources) {
69+
// OnConfigUpdate the collection of files
70+
func OnConfigUpdate(cfg repository.Config) {
6771
initGamesCollection()
72+
73+
// Create merged library
74+
mergedLibrary = make(map[string]interface{})
75+
76+
// Copy library
77+
for key, entry := range library {
78+
gameID := strings.ToUpper(key)
79+
80+
mergedLibrary[gameID] = entry
81+
}
82+
83+
// Copy CustomDB
84+
for key, entry := range config.GetConfig().CustomDB() {
85+
gameID := strings.ToUpper(key)
86+
if mergedLibrary[gameID] != nil {
87+
log.Println("Duplicate customDB entry from official titledb (consider removing from configuration)", gameID)
88+
} else {
89+
mergedLibrary[gameID] = entry
90+
}
91+
}
92+
93+
// Check if blacklist entries
94+
if len(config.GetConfig().BannedTheme()) != 0 {
95+
games.ThemeBlackList = config.GetConfig().BannedTheme()
96+
} else {
97+
games.ThemeBlackList = nil
98+
}
6899
}
69100

70101
// Library returns the titledb library
71-
func Library() map[string]map[string]interface{} {
72-
return library
102+
func Library() map[string]interface{} {
103+
return mergedLibrary
73104
}
74105

75106
// HasGameIDInLibrary tells if we have gameID information in library
76107
func HasGameIDInLibrary(gameID string) bool {
77-
return library[gameID] != nil
108+
return Library()[gameID] != nil
78109
}
79110

80111
// IsBaseGame tells if the gameID is a base game or not
81112
func IsBaseGame(gameID string) bool {
82-
return library[gameID]["iconUrl"] != nil
113+
return Library()[gameID].(map[string]interface{})["iconUrl"] != nil
83114
}
84115

85116
// Games returns the games inside the library
@@ -91,8 +122,14 @@ func Games() repository.GameType {
91122
func CountGames() int {
92123
var uniqueGames int
93124
for _, entry := range games.Titledb {
94-
if entry["iconUrl"] != nil {
95-
uniqueGames++
125+
if reflect.TypeOf(entry).String() == "repository.CustomDBEntry" {
126+
if entry.(repository.CustomDBEntry).IconURL != "" {
127+
uniqueGames++
128+
}
129+
} else {
130+
if entry.(map[string]interface{})["iconUrl"] != nil {
131+
uniqueGames++
132+
}
96133
}
97134
}
98135
return uniqueGames
@@ -114,7 +151,7 @@ func AddNewGames(newGames []repository.FileDesc) {
114151
if games.Titledb[file.GameID] != nil && IsBaseGame(file.GameID) {
115152
log.Println("Already added id!", file.GameID, file.Path)
116153
} else {
117-
games.Titledb[file.GameID] = library[file.GameID]
154+
games.Titledb[file.GameID] = Library()[file.GameID]
118155
}
119156
} else {
120157
log.Println("Game not found in database!", file.GameInfo, file.Path)

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func initServer() {
7777
collection.Load()
7878

7979
// Loading config
80-
config.HookOnSource(collection.Reset)
81-
config.HookOnSource(sources.Load)
80+
config.AddHook(collection.OnConfigUpdate)
81+
config.AddHook(sources.OnConfigUpdate)
8282
config.LoadConfig()
8383
}
8484

mock_repository/mock_config.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

repository/interfaces.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type Config interface {
3333
IsBlacklisted(string) bool
3434
IsWhitelisted(string) bool
3535
IsBannedTheme(string) bool
36+
BannedTheme() []string
37+
38+
CustomDB() map[string]CustomDBEntry
3639
}
3740

3841
// ShopTemplate contains all variables used for shop template
@@ -61,7 +64,19 @@ type FileDesc struct {
6164

6265
// GameType structure
6366
type GameType struct {
64-
Success string `json:"success"`
65-
Titledb map[string]map[string]interface{} `json:"titledb"`
66-
Files []interface{} `json:"files"`
67+
Success string `json:"success"`
68+
Titledb map[string]interface{} `json:"titledb"`
69+
Files []interface{} `json:"files"`
70+
ThemeBlackList []string `json:"themeBlackList,omitempty"`
71+
}
72+
73+
// CustomDBEntry describe the various fields for entries
74+
type CustomDBEntry struct {
75+
ID string `mapstructure:"id" json:"id"`
76+
Name string `mapstructure:"name" json:"name"`
77+
Region string `mapstructure:"region" json:"region"`
78+
Size int `mapstructure:"size" json:"size"`
79+
ReleaseDate int `mapstructure:"releaseDate" json:"releaseDate"`
80+
Description string `mapstructure:"description" json:"description"`
81+
IconURL string `mapstructure:"iconUrl" json:"iconUrl"`
6782
}

sources/sources.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010

1111
var gameFiles []repository.FileDesc
1212

13-
// Load from all sources
14-
func Load(src repository.Sources) {
13+
// OnConfigUpdate from all sources
14+
func OnConfigUpdate(cfg repository.Config) {
1515
log.Println("Sources loading...")
1616
gameFiles = make([]repository.FileDesc, 0)
17-
loadGamesDirectories(src.Directories, len(src.Nfs) == 0)
18-
loadGamesNfsShares(src.Nfs)
17+
loadGamesDirectories(cfg.Directories(), len(cfg.NfsShares()) == 0)
18+
loadGamesNfsShares(cfg.NfsShares())
1919
}
2020

2121
// GetFiles returns all games files in various sources

0 commit comments

Comments
 (0)