SnuviCommands.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "server/commands/SnuviCommands.h"
  2. #include "server/snuviscript/Snuvi.h"
  3. static Commands::Arguments commandArguments = Commands::Raw();
  4. static void helpScript() {
  5. puts("/script start <script>");
  6. puts("/script term");
  7. }
  8. static void commandScript(const Commands::Arguments& args) {
  9. if(args.getLength() < 2) {
  10. helpScript();
  11. } else if(strcmp(args[1], "start") == 0) {
  12. if(args.getLength() < 3) {
  13. puts("/script start <script>");
  14. return;
  15. }
  16. Snuvi::start(args[2]);
  17. } else if(strcmp(args[1], "term") == 0) {
  18. Snuvi::termAll();
  19. puts("all scripts were terminated");
  20. } else {
  21. helpScript();
  22. }
  23. }
  24. static void getArguments(Script* sc) {
  25. int length = commandArguments.getLength();
  26. Pointer p;
  27. p.offset = 0;
  28. p.array = asAllocate(&sc->arrays, sizeof(Pointer), length);
  29. sPushPointer(sc, &p);
  30. SnuviArray* array = asGet(&sc->arrays, p.array);
  31. if(array == nullptr) {
  32. sError(sc, "cannot allocate string memory");
  33. return;
  34. }
  35. Pointer* texts = static_cast<Pointer*>(array->data);
  36. for(int i = 0; i < length; i++) {
  37. Pointer text = Snuvi::toString(sc, commandArguments[i]);
  38. memcpy(texts + i, &text, sizeof(Pointer));
  39. }
  40. }
  41. void SnuviCommands::init() {
  42. Commands::add("script", commandScript);
  43. DataType type = dtText();
  44. dtDereference(&type);
  45. Snuvi::initFunction("getArguments", type, getArguments);
  46. Snuvi::addFunction();
  47. }
  48. void SnuviCommands::callEvent(Commands::Arguments& args) {
  49. commandArguments = args;
  50. Snuvi::callEvent(Snuvi::Event::COMMAND);
  51. }