aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 3e5ab115eab1a867fcaa4997a4f9b01739a74d3d (plain) (blame)
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
package proxy

import (
	"encoding/json"
	"log"
	"os"
	"path/filepath"
	"sync"
)

const latestSerializeVer = 28
const latestProtoVer = 39
const maxPlayerNameLen = 20
const playerNameChars = "^[a-zA-Z0-9-_]+$"
const bytesPerMediaBunch = 5000

const defaultCmdPrefix = ">"
const defaultSendInterval = 0.09
const defaultUserLimit = 10
const defaultAuthBackend = "sqlite3"
const defaultBindAddr = ":40000"

var config Config
var configMu sync.RWMutex

type Config struct {
	NoPlugins     bool
	CmdPrefix     string
	RequirePasswd bool
	SendInterval  float32
	UserLimit     int
	AuthBackend   string
	BindAddr      string
	Servers       []struct {
		Name string
		Addr string
	}
	CSMRF struct {
		NoCSMs          bool
		ChatMsgs        bool
		ItemDefs        bool
		NodeDefs        bool
		NoLimitMapRange bool
		PlayerList      bool
	}
	MapRange   uint32
	Groups     map[string][]string
	UserGroups map[string]string
}

func Conf() Config {
	configMu.RLock()
	defer configMu.RUnlock()

	return config
}

func LoadConfig() error {
	configMu.Lock()
	defer configMu.Unlock()

	oldConf := config

	config.CmdPrefix = defaultCmdPrefix
	config.SendInterval = defaultSendInterval
	config.UserLimit = defaultUserLimit
	config.AuthBackend = defaultAuthBackend
	config.BindAddr = defaultBindAddr
	config.Groups = make(map[string][]string)
	config.UserGroups = make(map[string]string)

	executable, err := os.Executable()
	if err != nil {
		return err
	}

	path := filepath.Dir(executable) + "/config.json"
	f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		config = oldConf
		return err
	}
	defer f.Close()

	if fi, _ := f.Stat(); fi.Size() == 0 {
		f.WriteString("{\n\t\n}\n")
		f.Seek(0, os.SEEK_SET)
	}

	decoder := json.NewDecoder(f)
	if err := decoder.Decode(&config); err != nil {
		config = oldConf
		return err
	}

	log.Print("{←|⇶} load config")
	return nil
}