bindings-cs.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. local codegen = require "codegen"
  2. local idl = codegen.idl "bgfx.idl"
  3. local csharp_template = [[
  4. /*
  5. * Copyright 2011-2020 Branimir Karadzic. All rights reserved.
  6. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  7. */
  8. /*
  9. *
  10. * AUTO GENERATED! DO NOT EDIT!
  11. *
  12. */
  13. using System;
  14. using System.Runtime.InteropServices;
  15. namespace Bgfx
  16. {
  17. public static partial class bgfx
  18. {
  19. $types
  20. $funcs
  21. #if !BGFX_CSHARP_CUSTOM_DLLNAME
  22. #if DEBUG
  23. const string DllName = "bgfx_debug.dll";
  24. #else
  25. const string DllName = "bgfx.dll";
  26. #endif
  27. #endif
  28. }
  29. }
  30. ]]
  31. local function hasPrefix(str, prefix)
  32. return prefix == "" or str:sub(1, #prefix) == prefix
  33. end
  34. local function hasSuffix(str, suffix)
  35. return suffix == "" or str:sub(-#suffix) == suffix
  36. end
  37. local function convert_type_0(arg)
  38. if hasPrefix(arg.ctype, "uint64_t") then
  39. return arg.ctype:gsub("uint64_t", "ulong")
  40. elseif hasPrefix(arg.ctype, "int64_t") then
  41. return arg.ctype:gsub("int64_t", "long")
  42. elseif hasPrefix(arg.ctype, "uint32_t") then
  43. return arg.ctype:gsub("uint32_t", "uint")
  44. elseif hasPrefix(arg.ctype, "int32_t") then
  45. return arg.ctype:gsub("int32_t", "int")
  46. elseif hasPrefix(arg.ctype, "uint16_t") then
  47. return arg.ctype:gsub("uint16_t", "ushort")
  48. elseif hasPrefix(arg.ctype, "bgfx_view_id_t") then
  49. return arg.ctype:gsub("bgfx_view_id_t", "ushort")
  50. elseif hasPrefix(arg.ctype, "uint8_t") then
  51. return arg.ctype:gsub("uint8_t", "byte")
  52. elseif hasPrefix(arg.ctype, "uintptr_t") then
  53. return arg.ctype:gsub("uintptr_t", "UIntPtr")
  54. elseif arg.ctype == "bgfx_caps_gpu_t" then
  55. return arg.ctype:gsub("bgfx_caps_gpu_t", "uint")
  56. elseif arg.ctype == "const char*" then
  57. return "[MarshalAs(UnmanagedType.LPStr)] string"
  58. elseif hasPrefix(arg.ctype, "char") then
  59. return arg.ctype:gsub("char", "byte")
  60. elseif hasSuffix(arg.fulltype, "Handle") then
  61. return arg.fulltype
  62. elseif arg.ctype == "..." then
  63. return "[MarshalAs(UnmanagedType.LPStr)] string args"
  64. elseif arg.ctype == "va_list"
  65. or arg.fulltype == "bx::AllocatorI*"
  66. or arg.fulltype == "CallbackI*"
  67. or arg.fulltype == "ReleaseFn" then
  68. return "IntPtr"
  69. elseif arg.fulltype == "const ViewId*" then
  70. return "ushort*"
  71. end
  72. return arg.fulltype
  73. end
  74. local function convert_type(arg)
  75. local ctype = convert_type_0(arg)
  76. ctype = ctype:gsub("::Enum", "")
  77. ctype = ctype:gsub("const ", "")
  78. ctype = ctype:gsub(" &", "*")
  79. ctype = ctype:gsub("&", "*")
  80. return ctype
  81. end
  82. local function convert_struct_type(arg)
  83. local ctype = convert_type(arg)
  84. if hasPrefix(arg.ctype, "bool") then
  85. ctype = ctype:gsub("bool", "byte")
  86. end
  87. return ctype
  88. end
  89. local function convert_ret_type(arg)
  90. local ctype = convert_type(arg)
  91. if hasPrefix(ctype, "[MarshalAs(UnmanagedType.LPStr)]") then
  92. return "IntPtr"
  93. end
  94. return ctype
  95. end
  96. local converter = {}
  97. local yield = coroutine.yield
  98. local indent = ""
  99. local gen = {}
  100. function gen.gen()
  101. local r = csharp_template:gsub("$(%l+)", function(what)
  102. local tmp = {}
  103. for _, object in ipairs(idl[what]) do
  104. local co = coroutine.create(converter[what])
  105. local any
  106. while true do
  107. local ok, v = coroutine.resume(co, object)
  108. assert(ok, debug.traceback(co, v))
  109. if not v then
  110. break
  111. end
  112. table.insert(tmp, v)
  113. any = true
  114. end
  115. if any and tmp[#tmp] ~= "" then
  116. table.insert(tmp, "")
  117. end
  118. end
  119. return table.concat(tmp, "\n\t")
  120. end)
  121. return r
  122. end
  123. local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
  124. for _, v in ipairs(combined) do
  125. combined[v] = {}
  126. end
  127. local lastCombinedFlag
  128. local function FlagBlock(typ)
  129. local format = "0x%08x"
  130. local enumType = " : uint"
  131. if typ.bits == 64 then
  132. format = "0x%016x"
  133. enumType = " : ulong"
  134. elseif typ.bits == 16 then
  135. format = "0x%04x"
  136. enumType = " : ushort"
  137. end
  138. yield("[Flags]")
  139. yield("public enum " .. typ.name .. "Flags" .. enumType)
  140. yield("{")
  141. for idx, flag in ipairs(typ.flag) do
  142. if flag.comment ~= nil then
  143. if idx ~= 1 then
  144. yield("")
  145. end
  146. yield("\t/// <summary>")
  147. for _, comment in ipairs(flag.comment) do
  148. yield("\t/// " .. comment)
  149. end
  150. yield("\t/// </summary>")
  151. end
  152. local flagName = flag.name:gsub("_", "")
  153. yield("\t"
  154. .. flagName
  155. .. string.rep(" ", 22 - #(flagName))
  156. .. " = "
  157. .. string.format(flag.format or format, flag.value)
  158. .. ","
  159. )
  160. end
  161. if typ.shift then
  162. yield("\t"
  163. .. "Shift"
  164. .. string.rep(" ", 22 - #("Shift"))
  165. .. " = "
  166. .. flag.shift
  167. )
  168. end
  169. -- generate Mask
  170. if typ.mask then
  171. yield("\t"
  172. .. "Mask"
  173. .. string.rep(" ", 22 - #("Mask"))
  174. .. " = "
  175. .. string.format(format, flag.mask)
  176. )
  177. end
  178. yield("}")
  179. end
  180. local function lastCombinedFlagBlock()
  181. if lastCombinedFlag then
  182. local typ = combined[lastCombinedFlag]
  183. if typ then
  184. FlagBlock(combined[lastCombinedFlag])
  185. yield("")
  186. end
  187. lastCombinedFlag = nil
  188. end
  189. end
  190. local enum = {}
  191. local function convert_array(member)
  192. if string.find(member.array, "::") then
  193. return string.format("[%d]", enum[member.array])
  194. else
  195. return member.array
  196. end
  197. end
  198. local function convert_struct_member(member)
  199. if member.array then
  200. return "fixed " .. convert_struct_type(member) .. " " .. member.name .. convert_array(member)
  201. else
  202. return convert_struct_type(member) .. " " .. member.name
  203. end
  204. end
  205. local namespace = ""
  206. function converter.types(typ)
  207. if typ.handle then
  208. lastCombinedFlagBlock()
  209. yield("public struct " .. typ.name .. "{ public ushort idx; }")
  210. elseif hasSuffix(typ.name, "::Enum") then
  211. lastCombinedFlagBlock()
  212. yield("public enum " .. typ.typename)
  213. yield("{")
  214. for idx, enum in ipairs(typ.enum) do
  215. if enum.comment ~= nil then
  216. if idx ~= 1 then
  217. yield("")
  218. end
  219. yield("\t/// <summary>")
  220. for _, comment in ipairs(enum.comment) do
  221. yield("\t/// " .. comment)
  222. end
  223. yield("\t/// </summary>")
  224. end
  225. yield("\t" .. enum.name .. ",")
  226. end
  227. yield("");
  228. yield("\tCount")
  229. yield("}")
  230. enum["[" .. typ.typename .. "::Count]"] = #typ.enum
  231. elseif typ.bits ~= nil then
  232. local prefix, name = typ.name:match "(%u%l+)(.*)"
  233. if prefix ~= lastCombinedFlag then
  234. lastCombinedFlagBlock()
  235. lastCombinedFlag = prefix
  236. end
  237. local combinedFlag = combined[prefix]
  238. if combinedFlag then
  239. combinedFlag.bits = typ.bits
  240. combinedFlag.name = prefix
  241. local flags = combinedFlag.flag or {}
  242. combinedFlag.flag = flags
  243. local lookup = combinedFlag.lookup or {}
  244. combinedFlag.lookup = lookup
  245. for _, flag in ipairs(typ.flag) do
  246. local flagName = name .. flag.name:gsub("_", "")
  247. local value = flag.value
  248. if value == nil then
  249. -- It's a combined flag
  250. value = 0
  251. for _, v in ipairs(flag) do
  252. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  253. end
  254. end
  255. lookup[flagName] = value
  256. table.insert(flags, {
  257. name = flagName,
  258. value = value,
  259. comment = flag.comment,
  260. })
  261. end
  262. if typ.shift then
  263. table.insert(flags, {
  264. name = name .. "Shift",
  265. value = typ.shift,
  266. format = "%d",
  267. comment = typ.comment,
  268. })
  269. end
  270. if typ.mask then
  271. -- generate Mask
  272. table.insert(flags, {
  273. name = name .. "Mask",
  274. value = typ.mask,
  275. comment = typ.comment,
  276. })
  277. lookup[name .. "Mask"] = typ.mask
  278. end
  279. else
  280. FlagBlock(typ)
  281. end
  282. elseif typ.struct ~= nil then
  283. local skip = false
  284. if typ.namespace ~= nil then
  285. if namespace ~= typ.namespace then
  286. yield("public unsafe struct " .. typ.namespace)
  287. yield("{")
  288. namespace = typ.namespace
  289. indent = "\t"
  290. end
  291. elseif namespace ~= "" then
  292. indent = ""
  293. namespace = ""
  294. skip = true
  295. end
  296. if not skip then
  297. yield(indent .. "public unsafe struct " .. typ.name)
  298. yield(indent .. "{")
  299. end
  300. for _, member in ipairs(typ.struct) do
  301. yield(
  302. indent .. "\tpublic " .. convert_struct_member(member) .. ";"
  303. )
  304. end
  305. yield(indent .. "}")
  306. end
  307. end
  308. function converter.funcs(func)
  309. if func.cpponly then
  310. return
  311. end
  312. if func.comments ~= nil then
  313. yield("/// <summary>")
  314. for _, line in ipairs(func.comments) do
  315. yield("/// " .. line)
  316. end
  317. yield("/// </summary>")
  318. yield("///")
  319. local hasParams = false
  320. for _, arg in ipairs(func.args) do
  321. if arg.comment ~= nil then
  322. local comment = table.concat(arg.comment, " ")
  323. yield("/// <param name=\""
  324. .. arg.name
  325. .. "\">"
  326. .. comment
  327. .. "</param>"
  328. )
  329. hasParams = true
  330. end
  331. end
  332. if hasParams then
  333. yield("///")
  334. end
  335. end
  336. yield("[DllImport(DllName, EntryPoint=\"bgfx_" .. func.cname .. "\", CallingConvention = CallingConvention.Cdecl)]")
  337. if func.ret.cpptype == "bool" then
  338. yield("[return: MarshalAs(UnmanagedType.I1)]")
  339. end
  340. local args = {}
  341. if func.this ~= nil then
  342. args[1] = func.this_type.type .. "* _this"
  343. end
  344. for _, arg in ipairs(func.args) do
  345. table.insert(args, convert_type(arg) .. " " .. arg.name)
  346. end
  347. yield("public static extern unsafe " .. convert_ret_type(func.ret) .. " " .. func.cname
  348. .. "(" .. table.concat(args, ", ") .. ");")
  349. end
  350. -- printtable("idl types", idl.types)
  351. -- printtable("idl funcs", idl.funcs)
  352. function gen.write(codes, outputfile)
  353. local out = assert(io.open(outputfile, "wb"))
  354. out:write(codes)
  355. out:close()
  356. print("Generating: " .. outputfile)
  357. end
  358. if (...) == nil then
  359. -- run `lua bindings-cs.lua` in command line
  360. print(gen.gen())
  361. end
  362. return gen