-
Notifications
You must be signed in to change notification settings - Fork 3
/
load_nowasm.go
102 lines (87 loc) · 1.97 KB
/
load_nowasm.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
//go:build !js
package save
import (
"encoding/json"
"os"
"path"
"path/filepath"
"strings"
serde "github.com/mlange-42/arche-serde"
"github.com/mlange-42/arche/ecs"
)
func loadWorld(world *ecs.World, folder, name string) error {
jsData, err := os.ReadFile(path.Join(folder, name) + ".json")
if err != nil {
return err
}
return serde.Deserialize(jsData, world)
}
func loadSaveTime(folder, name string) (saveTime, error) {
jsData, err := os.ReadFile(path.Join(folder, name) + ".json")
if err != nil {
return saveTime{}, err
}
helper := saveGameInfo{}
err = json.Unmarshal(jsData, &helper)
if err != nil {
return saveTime{}, err
}
return helper.Resources.SaveTime, nil
}
func loadAchievements(file string, completed *[]string) error {
jsData, err := os.ReadFile(file)
if err != nil {
return err
}
return json.Unmarshal(jsData, completed)
}
func listGames(folder string) ([]SaveGame, error) {
games := []SaveGame{}
files, err := os.ReadDir(folder)
if err != nil {
return nil, nil
}
for _, file := range files {
if file.IsDir() {
continue
}
ext := filepath.Ext(file.Name())
if ext == ".json" {
base := strings.TrimSuffix(file.Name(), ".json")
info, err := loadSaveTime(folder, base)
if err != nil {
return nil, err
}
games = append(games, SaveGame{
Name: base,
Time: info.Time,
})
}
}
return games, nil
}
func listMapsLocal(folder string) ([]MapLocation, error) {
maps := []MapLocation{}
files, err := os.ReadDir(folder)
if err != nil {
return nil, nil
}
for _, file := range files {
if file.IsDir() {
continue
}
ext := filepath.Ext(file.Name())
if ext == ".json" {
base := strings.TrimSuffix(file.Name(), ".json")
maps = append(maps, MapLocation{Name: base, IsEmbedded: false})
}
}
return maps, nil
}
func loadMapLocal(folder string, name string) (string, error) {
mapData, err := os.ReadFile(path.Join(folder, name) + ".json")
if err != nil {
return "", err
}
return string(mapData), nil
}