|
@@ -1,102 +0,0 @@
|
|
|
-#include <cstring>
|
|
|
-#include <iostream>
|
|
|
-
|
|
|
-#include "common/network/Packets.h"
|
|
|
-#include "server/commands/CommandManager.h"
|
|
|
-#include "server/snuviscript/Snuvi.h"
|
|
|
-
|
|
|
-static void commandTest(ServerState&, const CommandArguments& args) {
|
|
|
- std::cout << "test command\n";
|
|
|
- for(int i = 0; i < args.getLength(); i++) {
|
|
|
- std::cout << " - " << args[i] << '\n';
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static void commandStop(ServerState& sc, const CommandArguments&) {
|
|
|
- sc.running = false;
|
|
|
-}
|
|
|
-
|
|
|
-static void commandSay(ServerState& sc, const CommandArguments& args) {
|
|
|
- StringBuffer<256> s;
|
|
|
- for(int i = 1; i < args.getLength(); i++) {
|
|
|
- s.append(args[i]);
|
|
|
- s.append(' ');
|
|
|
- }
|
|
|
- OutPacket out = OutPacket::reliable(260);
|
|
|
- out.writeU16(ServerPacket::CHAT);
|
|
|
- out.writeString(s);
|
|
|
- sc.server.send(out);
|
|
|
- std::cout << s << '\n';
|
|
|
-}
|
|
|
-
|
|
|
-static void helpScript() {
|
|
|
- std::cout << "/script start <script>\n";
|
|
|
- std::cout << "/script term\n";
|
|
|
-}
|
|
|
-
|
|
|
-static void commandScript(ServerState&, const CommandArguments& args) {
|
|
|
- if(args.getLength() < 2) {
|
|
|
- helpScript();
|
|
|
- } else if(strcmp(args[1], "start") == 0) {
|
|
|
- if(args.getLength() < 3) {
|
|
|
- std::cout << "/script start <script>\n";
|
|
|
- return;
|
|
|
- }
|
|
|
- Snuvi::start(args[2]);
|
|
|
- } else if(strcmp(args[1], "term") == 0) {
|
|
|
- Snuvi::termAll();
|
|
|
- std::cout << "all scripts were terminated\n";
|
|
|
- } else {
|
|
|
- helpScript();
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static CommandArguments commandArguments = RawCommand();
|
|
|
-
|
|
|
-static void getArguments(Script* sc) {
|
|
|
- int length = commandArguments.getLength();
|
|
|
- Pointer p;
|
|
|
- p.offset = 0;
|
|
|
- p.array = asAllocate(&sc->arrays, sizeof(Pointer), length);
|
|
|
- sPushPointer(sc, &p);
|
|
|
- SnuviArray* array = asGet(&sc->arrays, p.array);
|
|
|
- if(array == nullptr) {
|
|
|
- sError(sc, "cannot allocate string memory");
|
|
|
- return;
|
|
|
- }
|
|
|
- Pointer* texts = static_cast<Pointer*>(array->data);
|
|
|
- for(int i = 0; i < length; i++) {
|
|
|
- Pointer text = Snuvi::toString(sc, commandArguments[i]);
|
|
|
- memcpy(texts + i, &text, sizeof(Pointer));
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-CommandManager::CommandManager() {
|
|
|
- commands.add("test", commandTest);
|
|
|
- commands.add("stop", commandStop);
|
|
|
- commands.add("say", commandSay);
|
|
|
- commands.add("script", commandScript);
|
|
|
-
|
|
|
- DataType type = dtText();
|
|
|
- dtDereference(&type);
|
|
|
- Snuvi::initFunction("getArguments", type, getArguments);
|
|
|
- Snuvi::addFunction();
|
|
|
-}
|
|
|
-
|
|
|
-void CommandManager::execute(ServerState& sc, const RawCommand& rawCommand) {
|
|
|
- CommandArguments args(rawCommand);
|
|
|
- if(args.getLength() == 0) {
|
|
|
- std::cout << "Invalid command syntax: '" << rawCommand << "'\n";
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- CommandName command(args[0]);
|
|
|
- Command* c = commands.search(command);
|
|
|
- if(c == nullptr) {
|
|
|
- commandArguments = args;
|
|
|
- Snuvi::callEvent(Snuvi::Event::COMMAND);
|
|
|
- std::cout << "Unknown command: '" << command << "'\n";
|
|
|
- return;
|
|
|
- }
|
|
|
- (*c)(sc, args);
|
|
|
-}
|