1 module voxelman.chat.plugin; 2 3 import std.experimental.logger; 4 import pluginlib; 5 import voxelman.utils.messagewindow : MessageWindow; 6 7 import voxelman.core.events; 8 import voxelman.net.packets; 9 10 import voxelman.client.plugin; 11 import voxelman.command.plugin; 12 import voxelman.eventdispatcher.plugin; 13 import voxelman.net.plugin; 14 import voxelman.clientdb.plugin; 15 16 shared static this() 17 { 18 pluginRegistry.regClientPlugin(new ChatPluginClient); 19 pluginRegistry.regServerPlugin(new ChatPluginServer); 20 } 21 22 final class ChatPluginClient : IPlugin 23 { 24 // IPlugin stuff 25 mixin IdAndSemverFrom!(voxelman.chat.plugininfo); 26 27 private ClientPlugin clientPlugin; 28 private NetClientPlugin connection; 29 private EventDispatcherPlugin evDispatcher; 30 MessageWindow messageWindow; 31 32 override void preInit() 33 { 34 messageWindow.init(); 35 messageWindow.messageHandler = 36 (string msg) => connection.send(MessagePacket(0, msg), 1); 37 } 38 39 override void init(IPluginManager pluginman) 40 { 41 clientPlugin = pluginman.getPlugin!ClientPlugin; 42 evDispatcher = pluginman.getPlugin!EventDispatcherPlugin; 43 evDispatcher.subscribeToEvent(&onUpdateEvent); 44 45 connection = pluginman.getPlugin!NetClientPlugin; 46 connection.registerPacketHandler!MessagePacket(&handleMessagePacket); 47 } 48 49 void handleMessagePacket(ubyte[] packetData, ClientId clientId) 50 { 51 import std.format : formattedWrite; 52 auto packet = unpackPacket!MessagePacket(packetData); 53 54 if (packet.clientId == 0) 55 messageWindow.putln(packet.msg); 56 else { 57 messageWindow.putf("%s> %s\n", clientPlugin.clientName(packet.clientId), packet.msg); 58 } 59 } 60 61 void onUpdateEvent(ref UpdateEvent event) 62 { 63 import derelict.imgui.imgui; 64 65 float h = 200; 66 igSetNextWindowPos(ImVec2(0, igGetIO().DisplaySize.y-h), ImGuiSetCond_FirstUseEver); 67 igSetNextWindowSize(ImVec2(500, h), ImGuiSetCond_FirstUseEver); 68 if (!igBegin("Chat")) return; 69 messageWindow.draw(); 70 igEnd(); 71 } 72 } 73 74 final class ChatPluginServer : IPlugin 75 { 76 // IPlugin stuff 77 mixin IdAndSemverFrom!(voxelman.chat.plugininfo); 78 79 private NetServerPlugin connection; 80 private ClientDb clientDb; 81 CommandPluginServer commandPlugin; 82 83 override void init(IPluginManager pluginman) 84 { 85 connection = pluginman.getPlugin!NetServerPlugin; 86 connection.registerPacketHandler!MessagePacket(&handleMessagePacket); 87 clientDb = pluginman.getPlugin!ClientDb; 88 commandPlugin = pluginman.getPlugin!CommandPluginServer; 89 commandPlugin.registerCommand("msg", &messageCommand); 90 } 91 92 void handleMessagePacket(ubyte[] packetData, ClientId clientId) 93 { 94 auto packet = unpackPacket!MessagePacket(packetData); 95 packet.clientId = clientId; 96 connection.sendToAll(packet); 97 infof("%s> %s", clientDb.clientName(clientId), packet.msg); 98 } 99 100 void messageCommand(CommandParams params) 101 { 102 import std.string : strip; 103 auto stripped = params.rawArgs.strip; 104 connection.sendToAll(MessagePacket(0, stripped)); 105 infof("> %s", stripped); 106 } 107 }