-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (147 loc) · 3.22 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"io/ioutil"
"log"
"os"
"os/signal"
"plugin"
"sync"
"github.com/andreasgoulas/go-mcc/mcc"
)
var defaultConfig = &mcc.Config{
Port: 25565,
Name: "Go-MCC",
MOTD: "Welcome!",
Verify: false,
Public: true,
MaxPlayers: 32,
Heartbeat: "http://www.classicube.net/heartbeat.jsp",
MainLevel: "main",
}
const (
PermOperator = 1 << 0
)
type console struct {
server *mcc.Server
waitGroup *sync.WaitGroup
signal chan os.Signal
}
func newConsole(server *mcc.Server, waitGroup *sync.WaitGroup) *console {
console := &console{
server,
waitGroup,
make(chan os.Signal),
}
server.AddCommand(&mcc.Command{
Name: "stop",
Description: "Stop the server.",
Usage: "/stop",
Permissions: PermOperator,
Handler: console.handleStop,
})
signal.Notify(console.signal, os.Interrupt)
go func() {
signal := <-console.signal
if signal == os.Interrupt {
console.stop()
}
}()
return console
}
func (console *console) run() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
console.server.ExecuteCommand(console, scanner.Text())
}
}
func (console *console) stop() {
console.server.Stop()
console.waitGroup.Wait()
os.Exit(0)
}
// Server implements mcc.CommandSender.
func (console *console) Server() *mcc.Server {
return console.server
}
// Name implements mcc.CommandSender.
func (console *console) Name() string {
return "Console"
}
// SendMessage implements mcc.CommandSender.
func (console *console) SendMessage(message string) {
log.Println(message)
}
// CanExecute implements mcc.CommandSender.
func (console *console) CanExecute(command *mcc.Command) bool {
return true
}
func (console *console) handleStop(sender mcc.CommandSender, command *mcc.Command, message string) {
console.stop()
}
func readConfig(path string) *mcc.Config {
file, err := ioutil.ReadFile(path)
if err != nil {
data, err := json.MarshalIndent(defaultConfig, "", "\t")
if err != nil {
log.Printf("readConfig: %s\n", err)
return defaultConfig
}
if err := ioutil.WriteFile(path, data, 0644); err != nil {
log.Printf("readConfig: %s\n", err)
}
return defaultConfig
} else {
config := &mcc.Config{}
err = json.Unmarshal(file, config)
if err != nil {
log.Printf("readConfig: %s\n", err)
config = defaultConfig
}
return config
}
}
func loadPlugins(path string, server *mcc.Server) {
files, err := ioutil.ReadDir(path)
if err != nil {
return
}
for _, file := range files {
if file.IsDir() {
continue
}
lib, err := plugin.Open(path + file.Name())
if err != nil {
log.Printf("loadPlugins: %s\n", err)
continue
}
sym, err := lib.Lookup("Initialize")
if err != nil {
log.Printf("loadPlugins: %s\n", err)
continue
}
initFn, ok := sym.(func() mcc.Plugin)
if !ok {
continue
}
plug := initFn()
server.AddPlugin(plug)
}
}
func main() {
config := readConfig("server.json")
cwstorage := mcc.NewCwStorage("levels/")
server := mcc.NewServer(config, cwstorage)
if server == nil {
return
}
loadPlugins("plugins/", server)
var wg sync.WaitGroup
if err := server.Start(&wg); err != nil {
log.Println(err)
return
}
console := newConsole(server, &wg)
console.run()
}