csharp.lua 6.7 KB

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