Snuvi.cpp 5.1 KB

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