csharp.lua 5.7 KB

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