create_lua_library.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. if param["type"].find("*") > -1:
  150. luafunc = "(%s)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon"))
  151. elif param["type"].find("&") > -1:
  152. luafunc = "*(%s*)lua_topointer" % (param["type"].replace("const", "").replace("&", "").replace("Polygon", "Polycode::Polygon"))
  153. else:
  154. luafunc = "*(%s*)lua_topointer" % (param["type"])
  155. lend = ".__ptr"
  156. if param["type"] == "int" or param["type"] == "unsigned int":
  157. luafunc = "lua_tointeger"
  158. luatype = "LUA_TNUMBER"
  159. lend = ""
  160. if param["type"] == "bool":
  161. luafunc = "lua_toboolean"
  162. luatype = "LUA_TBOOLEAN"
  163. lend = ""
  164. if param["type"] == "Number":
  165. luatype = "LUA_TNUMBER"
  166. luafunc = "lua_tonumber"
  167. lend = ""
  168. if param["type"] == "String":
  169. luatype = "LUA_TSTRING"
  170. luafunc = "lua_tostring"
  171. lend = ""
  172. param["type"] = param["type"].replace("Polygon", "Polycode::Polygon")
  173. out += "\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  174. out += "\t%s %s = %s(L, %d);\n" % (param["type"], param["name"], luafunc, idx)
  175. paramlist.append(param["name"])
  176. lparamlist.append(param["name"]+lend)
  177. idx = idx +1
  178. if pm["name"] == ckey:
  179. out += "\t%s *inst = new %s(%s);\n" % (ckey.replace("Polygon", "Polycode::Polygon"), ckey.replace("Polygon", "Polycode::Polygon"), ", ".join(paramlist))
  180. out += "\tlua_pushlightuserdata(L, (void*)inst);\n"
  181. out += "\treturn 1;\n"
  182. else:
  183. if pm["rtnType"].find("static ") == -1:
  184. call = "inst->%s(%s)" % (pm["name"], ", ".join(paramlist))
  185. else:
  186. call = "%s::%s(%s)" % (ckey, pm["name"], ", ".join(paramlist))
  187. if pm["rtnType"] == "void" or pm["rtnType"] == "static void" or pm["rtnType"] == "virtual void" or pm["rtnType"] == "inline void":
  188. out += "\t%s;\n" % (call)
  189. basicType = True
  190. voidRet = True
  191. out += "\treturn 0;\n"
  192. else:
  193. outfunc = "lua_pushlightuserdata"
  194. retFunc = ""
  195. basicType = False
  196. if pm["rtnType"] == "Number" or pm["rtnType"] == "inline Number":
  197. outfunc = "lua_pushnumber"
  198. basicType = True
  199. if pm["rtnType"] == "String" or pm["rtnType"] == "static String":
  200. outfunc = "lua_pushstring"
  201. basicType = True
  202. retFunc = ".c_str()"
  203. 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":
  204. outfunc = "lua_pushinteger"
  205. basicType = True
  206. if pm["rtnType"] == "bool" or pm["rtnType"] == "static bool" or pm["rtnType"] == "virtual bool":
  207. outfunc = "lua_pushboolean"
  208. basicType = True
  209. if pm["rtnType"].find("*") > -1:
  210. out += "\t%s(L, (void*)%s%s);\n" % (outfunc, call, retFunc)
  211. elif basicType == True:
  212. out += "\t%s(L, %s%s);\n" % (outfunc, call, retFunc)
  213. else:
  214. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "")
  215. if className == "Polygon":
  216. className = "Polycode::Polygon"
  217. out += "\t%s *retInst = new %s();\n" % (className, className)
  218. out += "\t*retInst = %s;\n" % (call)
  219. out += "\t%s(L, retInst);\n" % (outfunc)
  220. out += "\treturn 1;\n"
  221. out += "}\n\n"
  222. if pm["name"] == ckey:
  223. lout += "function %s:%s(...)\n" % (ckey, ckey)
  224. lout += "\tfor k,v in pairs(arg) do\n"
  225. lout += "\t\tif type(v) == \"table\" then\n"
  226. lout += "\t\t\tif v.__ptr ~= nil then\n"
  227. lout += "\t\t\t\targ[k] = v.__ptr\n"
  228. lout += "\t\t\tend\n"
  229. lout += "\t\tend\n"
  230. lout += "\tend\n"
  231. lout += "\tif self.__ptr == nil and arg[1] ~= \"__skip_ptr__\" then\n"
  232. lout += "\t\tself.__ptr = Polycore.%s(unpack(arg))\n" % (ckey)
  233. lout += "\tend\n"
  234. lout += "end\n\n"
  235. else:
  236. lout += "function %s:%s(%s)\n" % (ckey, pm["name"], ", ".join(paramlist))
  237. if pm["rtnType"].find("static ") == -1:
  238. if len(lparamlist):
  239. lout += "\tlocal retVal = Polycore.%s_%s(self.__ptr, %s)\n" % (ckey, pm["name"], ", ".join(lparamlist))
  240. else:
  241. lout += "\tlocal retVal = Polycore.%s_%s(self.__ptr)\n" % (ckey, pm["name"])
  242. else:
  243. if len(lparamlist):
  244. lout += "\tlocal retVal = Polycore.%s_%s(%s)\n" % (ckey, pm["name"], ", ".join(lparamlist))
  245. else:
  246. lout += "\tlocal retVal = Polycore.%s_%s()\n" % (ckey, pm["name"])
  247. if not voidRet:
  248. if basicType == True:
  249. lout += "\treturn retVal\n"
  250. else:
  251. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "").replace("*","").replace(" ", "")
  252. lout += "\tif Polycore.__ptr_lookup[retVal] ~= nil then\n"
  253. lout += "\t\treturn Polycore.__ptr_lookup[retVal]\n"
  254. lout += "\telse\n"
  255. lout += "\t\tPolycore.__ptr_lookup[retVal] = %s(\"__skip_ptr__\")\n" % (className)
  256. lout += "\t\tPolycore.__ptr_lookup[retVal].__ptr = retVal\n"
  257. lout += "\t\treturn Polycore.__ptr_lookup[retVal]\n"
  258. lout += "\tend\n"
  259. lout += "end\n\n"
  260. parsed_methods.append(pm["name"])
  261. lfout += "require \"Polycode/%s\"\n" % ckey
  262. fout = open("../../Contents/LUA/API/Polycode/%s.lua" % ckey, "w")
  263. fout.write(lout)
  264. except CppHeaderParser.CppParseError, e:
  265. print e
  266. sys.exit(1)
  267. out += "}"
  268. sout += "\t\t{NULL, NULL}\n"
  269. sout += "\t};\n"
  270. sout += "\tluaL_openlib(L, \"Polycore\", polycodeLib, 0);\n"
  271. sout += "\treturn 1;\n"
  272. sout += "}"
  273. fout = open("../../Contents/LUA/API/Polycode.lua", "w")
  274. fout.write(lfout)
  275. fout = open("../../Contents/LUA/Include/PolycodeLUAWrappers.h", "w")
  276. fout.write(out)
  277. fout = open("../../Contents/LUA/Source/PolycodeLUA.cpp", "w")
  278. fout.write(sout)
  279. #print cppHeader