Skip to content

Commit cac73a1

Browse files
authored
Merge pull request #138 from Unity-Technologies/custom-wrapper-name-ability
Support custom native wrapper name
2 parents 032106d + b9a6a45 commit cac73a1

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

unity/UnityEmbedHost.Generator/NativeGeneration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static class NativeGeneration
1212
const string NoNativeWrapperAttributeName = "NoNativeWrapperAttribute";
1313
public const string NativeWrapperTypeAttributeName = "NativeWrapperTypeAttribute";
1414
public const string NativeCallbackTypeAttributeName = "NativeCallbackTypeAttribute";
15+
public const string NativeWrapperNameAttributeName = "NativeWrapperNameAttribute";
1516

1617
static readonly DiagnosticDescriptor NativeDestinationNotFound = new (
1718
id: "EMBEDHOSTGEN001",

unity/UnityEmbedHost.Generator/NativeUtils.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ namespace UnityEmbedHost.Generator;
99
static class NativeUtils
1010
{
1111
public static string NativeWrapperName(this IMethodSymbol methodSymbol)
12-
=> $"mono_{methodSymbol.Name}";
12+
{
13+
if (methodSymbol.TryFirstAttributeValue<string>(NativeGeneration.NativeWrapperNameAttributeName, out var value))
14+
return value!;
15+
return $"mono_{methodSymbol.Name}";
16+
}
1317

1418
public static string NativeCallbackName(this IMethodSymbol methodSymbol)
1519
=> methodSymbol.Name;

unity/UnityEmbedHost.Generator/Utils.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public static bool HasAttribute(this ImmutableArray<AttributeData> attributes, s
1818
public static bool TryReturnTypeFirstAttributeValue<T>(this IMethodSymbol methodSymbol, string attributeName, out T? value)
1919
=> methodSymbol.GetReturnTypeAttributes().TryFirstAttributeValue(attributeName, out value);
2020

21+
public static bool TryFirstAttributeValue<T>(this IMethodSymbol methodSymbol, string attributeName, out T? value)
22+
=> methodSymbol.GetAttributes().TryFirstAttributeValue<T>(attributeName, out value);
23+
2124
public static bool TryFirstAttributeValue<T>(this ImmutableArray<AttributeData> attributes, string attributeName, out T? value)
2225
{
2326
var explicitNativeTypeAttribute = attributes.FirstOrDefault(attr => attr.AttributeClass!.Name == attributeName);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
6+
namespace Unity.CoreCLRHelpers;
7+
8+
[AttributeUsage(AttributeTargets.Method)]
9+
public class NativeWrapperNameAttribute : Attribute
10+
{
11+
public NativeWrapperNameAttribute(string name)
12+
{
13+
}
14+
}

0 commit comments

Comments
 (0)