csharp.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 bgfx
  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. local flagName = flag.name:gsub("_", "")
  127. yield("\t"
  128. .. flagName
  129. .. string.rep(" ", 22 - #(flagName))
  130. .. " = "
  131. .. string.format(flag.format or format, flag.value)
  132. .. ","
  133. )
  134. end
  135. if typ.shift then
  136. yield("\t"
  137. .. "Shift"
  138. .. string.rep(" ", 22 - #("Shift"))
  139. .. " = "
  140. .. flag.shift
  141. )
  142. end
  143. -- generate Mask
  144. if typ.mask then
  145. yield("\t"
  146. .. "Mask"
  147. .. string.rep(" ", 22 - #("Mask"))
  148. .. " = "
  149. .. string.format(format, flag.mask)
  150. )
  151. end
  152. yield("}")
  153. end
  154. local function lastCombinedFlagBlock()
  155. if lastCombinedFlag then
  156. FlagBlock(combined[lastCombinedFlag])
  157. lastCombinedFlag = nil
  158. end
  159. end
  160. function converter.types(typ)
  161. if typ.handle then
  162. lastCombinedFlagBlock()
  163. yield("public struct " .. typ.name .. "{ public ushort idx; }")
  164. elseif hasSuffix(typ.name, "::Enum") then
  165. lastCombinedFlagBlock()
  166. yield("public enum " .. typ.typename)
  167. yield("{")
  168. for _, enum in ipairs(typ.enum) do
  169. yield("\t" .. enum.name .. ",")
  170. end
  171. yield("}")
  172. elseif typ.bits ~= nil then
  173. local prefix, name = typ.name:match "(%u%l+)(.*)"
  174. if prefix ~= lastCombinedFlag then
  175. lastCombinedFlagBlock()
  176. lastCombinedFlag = prefix
  177. end
  178. local combinedFlag = combined[prefix]
  179. if combinedFlag then
  180. combinedFlag.bits = typ.bits
  181. combinedFlag.name = prefix
  182. local flags = combinedFlag.flag or {}
  183. combinedFlag.flag = flags
  184. local lookup = combinedFlag.lookup or {}
  185. combinedFlag.lookup = lookup
  186. for _, flag in ipairs(typ.flag) do
  187. local flagName = name .. flag.name:gsub("_", "")
  188. local value = flag.value
  189. if value == nil then
  190. -- It's a combined flag
  191. value = 0
  192. for _, v in ipairs(flag) do
  193. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  194. end
  195. end
  196. lookup[flagName] = value
  197. table.insert(flags, {
  198. name = flagName,
  199. value = value,
  200. })
  201. end
  202. if typ.shift then
  203. table.insert(flags, {
  204. name = name .. "Shift",
  205. value = typ.shift,
  206. format = "%d",
  207. })
  208. end
  209. if typ.mask then
  210. -- generate Mask
  211. table.insert(flags, {
  212. name = name .. "Mask",
  213. value = typ.mask,
  214. })
  215. lookup[name .. "Mask"] = typ.mask
  216. end
  217. else
  218. FlagBlock(typ)
  219. end
  220. elseif typ.struct ~= nil then
  221. if typ.namespace ~= nil then
  222. yield("public unsafe struct " .. typ.namespace .. typ.name)
  223. else
  224. yield("public unsafe struct " .. typ.name)
  225. end
  226. yield("{")
  227. for _, member in ipairs(typ.struct) do
  228. yield(
  229. "\tpublic " .. convert_type(member) .. " " .. member.name .. ";"
  230. )
  231. end
  232. yield("}")
  233. end
  234. end
  235. function converter.funcs(func)
  236. yield("[DllImport(DllName, EntryPoint=\"bgfx_" .. func.cname .. "\", CallingConvention = CallingConvention.Cdecl)]")
  237. if func.ret.cpptype == "bool" then
  238. yield("[return: MarshalAs(UnmanagedType.I1)]")
  239. elseif func.ret.cpptype == "const char*" then
  240. yield("[return: MarshalAs(UnmanagedType.LPStr)]")
  241. end
  242. local first = ""
  243. local args = "("
  244. for _, arg in ipairs(func.args) do
  245. local argtype = convert_type(arg)
  246. args = args .. first .. argtype .. " " .. arg.name
  247. first = ", "
  248. end
  249. yield("internal static extern unsafe " .. convert_ret_type(func.ret) .. " " .. func.cname .. args .. ");")
  250. end
  251. print(gen())
  252. -- printtable("idl types", idl.types)
  253. -- printtable("idl funcs", idl.funcs)