-
Notifications
You must be signed in to change notification settings - Fork 3
/
system.go
139 lines (120 loc) · 3.42 KB
/
system.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
/*
Copyright 2013 Matthew Collins (purggames@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package world
import (
"github.com/NetherrackDev/netherrack/format/msgpack"
"os"
"path/filepath"
)
/*
Systems are the underlying storage system for worlds. The
Write/Read and Chunk methods write/read structs into the system.
The methods should ignore fields tagged `ignore:"true"`
*/
type System interface {
//Loads or creates the system
Init(path string)
//Gets the name of the system
SystemName() string
//Writes the passed struct/struct pointer to the system's storage
//with the key 'name'.
Write(name string, v interface{}) error
//Reads 'name' into the passed struct pointer
Read(name string, v interface{}) error
//Returns the chunk at the coordinates, also returns if the chunk existed
//before this
Chunk(x, z int) (*Chunk, bool)
//Saves the chunk back to storage.
SaveChunk(x, z int, storage *Chunk)
//Closes the chunk in the system
CloseChunk(x, z int, storage *Chunk)
//Closes the system
Close()
}
var systems = map[string]func() System{}
//Add a system to be used for loading/saving worlds.
//Should only be called at init.
func AddSystem(name string, f func() System) {
systems[name] = f
}
//Loads the world by name using the passed system if
//the world doesn't exists.
func LoadWorld(name string, system System, gen Generator, dimension Dimension, tryClose chan TryClose) *World {
metapath := filepath.Join("./worlds/", name, "netherrack.meta")
_, err := os.Stat(metapath)
if err == nil {
//Load the world
return GetWorld(name, tryClose)
}
//Create the world
meta := netherrackMeta{
SystemName: system.SystemName(),
GeneratorName: gen.Name(),
}
os.MkdirAll(filepath.Join("./worlds/", name), 0777)
f, err := os.Create(metapath)
if err != nil {
panic(err)
}
defer f.Close()
msgpack.NewEncoder(f).Encode(&meta)
w := &World{
Name: name,
system: system,
generator: gen,
tryClose: tryClose,
}
w.worldData.Dimension = dimension
w.init()
w.system.Init(filepath.Join("./worlds/", w.Name))
w.generator.Save(w)
w.system.Write("levelData", &w.worldData)
go w.run()
return w
}
type netherrackMeta struct {
SystemName string
GeneratorName string
}
//Loads the world by name
func GetWorld(name string, tryClose chan TryClose) *World {
root := filepath.Join("./worlds/", name)
f, err := os.Open(filepath.Join(root, "netherrack.meta"))
if err != nil {
return nil
}
defer f.Close()
meta := netherrackMeta{}
msgpack.NewDecoder(f).Decode(&meta)
sys, ok := systems[meta.SystemName]
if !ok {
panic("Unknown world system")
}
system := sys()
gen, ok := generators[meta.GeneratorName]
if !ok {
panic("Unknown world generator")
}
generator := gen()
w := &World{
Name: name,
system: system,
generator: generator,
tryClose: tryClose,
}
w.init()
w.system.Init(filepath.Join("./worlds/", w.Name))
w.system.Read("levelData", &w.worldData)
go w.run()
return w
}