csharp.lua 6.5 KB

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