/
world.go
327 lines (257 loc) · 6.81 KB
/
world.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package config
import (
"crypto/tls"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/Gophercraft/core/home/rpcnet"
"github.com/Gophercraft/core/i18n"
"github.com/Gophercraft/core/vsn"
"github.com/Gophercraft/text"
)
type WorldVars map[string]string
var (
Presets map[RealmType]WorldVars
)
func init() {
Presets = map[RealmType]WorldVars{
RealmTypeRP: {
"XP.Rate": "1.0",
"Weather.On": "true",
"Sync.VisibilityRange": "250.0",
"PvP.Deathmatch": "false",
"PvP.AtWar": "false",
"Char.StartLevel": "255",
"Char.Cinematic": "true",
"Chat.HostAnnounceChannel": "true",
},
}
}
type RealmType uint8
const (
RealmTypeNone = 0
RealmTypePvP = 1
RealmTypeNormal = 4
RealmTypeRP = 6
RealmTypeRP_PvP = 8
)
func (rt RealmType) EncodeWord() (string, error) {
switch rt {
case RealmTypeNone:
return "None", nil
case RealmTypePvP:
return "PvP", nil
case RealmTypeNormal:
return "Normal", nil
case RealmTypeRP:
return "RP", nil
case RealmTypeRP_PvP:
return "RP-PvP", nil
default:
return "", fmt.Errorf("gcore: unknown RealmType %d", rt)
}
}
func (rt *RealmType) DecodeWord(wdata string) error {
data := strings.ToLower(wdata)
switch data {
case "none", "":
*rt = RealmTypeNone
case "pvp":
*rt = RealmTypePvP
case "normal":
*rt = RealmTypeNormal
case "rp":
*rt = RealmTypeRP
case "rp-pvp":
*rt = RealmTypeRP_PvP
default:
return fmt.Errorf("gcore: unrecognized realm type %s", wdata)
}
return nil
}
type WorldFile struct {
Version vsn.Build
Listen string
Redirect string
PublicRedirect string
RealmID uint64
Timezone i18n.Timezone
RealmType RealmType
RealmName string
RealmDescription string
FederalSovereignty bool
DBDriver string
DBURL string
PublicAddress string
WardenEnabled bool
ShowSQL bool
HomeServer string
HomeServerFingerprint string
CPUProfile string
WorldVars WorldVars
}
type World struct {
Dir string
WorldFile
Certificate tls.Certificate
}
func WorldExists(at string) bool {
fi, err := os.Stat(at)
if err != nil {
return false
}
if !fi.IsDir() {
return false
}
txtName := filepath.Join(at, "World.txt")
if _, err = os.Stat(txtName); err != nil {
return false
}
return true
}
func LoadWorld(at string) (*World, error) {
wc := new(World)
wc.Dir = at
c, err := ioutil.ReadFile(filepath.Join(wc.Dir, "World.txt"))
if err != nil {
return nil, err
}
err = text.Unmarshal(c, &wc.WorldFile)
if err != nil {
return nil, fmt.Errorf("%s: unmarshaling text in world config failed", err)
}
wc.Certificate, err = tls.LoadX509KeyPair(
filepath.Join(wc.Dir, "cert.pem"),
filepath.Join(wc.Dir, "key.pem"),
)
if err != nil {
return nil, err
}
return wc, nil
}
const DefaultWorld = `{
// build ID
Version %d
// the internal IP address to listen on
Listen 0.0.0.0:8085
// external address (should be accessible from the client's computer)
PublicAddress 127.0.0.1:8085
// Redirect 0.0.0.0:9090 // Uncomment these two lines if you are trying to run version 3368
// PublicRedirect 0.0.0.0:9090
// The display name of your server. You can change this as you please.
RealmName "%s"
// The type of server you want to create. A server of the type "RP" will allow for an all-GM roleplaying experience.
// Note: changing this value will also change the default WorldVars.
// You can always override these with custom values to fine-tune your server to the desired behavior!
RealmType RP
// Description of your server. This will appear in the Gophercraft website.
RealmDescription "Put the description for your server here!"
// If true, auth server admins are not admins on your world.
// It's recommended to keep this value false for most servers.
FederalSovereignty false
// Editing this can lead to duplicate entries in the realm list.
RealmID %d
// The timezone changes which tab the realm appears under.
Timezone "Development"
// database driver
DBDriver %s
// database URL
DBURL "%s"
// Address of RPC server (replace 127.0.0.1 with host_external in gcraft_auth/Config.txt)
AuthServer %s
// RPC server fingerprint
AuthServerFingerprint %s
// Game files
ContentSource "%s"
// Uncomment to perform CPU usage profiling.
// CPUProfile "cpu.prof"
WorldVars
{
//
}
}
`
// func GenerateDefaultWorld(version uint32, name string, id uint64, sqlDriver, sqlDB string, at string, authServer, serverFingerprint string) error {
// path := etc.ParseSystemPath(at)
// if path.IsExtant() {
// return ErrAlreadyExists
// }
// if err := path.MakeDir(); err != nil {
// return err
// }
// wfile := fmt.Sprintf(DefaultWorld, version, name, id, sqlDriver, sqlDB, authServer, serverFingerprint)
// path.Concat("World.txt").WriteAll([]byte(wfile))
// path.Concat("Vars.txt").WriteAll([]byte(DefaultVars))
// path.Concat("Datapacks").MakeDir()
// if err := GenerateTLSKeyPair(path.Render()); err != nil {
// return err
// }
// return nil
// }
// func (a *Home) GenerateDefaultWorld(version uint32, name string, id uint64, sqlDriver, sqlDB string, at string) error {
// asf, err := sys.GetCertFileFingerprint(a.Path.Concat("cert.pem").Render())
// if err != nil {
// return err
// }
// if err = GenerateDefaultWorld(version, name, id, sqlDriver, sqlDB, at, "127.0.0.1:3724", asf); err != nil {
// return nil
// }
// wsf, err := sys.GetCertFileFingerprint(etc.ParseSystemPath(at).Concat("cert.pem").Render())
// if err != nil {
// return err
// }
// realms := RealmsFile{}
// realmsFile := a.Path.Concat("Realms.txt")
// if realmsFile.IsExtant() {
// rdata, err := realmsFile.ReadAll()
// if err != nil {
// return err
// }
// err = text.Unmarshal(rdata, &realms)
// if err != nil {
// return err
// }
// } else {
// realms.Realms = make(map[uint64]Realm)
// }
// realms.Realms[id] = Realm{
// FP: wsf,
// }
// rdata, err := text.Marshal(realms)
// if err != nil {
// return err
// }
// return realmsFile.WriteAll(rdata)
// }
func (c *World) String() string {
return "[" + c.RealmName + "] @" + c.PublicAddress
}
func (c *World) GenerateKeyPair() error {
return GenerateTLSKeyPair(c.Dir)
}
func (c *World) Fingerprint() string {
f, err := rpcnet.GetCertFileFingerprint(filepath.Join(c.Dir, "cert.pem"))
if err != nil {
panic(err)
}
return f
}
// List all World configs in the chosen Gophercraft root directory.
func ListWorldConfigs(dir string) ([]string, error) {
list, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var names []string
for _, entry := range list {
if entry.IsDir() {
_, err := os.Stat(filepath.Join(dir, entry.Name(), "World.txt"))
if err == nil {
names = append(names, entry.Name())
}
}
}
return names, nil
}