Snuvi.cpp 5.3 KB

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