csharp.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. end
  53. return arg.fulltype
  54. end
  55. local function convert_type(arg)
  56. local ctype = convert_type_0(arg)
  57. ctype = ctype:gsub("::Enum", "")
  58. ctype = ctype:gsub("const ", "")
  59. ctype = ctype:gsub(" &", "*")
  60. ctype = ctype:gsub("&", "*")
  61. return ctype
  62. end
  63. local function convert_ret_type(arg)
  64. local ctype = convert_type(arg)
  65. if hasPrefix(ctype, "[MarshalAs(UnmanagedType.LPStr)]") then
  66. return "string"
  67. end
  68. return ctype
  69. end
  70. local converter = {}
  71. local yield = coroutine.yield
  72. local function gen()
  73. local r = csharp_template:gsub("$(%l+)", function(what)
  74. local tmp = {}
  75. for _, object in ipairs(idl[what]) do
  76. local co = coroutine.create(converter[what])
  77. local any
  78. while true do
  79. local ok, v = coroutine.resume(co, object)
  80. assert(ok, debug.traceback(co, v))
  81. if not v then
  82. break
  83. end
  84. table.insert(tmp, v)
  85. any = true
  86. end
  87. if any then
  88. table.insert(tmp, "")
  89. end
  90. end
  91. return table.concat(tmp, "\n\t")
  92. end)
  93. return r
  94. end
  95. local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
  96. for _, v in ipairs(combined) do
  97. combined[v] = {}
  98. end
  99. local lastCombinedFlag
  100. local function FlagBlock(typ)
  101. if typ == nil then
  102. return
  103. end
  104. local format = "0x%08x"
  105. local enumType = " : uint"
  106. if typ.bits == 64 then
  107. format = "0x%016x"
  108. enumType = " : ulong"
  109. elseif typ.bits == 16 then
  110. format = "0x%04x"
  111. enumType = " : ushort"
  112. end
  113. yield("[Flags]")
  114. yield("public enum " .. typ.name .. "Flags" .. enumType)
  115. yield("{")
  116. for _, flag in ipairs(typ.flag) do
  117. yield("\t"
  118. .. flag.name
  119. .. string.rep(" ", 22 - #(flag.name))
  120. .. " = "
  121. .. string.format(flag.format or format, flag.value)
  122. .. ","
  123. )
  124. end
  125. if typ.shift then
  126. yield("\t"
  127. .. "Shift"
  128. .. string.rep(" ", 22 - #("Shift"))
  129. .. " = "
  130. .. flag.shift
  131. )
  132. end
  133. -- generate Mask
  134. if typ.mask then
  135. yield("\t"
  136. .. "Mask"
  137. .. string.rep(" ", 22 - #("Mask"))
  138. .. " = "
  139. .. string.format(format, flag.mask)
  140. )
  141. end
  142. yield("}")
  143. end
  144. local function lastCombinedFlagBlock()
  145. if lastCombinedFlag then
  146. FlagBlock(combined[lastCombinedFlag])
  147. lastCombinedFlag = nil
  148. end
  149. end
  150. function converter.types(typ)
  151. if typ.handle then
  152. lastCombinedFlagBlock()
  153. yield("public struct " .. typ.name .. "{ public ushort idx; }")
  154. elseif hasSuffix(typ.name, "::Enum") then
  155. lastCombinedFlagBlock()
  156. yield("public enum " .. typ.typename)
  157. yield("{")
  158. for _, enum in ipairs(typ.enum) do
  159. yield("\t" .. enum.name .. ",")
  160. end
  161. yield("}")
  162. elseif typ.bits ~= nil then
  163. local prefix, name = typ.name:match "(%u%l+)(.*)"
  164. if prefix ~= lastCombinedFlag then
  165. lastCombinedFlagBlock()
  166. lastCombinedFlag = prefix
  167. end
  168. local combinedFlag = combined[prefix]
  169. if combinedFlag then
  170. combinedFlag.bits = typ.bits
  171. combinedFlag.name = prefix
  172. local flags = combinedFlag.flag or {}
  173. combinedFlag.flag = flags
  174. local lookup = combinedFlag.lookup or {}
  175. combinedFlag.lookup = lookup
  176. for _, flag in ipairs(typ.flag) do
  177. local flagName = name .. flag.name
  178. local value = flag.value
  179. if value == nil then
  180. -- It's a combined flag
  181. value = 0
  182. for _, v in ipairs(flag) do
  183. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  184. end
  185. end
  186. lookup[flagName] = value
  187. table.insert(flags, {
  188. name = flagName,
  189. value = value,
  190. })
  191. end
  192. if typ.shift then
  193. table.insert(flags, {
  194. name = name .. "Shift",
  195. value = typ.shift,
  196. format = "%d",
  197. })
  198. end
  199. if typ.mask then
  200. -- generate Mask
  201. table.insert(flags, {
  202. name = name .. "Mask",
  203. value = typ.mask,
  204. })
  205. lookup[name .. "Mask"] = typ.mask
  206. end
  207. else
  208. FlagBlock(typ)
  209. end
  210. elseif typ.struct ~= nil then
  211. if typ.namespace ~= nil then
  212. yield("public unsafe struct " .. typ.namespace .. typ.name)
  213. else
  214. yield("public unsafe struct " .. typ.name)
  215. end
  216. yield("{")
  217. for _, member in ipairs(typ.struct) do
  218. yield(
  219. "\t"
  220. .. convert_type(member) .. " " .. member.name .. ";"
  221. )
  222. end
  223. yield("}")
  224. end
  225. end
  226. function converter.funcs(func)
  227. yield("[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]")
  228. if func.ret.cpptype == "bool" then
  229. yield("[return: MarshalAs(UnmanagedType.I1)]")
  230. elseif func.ret.cpptype == "const char*" then
  231. yield("[return: MarshalAs(UnmanagedType.LPStr)]")
  232. end
  233. local first = ""
  234. local args = "("
  235. for _, arg in ipairs(func.args) do
  236. local argtype = convert_type(arg)
  237. args = args .. first .. argtype .. " " .. arg.name
  238. first = ", "
  239. end
  240. yield("internal static extern unsafe " .. convert_ret_type(func.ret) .. " bgfx_" .. func.cname .. args .. ");")
  241. end
  242. print(gen())
  243. -- printtable("idl types", idl.types)
  244. -- printtable("idl funcs", idl.funcs)