Snuvi.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "server/snuviscript/Snuvi.h"
  2. #include "Compiler.h"
  3. #include "data/HashMap.h"
  4. #undef MATH_H
  5. #include "common/Packets.h"
  6. #include "libraries/Math.h"
  7. #include "libraries/Time.h"
  8. #include "network/Server.h"
  9. #include "tokenizer/Tokenizer.h"
  10. #include "utils/StringBuffer.h"
  11. #include "vm/Script.h"
  12. static const char* unicode(int32 c) {
  13. static char buffer[5];
  14. int index = 0;
  15. if(c > 0xFFFF) {
  16. buffer[index++] = 0xF0 | ((c >> 18) & 0x07);
  17. buffer[index++] = 0x80 | ((c >> 12) & 0x3F);
  18. buffer[index++] = 0x80 | ((c >> 6) & 0x3F);
  19. buffer[index++] = 0x80 | (c & 0x3F);
  20. } else if(c > 0x7FF) {
  21. buffer[index++] = 0xE0 | ((c >> 12) & 0x0F);
  22. buffer[index++] = 0x80 | ((c >> 6) & 0x3F);
  23. buffer[index++] = 0x80 | (c & 0x3F);
  24. } else if(c > 0x7F) {
  25. buffer[index++] = 0xC0 | ((c >> 6) & 0x1F);
  26. buffer[index++] = 0x80 | (c & 0x3F);
  27. } else {
  28. buffer[index++] = c;
  29. }
  30. buffer[index++] = '\0';
  31. return buffer;
  32. }
  33. static void printString(Script* sc) {
  34. int length;
  35. Pointer p;
  36. if(!sPopPointer(sc, &p) || sGetPointerLength(sc, &p, &length)) {
  37. return;
  38. }
  39. OutPacket out = Packet::build(Packet::Type::CHAT);
  40. for(int i = 0; i < length; i++) {
  41. const void* data = sCheckAddress(sc, &p, sizeof(int));
  42. if(data != nullptr) {
  43. int c;
  44. memcpy(&c, data, sizeof(int));
  45. printf(unicode(c));
  46. out.writeU32(c);
  47. }
  48. p.offset += sizeof(int);
  49. }
  50. Server::send(out, PacketType::RELIABLE);
  51. }
  52. static void printInt32(Script* sc) {
  53. int32 i;
  54. if(sPopInt32(sc, &i)) {
  55. printf("%d", i);
  56. }
  57. }
  58. static void wait(Script* sc) {
  59. sError(sc, "w");
  60. }
  61. static Snuvi::Event event = Snuvi::Event::NONE;
  62. static void getEvent(Script* sc) {
  63. sPushInt32(sc, static_cast<int>(event));
  64. }
  65. static void initPrinter() {
  66. Snuvi::initFunction("print", dtVoid(), printString);
  67. Snuvi::addArgument(dtConst(dtText()));
  68. Snuvi::addFunction();
  69. Snuvi::initFunction("print", dtVoid(), printInt32);
  70. Snuvi::addArgument(dtInt32());
  71. Snuvi::addFunction();
  72. }
  73. static int scriptId = 0;
  74. static HashMap<int, Script*> scripts;
  75. static Function function;
  76. void Snuvi::init() {
  77. gfsInit();
  78. gstsInit();
  79. lTimeRegister();
  80. lMathRegister();
  81. initPrinter();
  82. Snuvi::initFunction("wait", dtVoid(), wait);
  83. Snuvi::addFunction();
  84. Snuvi::initFunction("getEvent", dtInt32(), getEvent);
  85. Snuvi::addFunction();
  86. atexit([]() {
  87. gfsDelete();
  88. gstsDelete();
  89. Snuvi::termAll();
  90. });
  91. }
  92. void Snuvi::initFunction(const char* name, DataType returnType,
  93. ScriptFunction sf) {
  94. gfInit(&function, name, returnType, sf);
  95. }
  96. void Snuvi::addArgument(DataType type) {
  97. gfAddArgument(&function, type);
  98. }
  99. void Snuvi::addFunction() {
  100. gfsAdd(&function);
  101. }
  102. static void logError(const char* msg, int line) {
  103. puts(msg);
  104. printf("line: %d\n", line);
  105. }
  106. static bool runScript(Script* script) {
  107. sRun(script);
  108. if(script->error[0] == 'w' && script->error[1] == '\0') {
  109. script->error[0] = '\0';
  110. return false;
  111. } else if(script->error[0] != '\0') {
  112. logError(script->error, script->line);
  113. return true;
  114. }
  115. return script->readIndex >= script->code->length;
  116. }
  117. int Snuvi::start(const char* path) {
  118. StringBuffer<256> s("resources/scripts/");
  119. s.append(path).append(".snuvi");
  120. if(tTokenize(s)) {
  121. logError(tGetError(), tGetLine());
  122. return -1;
  123. }
  124. ByteCode* code = cCompile();
  125. if(code == nullptr) {
  126. logError(cGetError(), cGetLine());
  127. return -1;
  128. }
  129. Script* script = sInit(code);
  130. if(runScript(script)) {
  131. sDelete(script);
  132. return -1;
  133. }
  134. int id = scriptId++;
  135. scripts.tryEmplace(id, script);
  136. return id;
  137. }
  138. void Snuvi::termAll() {
  139. for(auto& entry : scripts) {
  140. sDelete(entry.value);
  141. }
  142. scripts.clear();
  143. }
  144. void Snuvi::callEvent(Event e) {
  145. event = e;
  146. for(auto& entry : scripts) {
  147. if(runScript(entry.value)) {
  148. sDelete(entry.value);
  149. scripts.remove(entry.getKey());
  150. }
  151. }
  152. }
  153. static int32 readChar(int& index, const char* s) {
  154. if(s[index] == '\0') {
  155. return '\0';
  156. }
  157. return s[index++];
  158. }
  159. Pointer Snuvi::toString(Script* sc, const char* s) {
  160. List<int> data;
  161. int index = 0;
  162. while(s[index] != '\0') {
  163. int32 c = readChar(index, s);
  164. if((c & 0xE0) == 0xC0) {
  165. c = ((c & 0x1F) << 6) | (readChar(index, s) & 0x3F);
  166. } else if((c & 0xF0) == 0xE0) {
  167. c = ((c & 0xF) << 12) | ((readChar(index, s) & 0x3F) << 6);
  168. c |= readChar(index, s) & 0x3F;
  169. } else if((c & 0xF8) == 0xF0) {
  170. c = ((c & 0x7) << 18) | ((readChar(index, s) & 0x3F) << 12);
  171. c |= (readChar(index, s) & 0x3F) << 6;
  172. c |= readChar(index, s) & 0x3F;
  173. }
  174. data.add(c);
  175. }
  176. Pointer p;
  177. p.offset = 0;
  178. p.array = asAllocate(&sc->arrays, sizeof(int32), data.getLength());
  179. SnuviArray* array = asGet(&sc->arrays, p.array);
  180. if(array == nullptr) {
  181. sError(sc, "cannot allocate string memory");
  182. p.offset = -1;
  183. p.array = -1;
  184. } else {
  185. memcpy(array->data, data.begin(), sizeof(int32) * data.getLength());
  186. }
  187. return p;
  188. }