GodotSharpStringCacher.MSBuild 1.0.7-alpha

This is a prerelease version of GodotSharpStringCacher.MSBuild.
There is a newer version of this package available.
See the version list below for details.
dotnet add package GodotSharpStringCacher.MSBuild --version 1.0.7-alpha
                    
NuGet\Install-Package GodotSharpStringCacher.MSBuild -Version 1.0.7-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GodotSharpStringCacher.MSBuild" Version="1.0.7-alpha">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GodotSharpStringCacher.MSBuild" Version="1.0.7-alpha" />
                    
Directory.Packages.props
<PackageReference Include="GodotSharpStringCacher.MSBuild">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GodotSharpStringCacher.MSBuild --version 1.0.7-alpha
                    
#r "nuget: GodotSharpStringCacher.MSBuild, 1.0.7-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GodotSharpStringCacher.MSBuild@1.0.7-alpha
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GodotSharpStringCacher.MSBuild&version=1.0.7-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=GodotSharpStringCacher.MSBuild&version=1.0.7-alpha&prerelease
                    
Install as a Cake Tool

Automatically cache Godot Mono/C# StringName and NodePath without changing your code

NuGet

What ?

Godot uses its own string types for a lot of features in the engine: StringName and NodePath. They each have their advantage which make a speed difference for the engine.
However in C# they are often implicited declared and can have performance impact.
For example, when writing something like Input.IsActionJustPressed("my_input"), the constructor to StringName is called on "my_input" without you noticing. This results in the GC allocation of an object that will live for only one method. Worse, the implementation of these constructors does potentially expensive calculations (for StringName, a hash is computed, which is then searched in a linked list. For NodePath, the string is deconstructed in its individual nodes, and a list is allocated to cache the results)

There are different proposals to circumvent this issue, here is mine.

Integrating

Include the package in your csproj file with

<PackageReference Include="GodotSharpStringCacher.MSBuild" PrivateAssets="all" />

That's it ! The main assembly is now automatically patched.
If you want it to affect other packages, use

<ItemGroup>
    <PackageReference Include="Foo.Bar" CacheStrings="true" />
    <Reference Include=".../Foo.Bar.dll" CacheStrings="true" />
    <ProjectReference Include=".../Foo.Bar.csproj" CacheStrings="true" />

    
    <CacheStrings Include="Foo.Bar" />
</ItemGroup>

Quick Testing

If you want to test without integrating it, compile GodotSharpStringCacher.Console. The syntax is:

GodotSharpStringCacher.Console <in_file> <out_file> [--long-names] [--no-warn-non-constant-implicit-operator]

You can browse the resulting assembly and see the results for yourself.

Detailed Configuration

Warning On Implicit Operator With Non Constant

You are encouraged to use the new syntax when creating a non-constant StringName/NodePath at run-time. For example

// don't
StringName myStr = networkPacket.StringVariable;
// do
StringName myStr = new StringName(networkPacket.StringVariable);

This conveys your intentions better, and you will be warned by default if you don't do this.

To disable this warning (which is not recommended), either use WarnOnNonConstantImplicitOperator=true on a specific assembly or add a property like this for global effect:

<GDStringWarnOnNonConstantImplicitOperator>false</GDStringWarnOnNonConstantImplicitOperator>

Long Names

By default, the variables created from your constant strings in the assembly have a short numeric name. However, in niche cases, like statically reversing your own project, you may want these variables to have names that resemble their original value. For example, "my_input" would create a variable called _my_input. If you want that in a specific assembly, you can add LongNames=true. If you want that globally, add

<GDStringUseLongNames>true</GDStringUseLongNames>

How It Works

It executes after your project compiles and directly edits the resulting assembly. It uses Mono.Cecil, a library for assembly exploring and editing.
It looks for calls to these implicit conversion operators, then if the string is a constant, adds its value as a StringName/NodePath to a static class, then replaces the call with the load to this value.

Bugs and contributing

I only tested this project on my own Godot projects, and it is still new. Therefore, there may be bugs. If you find a bug please please PLEASE fill an issue. Thanks :>

Feel free to contribute too !

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 0 7/1/2026
1.0.8 52 6/29/2026
1.0.7 59 6/26/2026
1.0.7-alpha 73 6/20/2026
1.0.6-alpha 55 6/19/2026
1.0.5-alpha 53 6/19/2026
1.0.4-alpha 59 6/18/2026
1.0.3-alpha 61 6/18/2026
1.0.2-alpha 59 6/18/2026
1.0.1-alpha 55 6/17/2026
1.0.0-alpha 60 6/15/2026