Commit graph

1697 commits

Author SHA1 Message Date
Ignacio Roldán Etcheverry
17b2838f39 C#: Cleanup Variant marshaling code in source/bindings generators
This change aims to reduce the number of places that need to be changed
when adding or editing a Godot type to the bindings.

Since the addition of `Variant.From<T>/As<T>` and
`VariantUtils.CreateFrom<T>/ConvertTo<T>`, we can now replace a lot of
the previous code in the bindings generator and the source generators
that specify these conversions for each type manually.

The only exceptions are the generic Godot collections (`Array<T>` and
`Dictionary<TKey, TValue>`) which still use the old version, as that
one cannot be matched by our new conversion methods (limitation in the
language with generics, forcing us to use delegate pointers).

The cleanup applies to:

- Bindings generator:
  - `TypeInterface.cs_variant_to_managed`
  - `TypeInterface.cs_managed_to_variant`
- Source generators:
  - `MarshalUtils.AppendNativeVariantToManagedExpr`
  - `MarshalUtils.AppendManagedToNativeVariantExpr`
  - `MarshalUtils.AppendVariantToManagedExpr`
  - `MarshalUtils.AppendManagedToVariantExpr`
2022-12-02 14:47:12 +01:00
Ignacio Roldán Etcheverry
f86c6b6ac4 C#: Replace most conversions between Variant and System.Object
This commit replaces most usages of `ConvertManagedObjectToVariant` and
`ConvertVariantToManagedObjectOfType`, by using the `Godot.Variant`
struct instead of `System.Object`.

The most notable change is to the `GetGodotPropertyDefaultValues` method
that's generated for scripts. The dictionary it returns now stores
`Godot.Variant` values.

Remaining usages are:

- The `DelegateUtils` class, for the serialization of closure display
  classes during assembly reloading by the editor. These display classes
  are compiler generated classes to store values captured by a closure.
  Since it's generated by the compiler, the only way we have to access
  the fields is through reflection. This leads to using `System.Object`.
- Converting parameters when invoking constructors from the engine.
  This will be replaced with source generators in the future.
- Legacy support for old `GetGodotPropertyDefaultValues` return values.
  We need to keep supporting the old version of this generated method
  for some time. Otherwise, if loading a project built with the previous
  version, it could lead to the loss of exported property values.
  Ideally, we should remove this legacy support before a stable release.
2022-12-02 14:47:12 +01:00
Ignacio Roldán Etcheverry
3afeb28560 C#: Remove ConvertVariantToManagedObject
Its two usages were:

- The Array `ICollection.CopyTo` implementation.
  It's possible that this class shouldn't be implementing the
  non-generic `ICollection`, but this commit doesn't change that.
  The new implementation stores the elements as boxed `Variant` values.
- The `Variant.Obj` property.
  I'm not sure if this property's existence is justified, but for now
  I rewrote it as a simpler version of `ConvertVariantToManagedObject`.
2022-12-02 03:34:40 +01:00
Ignacio Roldán Etcheverry
bcc061edae
Merge pull request #67023 from raulsntos/dotnet/fix-signal-callback-generation
C#: Fix signature of generated signal callbacks
2022-12-02 03:19:49 +01:00
Ignacio Roldán Etcheverry
6745e199fb
Merge pull request #69278 from rhofour/remove-namespaces
Remove unnecessary namespaces in generated PropertyDefVal files.
2022-11-30 18:31:52 +01:00
R. Alex Hofer
215022c507 Remove unnecessary namespaces in generated PropertyDefVal files. 2022-11-29 22:12:34 -05:00
Ignacio Roldán Etcheverry
5826e96014
Merge pull request #65905 from raulsntos/dotnet/marshaling-collections
C#: Fix marshaling generic Godot collections
2022-11-30 01:00:59 +01:00
trollodel
c90d0bd84f Use forward-declarations in big editor classes 2022-11-29 09:59:43 +01:00
Rémi Verschelde
8253c28200
Merge pull request #67031 from raulsntos/dotnet/string-extensions
C#: Cleanup and sync StringExtensions with core
2022-11-28 08:21:23 +01:00
Ignacio Roldán Etcheverry
4a82d71d73
Merge pull request #65907 from magian1127/4.0FixPropertiesGenerator
C#: Fix Generated ScriptProperty Error.
2022-11-28 01:48:52 +01:00
Raul Santos
dc2ceef0ec
C#: Remove/deprecate unnecessary string extensions
- Removed `UnicodeAt`
- Removed `EndsWith`
- Removed `LPad` and `RPad`
- Deprecated `BeginsWith` in favor of `string.StartsWith`
- Deprecated `LStrip` and `RStrip` in favor of `string.TrimStart` and `string.TrimEnd`
2022-11-27 23:25:48 +01:00
Magian
c41196f0f3 C#: Fix Generated ScriptProperty Error.
1. Add "this." to prevent errors caused by duplicate variable names.
2. Try to find the default value of property getters.
2022-11-27 17:40:40 +08:00
Ignacio Roldán Etcheverry
690199b7dd
Merge pull request #69194 from raulsntos/dotnet/begone-variant-disposer
C#: Remove VariantSpanDisposer and use constants in stackalloc
2022-11-27 03:30:59 +01:00
Raul Santos
3ff181096a
C#: Remove VariantSpanDisposer and use constants in stackalloc
- Remove `VariantSpanDisposer`, no need to dispose of the Variant Spans
since we are now borrowing the Variants instead of copying them.
- Remove `VariantSpanExtensions.Cleared` that was only used so the
Span was initialized for `VariantSpanDisposer` to know what to dispose.
- Fix stackalloc Spans to use constant VarArgsSpanThreshold
and avoid bound checks.
2022-11-27 03:04:34 +01:00
Ignacio Roldán Etcheverry
68e3f49157
Merge pull request #69191 from raulsntos/dotnet/no-throw
Fix `VariantUtils.UnsupportedType` method throwing
2022-11-26 04:38:11 +01:00
Ignacio Roldán Etcheverry
50badbd1f4
Merge pull request #69088 from raulsntos/dotnet/global
C#: Add `global::` namespace to generated source
2022-11-26 04:36:26 +01:00
Raul Santos
cfab6b0566
Fix VariantUtils.UnsupportedType method throwing
This method was not supposed to throw, just return the new constructed
exception so it can be thrown by the caller.
2022-11-26 03:05:00 +01:00
Raul Santos
b9e1ca1e86
C#: Add global:: namespace to generated source
Adds `global::` to the fully qualified types in source generators to
prevent ambiguity.
2022-11-26 01:13:16 +01:00
Rémi Verschelde
fcdded2e3d
Merge pull request #68310 from neikeq/csharp-opt-variant-generic-conv
C#: Optimize Variant conversion callbacks
2022-11-25 19:27:26 +01:00
Raul Santos
d0b166d8e4
C#: Cleanup and sync crypto/buffer StringExtensions with core
- Replaced `MD5Buffer`, `MD5Text`, `SHA256Buffer` and `SHA256Text` implementation to use the `System.Security.Cryptography` classes and avoid marshaling.
- Added `SHA1Buffer` and `SHA1Text`.
- Renamed `ToUTF8` to `ToUTF8Buffer`.
- Renamed `ToAscii` to `ToASCIIBuffer`.
- Added `ToUTF16Buffer` and `ToUTF32Buffer`.
- Added `GetStringFromUTF16` and `GetStringFromUTF32`.
2022-11-25 17:30:45 +01:00
Raul Santos
d9c495f322
C#: Cleanup and sync IsValid* StringExtensions with core
- Renamed `IsValidInteger` to `IsValidInt`.
- Added `IsValidFileName`.
- Added `IsValidHexNumber`.
- Added support for IPv6 to `IsValidIPAddress`.
- Added `ValidateNodeName`.
- Updated the documentation of the `IsValid*` methods.
2022-11-25 17:30:45 +01:00
Raul Santos
6178407940
C#: Cleanup and sync StringExtensions with core
- Moved `GetBaseName` to keep methods alphabetically sorted.
- Removed `Length`, users should just use the Length property.
- Removed `Insert`, string already has a method with the same signature that takes precedence.
- Removed `Erase`.
- Removed `ToLower` and `ToUpper`, string already has methods with the same signature that take precedence.
- Removed `FindLast` in favor of `RFind`.
- Replaced `RFind` and `RFindN` implemenation with a ca ll to `string.LastIndexOf` to avoid marshaling.
- Added `LPad` and `RPad`.
- Added `StripEscapes`.
- Replaced `LStrip` and `RStrip` implementation with a call to `string.TrimStart` and `string.TrimEnd`.
- Added `TrimPrefix` and `TrimSuffix`.
- Renamed `OrdAt` to `UnicodeAt`.
- Added `CountN` and move the `caseSensitive` parameter of `Count` to the end.
- Added `Indent` and `Dedent`.
2022-11-25 17:30:32 +01:00
Rémi Verschelde
c3af45791c
Merge pull request #67511 from neikeq/issue-66060
C#: Load assemblies as collectible only in the Godot editor
2022-11-25 10:00:33 +01:00
Ignacio Roldán Etcheverry
3f645f980c C#: Optimize Variant conversion callbacks
These callbacks are used for marshaling by callables and generic Godot
collections.

C# generics don't support specialization the way C++ templates do.
I knew NativeAOT could optimize away many type checks when the types
are known at compile time, but I didn't trust the JIT would do as good
a job, so I initially went with cached function pointers.

Well, it turns out the JIT is also very good at optimizing in this
scenario, so I'm changing the methods to do the conversion directly,
rather than returning a function pointer for the conversion.

The methods were moved to `VariantUtils`, and were renamed from
`GetFromVariantCallback/GetToVariantCallback` to `ConvertTo/CreateFrom`.

The new implementation looks like it goes through many `if` checks
at runtime to find the right branch for the type, but in practice it
works pretty much like template specialization. The JIT only generates
code for the relevant branch. Together with inlining, the result is
very close or the same as doing the conversion manually:

```cs
godot_variant variant;

int foo = variant.Int;
int bar = VariantUtils.ConvertTo<int>(variant);
```

If the type is a generic Godot collection, the conversion still goes
through a function pointer call.

The new code happens to be much shorter as well, with the file going
from 1057 lines to 407.

Side note: `Variant.cs` was mistakenly created in the wrong folder,
so I moved it to the `Core` folder.
2022-11-25 03:14:10 +01:00
Raul Santos
ca5c51f47e
C#: Add Projection documentation
- Add documentation to Projection type
- Reorder Projection members to be consistent with other C# types
2022-11-25 00:29:58 +01:00
Rémi Verschelde
93be29be06
Merge pull request #69125 from raulsntos/dotnet/bezier_derivative
C#: Implement BezierDerivative
2022-11-24 18:56:02 +01:00
Raul Santos
d2f7314716
C#: Implement BezierDerivative
Adds `BezierDerivative` method to Mathf, Vector2 and Vector3 (already exposed in Core).
2022-11-24 17:55:07 +01:00
Silc Renew
b217c41d36 Refactor interpolating functions in some classes to use Math class 2022-11-24 20:31:43 +09:00
Rémi Verschelde
124594593f
Merge pull request #68580 from rhofour/fix-source-generation
First attempt at fully qualifying the default values of C# properties.
2022-11-23 17:19:26 +01:00
R. Alex Hofer
6ab93bd919 Fully qualify C# default values in exported fields.
This avoids issues when the default values rely on using
namespaces.
2022-11-22 18:10:34 -05:00
Rémi Verschelde
52c51563fd
Merge pull request #68866 from raulsntos/dotnet/godot-enums
C#: Synchronize Godot enums with core
2022-11-19 09:40:05 +01:00
Raul Santos
792e618749
C#: Synchronize Godot enums with core 2022-11-19 01:54:23 +01:00
Rémi Verschelde
c3132b42cb
Merge pull request #66816 from raulsntos/dotnet/readonly
Add `readonly` to C# methods and types that don't mutate
2022-11-18 16:20:29 +01:00
Raul Santos
ac8ea5c821
C#: Remove "?" from CEscape and CUnescape 2022-11-15 01:40:12 +01:00
Raul Santos
71df6d66ae
Add readonly to C# methods and types that don't mutate
Also removes a few unnecessary temp variables
2022-11-14 20:32:20 +01:00
Ignacio Roldán Etcheverry
ec521a405a
Merge pull request #68253 from dzil123/fix_array_shuffle
Fix c# Array.Shuffle incorrect mono bindings
2022-11-04 23:54:53 +01:00
dzil123
2fd2a16d92 Fix c# Array.Shuffle incorrect mono bindings 2022-11-04 06:25:31 -07:00
Ignacio Roldán Etcheverry
c0de8d32d5
Merge pull request #68092 from raulsntos/dotnet/variant-from-array
C#: Add implicit conversion from arrays to Variant
2022-11-03 14:46:26 +01:00
Aaron Franke
9e952c8386
Allow getting Quaternion rotation in different Euler orders 2022-11-02 19:20:10 -05:00
Raul Santos
99260d972f
C#: Add implicit conversion from arrays to Variant
Adds an implicit conversion from supported array types (the ones that
are equivalent to `Packed*Array` types in Godot) to Variant.
2022-11-02 21:46:32 +01:00
Aaron Franke
8556fdd4bc
Move EulerOrder enum to math_defs.h and global scope 2022-11-02 13:44:13 -05:00
Rémi Verschelde
d2e1b4b9f0
Merge pull request #68118 from aaronfranke/quat-from-euler
Replace Quaternion Euler constructor with `from_euler` method
2022-11-02 18:51:59 +01:00
Yuri Rubinsky
9487a6ea32 Add missed Quaternion constructor to C# 2022-11-02 11:41:32 +03:00
Aaron Franke
83634119d4
Replace Quaternion Euler constructor with from_euler method 2022-11-01 09:28:12 -05:00
Rémi Verschelde
5947f22be9
Merge pull request #67578 from KoBeWi/GEDITOR
Unify usage of GLOBAL/EDITOR_GET
2022-10-31 13:15:58 +01:00
Ignacio Roldán Etcheverry
f66a352c0f C#: Add Variant conversion callbacks for generic Godot collections
This allows using generic Godot collections as type arguments for other
generic Godot collections. This also allows generic Godot collections
as parameter or return type in dynamic Callable invocations.
2022-10-30 01:24:15 +02:00
Ignacio Roldán Etcheverry
282bd37e5c C#: Remove need for reflection to invoking callable delegates
We aim to make the C# API reflection-free, mainly for concerns about
performance, and to be able to target NativeAOT in refletion-free mode,
which reduces the binary size.

One of the main usages of reflection still left was the dynamic
invokation of callable delegates, and for some time I wasn't sure
I would find an alternative solution that I'd be happy with.

The new solution uses trampoline functions to invoke the delegates:

```
static void Trampoline(object delegateObj, NativeVariantPtrArgs args, out godot_variant ret)
{
    if (args.Count != 1)
        throw new ArgumentException($"Callable expected 1 arguments but received {args.Count}.");

    string res = ((Func<int, string>)delegateObj)(
        VariantConversionCallbacks.GetToManagedCallback<int>()(args[0])
    );

    ret = VariantConversionCallbacks.GetToVariantCallback<string>()(res);
}

Callable.CreateWithUnsafeTrampoline((int num) => "Foo" + num, &Trampoline);
```

Of course, this is too much boilerplate for user code. To improve this,
the `Callable.From` methods were added. These are overloads that take
`Action` and `Func` delegates, which covers the most common use cases:
lambdas and method groups:

```
// Lambda
Callable.From((int num) => "Foo" + num);

// Method group
string AppendNum(int num) => "Foo" + num;
Callable.From(AppendNum);
```

Unfortunately, due to limitations in the C# language, implicit
conversions from delegates to `Callable` are not supported.

`Callable.From` does not support custom delegates. These should be
uncommon, but the Godot C# API actually uses them for event signals.
As such, the bindings generator was updated to generate trampoline
functions for event signals. It was also optimized to use `Action`
instead of a custom delegate for parameterless signals, which removes
the need for the trampoline functions for those signals.

The change to reflection-free invokation removes one of the last needs
for `ConvertVariantToManagedObjectOfType`. The only remaining usage is
from calling script constructors with parameters from the engine
(`CreateManagedForGodotObjectScriptInstance`). Once that one is made
reflection-free, `ConvertVariantToManagedObjectOfType` can be removed.
2022-10-30 01:24:15 +02:00
Raul Santos
687633e5bd
Use .generated suffix for generated C# code
Use the `.generated` suffix instead of `_Generated` so .NET marks C#
file generated by Godot source generators as generated code.
2022-10-22 23:13:52 +02:00
kobewi
e48c5daddf Unify usage of GLOBAL/EDITOR_GET 2022-10-18 19:01:48 +02:00
Ignacio Roldán Etcheverry
9afc8337bd
Merge pull request #67440 from raulsntos/dotnet/bindings-conflicts
Guard against more potential conflicts in C# bindings generator
2022-10-17 00:28:07 +02:00