create_lua_library.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import sys
  2. import CppHeaderParser
  3. import os
  4. out = ""
  5. sout = ""
  6. lfout = ""
  7. sout += "#include \"PolycodeLUA.h\"\n"
  8. sout += "#include \"PolycodeLUAWrappers.h\"\n\n"
  9. sout += "int luaopen_Polycode(lua_State *L) {\n"
  10. sout += "\tstatic const struct luaL_reg polycodeLib [] = {"
  11. out += "#pragma once\n\n"
  12. out += "#include <Polycode.h>\n\n"
  13. out += "#include <PolyGLRenderer.h>\n\n"
  14. out += "extern \"C\" {\n\n"
  15. out += "#include <stdio.h>\n"
  16. out += "#include \"lua.h\"\n"
  17. out += "#include \"lualib.h\"\n"
  18. out += "#include \"lauxlib.h\"\n\n"
  19. out += "class LuaEventHandler : public EventHandler {\n"
  20. out += "public:\n"
  21. out += " LuaEventHandler() : EventHandler() {}\n"
  22. out += " ~LuaEventHandler();\n"
  23. out += " void handleEvent(Event *e) {\n"
  24. out += " lua_rawgeti( L, LUA_REGISTRYINDEX, wrapperIndex );\n"
  25. out += " lua_getfield(L, -1, \"__handleEvent\");\n"
  26. out += " lua_rawgeti( L, LUA_REGISTRYINDEX, wrapperIndex );\n"
  27. out += " lua_pushlightuserdata(L, e);\n"
  28. out += " lua_call(L, 2, 0);\n"
  29. out += " }\n"
  30. out += " int wrapperIndex;\n"
  31. out += " lua_State *L;\n"
  32. out += "};\n"
  33. files = os.listdir("../../../Core/Contents/Include")
  34. for fileName in files:
  35. ignore = ["PolyCocoaCore", "PolyAGLCore", "PolyGLES1Renderer", "PolyGLRenderer", "tinyxml", "tinystr", "PolyiPhoneCore", "PolyGLES1Texture", "PolyGLTexture", "PolyGLVertexBuffer", "PolyThreaded"]
  36. if fileName.split(".")[1] == "h" and fileName.split(".")[0] not in ignore:
  37. headerFile = "../../../Core/Contents/Include/%s" % fileName
  38. print "Parsing %s" % fileName
  39. try:
  40. f = open(headerFile)
  41. contents = f.read().replace("_PolyExport", "")
  42. cppHeader = CppHeaderParser.CppHeader(contents, "string")
  43. ignore_classes = ["PolycodeShaderModule", "Object", "Threaded"]
  44. for ckey in cppHeader.classes:
  45. c = cppHeader.classes[ckey]
  46. lout = ""
  47. inherits = False
  48. if len(c["inherits"]) > 0:
  49. if c["inherits"][0]["class"] not in ignore_classes:
  50. lout += "require \"Polycode/%s\"\n\n" % (c["inherits"][0]["class"])
  51. lout += "class \"%s\" (%s)\n\n" % (ckey, c["inherits"][0]["class"])
  52. inherits = True
  53. if inherits == False:
  54. lout += "class \"%s\"\n\n" % ckey
  55. if len(c["methods"]["public"]) < 2 or ckey in ignore_classes:
  56. continue
  57. if ckey == "OSFileEntry":
  58. print c["methods"]["public"]
  59. parsed_methods = []
  60. ignore_methods = ["readByte32", "readByte16", "getCustomEntitiesByType", "Core","ParticleEmitter", "Renderer", "Shader", "Texture", "handleEvent", "secondaryHandler"]
  61. lout += "\n\n"
  62. pps = []
  63. for pp in c["properties"]["public"]:
  64. if pp["type"].find("static ") != -1:
  65. lout += "%s = %s\n" % (pp["name"], pp["defaltValue"])
  66. else:
  67. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  68. pps.append(pp)
  69. #else:
  70. # print(">>> Skipping %s[%s %s]" % (ckey, pp["type"], pp["name"]))
  71. pidx = 0
  72. if len(pps) > 0:
  73. lout += "function %s:__index__(name)\n" % ckey
  74. for pp in pps:
  75. if pidx == 0:
  76. lout += "\tif name == \"%s\" then\n" % (pp["name"])
  77. else:
  78. lout += "\telseif name == \"%s\" then\n" % (pp["name"])
  79. lout += "\t\treturn Polycore.%s_get_%s(self.__ptr)\n" % (ckey, pp["name"])
  80. sout += "\t\t{\"%s_get_%s\", Polycore_%s_get_%s},\n" % (ckey, pp["name"], ckey, pp["name"])
  81. out += "static int Polycore_%s_get_%s(lua_State *L) {\n" % (ckey, pp["name"])
  82. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  83. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey.replace("Polygon", "Polycode::Polygon"), ckey.replace("Polygon", "Polycode::Polygon"))
  84. outfunc = "lua_pushlightuserdata"
  85. retFunc = ""
  86. if pp["type"] == "Number":
  87. outfunc = "lua_pushnumber"
  88. if pp["type"] == "String":
  89. outfunc = "lua_pushstring"
  90. retFunc = ".c_str()"
  91. if pp["type"] == "int":
  92. outfunc = "lua_pushinteger"
  93. if pp["type"] == "bool":
  94. outfunc = "lua_pushboolean"
  95. out += "\t%s(L, inst->%s%s);\n" % (outfunc, pp["name"], retFunc)
  96. out += "\treturn 1;\n"
  97. out += "}\n\n"
  98. pidx = pidx + 1
  99. lout += "\tend\n"
  100. lout += "end\n"
  101. lout += "\n\n"
  102. pidx = 0
  103. if len(pps) > 0:
  104. lout += "function %s:__set_callback(name,value)\n" % ckey
  105. for pp in pps:
  106. if pidx == 0:
  107. lout += "\tif name == \"%s\" then\n" % (pp["name"])
  108. else:
  109. lout += "\telseif name == \"%s\" then\n" % (pp["name"])
  110. lout += "\t\tPolycore.%s_set_%s(self.__ptr, value)\n" % (ckey, pp["name"])
  111. lout += "\t\treturn true\n"
  112. sout += "\t\t{\"%s_set_%s\", Polycore_%s_set_%s},\n" % (ckey, pp["name"], ckey, pp["name"])
  113. out += "static int Polycore_%s_set_%s(lua_State *L) {\n" % (ckey, pp["name"])
  114. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  115. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey.replace("Polygon", "Polycode::Polygon"), ckey.replace("Polygon", "Polycode::Polygon"))
  116. outfunc = "lua_topointer"
  117. if pp["type"] == "Number":
  118. outfunc = "lua_tonumber"
  119. if pp["type"] == "String":
  120. outfunc = "lua_tostring"
  121. if pp["type"] == "int":
  122. outfunc = "lua_tointeger"
  123. if pp["type"] == "bool":
  124. outfunc = "lua_toboolean"
  125. out += "\t%s param = %s(L, 2);\n" % (pp["type"], outfunc)
  126. out += "\tinst->%s = param;\n" % (pp["name"])
  127. out += "\treturn 0;\n"
  128. out += "}\n\n"
  129. pidx = pidx + 1
  130. lout += "\tend\n"
  131. lout += "\treturn false\n"
  132. lout += "end\n"
  133. lout += "\n\n"
  134. for pm in c["methods"]["public"]:
  135. if pm["name"] in parsed_methods or pm["name"].find("operator") > -1 or pm["name"] in ignore_methods:
  136. continue
  137. if pm["name"] == "~"+ckey or pm["rtnType"].find("<") > -1:
  138. out += ""
  139. else:
  140. basicType = False
  141. voidRet = False
  142. if pm["name"] == ckey:
  143. sout += "\t\t{\"%s\", Polycore_%s},\n" % (ckey, ckey)
  144. out += "static int Polycore_%s(lua_State *L) {\n" % (ckey)
  145. idx = 1
  146. else:
  147. sout += "\t\t{\"%s_%s\", Polycore_%s_%s},\n" % (ckey, pm["name"], ckey, pm["name"])
  148. out += "static int Polycore_%s_%s(lua_State *L) {\n" % (ckey, pm["name"])
  149. if pm["rtnType"].find("static ") == -1:
  150. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  151. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey.replace("Polygon", "Polycode::Polygon"), ckey.replace("Polygon", "Polycode::Polygon"))
  152. idx = 2
  153. paramlist = []
  154. lparamlist = []
  155. for param in pm["parameters"]:
  156. if not param.has_key("type"):
  157. continue
  158. if param["type"] == "0":
  159. continue
  160. param["name"] = param["name"].replace("end", "_end").replace("repeat", "_repeat")
  161. if"type" in param:
  162. luatype = "LUA_TLIGHTUSERDATA"
  163. checkfunc = "lua_islightuserdata"
  164. if param["type"].find("*") > -1:
  165. luafunc = "(%s)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon"))
  166. elif param["type"].find("&") > -1:
  167. luafunc = "*(%s*)lua_topointer" % (param["type"].replace("const", "").replace("&", "").replace("Polygon", "Polycode::Polygon"))
  168. else:
  169. luafunc = "*(%s*)lua_topointer" % (param["type"])
  170. lend = ".__ptr"
  171. if param["type"] == "int" or param["type"] == "unsigned int":
  172. luafunc = "lua_tointeger"
  173. luatype = "LUA_TNUMBER"
  174. checkfunc = "lua_isnumber"
  175. lend = ""
  176. if param["type"] == "bool":
  177. luafunc = "lua_toboolean"
  178. luatype = "LUA_TBOOLEAN"
  179. checkfunc = "lua_isboolean"
  180. lend = ""
  181. if param["type"] == "Number":
  182. luatype = "LUA_TNUMBER"
  183. luafunc = "lua_tonumber"
  184. checkfunc = "lua_isnumber"
  185. lend = ""
  186. if param["type"] == "String":
  187. luatype = "LUA_TSTRING"
  188. luafunc = "lua_tostring"
  189. checkfunc = "lua_isstring"
  190. lend = ""
  191. param["type"] = param["type"].replace("Polygon", "Polycode::Polygon")
  192. if "defaltValue" in param and checkfunc != "lua_islightuserdata":
  193. param["defaltValue"] = param["defaltValue"].replace(" 0f", ".0f")
  194. param["defaltValue"] = param["defaltValue"].replace(": :", "::")
  195. out += "\t%s %s;\n" % (param["type"], param["name"])
  196. out += "\tif(%s(L, %d)) {\n" % (checkfunc, idx)
  197. out += "\t\t%s = %s(L, %d);\n" % (param["name"], luafunc, idx)
  198. out += "\t} else {\n"
  199. out += "\t\t%s = %s;\n" % (param["name"], param["defaltValue"])
  200. out += "\t}\n"
  201. else:
  202. out += "\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  203. out += "\t%s %s = %s(L, %d);\n" % (param["type"], param["name"], luafunc, idx)
  204. paramlist.append(param["name"])
  205. lparamlist.append(param["name"]+lend)
  206. idx = idx +1
  207. if pm["name"] == ckey:
  208. if ckey == "EventHandler":
  209. out += "\tLuaEventHandler *inst = new LuaEventHandler();\n"
  210. out += "\tinst->wrapperIndex = luaL_ref(L, LUA_REGISTRYINDEX );\n"
  211. out += "\tinst->L = L;\n"
  212. else:
  213. out += "\t%s *inst = new %s(%s);\n" % (ckey.replace("Polygon", "Polycode::Polygon"), ckey.replace("Polygon", "Polycode::Polygon"), ", ".join(paramlist))
  214. out += "\tlua_pushlightuserdata(L, (void*)inst);\n"
  215. out += "\treturn 1;\n"
  216. else:
  217. if pm["rtnType"].find("static ") == -1:
  218. call = "inst->%s(%s)" % (pm["name"], ", ".join(paramlist))
  219. else:
  220. call = "%s::%s(%s)" % (ckey, pm["name"], ", ".join(paramlist))
  221. if pm["rtnType"] == "void" or pm["rtnType"] == "static void" or pm["rtnType"] == "virtual void" or pm["rtnType"] == "inline void":
  222. out += "\t%s;\n" % (call)
  223. basicType = True
  224. voidRet = True
  225. out += "\treturn 0;\n"
  226. else:
  227. outfunc = "lua_pushlightuserdata"
  228. retFunc = ""
  229. basicType = False
  230. if pm["rtnType"] == "Number" or pm["rtnType"] == "inline Number":
  231. outfunc = "lua_pushnumber"
  232. basicType = True
  233. if pm["rtnType"] == "String" or pm["rtnType"] == "static String":
  234. outfunc = "lua_pushstring"
  235. basicType = True
  236. retFunc = ".c_str()"
  237. 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":
  238. outfunc = "lua_pushinteger"
  239. basicType = True
  240. if pm["rtnType"] == "bool" or pm["rtnType"] == "static bool" or pm["rtnType"] == "virtual bool":
  241. outfunc = "lua_pushboolean"
  242. basicType = True
  243. if pm["rtnType"].find("*") > -1:
  244. out += "\t%s(L, (void*)%s%s);\n" % (outfunc, call, retFunc)
  245. elif basicType == True:
  246. out += "\t%s(L, %s%s);\n" % (outfunc, call, retFunc)
  247. else:
  248. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "")
  249. if className == "Polygon":
  250. className = "Polycode::Polygon"
  251. out += "\t%s *retInst = new %s();\n" % (className, className)
  252. out += "\t*retInst = %s;\n" % (call)
  253. out += "\t%s(L, retInst);\n" % (outfunc)
  254. out += "\treturn 1;\n"
  255. out += "}\n\n"
  256. if pm["name"] == ckey:
  257. lout += "function %s:%s(...)\n" % (ckey, ckey)
  258. lout += "\tfor k,v in pairs(arg) do\n"
  259. lout += "\t\tif type(v) == \"table\" then\n"
  260. lout += "\t\t\tif v.__ptr ~= nil then\n"
  261. lout += "\t\t\t\targ[k] = v.__ptr\n"
  262. lout += "\t\t\tend\n"
  263. lout += "\t\tend\n"
  264. lout += "\tend\n"
  265. lout += "\tif self.__ptr == nil and arg[1] ~= \"__skip_ptr__\" then\n"
  266. if ckey == "EventHandler":
  267. lout += "\t\tself.__ptr = Polycore.%s(self)\n" % (ckey)
  268. else:
  269. lout += "\t\tself.__ptr = Polycore.%s(unpack(arg))\n" % (ckey)
  270. lout += "\t\tPolycore.__ptr_lookup[self.__ptr] = self\n"
  271. lout += "\tend\n"
  272. lout += "end\n\n"
  273. else:
  274. lout += "function %s:%s(%s)\n" % (ckey, pm["name"], ", ".join(paramlist))
  275. if pm["rtnType"].find("static ") == -1:
  276. if len(lparamlist):
  277. lout += "\tlocal retVal = Polycore.%s_%s(self.__ptr, %s)\n" % (ckey, pm["name"], ", ".join(lparamlist))
  278. else:
  279. lout += "\tlocal retVal = Polycore.%s_%s(self.__ptr)\n" % (ckey, pm["name"])
  280. else:
  281. if len(lparamlist):
  282. lout += "\tlocal retVal = Polycore.%s_%s(%s)\n" % (ckey, pm["name"], ", ".join(lparamlist))
  283. else:
  284. lout += "\tlocal retVal = Polycore.%s_%s()\n" % (ckey, pm["name"])
  285. if not voidRet:
  286. if basicType == True:
  287. lout += "\treturn retVal\n"
  288. else:
  289. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "").replace("*","").replace(" ", "")
  290. lout += "\tif Polycore.__ptr_lookup[retVal] ~= nil then\n"
  291. lout += "\t\treturn Polycore.__ptr_lookup[retVal]\n"
  292. lout += "\telse\n"
  293. lout += "\t\tPolycore.__ptr_lookup[retVal] = %s(\"__skip_ptr__\")\n" % (className)
  294. lout += "\t\tPolycore.__ptr_lookup[retVal].__ptr = retVal\n"
  295. lout += "\t\treturn Polycore.__ptr_lookup[retVal]\n"
  296. lout += "\tend\n"
  297. lout += "end\n\n"
  298. parsed_methods.append(pm["name"])
  299. if ckey == "EventHandler":
  300. lout += "\n\n"
  301. lout += "function EventHandler:__handleEvent(event)\n"
  302. lout += "\tevt = Event(\"__skip_ptr__\")\n"
  303. lout += "\tevt.__ptr = event\n"
  304. lout += "\tself:handleEvent(evt)\n"
  305. #lout += "\tself:handleEvent(event)\n"
  306. lout += "end\n"
  307. lfout += "require \"Polycode/%s\"\n" % ckey
  308. fout = open("../../Contents/LUA/API/Polycode/%s.lua" % ckey, "w")
  309. fout.write(lout)
  310. except CppHeaderParser.CppParseError, e:
  311. print e
  312. sys.exit(1)
  313. out += "}"
  314. sout += "\t\t{NULL, NULL}\n"
  315. sout += "\t};\n"
  316. sout += "\tluaL_openlib(L, \"Polycore\", polycodeLib, 0);\n"
  317. sout += "\treturn 1;\n"
  318. sout += "}"
  319. fout = open("../../Contents/LUA/API/Polycode.lua", "w")
  320. fout.write(lfout)
  321. fout = open("../../Contents/LUA/Include/PolycodeLUAWrappers.h", "w")
  322. fout.write(out)
  323. fout = open("../../Contents/LUA/Source/PolycodeLUA.cpp", "w")
  324. fout.write(sout)
  325. #print cppHeader