Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Enable nullable environment for GodotSharp #83117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 65 additions & 62 deletions modules/mono/editor/bindings_generator.cpp

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions modules/mono/editor/bindings_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ class BindingsGenerator {
*/
String cs_type;

/**
* Value used when returning a default argument. Usually 'default'.
*/
String cs_default;

/**
* Formatting elements:
* %0: input expression of type `in godot_variant`
Expand Down Expand Up @@ -600,8 +605,10 @@ class BindingsGenerator {
TypeInterface() {
static String default_cs_variant_to_managed = "VariantUtils.ConvertTo<%1>(%0)";
static String default_cs_managed_to_variant = "VariantUtils.CreateFrom<%1>(%0)";
static String default_cs_default = "default";
cs_variant_to_managed = default_cs_variant_to_managed;
cs_managed_to_variant = default_cs_managed_to_variant;
cs_default = default_cs_default;
}
};

Expand Down
16 changes: 8 additions & 8 deletions modules/mono/glue/GodotSharp/GodotSharp/Compat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ partial class CodeEdit
{
/// <inheritdoc cref="AddCodeCompletionOption(CodeCompletionKind, string, string, Nullable{Color}, Resource, Variant, int)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public void AddCodeCompletionOption(CodeCompletionKind type, string displayText, string insertText, Nullable<Color> textColor, Resource icon, Nullable<Variant> value)
public void AddCodeCompletionOption(CodeCompletionKind type, string displayText, string insertText, Nullable<Color> textColor, Resource? icon, Nullable<Variant> value)
{
AddCodeCompletionOption(type, displayText, insertText, textColor, icon, value, location: 1024);
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public bool ArrangeNodesButtonHidden

/// <inheritdoc cref="GetMenuHBox()"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public HBoxContainer GetZoomHBox()
public HBoxContainer? GetZoomHBox()
{
return GetMenuHBox();
}
Expand Down Expand Up @@ -134,7 +134,7 @@ partial class ImporterMesh
{
/// <inheritdoc cref="AddSurface(Mesh.PrimitiveType, Godot.Collections.Array, Godot.Collections.Array{Godot.Collections.Array}, Godot.Collections.Dictionary, Material, string, ulong)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public void AddSurface(Mesh.PrimitiveType primitive, Godot.Collections.Array arrays, Godot.Collections.Array<Godot.Collections.Array> blendShapes, Godot.Collections.Dictionary lods, Material material, string name, uint flags)
public void AddSurface(Mesh.PrimitiveType primitive, Godot.Collections.Array arrays, Godot.Collections.Array<Godot.Collections.Array>? blendShapes, Godot.Collections.Dictionary? lods, Material? material, string name, uint flags)
{
AddSurface(primitive, arrays, blendShapes, lods, material, name, (ulong)flags);
}
Expand Down Expand Up @@ -172,9 +172,9 @@ partial class RenderingDevice
{
/// <inheritdoc cref="DrawListBegin(Rid, InitialAction, FinalAction, InitialAction, FinalAction, Color[], float, uint, Nullable{Rect2}, Godot.Collections.Array{Rid})"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public long DrawListBegin(Rid framebuffer, InitialAction initialColorAction, FinalAction finalColorAction, InitialAction initialDepthAction, FinalAction finalDepthAction, Color[] clearColorValues, float clearDepth, uint clearStencil, Nullable<Rect2> region, Godot.Collections.Array storageTextures)
public long DrawListBegin(Rid framebuffer, InitialAction initialColorAction, FinalAction finalColorAction, InitialAction initialDepthAction, FinalAction finalDepthAction, Color[]? clearColorValues, float clearDepth, uint clearStencil, Nullable<Rect2> region, Godot.Collections.Array? storageTextures)
{
return DrawListBegin(framebuffer, initialColorAction, finalColorAction, initialDepthAction, finalDepthAction, clearColorValues, clearDepth, clearStencil, region, new Godot.Collections.Array<Rid>(storageTextures));
return DrawListBegin(framebuffer, initialColorAction, finalColorAction, initialDepthAction, finalDepthAction, clearColorValues, clearDepth, clearStencil, region, new Godot.Collections.Array<Rid>(storageTextures ?? new()));
}
}

Expand All @@ -199,14 +199,14 @@ partial class SurfaceTool
{
/// <inheritdoc cref="AddTriangleFan(Vector3[], Vector2[], Color[], Vector2[], Vector3[], Godot.Collections.Array{Plane})"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public void AddTriangleFan(Vector3[] vertices, Vector2[] uvs, Color[] colors, Vector2[] uv2S, Vector3[] normals, Godot.Collections.Array tangents)
public void AddTriangleFan(Vector3[] vertices, Vector2[]? uvs, Color[]? colors, Vector2[]? uv2S, Vector3[]? normals, Godot.Collections.Array? tangents)
{
AddTriangleFan(vertices, uvs, colors, uv2S, normals, new Godot.Collections.Array<Plane>(tangents));
AddTriangleFan(vertices, uvs, colors, uv2S, normals, new Godot.Collections.Array<Plane>(tangents ?? new()));
}

/// <inheritdoc cref="Commit(ArrayMesh, ulong)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public ArrayMesh Commit(ArrayMesh existing, uint flags)
public ArrayMesh? Commit(ArrayMesh existing, uint flags)
{
return Commit(existing, (ulong)flags);
}
Expand Down
2 changes: 0 additions & 2 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Aabb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

#nullable enable

namespace Godot
{
/// <summary>
Expand Down
14 changes: 6 additions & 8 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Runtime.CompilerServices;
using Godot.NativeInterop;

#nullable enable

namespace Godot.Collections
{
/// <summary>
Expand Down Expand Up @@ -158,7 +156,7 @@ public Array(Span<Rid> array)
/// The <paramref name="array"/> is <see langword="null"/>.
/// </exception>
/// <returns>A new Godot Array.</returns>
public Array(ReadOnlySpan<GodotObject> array)
public Array(ReadOnlySpan<GodotObject?> array)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
Expand Down Expand Up @@ -1049,7 +1047,7 @@ public sealed class Array<[MustBeVariant] T> :
IEnumerable<T>,
IGenericGodotArray
{
private static godot_variant ToVariantFunc(in Array<T> godotArray) =>
private static godot_variant ToVariantFunc(in Array<T>? godotArray) =>
VariantUtils.CreateFromArray(godotArray);

private static Array<T> FromVariantFunc(in godot_variant variant) =>
Expand Down Expand Up @@ -1194,7 +1192,7 @@ public void Fill(T value)
/// is returned.
/// </summary>
/// <returns>The maximum value contained in the array.</returns>
public T Max()
public T? Max()
{
godot_variant resVariant;
var self = (godot_array)_underlyingArray.NativeValue;
Expand All @@ -1208,7 +1206,7 @@ public T Max()
/// is returned.
/// </summary>
/// <returns>The minimum value contained in the array.</returns>
public T Min()
public T? Min()
{
godot_variant resVariant;
var self = (godot_array)_underlyingArray.NativeValue;
Expand All @@ -1231,7 +1229,7 @@ public T PickRandom()
godot_variant resVariant;
var self = (godot_array)_underlyingArray.NativeValue;
NativeFuncs.godotsharp_array_pick_random(ref self, out resVariant);
return VariantUtils.ConvertTo<T>(resVariant);
return VariantUtils.ConvertTo<T>(resVariant)!;
}

/// <summary>
Expand Down Expand Up @@ -1410,7 +1408,7 @@ public unsafe T this[int index]
get
{
_underlyingArray.GetVariantBorrowElementAt(index, out godot_variant borrowElem);
return VariantUtils.ConvertTo<T>(borrowElem);
return VariantUtils.ConvertTo<T>(borrowElem)!;
}
set
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;

#nullable enable

namespace Godot
{
/// <summary>
Expand Down Expand Up @@ -46,5 +44,3 @@ public AssemblyHasScriptsAttribute(Type[] scriptTypes)
}
}
}

#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public sealed class ExportAttribute : Attribute
/// <summary>
/// Optional string that can contain additional metadata for the <see cref="Hint"/>.
/// </summary>
public string HintString { get; }
public string? HintString { get; }

/// <summary>
/// Constructs a new ExportAttribute Instance.
/// </summary>
/// <param name="hint">The hint for the exported property.</param>
/// <param name="hintString">A string that may contain additional metadata for the hint.</param>
public ExportAttribute(PropertyHint hint = PropertyHint.None, string hintString = "")
public ExportAttribute(PropertyHint hint = PropertyHint.None, string? hintString = null)
{
Hint = hint;
HintString = hintString;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;

#nullable enable

namespace Godot
{
/// <summary>
Expand All @@ -25,7 +23,7 @@ public sealed class ExportGroupAttribute : Attribute
/// </summary>
/// <param name="name">The name of the group.</param>
/// <param name="prefix">If provided, the group would make group to only consider properties that have this prefix.</param>
public ExportGroupAttribute(string name, string prefix = "")
public ExportGroupAttribute(string name, string? prefix = null)
{
Name = name;
Prefix = prefix;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;

#nullable enable

namespace Godot
{
/// <summary>
Expand All @@ -25,7 +23,7 @@ public sealed class ExportSubgroupAttribute : Attribute
/// </summary>
/// <param name="name">The name of the subgroup.</param>
/// <param name="prefix">If provided, the subgroup would make group to only consider properties that have this prefix.</param>
public ExportSubgroupAttribute(string name, string prefix = "")
public ExportSubgroupAttribute(string name, string? prefix = null)
{
Name = name;
Prefix = prefix;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;

#nullable enable

namespace Godot
{
/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Basis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Runtime.InteropServices;
using System.ComponentModel;

#nullable enable

namespace Godot
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static unsafe godot_bool Call(IntPtr godotObjectGCHandle, godot_string_
{
try
{
var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target;
var godotObject = (GodotObject?)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

if (godotObject == null)
{
Expand Down Expand Up @@ -49,7 +49,7 @@ internal static unsafe godot_bool Set(IntPtr godotObjectGCHandle, godot_string_n
{
try
{
var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target;
var godotObject = (GodotObject?)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

if (godotObject == null)
throw new InvalidOperationException();
Expand Down Expand Up @@ -79,7 +79,7 @@ internal static unsafe godot_bool Get(IntPtr godotObjectGCHandle, godot_string_n
{
try
{
var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target;
var godotObject = (GodotObject?)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

if (godotObject == null)
throw new InvalidOperationException();
Expand Down Expand Up @@ -134,7 +134,7 @@ internal static void CallDispose(IntPtr godotObjectGCHandle, godot_bool okIfNull
{
try
{
var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target;
var godotObject = (GodotObject?)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

if (okIfNull.ToBool())
godotObject?.Dispose();
Expand All @@ -152,7 +152,7 @@ internal static unsafe void CallToString(IntPtr godotObjectGCHandle, godot_strin
{
try
{
var self = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target;
var self = (GodotObject?)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

if (self == null)
{
Expand Down Expand Up @@ -186,7 +186,7 @@ internal static unsafe godot_bool HasMethodUnknownParams(IntPtr godotObjectGCHan
{
try
{
var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target;
var godotObject = (GodotObject?)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

if (godotObject == null)
return godot_bool.False;
Expand All @@ -209,7 +209,7 @@ internal static unsafe void SerializeState(
{
try
{
var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target;
var godotObject = (GodotObject?)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

if (godotObject == null)
return;
Expand Down Expand Up @@ -242,7 +242,7 @@ internal static unsafe void DeserializeState(
{
try
{
var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target;
var godotObject = (GodotObject?)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

if (godotObject == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal static godot_bool GCHandleIsTargetCollectible(IntPtr gcHandlePtr)
if (target is Delegate @delegate)
return DelegateUtils.IsDelegateCollectible(@delegate).ToGodotBool();

return target.GetType().IsCollectible.ToGodotBool();
return target!.GetType().IsCollectible.ToGodotBool();
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Godot.Bridge;

#nullable enable

public readonly struct MethodInfo
{
public StringName Name { get; init; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Godot.Bridge;

#nullable enable

public readonly struct PropertyInfo
{
public Variant.Type Type { get; init; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

namespace Godot.Bridge;

#nullable enable

public static partial class ScriptManagerBridge
{
private class ScriptTypeBiMap
Expand Down
Loading
Loading