AUTOMATIC1111

Untitled

Mar 27th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. #include "lua-functions.h"
  2. #include "utils.h"
  3. #include "os.h"
  4. #include <windows.h>
  5. #include "sdl-utils.h"
  6. #include "LuaBridge/LuaBridge.h"
  7.  
  8. using namespace luabridge;
  9.  
  10. #include <SDL.h>
  11. #include "SDL_syswm.h"
  12. #include <vector>
  13. #include <algorithm>
  14.  
  15. int listDirectoryFull(lua_State *L, int mode) {
  16. const char *dirname = lua_tostring(L, 1);
  17.  
  18. lua_newtable(L);
  19.  
  20. WIN32_FIND_DATAA fdFile;
  21. HANDLE handle = NULL;
  22.  
  23. std::string path = format("%s\\*.*", dirname);
  24. if((handle = FindFirstFileA(path.c_str(), &fdFile)) == INVALID_HANDLE_VALUE) {
  25. return 1;
  26. }
  27.  
  28. int index = 0;
  29. do {
  30. if(strcmp(fdFile.cFileName, ".") == 0 || strcmp(fdFile.cFileName, "..") == 0)
  31. continue;
  32.  
  33. int isDir = fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? 1 : 0;
  34. if(mode != -1 && mode != isDir)
  35. continue;
  36.  
  37. lua_pushnumber(L, index++);
  38. lua_pushstring(L, fdFile.cFileName);
  39. lua_settable(L, -3);
  40.  
  41. } while(FindNextFileA(handle, &fdFile));
  42.  
  43. FindClose(handle);
  44.  
  45. return 1;
  46. }
  47.  
  48. int listDirectory(lua_State *L) {
  49. return listDirectoryFull(L, -1);
  50. }
  51.  
  52. int listDirectoryFiles(lua_State *L) {
  53. return listDirectoryFull(L, 0);
  54. }
  55.  
  56. int listDirectoryDirs(lua_State *L) {
  57. return listDirectoryFull(L, 1);
  58. }
  59.  
  60. int messagebox(lua_State *L) {
  61. const char *text = lua_tostring(L, 1);
  62.  
  63. MessageBoxA(NULL, text, "Message", 0);
  64.  
  65. return 0;
  66. }
  67.  
  68. int geti1(lua_State *L) {
  69. unsigned char *p = (unsigned char *)lua_touserdata(L, 1);
  70. int offset = luaL_checkint(L, 2);
  71. lua_pushnumber(L, p[offset]);
  72.  
  73. return 1;
  74. }
  75. int addr(lua_State *L) {
  76. void *p = (void *)lua_touserdata(L, 1);
  77. lua_pushnumber(L, (unsigned int) p);
  78.  
  79. return 1;
  80. }
  81.  
  82. struct CObject {
  83. void *p;
  84.  
  85. CObject(LuaRef ref) {
  86. lua_State *L = ref.state();
  87. ref.push(L);
  88. p = (void *)lua_touserdata(L, -1);
  89. lua_pop(L, 1);
  90.  
  91. if(p == NULL) return;
  92. p = ((void **)p)[0];
  93. p = ((void **)p)[2];
  94. }
  95.  
  96. unsigned char i8(int offset) {
  97. if(p == NULL) return 0;
  98.  
  99. return ((unsigned char *)p)[offset];
  100. }
  101. unsigned char wi8(int offset,unsigned char value) {
  102. if(p == NULL) return value;
  103.  
  104. return ((unsigned char *)p)[offset] = value;
  105. }
  106. };
  107.  
  108. namespace event{
  109. int quit = SDL_QUIT;
  110. int keydown = SDL_KEYDOWN;
  111. int keyup = SDL_KEYUP;
  112. int mousemotion = SDL_MOUSEMOTION;
  113. int mousebuttondown = SDL_MOUSEBUTTONDOWN;
  114. int mousebuttonup = SDL_MOUSEBUTTONUP;
  115. int mousewheel = SDL_MOUSEWHEEL;
  116. }
  117.  
  118. struct DrawHook :public SDL::DrawHook {
  119. LuaRef ref;
  120.  
  121. DrawHook(LuaRef r) :ref(r) {
  122.  
  123. }
  124.  
  125. void draw(SDL::Screen &screen) {
  126. try {
  127. ref(screen);
  128. } catch(luabridge::LuaException const& e) {
  129. panic(e.what());
  130. }
  131. }
  132. };
  133.  
  134. struct EventHook :public SDL::EventHook {
  135. LuaRef ref;
  136.  
  137. EventHook(LuaRef r) :ref(r) {
  138.  
  139. }
  140.  
  141. bool handle(SDL::Event &evt) {
  142. try {
  143. return ref(evt);
  144. } catch(luabridge::LuaException const& e) {
  145. panic(e.what());
  146.  
  147. return false;
  148. }
  149. }
  150. };
  151.  
  152. static std::vector<SDL::Color *> createColormapFromLua(LuaRef & r) {
  153. std::vector<SDL::Color *> res;
  154.  
  155. try {
  156. for(int i = 1; i <= r.length(); i++) {
  157. SDL::Color *a = r[i].cast<SDL::Color *>();
  158. res.push_back(a);
  159. }
  160. } catch(luabridge::LuaException const& e) {
  161. panic(e.what());
  162. }
  163.  
  164. return res;
  165. }
  166.  
  167. struct SurfaceColorMapped :public SDL::Surface {
  168. SurfaceColorMapped(SDL::Surface *parent, LuaRef r) :SDL::Surface(parent, createColormapFromLua(r)) {
  169.  
  170. }
  171. };
  172.  
  173. void installFunctions(lua_State *L) {
  174. luaL_openlibs(L);
  175.  
  176. getGlobalNamespace(L)
  177. .beginNamespace("sdl")
  178.  
  179. .beginClass <SDL::Color>("color")
  180. .addData("r", &SDL::Color::r)
  181. .addData("g", &SDL::Color::g)
  182. .addData("b", &SDL::Color::b)
  183. .addData("a", &SDL::Color::a)
  184. .endClass()
  185.  
  186. .deriveClass<SDL::Color, SDL::Color>("rgba")
  187. .addConstructor <void(*) (int r, int g, int b, int a)>()
  188. .endClass()
  189.  
  190. .deriveClass<SDL::Color, SDL::Color>("rgb")
  191. .addConstructor <void(*) (int r, int g, int b)>()
  192. .endClass()
  193.  
  194. .beginClass<ResourceDatFile>("resourceDat")
  195. .addConstructor <void(*) (const std::string & filename)>()
  196. .addFunction("reload", &ResourceDatFile::reload)
  197. .endClass()
  198.  
  199. .beginClass<Blob>("blob")
  200. .addData("length", &Blob::length, false)
  201. .endClass()
  202.  
  203. .deriveClass<BlobFromFile, Blob>("blobFromFile")
  204. .addConstructor <void(*) (const std::string & filename)>()
  205. .endClass()
  206.  
  207. .deriveClass<BlobFromResourceDat, Blob>("blobFromResourceDat")
  208. .addConstructor <void(*) (const ResourceDatFile *dat, const std::string & filename)>()
  209. .endClass()
  210.  
  211. .beginClass <SDL::TextSettings>("textsettings")
  212. .addConstructor <void(*) ()>()
  213. .addData("antialias", &SDL::TextSettings::antialias)
  214. .addData("color", &SDL::TextSettings::color)
  215. .addData("outlineWidth", &SDL::TextSettings::outlineWidth)
  216. .addData("outlineColor", &SDL::TextSettings::outlineColor)
  217. .endClass()
  218.  
  219. .beginClass <SDL::Font>("font")
  220. .addConstructor <void(*) (const std::string & name, double size)>()
  221. .endClass()
  222.  
  223. .deriveClass <SDL::FileFont, SDL::Font>("filefont")
  224. .addConstructor <void(*) (const std::string & name, double size)>()
  225. .endClass()
  226.  
  227. .deriveClass <SDL::FileFont, SDL::Font>("filefontFromBlob")
  228. .addConstructor <void(*) (const Blob *blob, double size)>()
  229. .endClass()
  230.  
  231. .beginClass <SDL::Surface>("surface")
  232. .addConstructor <void(*) (const std::string & s)>()
  233. .addFunction("w", &SDL::Surface::w)
  234. .addFunction("h", &SDL::Surface::h)
  235. .addData("x", &SDL::Surface::x, false)
  236. .addData("y", &SDL::Surface::y, false)
  237. .addFunction("wasDrawn", &SDL::Surface::wasDrawn)
  238. .endClass()
  239.  
  240. .deriveClass<SDL::Surface, SDL::Surface>("surfaceFromBlob")
  241. .addConstructor <void(*) (Blob *blob)>()
  242. .endClass()
  243.  
  244. .deriveClass<SDL::Surface, SDL::Surface>("text")
  245. .addConstructor <void(*) (const SDL::Font *, const SDL::TextSettings *settings, const std::string & s)>()
  246. .endClass()
  247.  
  248. .deriveClass<SDL::Surface, SDL::Surface>("outlined")
  249. .addConstructor <void(*) (SDL::Surface *base, int levels, SDL::Color *color)>()
  250. .endClass()
  251.  
  252. .deriveClass<SDL::Surface, SDL::Surface>("scaled")
  253. .addConstructor <void(*) (int scaling, SDL::Surface *base)>()
  254. .endClass()
  255.  
  256.  
  257. .deriveClass<SurfaceColorMapped, SDL::Surface>("colormapped")
  258. .addConstructor <void(*) (SDL::Surface *parent, LuaRef r)>()
  259. .endClass()
  260.  
  261. .deriveClass<SDL::SurfaceScreenshot, SDL::Surface>("screenshot")
  262. .addConstructor <void(*) ()>()
  263. .endClass()
  264.  
  265. .beginClass <SDL::Screen>("screen")
  266. .addConstructor <void(*) ()>()
  267. .addFunction("w", &SDL::Screen::w)
  268. .addFunction("h", &SDL::Screen::h)
  269. .addFunction("begin", &SDL::Screen::begin)
  270. .addFunction("finish", &SDL::Screen::finish)
  271. .addFunction("blit", &SDL::Screen::blit)
  272. .addFunction("drawrect", &SDL::Screen::drawrect)
  273. .addFunction("clip", &SDL::Screen::clip)
  274. .addFunction("unclip", &SDL::Screen::unclip)
  275. .endClass()
  276.  
  277. .beginClass <DrawHook>("drawHook")
  278. .addConstructor <void(*) (LuaRef r)>()
  279. .endClass()
  280.  
  281. .beginClass <EventHook>("eventHook")
  282. .addConstructor <void(*) (LuaRef r)>()
  283. .endClass()
  284.  
  285. .beginClass <SDL::Event>("event")
  286. .addConstructor <void(*) ()>()
  287. .addFunction("type", &SDL::Event::type)
  288. .addFunction("x", &SDL::Event::x)
  289. .addFunction("y", &SDL::Event::y)
  290. .addFunction("wheel", &SDL::Event::wheely)
  291. .addFunction("keycode", &SDL::Event::keycode)
  292. .addFunction("mousebutton", &SDL::Event::mousebutton)
  293. .endClass()
  294.  
  295. .deriveClass <SDL::EventLoop, SDL::Event>("eventloop")
  296. .addConstructor <void(*) ()>()
  297. .addFunction("next", &SDL::EventLoop::next)
  298. .endClass()
  299.  
  300. .beginClass <SDL::Rect>("rect")
  301. .addConstructor <void(*) (int, int, int, int)>()
  302. .addData("x", &SDL::Rect::x)
  303. .addData("y", &SDL::Rect::y)
  304. .addData("w", &SDL::Rect::w)
  305. .addData("h", &SDL::Rect::h)
  306. .endClass()
  307.  
  308. .beginClass <SDL::Timer>("timer")
  309. .addConstructor <void(*) ()>()
  310. .addFunction("elapsed", &SDL::Timer::elapsed)
  311. .addFunction("reset", &SDL::Timer::reset)
  312. .endClass()
  313.  
  314. .beginNamespace("events")
  315. .addVariable("quit", &event::quit, false)
  316. .addVariable("keydown", &event::keydown, false)
  317. .addVariable("keyup", &event::keyup, false)
  318. .addVariable("mousemotion", &event::mousemotion, false)
  319. .addVariable("mousebuttondown", &event::mousebuttondown, false)
  320. .addVariable("mousebuttonup", &event::mousebuttonup, false)
  321. .addVariable("mousewheel", &event::mousewheel, false)
  322. .endNamespace()
  323.  
  324. .beginNamespace("mouse")
  325. .addFunction("x", SDL::mousex)
  326. .addFunction("y", SDL::mousey)
  327. .endNamespace()
  328.  
  329. .endNamespace();
  330.  
  331. getGlobalNamespace(L)
  332. .beginNamespace("os")
  333.  
  334. .addFunction("log", OS::log)
  335. .addFunction("isshiftdown", OS::isshiftdown)
  336. .addFunction("mtime", OS::mtime)
  337. .addFunction("getKnownFolder", OS::getKnownFolder)
  338. .addFunction("mkdir", OS::mkdir)
  339.  
  340. .addCFunction("messagebox", &messagebox)
  341. .addCFunction("listall", &listDirectory)
  342. .addCFunction("listfiles", &listDirectoryFiles)
  343. .addCFunction("listdirs", &listDirectoryDirs)
  344.  
  345. .endNamespace();
  346.  
  347. getGlobalNamespace(L)
  348. .beginNamespace("hacks")
  349.  
  350. .beginClass <CObject>("cobj")
  351. .addConstructor <void(*) (LuaRef ref)>()
  352. .addFunction("i8", &CObject::i8)
  353. .addFunction("wi8", &CObject::wi8)
  354. .endClass()
  355.  
  356. .addCFunction("addr", &addr)
  357. .addCFunction("i1", &geti1)
  358. .endNamespace();
  359.  
  360. }
  361.  
  362. void installAutoexec(lua_State *L) {
  363. std::vector<std::string> items;
  364.  
  365. listDir("scripts/autoexec", [&](const std::string &filename, bool isDir) {
  366. if(!isDir) items.emplace_back(filename);
  367. });
  368.  
  369. std::sort(items.begin(), items.end());
  370. for(const std::string & filename : items) {
  371. if(luaL_loadfile(L, filename.c_str()) == 0) {
  372. lua_pcall(L, 0, 0, 0);
  373. }
  374. }
  375. }
Advertisement
Add Comment
Please, Sign In to add comment