This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
forked from ZeppelinMC/Zeppelin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (144 loc) · 3.99 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
package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"
"github.com/pelletier/go-toml/v2"
"golang.org/x/term"
"github.com/dynamitemc/dynamite/core_commands"
"github.com/dynamitemc/dynamite/logger"
"github.com/dynamitemc/dynamite/server"
"github.com/dynamitemc/dynamite/server/config"
"github.com/dynamitemc/dynamite/server/world/tick"
"github.com/dynamitemc/dynamite/util"
"github.com/dynamitemc/dynamite/web"
)
var log = logger.New()
var startTime = time.Now()
func startProfile() {
file, _ := os.Create("cpu.out")
pprof.StartCPUProfile(file)
}
func stopProfile() {
pprof.StopCPUProfile()
file, _ := os.Create("ram.out")
runtime.GC()
pprof.WriteHeapProfile(file)
file.Close()
}
func start(cfg *config.Config) {
srv, err := server.New(cfg, cfg.ServerIP+":"+strconv.Itoa(cfg.ServerPort), log, core_commands.Commands)
log.Info("Started TCP server on %s:%d", cfg.ServerIP, cfg.ServerPort)
if err != nil {
log.Error("Failed to open TCP server: %s", err)
os.Exit(1)
}
ticker := tick.New(srv, srv.Config.TPS)
ticker.Start()
log.Info("Started tick loop")
log.Info("Done! (%v)", time.Since(startTime))
go scanConsole(srv)
if prof {
defer stopProfile()
}
err = srv.Start()
if err != nil {
log.Error("Failed to start server: %s", err)
os.Exit(1)
}
}
var cfg config.Config
var prof bool
func main() {
if util.HasArg("-gencfg") {
log.Info("Generating config file")
f, _ := os.OpenFile("config.toml", os.O_RDWR|os.O_CREATE, 0666)
toml.NewEncoder(f).Encode(config.DefaultConfig)
f.Close()
return
}
server.OldState, _ = term.MakeRaw(int(os.Stdin.Fd()))
log.Info("Starting Dynamite 1.20.1 server")
if util.HasArg("-prof") {
prof = true
log.Info("Starting CPU/RAM profiler")
startProfile()
}
if err := config.LoadConfig("config.toml", &cfg); err != nil {
log.Info("%v loading config.toml. Using default config", err)
cfg = config.DefaultConfig
f, _ := os.OpenFile("config.toml", os.O_RDWR|os.O_CREATE, 0666)
toml.NewEncoder(f).Encode(cfg)
}
log.Debug("Loaded config")
if !cfg.Online && !util.HasArg("-no_offline_warn") {
log.Warn("Offline mode is insecure and you should not use it unless for a private server.\nRead https://github.com/DynamiteMC/Dynamite/wiki/Why-you-shouldn't-use-offline-mode")
}
if cfg.Web.Enable {
if !util.HasArg("-nogui") {
go web.LaunchWebPanel(fmt.Sprintf("%s:%d", cfg.Web.ServerIP, cfg.Web.ServerPort), cfg.Web.Password, log)
} else {
log.Warn("Remove the -nogui argument to load the web panel")
}
}
start(&cfg)
}
// The extremely fancy custom terminal thing
func scanConsole(srv *server.Server) {
var command string
var b [1]byte
for {
os.Stdin.Read(b[:])
switch b[0] {
case 127, 8: // backspace - delete character
if len(command) > 0 {
fmt.Print("\b \b")
command = command[:len(command)-1]
args := strings.Split(command, " ")
cmd := srv.GetCommandGraph().FindCommand(args[0])
if cmd == nil {
fmt.Printf("\r> %s", logger.HR(command))
} else {
if len(args) > 1 {
fmt.Printf("\r> %s %s", args[0], logger.C(strings.Join(args[1:], " ")))
} else {
fmt.Printf("\r> %s", args[0])
}
}
}
case 3: // ctrl c - stop the server
fmt.Print("\r")
if len(command) > len("stop") {
fmt.Print("\x1b[K")
}
fmt.Print("> ")
srv.ConsoleCommand("stop")
case 13: // enter - run the command and clear it
command = strings.TrimSpace(command)
if command == "" {
continue
}
fmt.Printf("\r> %s\r> ", strings.Repeat(" ", len(command)))
srv.ConsoleCommand(command)
command = ""
case 65, 27, 66, 91, 67, 68:
default: // regular character - add to current command input
command += string(b[0])
args := strings.Split(command, " ")
cmd := srv.GetCommandGraph().FindCommand(args[0])
if cmd == nil {
fmt.Printf("\r> %s", logger.HR(command))
} else {
if len(args) > 1 {
fmt.Printf("\r> %s %s", args[0], logger.C(strings.Join(args[1:], " ")))
} else {
fmt.Printf("\r> %s", args[0])
}
}
}
}
}