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 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. if typ == nil then
  106. return
  107. end
  108. local format = "0x%08x"
  109. local enumType = " : uint"
  110. if typ.bits == 64 then
  111. format = "0x%016x"
  112. enumType = " : ulong"
  113. elseif typ.bits == 16 then
  114. format = "0x%04x"
  115. enumType = " : ushort"
  116. end
  117. yield("[Flags]")
  118. yield("public enum " .. typ.name .. "Flags" .. enumType)
  119. yield("{")
  120. for _, flag in ipairs(typ.flag) do
  121. local flagName = flag.name:gsub("_", "")
  122. yield("\t"
  123. .. flagName
  124. .. string.rep(" ", 22 - #(flagName))
  125. .. " = "
  126. .. string.format(flag.format or format, flag.value)
  127. .. ","
  128. )
  129. end
  130. if typ.shift then
  131. yield("\t"
  132. .. "Shift"
  133. .. string.rep(" ", 22 - #("Shift"))
  134. .. " = "
  135. .. flag.shift
  136. )
  137. end
  138. -- generate Mask
  139. if typ.mask then
  140. yield("\t"
  141. .. "Mask"
  142. .. string.rep(" ", 22 - #("Mask"))
  143. .. " = "
  144. .. string.format(format, flag.mask)
  145. )
  146. end
  147. yield("}")
  148. end
  149. local function lastCombinedFlagBlock()
  150. if lastCombinedFlag then
  151. FlagBlock(combined[lastCombinedFlag])
  152. lastCombinedFlag = nil
  153. end
  154. end
  155. local namespace = ""
  156. function converter.types(typ)
  157. if typ.handle then
  158. lastCombinedFlagBlock()
  159. yield("public struct " .. typ.name .. "{ public ushort idx; }")
  160. elseif hasSuffix(typ.name, "::Enum") then
  161. lastCombinedFlagBlock()
  162. yield("public enum " .. typ.typename)
  163. yield("{")
  164. for _, enum in ipairs(typ.enum) do
  165. yield("\t" .. enum.name .. ",")
  166. end
  167. yield("}")
  168. elseif typ.bits ~= nil then
  169. local prefix, name = typ.name:match "(%u%l+)(.*)"
  170. if prefix ~= lastCombinedFlag then
  171. lastCombinedFlagBlock()
  172. lastCombinedFlag = prefix
  173. end
  174. local combinedFlag = combined[prefix]
  175. if combinedFlag then
  176. combinedFlag.bits = typ.bits
  177. combinedFlag.name = prefix
  178. local flags = combinedFlag.flag or {}
  179. combinedFlag.flag = flags
  180. local lookup = combinedFlag.lookup or {}
  181. combinedFlag.lookup = lookup
  182. for _, flag in ipairs(typ.flag) do
  183. local flagName = name .. flag.name:gsub("_", "")
  184. local value = flag.value
  185. if value == nil then
  186. -- It's a combined flag
  187. value = 0
  188. for _, v in ipairs(flag) do
  189. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  190. end
  191. end
  192. lookup[flagName] = value
  193. table.insert(flags, {
  194. name = flagName,
  195. value = value,
  196. })
  197. end
  198. if typ.shift then
  199. table.insert(flags, {
  200. name = name .. "Shift",
  201. value = typ.shift,
  202. format = "%d",
  203. })
  204. end
  205. if typ.mask then
  206. -- generate Mask
  207. table.insert(flags, {
  208. name = name .. "Mask",
  209. value = typ.mask,
  210. })
  211. lookup[name .. "Mask"] = typ.mask
  212. end
  213. else
  214. FlagBlock(typ)
  215. end
  216. elseif typ.struct ~= nil then
  217. local skip = false
  218. if typ.namespace ~= nil then
  219. if namespace ~= typ.namespace then
  220. yield("public unsafe struct " .. typ.namespace)
  221. yield("{")
  222. namespace = typ.namespace
  223. indent = "\t"
  224. end
  225. elseif namespace ~= "" then
  226. indent = ""
  227. namespace = ""
  228. skip = true
  229. end
  230. if not skip then
  231. yield(indent .. "public unsafe struct " .. typ.name)
  232. yield(indent .. "{")
  233. end
  234. for _, member in ipairs(typ.struct) do
  235. yield(
  236. indent .. "\tpublic " .. convert_type(member) .. " " .. member.name .. ";"
  237. )
  238. end
  239. yield(indent .. "}")
  240. end
  241. end
  242. function converter.funcs(func)
  243. if func.cpponly then
  244. return
  245. end
  246. if func.comments ~= nil then
  247. yield("/// <summary>")
  248. for _, line in ipairs(func.comments) do
  249. yield("/// " .. line)
  250. end
  251. yield("/// </summary>")
  252. end
  253. yield("[DllImport(DllName, EntryPoint=\"bgfx_" .. func.cname .. "\", CallingConvention = CallingConvention.Cdecl)]")
  254. if func.ret.cpptype == "bool" then
  255. yield("[return: MarshalAs(UnmanagedType.I1)]")
  256. elseif func.ret.cpptype == "const char*" then
  257. yield("[return: MarshalAs(UnmanagedType.LPStr)]")
  258. end
  259. local args = {}
  260. if func.this ~= nil then
  261. args[1] = func.this_type.type .. "* _this"
  262. end
  263. for _, arg in ipairs(func.args) do
  264. table.insert(args, convert_type(arg) .. " " .. arg.name)
  265. end
  266. yield("internal static extern unsafe " .. convert_ret_type(func.ret) .. " " .. func.cname
  267. .. "(" .. table.concat(args, ", ") .. ");")
  268. end
  269. -- printtable("idl types", idl.types)
  270. -- printtable("idl funcs", idl.funcs)
  271. function gen.write(codes, outputfile)
  272. local out = assert(io.open(outputfile, "wb"))
  273. out:write(codes)
  274. out:close()
  275. end
  276. return gen