create_lua_library.py 13 KB

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