Skip to content

Commit 1af8319

Browse files
authored
feat: implement server info message (#25)
* feat: implement server info message * various changes
1 parent ffe6f63 commit 1af8319

16 files changed

Lines changed: 2168 additions & 385 deletions

File tree

cmd/plugins/plugins.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ plugins:
5050
work_dir:
5151
git:
5252
enabled: true
53-
persistent: false # persistent can be set to true if you don't
53+
#persistent: true # persistent can be set to true if you don't
5454
# want the plugin to be cloned at every startup
55-
version: tags/v0.0.1 # can also specify commit hashes
55+
#version: tags/v0.0.1 # can also specify commit hashes
5656
path: https://github.com/secmc/plugin-go

plugin/adapters/plugin/manager.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,10 @@ func (m *Manager) handlePluginMessage(p *pluginProcess, msg *pb.PluginToHost) {
430430
if result := msg.GetEventResult(); result != nil {
431431
p.deliverEventResult(result)
432432
}
433-
if hello := msg.GetHello(); hello != nil {
433+
434+
switch payload := msg.GetPayload().(type) {
435+
case *pb.PluginToHost_Hello:
436+
hello := payload.Hello
434437
cmdNames := mapSlice(hello.Commands, func(cmd *pb.CommandSpec) string {
435438
if len(cmd.Aliases) > 0 {
436439
return fmt.Sprintf("%s (aliases: %v)", cmd.Name, cmd.Aliases)
@@ -441,8 +444,8 @@ func (m *Manager) handlePluginMessage(p *pluginProcess, msg *pb.PluginToHost) {
441444
p.setHello(hello)
442445
m.registerCommands(p, hello.Commands)
443446
m.registerCustomItems(p, hello.CustomItems)
444-
}
445-
if subscribe := msg.GetSubscribe(); subscribe != nil {
447+
case *pb.PluginToHost_Subscribe:
448+
subscribe := payload.Subscribe
446449
eventNames := mapSlice(subscribe.Events, func(evt pb.EventType) string {
447450
return evt.String()
448451
})
@@ -452,11 +455,10 @@ func (m *Manager) handlePluginMessage(p *pluginProcess, msg *pb.PluginToHost) {
452455
}
453456
m.log.Info(fmt.Sprintf(" %s subscribed to %d events", pluginName, len(eventNames)), "events", eventNames)
454457
p.updateSubscriptions(subscribe.Events)
455-
}
456-
if actions := msg.GetActions(); actions != nil {
457-
m.applyActions(p, actions)
458-
}
459-
if logMsg := msg.GetLog(); logMsg != nil {
458+
case *pb.PluginToHost_Actions:
459+
m.applyActions(p, payload.Actions)
460+
case *pb.PluginToHost_Log:
461+
logMsg := payload.Log
460462
level := strings.ToLower(logMsg.Level)
461463
switch level {
462464
case "warn", "warning":
@@ -466,6 +468,15 @@ func (m *Manager) handlePluginMessage(p *pluginProcess, msg *pb.PluginToHost) {
466468
default:
467469
p.log.Info(logMsg.Message)
468470
}
471+
case *pb.PluginToHost_ServerInfo:
472+
var pluginNames []string
473+
474+
for _, pl := range m.plugins {
475+
pluginNames = append(pluginNames, pl.cfg.Name)
476+
}
477+
p.sendServerInfo(pluginNames)
478+
default:
479+
p.log.Info(fmt.Sprintf("unhandled event: %#v", payload))
469480
}
470481
}
471482

plugin/adapters/plugin/process.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@ func (p *pluginProcess) consumeOutput(r io.Reader) {
171171
}
172172
}
173173

174+
func (p *pluginProcess) sendServerInfo(plugins []string) error {
175+
msg := &pb.HostToPlugin{
176+
PluginId: p.id,
177+
Payload: &pb.HostToPlugin_ServerInfo{
178+
ServerInfo: &pb.ServerInformationResponse{
179+
Plugins: plugins,
180+
},
181+
},
182+
}
183+
payload, err := proto.Marshal(msg)
184+
if err != nil {
185+
return err
186+
}
187+
return p.stream.Send(payload)
188+
}
189+
174190
func (p *pluginProcess) sendHello() error {
175191
msg := &pb.HostToPlugin{
176192
PluginId: p.id,

proto/generated/cpp/plugin.pb.cc

Lines changed: 730 additions & 180 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)