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

Allow UTF-8 names for registered members #18

Merged
merged 1 commit into from
Sep 2, 2024
Merged
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
32 changes: 29 additions & 3 deletions src/Godot.SourceGenerators/BindMethodsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ private static void WriteCachedStringNames(IndentedStringBuilder sb, GodotClassS

void AddCachedStringName(string symbolName, string value)
{
sb.AppendLine($$"""public static global::Godot.StringName @{{symbolName}} { get; } = global::Godot.StringName.CreateStaticFromAscii("{{value}}"u8);""");
if (value.IsAscii())
{
sb.AppendLine($$"""public static global::Godot.StringName @{{symbolName}} { get; } = global::Godot.StringName.CreateStaticFromAscii("{{value}}"u8);""");
}
else
{
sb.AppendLine($$"""public static global::Godot.StringName @{{symbolName}} { get; } = global::Godot.StringName.CreateFromUtf8("{{value}}"u8);""");
}
}
}

Expand Down Expand Up @@ -421,7 +428,14 @@ private static void AppendParameterInfo(this IndentedStringBuilder sb, GodotProp
: parameter.SymbolName;

sb.Append("new global::Godot.Bridge.ParameterInfo(");
sb.Append($"""global::Godot.StringName.CreateStaticFromAscii("{nameValue}"u8), """);
if (nameValue.IsAscii())
{
sb.Append($"""global::Godot.StringName.CreateStaticFromAscii("{nameValue}"u8), """);
}
else
{
sb.Append($"""global::Godot.StringName.CreateFromUtf8("{nameValue}"u8), """);
}
sb.Append(parameter.MarshalInfo.VariantType.FullNameWithGlobal());
if (parameter.MarshalInfo.VariantTypeMetadata != VariantTypeMetadata.None || parameter.HasExplicitDefaultValue)
{
Expand Down Expand Up @@ -501,7 +515,14 @@ private static void AppendPropertyInfoObjectInitializer(IndentedStringBuilder sb
}
if (!string.IsNullOrEmpty(marshalInfo.ClassName))
{
lines.Add($"""ClassName = global::Godot.StringName.CreateStaticFromAscii("{marshalInfo.ClassName}"u8),""");
if (marshalInfo.ClassName!.IsAscii())
{
lines.Add($"""ClassName = global::Godot.StringName.CreateStaticFromAscii("{marshalInfo.ClassName}"u8),""");
}
else
{
lines.Add($"""ClassName = global::Godot.StringName.CreateFromUtf8("{marshalInfo.ClassName}"u8),""");
}
}

if (lines.Count == 0)
Expand Down Expand Up @@ -536,4 +557,9 @@ private static string RemoveSignalDelegateSuffix(string delegateName)

return delegateName.Substring(0, delegateName.Length - "EventHandler".Length);
}

private static bool IsAscii(this string value)
{
return System.Text.Encoding.UTF8.GetByteCount(value) == value.Length;
}
}
Loading