bindings-cs.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. local codegen = require "codegen"
  2. local idl = codegen.idl "bgfx.idl"
  3. local csharp_template = [[
  4. /*
  5. * Copyright 2011-2019 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 _, flag in ipairs(typ.flag) do
  142. local flagName = flag.name:gsub("_", "")
  143. yield("\t"
  144. .. flagName
  145. .. string.rep(" ", 22 - #(flagName))
  146. .. " = "
  147. .. string.format(flag.format or format, flag.value)
  148. .. ","
  149. )
  150. end
  151. if typ.shift then
  152. yield("\t"
  153. .. "Shift"
  154. .. string.rep(" ", 22 - #("Shift"))
  155. .. " = "
  156. .. flag.shift
  157. )
  158. end
  159. -- generate Mask
  160. if typ.mask then
  161. yield("\t"
  162. .. "Mask"
  163. .. string.rep(" ", 22 - #("Mask"))
  164. .. " = "
  165. .. string.format(format, flag.mask)
  166. )
  167. end
  168. yield("}")
  169. end
  170. local function lastCombinedFlagBlock()
  171. if lastCombinedFlag then
  172. local typ = combined[lastCombinedFlag]
  173. if typ then
  174. FlagBlock(combined[lastCombinedFlag])
  175. yield("")
  176. end
  177. lastCombinedFlag = nil
  178. end
  179. end
  180. local enum = {}
  181. local function convert_array(member)
  182. if string.find(member.array, "::") then
  183. return string.format("[%d]", enum[member.array])
  184. else
  185. return member.array
  186. end
  187. end
  188. local function convert_struct_member(member)
  189. if member.array then
  190. return "fixed " .. convert_struct_type(member) .. " " .. member.name .. convert_array(member)
  191. else
  192. return convert_struct_type(member) .. " " .. member.name
  193. end
  194. end
  195. local namespace = ""
  196. function converter.types(typ)
  197. if typ.handle then
  198. lastCombinedFlagBlock()
  199. yield("public struct " .. typ.name .. "{ public ushort idx; }")
  200. elseif hasSuffix(typ.name, "::Enum") then
  201. lastCombinedFlagBlock()
  202. yield("public enum " .. typ.typename)
  203. yield("{")
  204. for _, enum in ipairs(typ.enum) do
  205. yield("\t" .. enum.name .. ",")
  206. end
  207. yield("");
  208. yield("\tCount")
  209. yield("}")
  210. enum["[" .. typ.typename .. "::Count]"] = #typ.enum
  211. elseif typ.bits ~= nil then
  212. local prefix, name = typ.name:match "(%u%l+)(.*)"
  213. if prefix ~= lastCombinedFlag then
  214. lastCombinedFlagBlock()
  215. lastCombinedFlag = prefix
  216. end
  217. local combinedFlag = combined[prefix]
  218. if combinedFlag then
  219. combinedFlag.bits = typ.bits
  220. combinedFlag.name = prefix
  221. local flags = combinedFlag.flag or {}
  222. combinedFlag.flag = flags
  223. local lookup = combinedFlag.lookup or {}
  224. combinedFlag.lookup = lookup
  225. for _, flag in ipairs(typ.flag) do
  226. local flagName = name .. flag.name:gsub("_", "")
  227. local value = flag.value
  228. if value == nil then
  229. -- It's a combined flag
  230. value = 0
  231. for _, v in ipairs(flag) do
  232. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  233. end
  234. end
  235. lookup[flagName] = value
  236. table.insert(flags, {
  237. name = flagName,
  238. value = value,
  239. })
  240. end
  241. if typ.shift then
  242. table.insert(flags, {
  243. name = name .. "Shift",
  244. value = typ.shift,
  245. format = "%d",
  246. })
  247. end
  248. if typ.mask then
  249. -- generate Mask
  250. table.insert(flags, {
  251. name = name .. "Mask",
  252. value = typ.mask,
  253. })
  254. lookup[name .. "Mask"] = typ.mask
  255. end
  256. else
  257. FlagBlock(typ)
  258. end
  259. elseif typ.struct ~= nil then
  260. local skip = false
  261. if typ.namespace ~= nil then
  262. if namespace ~= typ.namespace then
  263. yield("public unsafe struct " .. typ.namespace)
  264. yield("{")
  265. namespace = typ.namespace
  266. indent = "\t"
  267. end
  268. elseif namespace ~= "" then
  269. indent = ""
  270. namespace = ""
  271. skip = true
  272. end
  273. if not skip then
  274. yield(indent .. "public unsafe struct " .. typ.name)
  275. yield(indent .. "{")
  276. end
  277. for _, member in ipairs(typ.struct) do
  278. yield(
  279. indent .. "\tpublic " .. convert_struct_member(member) .. ";"
  280. )
  281. end
  282. yield(indent .. "}")
  283. end
  284. end
  285. function converter.funcs(func)
  286. if func.cpponly then
  287. return
  288. end
  289. if func.comments ~= nil then
  290. yield("/// <summary>")
  291. for _, line in ipairs(func.comments) do
  292. yield("/// " .. line)
  293. end
  294. yield("/// </summary>")
  295. yield("///")
  296. local hasParams = false
  297. for _, arg in ipairs(func.args) do
  298. if arg.comment ~= nil then
  299. local comment = ""
  300. if (type(arg.comment) == "table") then
  301. comment = table.concat(arg.comment, " ")
  302. else
  303. comment = arg.comment
  304. end
  305. yield("/// <param name=\""
  306. .. arg.name
  307. .. "\">"
  308. .. comment
  309. .. "</param>"
  310. )
  311. hasParams = true
  312. end
  313. end
  314. if hasParams then
  315. yield("///")
  316. end
  317. end
  318. yield("[DllImport(DllName, EntryPoint=\"bgfx_" .. func.cname .. "\", CallingConvention = CallingConvention.Cdecl)]")
  319. if func.ret.cpptype == "bool" then
  320. yield("[return: MarshalAs(UnmanagedType.I1)]")
  321. end
  322. local args = {}
  323. if func.this ~= nil then
  324. args[1] = func.this_type.type .. "* _this"
  325. end
  326. for _, arg in ipairs(func.args) do
  327. table.insert(args, convert_type(arg) .. " " .. arg.name)
  328. end
  329. yield("public static extern unsafe " .. convert_ret_type(func.ret) .. " " .. func.cname
  330. .. "(" .. table.concat(args, ", ") .. ");")
  331. end
  332. -- printtable("idl types", idl.types)
  333. -- printtable("idl funcs", idl.funcs)
  334. function gen.write(codes, outputfile)
  335. local out = assert(io.open(outputfile, "wb"))
  336. out:write(codes)
  337. out:close()
  338. end
  339. return gen