-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
64 lines (57 loc) · 1.15 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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"sync"
)
const configFile = "./config.json"
var configMutex = sync.Mutex{}
type config struct {
SlotMap []slotConfig
}
type slotConfig struct {
Slot uint
Filename string
Player playerItem
}
func writeConfig() (err error) {
configMutex.Lock()
defer configMutex.Unlock()
slotItems := slotMap.getSlotItems()
slotMap := make([]slotConfig, 0, len(slotItems))
for _, item := range slotItems {
slotMap = append(slotMap, slotConfig{
Filename: item.Item.Name + "." + item.Item.Extension,
Slot: item.Slot,
Player: item.Player,
})
}
conf := config{
SlotMap: slotMap,
}
//Write data to file:
data, err := json.Marshal(conf)
if err != nil {
return
}
f, err := os.OpenFile(configFile, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return
}
defer f.Close()
f.Truncate(0) //clear existing data in the file
_, err = f.Write(data)
return
}
func readConfig() (conf config) {
configMutex.Lock()
defer configMutex.Unlock()
conf.SlotMap = make([]slotConfig, 0)
file, err := ioutil.ReadFile(configFile)
if err != nil {
return
}
json.Unmarshal(file, &conf)
return
}