-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf.go
More file actions
64 lines (56 loc) · 1.67 KB
/
conf.go
File metadata and controls
64 lines (56 loc) · 1.67 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
54
55
56
57
58
59
60
61
62
63
64
package mcpb
import (
"fmt"
"log/slog"
"os"
"github.com/cockroachdb/pebble"
"github.com/df-mc/dragonfly/server/world"
)
// Config holds the optional parameters of a DB.
type Config struct {
// Log is the Logger that will be used to log errors and debug messages to.
// If set to nil, Log is set to slog.Default().
Log *slog.Logger
// PDBOptions holds PebbleDB specific default options, such as the block size
// or compression used in the database.
PDBOptions *pebble.Options
WorldSettings *world.Settings
}
// Open creates a new DB reading and writing from/to files under the path
// passed. If a world is present at the path, Open will parse its data and
// initialise the world with it. If the data cannot be parsed, an error is
// returned.
func (conf Config) Open(dir string) (*DB, error) {
if conf.Log == nil {
conf.Log = slog.Default()
}
conf.Log = conf.Log.With("provider", "mcpb")
if conf.PDBOptions == nil {
conf.PDBOptions = new(pebble.Options)
}
if len(conf.PDBOptions.Levels) == 0 {
conf.PDBOptions.Levels = make([]pebble.LevelOptions, 7)
for i := range conf.PDBOptions.Levels {
conf.PDBOptions.Levels[i].BlockSize = 16 * 1024 // 16 KiB
}
}
_ = os.MkdirAll(dir, 0777)
if conf.WorldSettings == nil {
conf.WorldSettings = &world.Settings{
Name: "World",
DefaultGameMode: world.GameModeSurvival,
Difficulty: world.DifficultyNormal,
TimeCycle: true,
WeatherCycle: true,
TickRange: 6,
}
}
db := &DB{conf: conf, dir: dir}
db.set = conf.WorldSettings
pdb, err := pebble.Open(dir, conf.PDBOptions)
if err != nil {
return nil, fmt.Errorf("open db: pebble: %w", err)
}
db.pdb = pdb
return db, nil
}