-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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
Re-add project features as define constants in C# #53920
base: master
Are you sure you want to change the base?
Conversation
c4c616c
to
60dae5a
Compare
string sanitizedFeature = feature.ToUpperInvariant() | ||
.Replace("-", "_") | ||
.Replace(" ", "_") | ||
.Replace(";", "_"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be too naive, it follows the same rules that were implemented by #28786
@@ -170,9 +171,32 @@ public static bool BuildProjectBlocking(string config, [CanBeNull] string[] targ | |||
if (Internal.GodotIsRealTDouble()) | |||
buildInfo.CustomProperties.Add("GodotRealTIsDouble=true"); | |||
|
|||
if (features != null && features.Length > 0) | |||
buildInfo.CustomProperties.Add($"GodotFeatures={string.Join("%3B", SanitizeFeatures(features))}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%3B
is ;
escaped, I couldn't get it to work otherwise
The reason for the removal of the engine feature constants is as I commented in #40595:
These problems are still present in this new PR and I don't think there's a way to solve them hence why I removed them. |
Yeah, but that's how feature tags work. The method call to
Most IDEs should allow you to specify your own arguments for the build command so you could add the constants there the same way this PR adds them to the msbuild /p:GodotFeatures=GODOT_FEATURE_EXAMPLE Or you could temporarily add it to the <GodotFeatures>GODOT_FEATURE_EXAMPLE</GodotFeatures>
I agree, I don't think there's a way to solve it but I don't think it needs to be solved. We could consider this an advanced feature, most people can use runtime checks, but for users that know how this works they can take advantage of it so I think it's still worth having even with those limitations. One example I just thought of that having these constants defined allows would be including a nuget package conditionally like so: <Project Sdk="Godot.NET.Sdk/3.4.0">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup Condition=" $(GodotFeatures.Contains('GODOT_FEATURE_THAT_USES_JSON')) ">
<PackageReference Include="System.Text.Json" Version="5.0.0" />
</ItemGroup>
</Project> Footnotes
|
60dae5a
to
c9e9181
Compare
@neikeq Would be nice to have this reviewed :) (also the |
I recently added the ability to generate a C# API for my Will this PR be merged? |
@DmitriySalnikov You should be able to use the same API that is available in GDScript to check if a feature tag is enabled: bool isDebug =
#if DEBUG
true;
#else
false;
#endif
if (isDebug || OS.HasFeature("forced_dd3d"))
{
// Use the normal library.
}
else
{
// Use the dummy library.
} |
wrong mention I would like users to lose as little performance as possible from calling such methods. Because this API allows users not to delete debugging methods in the release build, both in GDScript and in C#. public static void DrawSphereXf(Transform3D transform, Color? color = null, float duration = 0f)
{
#if DEBUG || FORCED_DD3D
Instance?.Call(__draw_sphere_xf, transform, color ?? _DebugDrawUtils_.DefaultArgumentsData.arg_0, duration);
#endif
}
public static void DrawSphereHd(Vector3 position, float radius = 0.5f, Color? color = null, float duration = 0f)
{
#if DEBUG || FORCED_DD3D
Instance?.Call(__draw_sphere_hd, position, radius, color ?? _DebugDrawUtils_.DefaultArgumentsData.arg_0, duration);
#endif
} |
Sorry about that.
I'm not sure the performance cost is significant enough to be noticeable, but I haven't benchmarked it. The biggest cost is probably the interop call to private bool _isDebug =
#if DEBUG
true;
#else
false;
#endif
private bool? _cachedForcedDD3D;
private bool ForcedDD3D => _cachedForcedDD3D ??= OS.HasFeature("forced_dd3d");
public static void DrawSphereXf(Transform3D transform, Color? color = null, float duration = 0f)
{
if (_isDebug || ForcedDD3D)
{
Instance?.Call(__draw_sphere_xf, transform, color ?? _DebugDrawUtils_.DefaultArgumentsData.arg_0, duration);
}
} I do think having the feature tags as define constants in C# would be nice, and that's why I created this PR. I can't guarantee that this PR will ever get merged, so in the meantime, I hope this workaround can be useful to you. |
In this form, I can try to use it. Thanks for the idea. Unfortunately, all this code will still be compiled into one assembly, but it is unlikely to make any difference. upd. Runtime check is 2-3 times slower when Methods: public static void DrawBoxXf(Transform3D transform, Color? color = null, bool is_box_centered = true, float duration = 0f)
{
if (_DebugDrawUtils_.IsCallEnabled) // Equal to DEBUG
{
Instance?.Call(__draw_box_xf, transform, color ?? _DebugDrawUtils_.DefaultArgumentsData.arg_0, is_box_centered, duration);
}
}
public static void DrawBoxXfDEF(Transform3D transform, Color? color = null, bool is_box_centered = true, float duration = 0f)
{
#if DEBUG
Instance?.Call(__draw_box_xf, transform, color ?? _DebugDrawUtils_.DefaultArgumentsData.arg_0, is_box_centered, duration);
#endif
} Benchmark: int iters = 100000;
GD.Print($"Iterations = {iters}");
GD.Print($"Is enabled? {_DebugDrawUtils_.IsCallEnabled}");
var time = Time.GetTicksUsec();
for (int i = 0; i < iters; i++)
{
DebugDraw3D.DrawBoxXf(Transform3D.Identity);
}
GD.Print($"Call IF: {((Time.GetTicksUsec() - time) / 1000.0):F3} ms");
time = Time.GetTicksUsec();
for (int i = 0; i < iters; i++)
{
DebugDraw3D.DrawBoxXfDEF(Transform3D.Identity);
}
GD.Print($"Call DEF: {((Time.GetTicksUsec() - time) / 1000.0):F3} ms"); Results (Optimize = on):
|
@raulsntos / @neikeq - Does something additional happen to be injected/build into the pipeline when exporting in Godot that would affect the produced project.dll files? I guess, if it's the case that supplanting .dll's is safe, then it's a sort of workaround (not completely ofc), even if it's slightly awkward to do. |
c9e9181
to
4e5d6fc
Compare
4e5d6fc
to
bc19336
Compare
Adds the project feature tags as C# define constants.
This includes custom features, support for this was added in #28786 but got removed in #40595 and #41408 on the move to
Godot.NET.Sdk
.For a project exported with this configuration:
The C# project will have the following constants defined:
Then in the code you can use it like so: