csharp.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 arg.ctype == "uint64_t" then
  33. return "ulong"
  34. elseif arg.ctype == "uint32_t" then
  35. return "uint"
  36. elseif arg.ctype == "uint16_t" then
  37. return "ushort"
  38. elseif arg.ctype == "uint8_t" then
  39. return "byte"
  40. elseif arg.ctype == "const char*" then
  41. return "[MarshalAs(UnmanagedType.LPStr)] string"
  42. elseif hasSuffix(arg.fulltype, "Handle") then
  43. return arg.fulltype
  44. elseif hasSuffix(arg.fulltype, "::Enum") then
  45. return arg.fulltype:gsub("::Enum", "")
  46. end
  47. return arg.ctype
  48. end
  49. local function convert_type(arg)
  50. local ctype = convert_type_0(arg)
  51. return ctype:gsub("const ", "")
  52. end
  53. local converter = {}
  54. local yield = coroutine.yield
  55. local function gen()
  56. local r = csharp_template:gsub("$(%l+)", function(what)
  57. local tmp = {}
  58. for _, object in ipairs(idl[what]) do
  59. local co = coroutine.create(converter[what])
  60. local any
  61. while true do
  62. local ok, v = coroutine.resume(co, object)
  63. assert(ok, debug.traceback(co, v))
  64. if not v then
  65. break
  66. end
  67. table.insert(tmp, v)
  68. any = true
  69. end
  70. if any then
  71. table.insert(tmp, "")
  72. end
  73. end
  74. return table.concat(tmp, "\n\t")
  75. end)
  76. return r
  77. end
  78. local lastCombinedIdx = -1
  79. local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
  80. local function isCombinedBlock(str)
  81. for idx, prefix in ipairs(combined) do
  82. if hasPrefix(str, prefix) then
  83. return idx
  84. end
  85. end
  86. return -1
  87. end
  88. local function endCombinedBlock()
  89. if lastCombinedIdx ~= -1 then
  90. yield("}")
  91. end
  92. lastCombinedIdx = -1
  93. end
  94. function converter.types(typ)
  95. if typ.handle then
  96. endCombinedBlock()
  97. yield("public struct " .. typ.name .. "{ public ushort idx; }")
  98. elseif hasSuffix(typ.name, "::Enum") then
  99. endCombinedBlock()
  100. yield("public enum " .. typ.typename)
  101. yield("{")
  102. for _, enum in ipairs(typ.enum) do
  103. yield("\t" .. enum.name .. ",")
  104. end
  105. yield("}")
  106. elseif typ.bits ~= nil then
  107. local idx = isCombinedBlock(typ.name)
  108. if idx ~= lastCombinedIdx then
  109. endCombinedBlock()
  110. end
  111. local format = "0x%08x"
  112. local enumType = ""
  113. if typ.bits == 64 then
  114. format = "0x%016x"
  115. enumType = " : ulong"
  116. elseif typ.bits == 16 then
  117. format = "0x%04x"
  118. enumType = " : ushort"
  119. end
  120. if lastCombinedIdx == -1 then
  121. yield("[Flags]")
  122. if idx ~= -1 then
  123. yield("public enum " .. combined[idx] .. enumType)
  124. else
  125. yield("public enum " .. typ.name .. enumType)
  126. end
  127. lastCombinedIdx = idx
  128. yield("{")
  129. end
  130. for _, flag in ipairs(typ.flag) do
  131. if flag.value then
  132. yield("\t"
  133. .. flag.name
  134. .. string.rep(" ", 22 - #(flag.name))
  135. .. " = "
  136. .. string.format(format, flag.value)
  137. .. ","
  138. )
  139. else
  140. yield("\t// Combined: "
  141. .. flag.name
  142. .. " = "
  143. .. table.concat(flag, " | ")
  144. )
  145. end
  146. end
  147. if lastCombinedIdx == -1 then
  148. yield("}")
  149. end
  150. end
  151. end
  152. function converter.funcs(func)
  153. yield("[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]")
  154. if func.ret.cpptype == "bool" then
  155. yield("[return: MarshalAs(UnmanagedType:I1)]")
  156. end
  157. local first = ""
  158. local args = "("
  159. for _, arg in ipairs(func.args) do
  160. local argtype = convert_type(arg)
  161. args = args .. first .. argtype .. " " .. arg.name
  162. first = ", "
  163. end
  164. yield("internal static extern unsafe " .. convert_type(func.ret) .. " bgfx_" .. func.cname .. args .. ");")
  165. end
  166. print(gen())
  167. -- printtable("idl types", idl.types)
  168. -- printtable("idl funcs", idl.funcs)