Cat-Lips / GodotSharp.SourceGenerators

C# source generators for the Godot Game Engine
MIT License
128 stars 13 forks source link

Compile fails when using SceneTree with GpuParticles3D #55

Closed Vodurden closed 7 months ago

Vodurden commented 7 months ago

Problem

Thanks for the really useful library! Unfortunately I think I've run into an issue with the SceneTree generator.

[SceneTree] seems to generate a property / field with the type GPUParticles3D, but the actual C# type is GpuParticles3D (presumably because of this)

This fails to build with

CS0234: The type or namespace name 'GPUParticles3D' does not exist in the namespace 'Godot' (are you missing an assembly reference?) .../GodotSharp.SourceGenerators/GodotSharp.SourceGenerators.SceneTreeExtensions.SceneTreeSourceGenerator/MyScene.g.cs

I imagine this will affect other similar classes where the C# type name doesn't match the casing conventions stored in the .tscn file.

Example

If I have a scene like this:

MyScene (Node)
+- Particles (GPUParticles3D)

And I have a script like this:

[SceneTree]
public partial class MyScene : Node {}

This will generate (snippet):

[EditorBrowsable(EditorBrowsableState.Never)]
private Godot.GPUParticles3D _Particles;

public Godot.GPUParticles3D Particles => _Particles ??= node.GetNodeOrNull<Godot.GPUParticles3D>("Particles");

The generated code is invalid because it uses GPUParticles3D, but the GodotSharp type is GpuParticles3D. This causes the build to fail with:

CS0234: The type or namespace name 'GPUParticles3D' does not exist in the namespace 'Godot' (are you missing an assembly reference?) .../GodotSharp.SourceGenerators/GodotSharp.SourceGenerators.SceneTreeExtensions.SceneTreeSourceGenerator/MyScene.g.cs

Possible Fixes

It seems like the naming transaction for C# is done in the get_base_class_name C++ function, which ultimately uses the pascal_to_pascal_case function.

There also seems to be a few hardcoded overrides here.

I'm guessing we can't directly call the bindgen functions, so I imagine we'd need to title case the functions and then apply the same set of hardcoded overrides?

Vodurden commented 7 months ago

Oh it looks like GodotSharp does have a native function exposed. Not sure if we can access it in this source generator though :thinking:

Cat-Lips commented 7 months ago

Hi Vodurden.

Thanks for finding this and for investigating. Pull request contains tests & changes required to fix the problem. Much appreciated if you could test it your end before we release :) I've uploaded a pre-release nuget and also a config you can use to easily add local packages via visual studio if that helps.

Caveats:

Cheers.

GodotSharp.SourceGenerators.2.3.4-240424-1056.Release.zip

nuget.config.zip

Vodurden commented 7 months ago

I tried this locally and it works! Thanks for resolving this so quickly :slightly_smiling_face:

Cat-Lips commented 7 months ago

Great, thanks for that. Nuget pre-release uploaded. I'll give it a few days/downloads, then create a proper release.

Cheers.