script.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include <precomp.h>
  2. #include <time.h>
  3. #include <api/script/script.h>
  4. #include <api/script/scriptmgr.h>
  5. #include <api/script/objects/systemobj.h>
  6. #include <api/script/vcpu.h>
  7. #include <api/wndmgr/msgbox.h>
  8. #include <api/script/guru.h>
  9. #ifdef WASABI_COMPILE_SKIN
  10. #include <api/skin/skin.h>
  11. #endif
  12. #include <bfc/string/bfcstring.h>
  13. #ifdef WASABI_COMPILE_COMPONENTS
  14. #include <api/wac/main.h>//CUT!!
  15. #endif
  16. #include <api/skin/widgets/group.h>
  17. #include <api/script/objecttable.h>
  18. #include <api/application/wkc.h>
  19. extern StringW g_resourcepath;
  20. // insert the given script in the virtual machine
  21. int Script::addScript(const wchar_t *path, const wchar_t *filename, const wchar_t *id)
  22. {
  23. static int reentering = 0;
  24. static int redisplay = 0;
  25. StringPathCombine fpn(path, filename);
  26. wchar_t olddir[WA_MAX_PATH] = {0};
  27. Wasabi::Std::getCurDir(olddir, WA_MAX_PATH);
  28. Wasabi::Std::setCurDir(WASABI_API_APP->path_getAppPath());
  29. OSFILETYPE fp = WFOPEN(fpn, WF_READONLY_BINARY, NO_FILEREADERS);
  30. Wasabi::Std::setCurDir(olddir);
  31. #ifdef WASABI_COMPILE_SKIN
  32. if (fp == OPEN_FAILED)
  33. {
  34. fpn = StringPathCombine(Skin::getDefaultSkinPath(), filename);
  35. Wasabi::Std::getCurDir(olddir, WA_MAX_PATH);
  36. Wasabi::Std::setCurDir(WASABI_API_APP->path_getAppPath());
  37. fp = WFOPEN(fpn, WF_READONLY_BINARY, NO_FILEREADERS);
  38. Wasabi::Std::setCurDir(olddir);
  39. #endif
  40. if (fp == OPEN_FAILED)
  41. {
  42. if (reentering == 1)
  43. {
  44. redisplay = 1;
  45. return -1;
  46. }
  47. reentering = 1;
  48. #ifdef WANT_WASABI_API_WNDMGR
  49. WASABI_API_SKIN->messageBox(StringPrintf("Could not load script \'%s%s\'\r\n\r\nPress \'OK\' to continue.", path, filename), "Skin Warning", MSGBOX_OK, NULL, NULL);
  50. #else
  51. Wasabi::Std::messageBox(StringPrintfW(L"Could not load script \'%s%s\'\r\n\r\nPress \'OK\' to continue.", path, filename), L"Skin Warning", 0);
  52. #endif
  53. if (redisplay)
  54. {
  55. #ifdef WANT_WASABI_API_WNDMGR
  56. WASABI_API_WND->appdeactivation_setbypass(1);
  57. #endif
  58. Wasabi::Std::messageBox(StringPrintfW(L"Could not load script \'%s%s\'\r\n\r\nPress \'OK\' to continue.", path, filename), L"Skin Warning", 0);
  59. #ifdef WANT_WASABI_API_WNDMGR
  60. WASABI_API_WND->appdeactivation_setbypass(0);
  61. #endif
  62. redisplay = 0;
  63. }
  64. reentering = 0;
  65. return -1;
  66. }
  67. #ifdef WASABI_COMPILE_SKIN
  68. }
  69. #endif
  70. int size = (int)FGETSIZE(fp);
  71. void *scriptBlock = MALLOC(size);
  72. FREAD(scriptBlock, size, 1, fp);
  73. FCLOSE(fp);
  74. #ifdef WASABI_COMPILE_COMPONENTS
  75. WasabiKernelController *wkc = Main::getKernelController();
  76. if (wkc && !wkc->testScript(fpn, scriptBlock, size)) return -1;
  77. #endif
  78. int vcpuid = VCPU::assignNewScriptId();
  79. SystemObject *system = new SystemObject();
  80. system->setScriptId(vcpuid);
  81. system->setFilename(filename);
  82. int r = VCPU::addScript(scriptBlock, size, vcpuid);
  83. if (r != vcpuid)
  84. {
  85. FREE(scriptBlock);
  86. delete system;
  87. return -1;
  88. }
  89. scriptEntry *e = new scriptEntry;
  90. e->scriptBlock = scriptBlock;
  91. e->vcpuId = vcpuid;
  92. scriptslist.addItem(e);
  93. if (!ObjectTable::checkScript(system))
  94. {
  95. unloadScript(vcpuid);
  96. return -1;
  97. }
  98. return vcpuid;
  99. }
  100. // unload the given script
  101. void Script::unloadScript(int id)
  102. {
  103. for (int i=0;i<scriptslist.getNumItems();i++) {
  104. if (scriptslist.enumItem(i)->vcpuId == id) {
  105. VCPU::removeScript(id);
  106. FREE(scriptslist.enumItem(i)->scriptBlock);
  107. delete scriptslist.enumItem(i);
  108. scriptslist.removeByPos(i);
  109. }
  110. }
  111. if (scriptslist.getNumItems() == 0) scriptslist.removeAll(); // disable fortify warning
  112. }
  113. // unload ALL scripts
  114. void Script::unloadAllScripts()
  115. {
  116. while (scriptslist.getNumItems()>0)
  117. {
  118. int n = scriptslist.getNumItems()-1;
  119. VCPU::removeScript(scriptslist.enumItem(n)->vcpuId);
  120. FREE(scriptslist.enumItem(n)->scriptBlock);
  121. delete scriptslist.enumItem(n);
  122. scriptslist.removeByPos(n);
  123. }
  124. scriptslist.removeAll(); // disable fortify warning
  125. }
  126. PtrList <scriptEntry> Script::scriptslist;
  127. int Script::codeToSeverity(int code, wchar_t *t, int len)
  128. {
  129. switch (code)
  130. {
  131. case GURU_INVALIDHEADER:
  132. case GURU_OLDFORMAT:
  133. wcsncpy(t, L"Ignoring script", len);
  134. return 5;
  135. case GURU_DIVBYZERO:
  136. wcsncpy(t, L"Returning 0", len);
  137. return 4;
  138. case GURU_NEWFAILED:
  139. wcsncpy(t, L"Returning NULL", len);
  140. return 4;
  141. case GURU_NULLCALLED:
  142. wcsncpy(t, L"Ignoring call", len);
  143. return 4;
  144. case GURU_INCOMPATIBLEOBJECT:
  145. wcsncpy(t, L"Assigning NULL", len);
  146. return 4;
  147. default:
  148. wcsncpy(t, L"Internal error", len);
  149. return 9;
  150. }
  151. }
  152. void Script::guruMeditation(SystemObject *script, int code, const wchar_t *pub, int intinfo) {
  153. wchar_t t[256] = {0}, u[256] = {0};
  154. codeToSeverity(code, u, 256);
  155. if (pub)
  156. WCSNPRINTF(t, 256, L"guru: %s - #%04X.%04X%04X", pub, code, (intinfo & 0xFFFF), VCPU::VIP & 0xFFFF);
  157. else
  158. WCSNPRINTF(t, 256, L"guru: #%04X.%04X%04X", pub, code, (intinfo & 0xFFFF), VCPU::VIP & 0xFFFF);
  159. if (*u) {
  160. wcscat(t, L" - ");
  161. wcscat(t, u);
  162. }
  163. DebugStringW(L"%s\n", t);
  164. time_t now;
  165. time(&now);
  166. struct tm *lt = localtime(&now);
  167. #ifdef _WIN32
  168. wchar_t *p = _wasctime(lt);
  169. #else
  170. #warning port me
  171. wchar_t *p = L"port me";
  172. #endif
  173. if (p && *p) p[wcslen(p)-1]=0;
  174. FILE *fout = _wfopen(StringPathCombine(WASABI_API_APP->path_getUserSettingsPath(), L"guru.log"), WF_APPEND_RW);
  175. if (fout)
  176. {
  177. StringPrintfW z(L"%s (%s/%s) - ", p, WASABI_API_SKIN->getSkinName(),script->getFilename());
  178. if (*u)
  179. {
  180. z.cat(u);
  181. z.cat(L" - ");
  182. }
  183. StringPrintfW log(L"%s#%04X.%04X%04X.%d%s%s\n", z.getValue(), code, (intinfo & 0xFFFF), VCPU::VIP & 0xFFFF, VCPU::VSD, pub?L" ":L"", pub?pub:L"");
  184. fputws(log.getValue(), fout);
  185. fclose(fout);
  186. }
  187. Guru::spawn(script, code, pub, intinfo);
  188. #ifdef WASABI_COMPILE_MAKIDEBUG
  189. // WASABI_API_MAKIDEBUG->debugger_createJITD(script->getScriptId(), 1);
  190. // debugApi->debugger_trace();
  191. #endif
  192. }
  193. void Script::setScriptParam(int id, const wchar_t *p)
  194. {
  195. SystemObject *s = SOM::getSystemObjectByScriptId(id);
  196. if (s) s->setParam(p);
  197. }
  198. void Script::setParentGroup(int id, Group *g)
  199. {
  200. SystemObject *s = SOM::getSystemObjectByScriptId(id);
  201. if (s) s->setParentGroup(g);
  202. }
  203. void Script::setSkinPartId(int id, int skinpartid)
  204. {
  205. SystemObject *s = SOM::getSystemObjectByScriptId(id);
  206. if (s) s->setSkinPartId(skinpartid);
  207. }
  208. int Script::varIdToGlobal(int id, int script)
  209. {
  210. return id + VCPU::varBase(script);
  211. }
  212. int Script::getNumEventsLinked()
  213. {
  214. return VCPU::eventsTable.getNumItems();
  215. }
  216. int Script::getLinkedEventParams(int num, int *dlfid, int *scriptid, int *varid)
  217. {
  218. VCPUeventEntry *e = VCPU::eventsTable.enumItem(num);
  219. if (dlfid) *dlfid = e->DLFid;
  220. if (scriptid) *scriptid = e->scriptId;
  221. if (varid) *varid = e->varId;
  222. return num;
  223. }
  224. int Script::getCacheCount()
  225. {
  226. return VCPU::getCacheCount();
  227. }
  228. int Script::getUserAncestor(int varid, int scriptid)
  229. {
  230. return VCPU::getUserAncestor(varid, scriptid);
  231. }
  232. int Script::isValidScriptId(int id)
  233. {
  234. return VCPU::isValidScriptId(id);
  235. }