diff --git a/scripts/csharp.lua b/scripts/csharp.lua index 2f44d149d..f13e2507b 100644 --- a/scripts/csharp.lua +++ b/scripts/csharp.lua @@ -29,34 +29,55 @@ internal struct NativeFunctions } ]] +local function hasPrefix(str, prefix) + return prefix == "" or str:sub(1, #prefix) == prefix +end + local function hasSuffix(str, suffix) return suffix == "" or str:sub(-#suffix) == suffix end local function convert_type_0(arg) - if arg.ctype == "uint64_t" then - return "ulong" - elseif arg.ctype == "uint32_t" then - return "uint" - elseif arg.ctype == "uint16_t" then + if hasPrefix(arg.ctype, "uint64_t") then + return arg.ctype:gsub("uint64_t", "ulong") + elseif hasPrefix(arg.ctype, "uint32_t") then + return arg.ctype:gsub("uint32_t", "uint") + elseif hasPrefix(arg.ctype, "int32_t") then + return arg.ctype:gsub("int32_t", "int") + elseif hasPrefix(arg.ctype, "uint16_t") then + return arg.ctype:gsub("uint16_t", "ushort") + elseif arg.ctype == "bgfx_view_id_t" then return "ushort" - elseif arg.ctype == "uint8_t" then - return "byte" + elseif hasPrefix(arg.ctype, "uint8_t") then + return arg.ctype:gsub("uint8_t", "byte") + elseif hasPrefix(arg.ctype, "uintptr_t") then + return arg.ctype:gsub("uintptr_t", "UIntPtr") elseif arg.ctype == "const char*" then return "[MarshalAs(UnmanagedType.LPStr)] string" elseif hasSuffix(arg.fulltype, "Handle") then return arg.fulltype - elseif hasSuffix(arg.fulltype, "::Enum") then - return arg.fulltype:gsub("::Enum", "") end - return arg.ctype + return arg.fulltype end local function convert_type(arg) local ctype = convert_type_0(arg) - return ctype:gsub("const ", "") + ctype = ctype:gsub("::Enum", "") + ctype = ctype:gsub("const ", "") + ctype = ctype:gsub(" &", "*") + ctype = ctype:gsub("&", "*") + return ctype +end + +local function convert_ret_type(arg) + local ctype = convert_type(arg) + if hasPrefix(ctype, "[MarshalAs(UnmanagedType.LPStr)]") then + return "string" + end + + return ctype end local converter = {} @@ -100,7 +121,7 @@ local function FlagBlock(typ) end local format = "0x%08x" - local enumType = "" + local enumType = " : uint" if typ.bits == 64 then format = "0x%016x" enumType = " : ulong" @@ -220,7 +241,9 @@ function converter.funcs(func) yield("[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]") if func.ret.cpptype == "bool" then - yield("[return: MarshalAs(UnmanagedType:I1)]") + yield("[return: MarshalAs(UnmanagedType.I1)]") + elseif func.ret.cpptype == "const char*" then + yield("[return: MarshalAs(UnmanagedType.LPStr)]") end local first = "" @@ -234,7 +257,7 @@ function converter.funcs(func) first = ", " end - yield("internal static extern unsafe " .. convert_type(func.ret) .. " bgfx_" .. func.cname .. args .. ");") + yield("internal static extern unsafe " .. convert_ret_type(func.ret) .. " bgfx_" .. func.cname .. args .. ");") end print(gen())