create_lua_library.py 19 KB

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