create_lua_library.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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", "PolyRenderer", "PolyThreaded", "PolyTexture", "PolyShader", "PolyScene"]
  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 = ["ParticleEmitter", "PolycodeShaderModule", "Core", "Object", "Shader", "Texture", "Scene", "Threaded", "Renderer", "ShaderBinding"]
  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"]
  47. for pm in c["methods"]["public"]:
  48. if pm["name"] in parsed_methods or pm["name"].find("operator") > -1 or pm["name"] in ignore_methods:
  49. continue
  50. if pm["name"] == "~"+ckey:
  51. out += ""
  52. else:
  53. if pm["name"] == ckey:
  54. sout += "\t\t{\"%s\", Polycore_%s},\n" % (ckey, ckey)
  55. out += "static int Polycore_%s(lua_State *L) {\n" % (ckey)
  56. idx = 1
  57. else:
  58. sout += "\t\t{\"%s_%s\", Polycore_%s_%s},\n" % (ckey, pm["name"], ckey, pm["name"])
  59. out += "static int Polycore_%s_%s(lua_State *L) {\n" % (ckey, pm["name"])
  60. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  61. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey.replace("Polygon", "Polycode::Polygon"), ckey.replace("Polygon", "Polycode::Polygon"))
  62. idx = 2
  63. paramlist = []
  64. lparamlist = []
  65. for param in pm["parameters"]:
  66. if not param.has_key("type"):
  67. continue
  68. if param["type"] == "0":
  69. continue
  70. param["name"] = param["name"].replace("end", "_end").replace("repeat", "_repeat")
  71. if"type" in param:
  72. luatype = "LUA_TLIGHTUSERDATA"
  73. if param["type"].find("*") > -1:
  74. luafunc = "(%s)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon"))
  75. elif param["type"].find("&") > -1:
  76. luafunc = "*(%s*)lua_topointer" % (param["type"].replace("const", "").replace("&", "").replace("Polygon", "Polycode::Polygon"))
  77. else:
  78. luafunc = "*(%s*)lua_topointer" % (param["type"])
  79. lend = ".__ptr"
  80. if param["type"] == "int" or param["type"] == "unsigned int":
  81. luafunc = "lua_tointeger"
  82. luatype = "LUA_TNUMBER"
  83. lend = ""
  84. if param["type"] == "bool":
  85. luafunc = "lua_toboolean"
  86. luatype = "LUA_TBOOLEAN"
  87. lend = ""
  88. if param["type"] == "Number":
  89. luatype = "LUA_TNUMBER"
  90. luafunc = "lua_tonumber"
  91. lend = ""
  92. if param["type"] == "String":
  93. luatype = "LUA_TSTRING"
  94. luafunc = "lua_tostring"
  95. lend = ""
  96. param["type"] = param["type"].replace("Polygon", "Polycode::Polygon")
  97. out += "\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  98. out += "\t%s %s = %s(L, %d);\n" % (param["type"], param["name"], luafunc, idx)
  99. paramlist.append(param["name"])
  100. lparamlist.append(param["name"]+lend)
  101. idx = idx +1
  102. if pm["name"] == ckey:
  103. out += "\t%s *inst = new %s(%s);\n" % (ckey.replace("Polygon", "Polycode::Polygon"), ckey.replace("Polygon", "Polycode::Polygon"), ", ".join(paramlist))
  104. out += "\tlua_pushlightuserdata(L, (void*)inst);\n"
  105. out += "\treturn 1;\n"
  106. else:
  107. call = "inst->%s(%s)" % (pm["name"], ", ".join(paramlist))
  108. if pm["rtnType"] == "void" or pm["rtnType"] == "static void" or pm["rtnType"] == "virtual void" or pm["rtnType"] == "inline void":
  109. out += "\t%s;\n" % (call)
  110. out += "\treturn 0;\n"
  111. else:
  112. outfunc = "lua_pushlightuserdata"
  113. basicType = False
  114. retFunc = ""
  115. if pm["rtnType"] == "Number" or pm["rtnType"] == "inline Number":
  116. outfunc = "lua_pushnumber"
  117. basicType = True
  118. if pm["rtnType"] == "String" or pm["rtnType"] == "static String":
  119. outfunc = "lua_pushstring"
  120. basicType = True
  121. retFunc = ".c_str()"
  122. 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":
  123. outfunc = "lua_pushinteger"
  124. basicType = True
  125. if pm["rtnType"] == "bool" or pm["rtnType"] == "static bool" or pm["rtnType"] == "virtual bool":
  126. outfunc = "lua_pushboolean"
  127. basicType = True
  128. if pm["rtnType"].find("*") > -1:
  129. out += "\t%s(L, (void*)%s%s);\n" % (outfunc, call, retFunc)
  130. elif basicType == True:
  131. out += "\t%s(L, %s%s);\n" % (outfunc, call, retFunc)
  132. else:
  133. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "")
  134. if className == "Polygon":
  135. className = "Polycode::Polygon"
  136. out += "\t%s *retInst = new %s();\n" % (className, className)
  137. out += "\t*retInst = %s;\n" % (call)
  138. out += "\t%s(L, retInst);\n" % (outfunc)
  139. out += "\treturn 1;\n"
  140. out += "}\n\n"
  141. if pm["name"] == ckey:
  142. lout += "function %s:%s(%s)\n" % (ckey, ckey, ", ".join(paramlist))
  143. lout += "\tif self.__ptr == nil then\n"
  144. lout += "\t\tself.__ptr = Polycore.%s(%s)\n" % (ckey, ", ".join(paramlist))
  145. lout += "\tend\n"
  146. lout += "end\n\n"
  147. else:
  148. lout += "function %s:%s(%s)\n" % (ckey, pm["name"], ", ".join(paramlist))
  149. if len(lparamlist):
  150. lout += "\treturn Polycore.%s_%s(self.__ptr, %s)\n" % (ckey, pm["name"], ", ".join(lparamlist))
  151. else:
  152. lout += "\treturn Polycore.%s_%s(self.__ptr)\n" % (ckey, pm["name"])
  153. lout += "end\n\n"
  154. parsed_methods.append(pm["name"])
  155. lfout += "require \"Polycode/%s\"\n" % ckey
  156. fout = open("../../Contents/LUA/API/Polycode/%s.lua" % ckey, "w")
  157. fout.write(lout)
  158. except CppHeaderParser.CppParseError, e:
  159. print e
  160. sys.exit(1)
  161. out += "}"
  162. sout += "\t\t{NULL, NULL}\n"
  163. sout += "\t};\n"
  164. sout += "\tluaL_openlib(L, \"Polycore\", polycodeLib, 0);\n"
  165. sout += "\treturn 1;\n"
  166. sout += "}"
  167. fout = open("../../Contents/LUA/API/Polycode.lua", "w")
  168. fout.write(lfout)
  169. fout = open("../../Contents/LUA/Include/PolycodeLUAWrappers.h", "w")
  170. fout.write(out)
  171. fout = open("../../Contents/LUA/Source/PolycodeLUA.cpp", "w")
  172. fout.write(sout)
  173. #print cppHeader