create_lua_library.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. import sys
  2. import CppHeaderParser
  3. import os
  4. import errno
  5. import re
  6. from zipfile import *
  7. import fnmatch
  8. def mkdir_p(path): # Same effect as mkdir -p, create dir and all necessary parent dirs
  9. try:
  10. os.makedirs(path)
  11. except OSError as e:
  12. if e.errno == errno.EEXIST: # Dir already exists; not really an error
  13. pass
  14. else: raise
  15. def createLUABindings(inputPath, prefix, mainInclude, libSmallName, libName, apiPath, apiClassPath, includePath, sourcePath):
  16. out = ""
  17. sout = ""
  18. lfout = ""
  19. sout += "#include \"%sLUA.h\"\n" % (prefix)
  20. sout += "#include \"%sLUAWrappers.h\"\n" % (prefix)
  21. sout += "#include \"PolyCoreServices.h\"\n\n"
  22. sout += "using namespace Polycode;\n\n"
  23. sout += "int luaopen_%s(lua_State *L) {\n" % (prefix)
  24. if prefix != "Polycode":
  25. sout += "CoreServices *inst = (CoreServices*)lua_topointer(L, 1);\n"
  26. sout += "CoreServices::setInstance(inst);\n"
  27. sout += "\tstatic const struct luaL_reg %sLib [] = {" % (libSmallName)
  28. out += "#pragma once\n\n"
  29. out += "extern \"C\" {\n\n"
  30. out += "#include <stdio.h>\n"
  31. out += "#include \"lua.h\"\n"
  32. out += "#include \"lualib.h\"\n"
  33. out += "#include \"lauxlib.h\"\n"
  34. out += "} // extern \"C\" \n\n"
  35. files = os.listdir(inputPath)
  36. filteredFiles = []
  37. for fileName in files:
  38. ignore = ["PolyGLSLProgram", "PolyGLSLShader", "PolyGLSLShaderModule", "PolyWinCore", "PolyCocoaCore", "PolyAGLCore", "PolySDLCore", "Poly_iPhone", "PolyGLES1Renderer", "PolyGLRenderer", "tinyxml", "tinystr", "OpenGLCubemap", "PolyiPhoneCore", "PolyGLES1Texture", "PolyGLTexture", "PolyGLVertexBuffer", "PolyThreaded", "PolyGLHeaders", "GLee"]
  39. if fileName.split(".")[1] == "h" and fileName.split(".")[0] not in ignore:
  40. filteredFiles.append(fileName)
  41. out += "#include \"%s\"\n" % (fileName)
  42. out += "\nusing namespace std;\n\n"
  43. out += "\nnamespace Polycode {\n\n"
  44. if prefix == "Polycode":
  45. out += "class LuaEventHandler : public EventHandler {\n"
  46. out += "public:\n"
  47. out += " LuaEventHandler() : EventHandler() {}\n"
  48. out += " void handleEvent(Event *e) {\n"
  49. out += " lua_rawgeti( L, LUA_REGISTRYINDEX, wrapperIndex );\n"
  50. out += " lua_getfield(L, -1, \"__handleEvent\");\n"
  51. out += " lua_rawgeti( L, LUA_REGISTRYINDEX, wrapperIndex );\n"
  52. out += " lua_pushlightuserdata(L, e);\n"
  53. out += " if(lua_pcall(L, 2, 0, 0) != 0) {\n"
  54. out += " const char *msg = lua_tostring(L, -1);\n"
  55. out += " lua_getfield(L, LUA_GLOBALSINDEX, \"__customError\");\n"
  56. out += " lua_pushstring(L, msg);\n"
  57. out += " lua_call(L, 1, 0);\n"
  58. out += " }\n"
  59. out += " }\n"
  60. out += " int wrapperIndex;\n"
  61. out += " lua_State *L;\n"
  62. out += "};\n\n"
  63. for fileName in filteredFiles:
  64. inheritInModule = ["PhysicsSceneEntity", "CollisionScene", "CollisionSceneEntity"]
  65. headerFile = "%s/%s" % (inputPath, fileName)
  66. print "Parsing %s" % fileName
  67. try:
  68. f = open(headerFile)
  69. contents = f.read().replace("_PolyExport", "")
  70. cppHeader = CppHeaderParser.CppHeader(contents, "string")
  71. ignore_classes = ["PolycodeShaderModule", "Object", "Threaded", "OpenGLCubemap", "ParticleEmitter"]
  72. for ckey in cppHeader.classes:
  73. print ">> Parsing class %s" % ckey
  74. c = cppHeader.classes[ckey]
  75. # if ckey == "ParticleEmitter":
  76. # print c
  77. lout = ""
  78. inherits = False
  79. if len(c["inherits"]) > 0:
  80. if c["inherits"][0]["class"] not in ignore_classes:
  81. if c["inherits"][0]["class"] in inheritInModule:
  82. lout += "require \"%s/%s\"\n\n" % (prefix, c["inherits"][0]["class"])
  83. else:
  84. lout += "require \"Polycode/%s\"\n\n" % (c["inherits"][0]["class"])
  85. lout += "class \"%s\" (%s)\n\n" % (ckey, c["inherits"][0]["class"])
  86. inherits = True
  87. if inherits == False:
  88. lout += "class \"%s\"\n\n" % ckey
  89. if len(c["methods"]["public"]) < 2 or ckey in ignore_classes:
  90. continue
  91. if ckey == "OSFileEntry":
  92. print c["methods"]["public"]
  93. parsed_methods = []
  94. ignore_methods = ["readByte32", "readByte16", "getCustomEntitiesByType", "Core", "Renderer", "Shader", "Texture", "handleEvent", "secondaryHandler", "getSTLString"]
  95. lout += "\n\n"
  96. pps = []
  97. for pp in c["properties"]["public"]:
  98. pp["type"] = pp["type"].replace("Polycode::", "")
  99. pp["type"] = pp["type"].replace("std::", "")
  100. if pp["type"].find("static ") != -1:
  101. if "defaltValue" in pp:
  102. lout += "%s = %s\n" % (pp["name"], pp["defaltValue"])
  103. else:
  104. #there are some bugs in the class parser that cause it to return junk
  105. if pp["type"].find("*") == -1 and pp["type"].find("vector") == -1 and pp["name"] != "16" and pp["name"] != "setScale" and pp["name"] != "setPosition" and pp["name"] != "BUFFER_CACHE_PRECISION":
  106. pps.append(pp)
  107. #if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  108. # pps.append(pp)
  109. #else:
  110. # print(">>> Skipping %s[%s %s]" % (ckey, pp["type"], pp["name"]))
  111. pidx = 0
  112. # hack to fix the lack of multiple inheritance
  113. #if ckey == "ScreenParticleEmitter" or ckey == "SceneParticleEmitter":
  114. # pps.append({"name": "emitter", "type": "ParticleEmitter"})
  115. if len(pps) > 0:
  116. lout += "function %s:__index__(name)\n" % ckey
  117. for pp in pps:
  118. pp["type"] = pp["type"].replace("Polycode::", "")
  119. pp["type"] = pp["type"].replace("std::", "")
  120. if pidx == 0:
  121. lout += "\tif name == \"%s\" then\n" % (pp["name"])
  122. else:
  123. lout += "\telseif name == \"%s\" then\n" % (pp["name"])
  124. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  125. lout += "\t\treturn %s.%s_get_%s(self.__ptr)\n" % (libName, ckey, pp["name"])
  126. elif (ckey == "ScreenParticleEmitter" or ckey == "SceneParticleEmitter") and pp["name"] == "emitter":
  127. lout += "\t\tlocal ret = %s(\"__skip_ptr__\")\n" % (pp["type"])
  128. lout += "\t\tret.__ptr = self.__ptr\n"
  129. lout += "\t\treturn ret\n"
  130. else:
  131. lout += "\t\tretVal = %s.%s_get_%s(self.__ptr)\n" % (libName, ckey, pp["name"])
  132. lout += "\t\tif Polycore.__ptr_lookup[retVal] ~= nil then\n"
  133. lout += "\t\t\treturn Polycore.__ptr_lookup[retVal]\n"
  134. lout += "\t\telse\n"
  135. lout += "\t\t\tPolycore.__ptr_lookup[retVal] = %s(\"__skip_ptr__\")\n" % (pp["type"])
  136. lout += "\t\t\tPolycore.__ptr_lookup[retVal].__ptr = retVal\n"
  137. lout += "\t\t\treturn Polycore.__ptr_lookup[retVal]\n"
  138. lout += "\t\tend\n"
  139. if not ((ckey == "ScreenParticleEmitter" or ckey == "SceneParticleEmitter") and pp["name"] == "emitter"):
  140. sout += "\t\t{\"%s_get_%s\", %s_%s_get_%s},\n" % (ckey, pp["name"], libName, ckey, pp["name"])
  141. out += "static int %s_%s_get_%s(lua_State *L) {\n" % (libName, ckey, pp["name"])
  142. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  143. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  144. outfunc = "lua_pushlightuserdata"
  145. retFunc = ""
  146. if pp["type"] == "Number":
  147. outfunc = "lua_pushnumber"
  148. if pp["type"] == "String":
  149. outfunc = "lua_pushstring"
  150. retFunc = ".c_str()"
  151. if pp["type"] == "int":
  152. outfunc = "lua_pushinteger"
  153. if pp["type"] == "bool":
  154. outfunc = "lua_pushboolean"
  155. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  156. out += "\t%s(L, inst->%s%s);\n" % (outfunc, pp["name"], retFunc)
  157. else:
  158. out += "\t%s(L, &inst->%s%s);\n" % (outfunc, pp["name"], retFunc)
  159. out += "\treturn 1;\n"
  160. out += "}\n\n"
  161. pidx = pidx + 1
  162. lout += "\tend\n"
  163. lout += "end\n"
  164. lout += "\n\n"
  165. pidx = 0
  166. if len(pps) > 0:
  167. lout += "function %s:__set_callback(name,value)\n" % ckey
  168. for pp in pps:
  169. pp["type"] = pp["type"].replace("Polycode::", "")
  170. pp["type"] = pp["type"].replace("std::", "")
  171. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  172. if pidx == 0:
  173. lout += "\tif name == \"%s\" then\n" % (pp["name"])
  174. else:
  175. lout += "\telseif name == \"%s\" then\n" % (pp["name"])
  176. lout += "\t\t%s.%s_set_%s(self.__ptr, value)\n" % (libName, ckey, pp["name"])
  177. lout += "\t\treturn true\n"
  178. sout += "\t\t{\"%s_set_%s\", %s_%s_set_%s},\n" % (ckey, pp["name"], libName, ckey, pp["name"])
  179. out += "static int %s_%s_set_%s(lua_State *L) {\n" % (libName, ckey, pp["name"])
  180. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  181. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  182. outfunc = "lua_topointer"
  183. if pp["type"] == "Number":
  184. outfunc = "lua_tonumber"
  185. if pp["type"] == "String":
  186. outfunc = "lua_tostring"
  187. if pp["type"] == "int":
  188. outfunc = "lua_tointeger"
  189. if pp["type"] == "bool":
  190. outfunc = "lua_toboolean"
  191. out += "\t%s param = %s(L, 2);\n" % (pp["type"], outfunc)
  192. out += "\tinst->%s = param;\n" % (pp["name"])
  193. out += "\treturn 0;\n"
  194. out += "}\n\n"
  195. pidx = pidx + 1
  196. if pidx != 0:
  197. lout += "\tend\n"
  198. lout += "\treturn false\n"
  199. lout += "end\n"
  200. lout += "\n\n"
  201. for pm in c["methods"]["public"]:
  202. if pm["name"] in parsed_methods or pm["name"].find("operator") > -1 or pm["name"] in ignore_methods:
  203. continue
  204. if pm["name"] == "~"+ckey or pm["rtnType"].find("<") > -1:
  205. out += ""
  206. else:
  207. basicType = False
  208. voidRet = False
  209. if pm["name"] == ckey:
  210. sout += "\t\t{\"%s\", %s_%s},\n" % (ckey, libName, ckey)
  211. out += "static int %s_%s(lua_State *L) {\n" % (libName, ckey)
  212. idx = 1
  213. else:
  214. sout += "\t\t{\"%s_%s\", %s_%s_%s},\n" % (ckey, pm["name"], libName, ckey, pm["name"])
  215. out += "static int %s_%s_%s(lua_State *L) {\n" % (libName, ckey, pm["name"])
  216. if pm["rtnType"].find("static ") == -1:
  217. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  218. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  219. idx = 2
  220. paramlist = []
  221. lparamlist = []
  222. for param in pm["parameters"]:
  223. if not param.has_key("type"):
  224. continue
  225. if param["type"] == "0":
  226. continue
  227. param["type"] = param["type"].replace("Polycode::", "")
  228. param["type"] = param["type"].replace("std::", "")
  229. param["type"] = param["type"].replace("const", "")
  230. param["type"] = param["type"].replace("&", "")
  231. param["type"] = param["type"].replace(" ", "")
  232. param["type"] = param["type"].replace("long", "long ")
  233. param["type"] = param["type"].replace("unsigned", "unsigned ")
  234. param["name"] = param["name"].replace("end", "_end").replace("repeat", "_repeat")
  235. if"type" in param:
  236. luatype = "LUA_TLIGHTUSERDATA"
  237. checkfunc = "lua_islightuserdata"
  238. if param["type"].find("*") > -1:
  239. luafunc = "(%s)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  240. elif param["type"].find("&") > -1:
  241. luafunc = "*(%s*)lua_topointer" % (param["type"].replace("const", "").replace("&", "").replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  242. else:
  243. luafunc = "*(%s*)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  244. lend = ".__ptr"
  245. if param["type"] == "int" or param["type"] == "unsigned int":
  246. luafunc = "lua_tointeger"
  247. luatype = "LUA_TNUMBER"
  248. checkfunc = "lua_isnumber"
  249. lend = ""
  250. if param["type"] == "bool":
  251. luafunc = "lua_toboolean"
  252. luatype = "LUA_TBOOLEAN"
  253. checkfunc = "lua_isboolean"
  254. lend = ""
  255. if param["type"] == "Number" or param["type"] == "float" or param["type"] == "double":
  256. luatype = "LUA_TNUMBER"
  257. luafunc = "lua_tonumber"
  258. checkfunc = "lua_isnumber"
  259. lend = ""
  260. if param["type"] == "String":
  261. luatype = "LUA_TSTRING"
  262. luafunc = "lua_tostring"
  263. checkfunc = "lua_isstring"
  264. lend = ""
  265. param["type"] = param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle")
  266. if "defaltValue" in param:
  267. if checkfunc != "lua_islightuserdata" or (checkfunc == "lua_islightuserdata" and param["defaltValue"] == "NULL"):
  268. #param["defaltValue"] = param["defaltValue"].replace(" 0f", ".0f")
  269. param["defaltValue"] = param["defaltValue"].replace(": :", "::")
  270. #param["defaltValue"] = param["defaltValue"].replace("0 ", "0.")
  271. param["defaltValue"] = re.sub(r'([0-9]+) ([0-9])+', r'\1.\2', param["defaltValue"])
  272. out += "\t%s %s;\n" % (param["type"], param["name"])
  273. out += "\tif(%s(L, %d)) {\n" % (checkfunc, idx)
  274. out += "\t\t%s = %s(L, %d);\n" % (param["name"], luafunc, idx)
  275. out += "\t} else {\n"
  276. out += "\t\t%s = %s;\n" % (param["name"], param["defaltValue"])
  277. out += "\t}\n"
  278. else:
  279. out += "\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  280. if param["type"] == "String":
  281. out += "\t%s %s = String(%s(L, %d));\n" % (param["type"], param["name"], luafunc, idx)
  282. else:
  283. out += "\t%s %s = %s(L, %d);\n" % (param["type"], param["name"], luafunc, idx)
  284. else:
  285. out += "\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  286. if param["type"] == "String":
  287. out += "\t%s %s = String(%s(L, %d));\n" % (param["type"], param["name"], luafunc, idx)
  288. else:
  289. out += "\t%s %s = %s(L, %d);\n" % (param["type"], param["name"], luafunc, idx)
  290. paramlist.append(param["name"])
  291. lparamlist.append(param["name"]+lend)
  292. idx = idx +1
  293. if pm["name"] == ckey:
  294. if ckey == "EventHandler":
  295. out += "\tLuaEventHandler *inst = new LuaEventHandler();\n"
  296. out += "\tinst->wrapperIndex = luaL_ref(L, LUA_REGISTRYINDEX );\n"
  297. out += "\tinst->L = L;\n"
  298. else:
  299. out += "\t%s *inst = new %s(%s);\n" % (ckey, ckey, ", ".join(paramlist))
  300. out += "\tlua_pushlightuserdata(L, (void*)inst);\n"
  301. out += "\treturn 1;\n"
  302. else:
  303. if pm["rtnType"].find("static ") == -1:
  304. call = "inst->%s(%s)" % (pm["name"], ", ".join(paramlist))
  305. else:
  306. call = "%s::%s(%s)" % (ckey, pm["name"], ", ".join(paramlist))
  307. if pm["rtnType"] == "void" or pm["rtnType"] == "static void" or pm["rtnType"] == "virtual void" or pm["rtnType"] == "inline void":
  308. out += "\t%s;\n" % (call)
  309. basicType = True
  310. voidRet = True
  311. out += "\treturn 0;\n"
  312. else:
  313. outfunc = "lua_pushlightuserdata"
  314. retFunc = ""
  315. basicType = False
  316. if pm["rtnType"] == "Number" or pm["rtnType"] == "inline Number":
  317. outfunc = "lua_pushnumber"
  318. basicType = True
  319. if pm["rtnType"] == "String" or pm["rtnType"] == "static String":
  320. outfunc = "lua_pushstring"
  321. basicType = True
  322. retFunc = ".c_str()"
  323. if pm["rtnType"] == "int" or pm["rtnType"] == "static int" or pm["rtnType"] == "size_t" or pm["rtnType"] == "static size_t" or pm["rtnType"] == "long" or pm["rtnType"] == "unsigned int" or pm["rtnType"] == "static long":
  324. outfunc = "lua_pushinteger"
  325. basicType = True
  326. if pm["rtnType"] == "bool" or pm["rtnType"] == "static bool" or pm["rtnType"] == "virtual bool":
  327. outfunc = "lua_pushboolean"
  328. basicType = True
  329. if pm["rtnType"].find("*") > -1:
  330. out += "\tvoid *ptrRetVal = (void*)%s%s;\n" % (call, retFunc)
  331. out += "\tif(ptrRetVal == NULL) {\n"
  332. out += "\t\tlua_pushnil(L);\n"
  333. out += "\t} else {\n"
  334. out += "\t\t%s(L, ptrRetVal);\n" % (outfunc)
  335. out += "\t}\n"
  336. elif basicType == True:
  337. out += "\t%s(L, %s%s);\n" % (outfunc, call, retFunc)
  338. else:
  339. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "")
  340. if className == "Polygon":
  341. className = "Polycode::Polygon"
  342. if className == "Rectangle":
  343. className = "Polycode::Rectangle"
  344. out += "\t%s *retInst = new %s();\n" % (className, className)
  345. out += "\t*retInst = %s;\n" % (call)
  346. out += "\t%s(L, retInst);\n" % (outfunc)
  347. out += "\treturn 1;\n"
  348. out += "}\n\n"
  349. if pm["name"] == ckey:
  350. lout += "function %s:%s(...)\n" % (ckey, ckey)
  351. if inherits:
  352. lout += "\tif type(arg[1]) == \"table\" and count(arg) == 1 then\n"
  353. lout += "\t\tif \"\"..arg[1]:class() == \"%s\" then\n" % (c["inherits"][0]["class"])
  354. lout += "\t\t\tself.__ptr = arg[1].__ptr\n"
  355. lout += "\t\t\treturn\n"
  356. lout += "\t\tend\n"
  357. lout += "\tend\n"
  358. lout += "\tfor k,v in pairs(arg) do\n"
  359. lout += "\t\tif type(v) == \"table\" then\n"
  360. lout += "\t\t\tif v.__ptr ~= nil then\n"
  361. lout += "\t\t\t\targ[k] = v.__ptr\n"
  362. lout += "\t\t\tend\n"
  363. lout += "\t\tend\n"
  364. lout += "\tend\n"
  365. lout += "\tif self.__ptr == nil and arg[1] ~= \"__skip_ptr__\" then\n"
  366. if ckey == "EventHandler":
  367. lout += "\t\tself.__ptr = %s.%s(self)\n" % (libName, ckey)
  368. else:
  369. lout += "\t\tself.__ptr = %s.%s(unpack(arg))\n" % (libName, ckey)
  370. lout += "\t\tPolycore.__ptr_lookup[self.__ptr] = self\n"
  371. lout += "\tend\n"
  372. lout += "end\n\n"
  373. else:
  374. lout += "function %s:%s(%s)\n" % (ckey, pm["name"], ", ".join(paramlist))
  375. if pm["rtnType"].find("static ") == -1:
  376. if len(lparamlist):
  377. lout += "\tlocal retVal = %s.%s_%s(self.__ptr, %s)\n" % (libName, ckey, pm["name"], ", ".join(lparamlist))
  378. else:
  379. lout += "\tlocal retVal = %s.%s_%s(self.__ptr)\n" % (libName, ckey, pm["name"])
  380. else:
  381. if len(lparamlist):
  382. lout += "\tlocal retVal = %s.%s_%s(%s)\n" % (libName, ckey, pm["name"], ", ".join(lparamlist))
  383. else:
  384. lout += "\tlocal retVal = %s.%s_%s()\n" % (libName, ckey, pm["name"])
  385. if not voidRet:
  386. if basicType == True:
  387. lout += "\treturn retVal\n"
  388. else:
  389. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "").replace("*","").replace(" ", "")
  390. lout += "\tif retVal == nil then return nil end\n"
  391. lout += "\tif Polycore.__ptr_lookup[retVal] ~= nil then\n"
  392. lout += "\t\treturn Polycore.__ptr_lookup[retVal]\n"
  393. lout += "\telse\n"
  394. lout += "\t\tPolycore.__ptr_lookup[retVal] = %s(\"__skip_ptr__\")\n" % (className)
  395. lout += "\t\tPolycore.__ptr_lookup[retVal].__ptr = retVal\n"
  396. lout += "\t\treturn Polycore.__ptr_lookup[retVal]\n"
  397. lout += "\tend\n"
  398. lout += "end\n\n"
  399. parsed_methods.append(pm["name"])
  400. #cleanup
  401. sout += "\t\t{\"delete_%s\", %s_delete_%s},\n" % (ckey, libName, ckey)
  402. out += "static int %s_delete_%s(lua_State *L) {\n" % (libName, ckey)
  403. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  404. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  405. out += "\tdelete inst;\n"
  406. out += "\treturn 0;\n"
  407. out += "}\n\n"
  408. lout += "\n\n"
  409. lout += "function %s:__delete()\n" % (ckey)
  410. lout += "\tPolycore.__ptr_lookup[self.__ptr] = nil\n"
  411. lout += "\t%s.delete_%s(self.__ptr)\n" % (libName, ckey)
  412. lout += "end\n"
  413. if ckey == "EventHandler":
  414. lout += "\n\n"
  415. lout += "function EventHandler:__handleEvent(event)\n"
  416. lout += "\tevt = Event(\"__skip_ptr__\")\n"
  417. lout += "\tevt.__ptr = event\n"
  418. lout += "\tself:handleEvent(evt)\n"
  419. #lout += "\tself:handleEvent(event)\n"
  420. lout += "end\n"
  421. lfout += "require \"%s/%s\"\n" % (prefix, ckey)
  422. mkdir_p(apiClassPath)
  423. fout = open("%s/%s.lua" % (apiClassPath, ckey), "w")
  424. fout.write(lout)
  425. except CppHeaderParser.CppParseError, e:
  426. print e
  427. sys.exit(1)
  428. out += "} // namespace Polycode\n"
  429. sout += "\t\t{NULL, NULL}\n"
  430. sout += "\t};\n"
  431. sout += "\tluaL_openlib(L, \"%s\", %sLib, 0);\n" % (libName, libSmallName)
  432. sout += "\treturn 1;\n"
  433. sout += "}"
  434. shout = ""
  435. shout += "#pragma once\n"
  436. shout += "#include <%s>\n" % (mainInclude)
  437. shout += "extern \"C\" {\n"
  438. shout += "#include <stdio.h>\n"
  439. shout += "#include \"lua.h\"\n"
  440. shout += "#include \"lualib.h\"\n"
  441. shout += "#include \"lauxlib.h\"\n"
  442. shout += "int _PolyExport luaopen_%s(lua_State *L);\n" % (prefix)
  443. shout += "}\n"
  444. mkdir_p(includePath)
  445. mkdir_p(apiPath)
  446. mkdir_p(sourcePath)
  447. fout = open("%s/%sLUA.h" % (includePath, prefix), "w")
  448. fout.write(shout)
  449. fout = open("%s/%s.lua" % (apiPath, prefix), "w")
  450. fout.write(lfout)
  451. fout = open("%s/%sLUAWrappers.h" % (includePath, prefix), "w")
  452. fout.write(out)
  453. fout = open("%s/%sLUA.cpp" % (sourcePath, prefix), "w")
  454. fout.write(sout)
  455. pattern = '*.lua'
  456. os.chdir(apiPath)
  457. if libName == "Polycore":
  458. with ZipFile("api.pak", 'w') as myzip:
  459. for root, dirs, files in os.walk("."):
  460. for filename in fnmatch.filter(files, pattern):
  461. myzip.write(os.path.join(root, filename))
  462. #print cppHeader
  463. createLUABindings(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7], sys.argv[8], sys.argv[9])