/
load.go
193 lines (167 loc) · 4.36 KB
/
load.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
// Loads all spell data that it can fetch. Checks if API spells are cached
// and fetches them if available. If cached content doesn't exist, it fetches
// them from an API and caches them. Then it checks for user files at the
// specific location and merges them with API spells.
// KNOWN ISSUES:
// - if the spell parsing from local files fails, it will panic
// - if fetching from API is interrupted, it will panic
func loadAllData(spellChan chan []Spell, statusChan chan string) {
defer func() {
close(spellChan)
statusChan <- "Done"
}()
statusChan <- "Loading spells..."
config, err := os.UserConfigDir()
if err != nil {
panic(err)
}
readyDir(config + "/banshie/cache")
readyDir(config + "/banshie/local")
// get the spell API data, cached or fetched
// TODO: make this run in a goroutine
dataAPI, err := loadAPISpells(config+"/banshie/cache/spells.json", statusChan)
if err != nil {
// should be handled via errchannel
panic(err)
}
// if local custom spells file doesnt exist, return only API spells
if !checkFile(config + "/banshie/local/spells.json") {
spellChan <- *dataAPI
return
}
statusChan <- "Loading custom spells..."
var userData []Spell
err = loadJSONFromFile(config+"/banshie/local/spells.json", &userData)
if err != nil {
// should be handled via errchannel
panic(err)
}
allSpells := mergeMultipleSources(&userData, dataAPI)
spellChan <- *allSpells
}
// Load API spells, attempt to load cached data and if cache doesn't exist,
// fetch the spells from the API
func loadAPISpells(fileStr string, statusChan chan string) (*[]Spell, error) {
// if the file exists, load it
if checkFile(fileStr) {
statusChan <- "Loading cached spells..."
var data []Spell
if err := loadJSONFromFile(fileStr, &data); err != nil {
return nil, err
}
return &data, nil
}
statusChan <- "Fetching spells..."
fmt.Println("spells.json doesnt exist")
// fetch the spells
data, err := fetchSpells()
if err != nil {
return nil, err
}
// marshall the fetched spells in human-readable format
cached, err := json.MarshalIndent(data, "", " ")
if err != nil {
panic(err)
}
// create the files where spells will be cached. It should not be created
// because if it was, then spells would be read from it instead, so there
// should be no error
file, err := os.Create(fileStr)
if err != nil {
panic(err)
}
defer file.Close()
// cache the spells
file.Write(cached)
return &data, nil
}
// Check if file/dir exists, true if does, false if doesnt
func checkFile(dir string) bool {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
return false
}
if !os.IsNotExist(err) && err != nil {
panic(err)
}
return true
}
// check if dir exists, make it if it doesnt
func readyDir(dir string) {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
fmt.Println(dir, "does not exist, creating...")
os.MkdirAll(dir, 0755)
}
if !os.IsNotExist(err) && err != nil {
panic(err)
}
}
// Load JSON from file
func loadJSONFromFile(file string, dest interface{}) error {
cachedFile, err := os.Open(file)
if err != nil {
panic(err)
}
stuff, err := ioutil.ReadAll(cachedFile)
if err != nil {
panic(err)
}
err = json.Unmarshal(stuff, dest)
if _, iserr := err.(*json.SyntaxError); iserr {
return err
}
if err != nil {
panic(err)
}
return nil
}
// Merge two spell lists alphabetically. It assumes that both lists are already
// ordered alphabetically. Spells in s1 have priority against those in s2,
// meaning that if spells with same indices occur in s1 and s2, the one in s1
// will end up in the final list.
func mergeMultipleSources(s1 *[]Spell, s2 *[]Spell) *[]Spell {
arr1, arr2 := *s1, *s2
if arr1 == nil {
return &arr2
}
if arr2 == nil {
return &arr1
}
var final []Spell
var i1, i2 int
// len is expensive so it is calculated since it doesnt change
len1, len2 := len(arr1), len(arr2)
for i1 < len1 && i2 < len2 {
spell1 := arr1[len1-1]
spell2 := arr2[len2-1]
if i1 < len1 {
spell1 = arr1[i1]
}
if i2 < len2 {
spell2 = arr2[i2]
}
switch {
case spell1.Index == spell2.Index:
final = append(final, spell1)
i1++
i2++
// spell1 comes before spell2
case spell1.Index < spell2.Index:
final = append(final, spell1)
i1++
// spell1 comes after spell2
case spell1.Index > spell2.Index:
final = append(final, spell2)
i2++
}
}
return &final
}