bindings-cs.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. local codegen = require "codegen"
  2. local idl = codegen.idl "bgfx.idl"
  3. local csharp_template = [[
  4. using System;
  5. using System.Runtime.InteropServices;
  6. using System.Security;
  7. internal struct bgfx
  8. {
  9. $types
  10. $funcs
  11. #if DEBUG
  12. const string DllName = "bgfx_debug.dll";
  13. #else
  14. const string DllName = "bgfx.dll";
  15. #endif
  16. }
  17. ]]
  18. local function hasPrefix(str, prefix)
  19. return prefix == "" or str:sub(1, #prefix) == prefix
  20. end
  21. local function hasSuffix(str, suffix)
  22. return suffix == "" or str:sub(-#suffix) == suffix
  23. end
  24. local function convert_type_0(arg)
  25. if hasPrefix(arg.ctype, "uint64_t") then
  26. return arg.ctype:gsub("uint64_t", "ulong")
  27. elseif hasPrefix(arg.ctype, "int64_t") then
  28. return arg.ctype:gsub("int64_t", "long")
  29. elseif hasPrefix(arg.ctype, "uint32_t") then
  30. return arg.ctype:gsub("uint32_t", "uint")
  31. elseif hasPrefix(arg.ctype, "int32_t") then
  32. return arg.ctype:gsub("int32_t", "int")
  33. elseif hasPrefix(arg.ctype, "uint16_t") then
  34. return arg.ctype:gsub("uint16_t", "ushort")
  35. elseif hasPrefix(arg.ctype, "bgfx_view_id_t") then
  36. return arg.ctype:gsub("bgfx_view_id_t", "ushort")
  37. elseif hasPrefix(arg.ctype, "uint8_t") then
  38. return arg.ctype:gsub("uint8_t", "byte")
  39. elseif hasPrefix(arg.ctype, "uintptr_t") then
  40. return arg.ctype:gsub("uintptr_t", "UIntPtr")
  41. elseif arg.ctype == "const char*" then
  42. return "[MarshalAs(UnmanagedType.LPStr)] string"
  43. elseif hasSuffix(arg.fulltype, "Handle") then
  44. return arg.fulltype
  45. elseif arg.ctype == "..." then
  46. return "[MarshalAs(UnmanagedType.LPStr)] string args"
  47. elseif arg.ctype == "va_list"
  48. or arg.fulltype == "bx::AllocatorI*"
  49. or arg.fulltype == "CallbackI*"
  50. or arg.fulltype == "ReleaseFn" then
  51. return "IntPtr"
  52. elseif arg.fulltype == "const ViewId*" then
  53. return "ushort*"
  54. end
  55. return arg.fulltype
  56. end
  57. local function convert_type(arg)
  58. local ctype = convert_type_0(arg)
  59. ctype = ctype:gsub("::Enum", "")
  60. ctype = ctype:gsub("const ", "")
  61. ctype = ctype:gsub(" &", "*")
  62. ctype = ctype:gsub("&", "*")
  63. return ctype
  64. end
  65. local function convert_ret_type(arg)
  66. local ctype = convert_type(arg)
  67. if hasPrefix(ctype, "[MarshalAs(UnmanagedType.LPStr)]") then
  68. return "string"
  69. end
  70. return ctype
  71. end
  72. local converter = {}
  73. local yield = coroutine.yield
  74. local indent = ""
  75. local gen = {}
  76. function gen.gen()
  77. local r = csharp_template:gsub("$(%l+)", function(what)
  78. local tmp = {}
  79. for _, object in ipairs(idl[what]) do
  80. local co = coroutine.create(converter[what])
  81. local any
  82. while true do
  83. local ok, v = coroutine.resume(co, object)
  84. assert(ok, debug.traceback(co, v))
  85. if not v then
  86. break
  87. end
  88. table.insert(tmp, v)
  89. any = true
  90. end
  91. if any and tmp[#tmp] ~= "" then
  92. table.insert(tmp, "")
  93. end
  94. end
  95. return table.concat(tmp, "\n\t")
  96. end)
  97. return r
  98. end
  99. local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
  100. for _, v in ipairs(combined) do
  101. combined[v] = {}
  102. end
  103. local lastCombinedFlag
  104. local function FlagBlock(typ)
  105. local format = "0x%08x"
  106. local enumType = " : uint"
  107. if typ.bits == 64 then
  108. format = "0x%016x"
  109. enumType = " : ulong"
  110. elseif typ.bits == 16 then
  111. format = "0x%04x"
  112. enumType = " : ushort"
  113. end
  114. yield("[Flags]")
  115. yield("public enum " .. typ.name .. "Flags" .. enumType)
  116. yield("{")
  117. for _, flag in ipairs(typ.flag) do
  118. local flagName = flag.name:gsub("_", "")
  119. yield("\t"
  120. .. flagName
  121. .. string.rep(" ", 22 - #(flagName))
  122. .. " = "
  123. .. string.format(flag.format or format, flag.value)
  124. .. ","
  125. )
  126. end
  127. if typ.shift then
  128. yield("\t"
  129. .. "Shift"
  130. .. string.rep(" ", 22 - #("Shift"))
  131. .. " = "
  132. .. flag.shift
  133. )
  134. end
  135. -- generate Mask
  136. if typ.mask then
  137. yield("\t"
  138. .. "Mask"
  139. .. string.rep(" ", 22 - #("Mask"))
  140. .. " = "
  141. .. string.format(format, flag.mask)
  142. )
  143. end
  144. yield("}")
  145. end
  146. local function lastCombinedFlagBlock()
  147. if lastCombinedFlag then
  148. local typ = combined[lastCombinedFlag]
  149. if typ then
  150. FlagBlock(combined[lastCombinedFlag])
  151. yield("")
  152. end
  153. lastCombinedFlag = nil
  154. end
  155. end
  156. local namespace = ""
  157. function converter.types(typ)
  158. if typ.handle then
  159. lastCombinedFlagBlock()
  160. yield("public struct " .. typ.name .. "{ public ushort idx; }")
  161. elseif hasSuffix(typ.name, "::Enum") then
  162. lastCombinedFlagBlock()
  163. yield("public enum " .. typ.typename)
  164. yield("{")
  165. for _, enum in ipairs(typ.enum) do
  166. yield("\t" .. enum.name .. ",")
  167. end
  168. yield("}")
  169. elseif typ.bits ~= nil then
  170. local prefix, name = typ.name:match "(%u%l+)(.*)"
  171. if prefix ~= lastCombinedFlag then
  172. lastCombinedFlagBlock()
  173. lastCombinedFlag = prefix
  174. end
  175. local combinedFlag = combined[prefix]
  176. if combinedFlag then
  177. combinedFlag.bits = typ.bits
  178. combinedFlag.name = prefix
  179. local flags = combinedFlag.flag or {}
  180. combinedFlag.flag = flags
  181. local lookup = combinedFlag.lookup or {}
  182. combinedFlag.lookup = lookup
  183. for _, flag in ipairs(typ.flag) do
  184. local flagName = name .. flag.name:gsub("_", "")
  185. local value = flag.value
  186. if value == nil then
  187. -- It's a combined flag
  188. value = 0
  189. for _, v in ipairs(flag) do
  190. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  191. end
  192. end
  193. lookup[flagName] = value
  194. table.insert(flags, {
  195. name = flagName,
  196. value = value,
  197. })
  198. end
  199. if typ.shift then
  200. table.insert(flags, {
  201. name = name .. "Shift",
  202. value = typ.shift,
  203. format = "%d",
  204. })
  205. end
  206. if typ.mask then
  207. -- generate Mask
  208. table.insert(flags, {
  209. name = name .. "Mask",
  210. value = typ.mask,
  211. })
  212. lookup[name .. "Mask"] = typ.mask
  213. end
  214. else
  215. FlagBlock(typ)
  216. end
  217. elseif typ.struct ~= nil then
  218. local skip = false
  219. if typ.namespace ~= nil then
  220. if namespace ~= typ.namespace then
  221. yield("public unsafe struct " .. typ.namespace)
  222. yield("{")
  223. namespace = typ.namespace
  224. indent = "\t"
  225. end
  226. elseif namespace ~= "" then
  227. indent = ""
  228. namespace = ""
  229. skip = true
  230. end
  231. if not skip then
  232. yield(indent .. "public unsafe struct " .. typ.name)
  233. yield(indent .. "{")
  234. end
  235. for _, member in ipairs(typ.struct) do
  236. yield(
  237. indent .. "\tpublic " .. convert_type(member) .. " " .. member.name .. ";"
  238. )
  239. end
  240. yield(indent .. "}")
  241. end
  242. end
  243. function converter.funcs(func)
  244. if func.cpponly then
  245. return
  246. end
  247. if func.comments ~= nil then
  248. yield("/// <summary>")
  249. for _, line in ipairs(func.comments) do
  250. yield("/// " .. line)
  251. end
  252. yield("/// </summary>")
  253. end
  254. yield("[DllImport(DllName, EntryPoint=\"bgfx_" .. func.cname .. "\", CallingConvention = CallingConvention.Cdecl)]")
  255. if func.ret.cpptype == "bool" then
  256. yield("[return: MarshalAs(UnmanagedType.I1)]")
  257. elseif func.ret.cpptype == "const char*" then
  258. yield("[return: MarshalAs(UnmanagedType.LPStr)]")
  259. end
  260. local args = {}
  261. if func.this ~= nil then
  262. args[1] = func.this_type.type .. "* _this"
  263. end
  264. for _, arg in ipairs(func.args) do
  265. table.insert(args, convert_type(arg) .. " " .. arg.name)
  266. end
  267. yield("internal static extern unsafe " .. convert_ret_type(func.ret) .. " " .. func.cname
  268. .. "(" .. table.concat(args, ", ") .. ");")
  269. end
  270. -- printtable("idl types", idl.types)
  271. -- printtable("idl funcs", idl.funcs)
  272. function gen.write(codes, outputfile)
  273. local out = assert(io.open(outputfile, "wb"))
  274. out:write(codes)
  275. out:close()
  276. end
  277. return gen