Skip to content

Commit

Permalink
Magic array detection!
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam committed Oct 28, 2024
1 parent e901e2f commit 09ebdcf
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 114 deletions.
29 changes: 21 additions & 8 deletions src/Bindgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,22 +430,30 @@ private string GenerateFunction(CppFunction function, out string output)
{
var paramPointerCount = 0;
string paramType = ConvertCppTypeToCSharp(functionName, parameter.Name, parameter.Type, ref paramPointerCount, out _);
paramType = AppendPointer(paramType, paramPointerCount);
string paramName = MapName(parameter.Name);
bool isArray = options.DetectArray(function.Name, parameter.Name);

if (paramPointerCount > 0)
{
isUnsafe = true;
if (paramPointerCount == 1 && IsSimpleRef(function, paramName, paramType) && !isArray)
{
paramType = "ref " + paramType;
}
else
{
isUnsafe = true;
paramType = AppendPointer(paramType, paramPointerCount);
}
}

parameters.Add($"{paramType} {paramName}");
}

if (function.Flags.HasFlag(CppFunctionFlags.Variadic))
{
// FIXME: Use something real for varargs
// https://github.com/dotnet/runtime/issues/48796
// parameters.Add("__arglist");
Console.WriteLine($"- Unsupported function param: {functionName} (variadic)");
parameters.Add("IntPtr args");
}

string parametersString = string.Join(", ", parameters);
Expand Down Expand Up @@ -644,22 +652,27 @@ private static bool IsNotPrimitive(string identifier)
return identifier[0].ToString().ToLower() != identifier[0].ToString();
}

private static bool IsNotArray(CppFunction fn, CppParameter param)
private static bool IsSimpleRef(CppFunction fn, string name, string type)
{
var name = param.Name;
if (type.Contains("void") || type.Contains("delegate") || type == "byte")
{
return false;
}

var baseName = name.EndsWith("s") ? name.Substring(0, name.Length - 1) : name;
var possibleSuffixes = new List<string> { "Count", "Length", "sCount", "sLength" };
var possibleSuffixes = new List<string> { "Count", "sCount", "Length", "sLength", "Size", "sSize", "Len", "sLen" };

foreach (var p in fn.Parameters)
{
foreach (var suffix in possibleSuffixes)
{
if (p.Name == baseName + suffix)
if (p.Name == baseName + suffix || p.Name == suffix.ToLower())
{
return false; // It's an array
}
}
}

return true; // It's not an array
}

Expand Down
3 changes: 2 additions & 1 deletion src/Bindgen/GeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public struct GeneratorOptions
public string[] SystemIncludeFolders = { };
public string[] IncludeFolders = { };
public string[] Defines = { };
public Func<string, string, string, string?> TransformType = (parent, name, type) => type;
public Func<string, string, string, string?> TransformType = (parent, name, type) => null;
public Func<string, string, bool> DetectArray = (parent, name) => false;
public Dictionary<string, string> ExistingTypes = new();

public GeneratorOptions()
Expand Down
19 changes: 19 additions & 0 deletions src/Raylib.NET.Bindgen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@

var options = new GeneratorOptions
{
DetectArray = (string parent, string name) => {
if (parent == "rlGenTextureMipmaps") {
return true;
}
if (parent == "rlMultMatrixf") {
return true;
}
if (parent == "rlSetShader") {
return true;
}
if (parent.StartsWith("Unload")) {
return true;
}
return false;
},
TransformType = (string parent, string name, string type) =>
type switch
{
Expand Down
44 changes: 22 additions & 22 deletions src/Raylib.NET/Raygui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static unsafe partial class Raygui
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiScrollPanel(Vector4 bounds, string text, Vector4 content, Vector2* scroll, Vector4* view);
public static partial int GuiScrollPanel(Vector4 bounds, string text, Vector4 content, ref Vector2 scroll, ref Vector4 view);

/// <summary>
/// Label control
Expand Down Expand Up @@ -244,63 +244,63 @@ public static unsafe partial class Raygui
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiToggle(Vector4 bounds, string text, NativeBool* active);
public static partial int GuiToggle(Vector4 bounds, string text, ref NativeBool active);

/// <summary>
/// Toggle Group control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiToggleGroup(Vector4 bounds, string text, int* active);
public static partial int GuiToggleGroup(Vector4 bounds, string text, ref int active);

/// <summary>
/// Toggle Slider control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiToggleSlider(Vector4 bounds, string text, int* active);
public static partial int GuiToggleSlider(Vector4 bounds, string text, ref int active);

/// <summary>
/// Check Box control, returns true when active
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiCheckBox(Vector4 bounds, string text, NativeBool* @checked);
public static partial int GuiCheckBox(Vector4 bounds, string text, ref NativeBool @checked);

/// <summary>
/// Combo Box control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiComboBox(Vector4 bounds, string text, int* active);
public static partial int GuiComboBox(Vector4 bounds, string text, ref int active);

/// <summary>
/// Dropdown Box control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiDropdownBox(Vector4 bounds, string text, int* active, NativeBool editMode);
public static partial int GuiDropdownBox(Vector4 bounds, string text, ref int active, NativeBool editMode);

/// <summary>
/// Spinner control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiSpinner(Vector4 bounds, string text, int* value, int minValue, int maxValue, NativeBool editMode);
public static partial int GuiSpinner(Vector4 bounds, string text, ref int value, int minValue, int maxValue, NativeBool editMode);

/// <summary>
/// Value Box control, updates input text with numbers
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiValueBox(Vector4 bounds, string text, int* value, int minValue, int maxValue, NativeBool editMode);
public static partial int GuiValueBox(Vector4 bounds, string text, ref int value, int minValue, int maxValue, NativeBool editMode);

/// <summary>
/// Value box control for float values
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiValueBoxFloat(Vector4 bounds, string text, string textValue, float* value, NativeBool editMode);
public static partial int GuiValueBoxFloat(Vector4 bounds, string text, string textValue, ref float value, NativeBool editMode);

/// <summary>
/// Text Box control, updates input text
Expand All @@ -314,21 +314,21 @@ public static unsafe partial class Raygui
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiSlider(Vector4 bounds, string textLeft, string textRight, float* value, float minValue, float maxValue);
public static partial int GuiSlider(Vector4 bounds, string textLeft, string textRight, ref float value, float minValue, float maxValue);

/// <summary>
/// Slider Bar control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiSliderBar(Vector4 bounds, string textLeft, string textRight, float* value, float minValue, float maxValue);
public static partial int GuiSliderBar(Vector4 bounds, string textLeft, string textRight, ref float value, float minValue, float maxValue);

/// <summary>
/// Progress Bar control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiProgressBar(Vector4 bounds, string textLeft, string textRight, float* value, float minValue, float maxValue);
public static partial int GuiProgressBar(Vector4 bounds, string textLeft, string textRight, ref float value, float minValue, float maxValue);

/// <summary>
/// Status Bar control, shows info text
Expand All @@ -349,14 +349,14 @@ public static unsafe partial class Raygui
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiGrid(Vector4 bounds, string text, float spacing, int subdivs, Vector2* mouseCell);
public static partial int GuiGrid(Vector4 bounds, string text, float spacing, int subdivs, ref Vector2 mouseCell);

/// <summary>
/// List View control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiListView(Vector4 bounds, string text, int* scrollIndex, int* active);
public static partial int GuiListView(Vector4 bounds, string text, ref int scrollIndex, ref int active);

/// <summary>
/// List View with extended parameters
Expand All @@ -377,47 +377,47 @@ public static unsafe partial class Raygui
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiTextInputBox(Vector4 bounds, string title, string message, string buttons, string text, int textMaxSize, NativeBool* secretViewActive);
public static partial int GuiTextInputBox(Vector4 bounds, string title, string message, string buttons, string text, int textMaxSize, ref NativeBool secretViewActive);

/// <summary>
/// Color Picker control (multiple color controls)
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiColorPicker(Vector4 bounds, string text, Color* color);
public static partial int GuiColorPicker(Vector4 bounds, string text, ref Color color);

/// <summary>
/// Color Panel control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiColorPanel(Vector4 bounds, string text, Color* color);
public static partial int GuiColorPanel(Vector4 bounds, string text, ref Color color);

/// <summary>
/// Color Bar Alpha control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiColorBarAlpha(Vector4 bounds, string text, float* alpha);
public static partial int GuiColorBarAlpha(Vector4 bounds, string text, ref float alpha);

/// <summary>
/// Color Bar Hue control
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiColorBarHue(Vector4 bounds, string text, float* value);
public static partial int GuiColorBarHue(Vector4 bounds, string text, ref float value);

/// <summary>
/// Color Picker control that avoids conversion to RGB on each call (multiple color controls)
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiColorPickerHSV(Vector4 bounds, string text, Vector3* colorHsv);
public static partial int GuiColorPickerHSV(Vector4 bounds, string text, ref Vector3 colorHsv);

/// <summary>
/// Color Panel control that updates Hue-Saturation-Value color value, used by GuiColorPickerHSV()
/// </summary>
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial int GuiColorPanelHSV(Vector4 bounds, string text, Vector3* colorHsv);
public static partial int GuiColorPanelHSV(Vector4 bounds, string text, ref Vector3 colorHsv);
}
Loading

0 comments on commit 09ebdcf

Please sign in to comment.