csharp.lua 5.0 KB

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