-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.go
More file actions
53 lines (46 loc) · 1.22 KB
/
Copy pathio.go
File metadata and controls
53 lines (46 loc) · 1.22 KB
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
package main
import (
"GoConsoleBT/controller"
"encoding/json"
"github.com/buger/jsonparser"
"os"
)
const spritePath = "./sprite/"
const statePath = "./state/"
const scenarioPath = "./scenario/"
func loadSprite(filename string) ([]byte, error) {
return os.ReadFile(spritePath + filename)
}
func loadState(filename string) ([]byte, error) {
return os.ReadFile(statePath + filename + ".json")
}
func loadScenario(filename string) ([]byte, error) {
return os.ReadFile(scenarioPath + filename + ".json")
}
func saveConfig(config *GameConfig) (int, error) {
payload, err := json.Marshal(config)
if err != nil {
return 0, err
}
return len(payload), os.WriteFile("config.json", payload, 644)
}
func loadConfig() (*GameConfig, error) {
payload, err := os.ReadFile("config.json")
if err != nil {
return nil, err
}
config := new(GameConfig)
err = json.Unmarshal(payload, config)
kb, dType, _, _ := jsonparser.Get(payload, "keyBindings")
switch dType {
case jsonparser.Array:
idx := 0
jsonparser.ArrayEach(kb, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
bind := &controller.KeyBind{}
json.Unmarshal(value, bind)
config.KeyBindings[idx] = *bind
idx++
})
}
return config, err
}