Skip to content

Commit

Permalink
fix: replacing const with attribute (#1076)
Browse files Browse the repository at this point in the history
const does not behave well with some types like enums
  • Loading branch information
James-Frowen committed May 12, 2022
1 parent 3d68c34 commit de6c97c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
20 changes: 20 additions & 0 deletions Assets/Mirage/Runtime/Serialization/WeaverAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
using System;

namespace Mirage.Serialization.Internal.Codegen
{
/// <summary>
/// Used by weaver
/// <para>
/// Weavers Adds to types that have readers generared for them. Checked by other asmdefs so they dont generate their own readers
/// </para>
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class WeaverReaderGeneratedAttribute : Attribute { }

/// <summary>
/// Used by weaver
/// <para>
/// Weavers Adds to types that have writers generared for them. Checked by other asmdefs so they dont generate their own writers
/// </para>
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class WeaverWriterGeneratedAttribute : Attribute { }
}
namespace Mirage.Serialization
{
// weaver doesn't need constructor parameters to be used, so we can have constructor without fields/properties
Expand Down
8 changes: 8 additions & 0 deletions Assets/Mirage/Weaver/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using Mono.Cecil;
using ConstructorInfo = System.Reflection.ConstructorInfo;

namespace Mirage.Weaver
{
Expand Down Expand Up @@ -278,6 +279,13 @@ public static bool HasCustomAttribute(this ICustomAttributeProvider attributePro
return attributeProvider.CustomAttributes.Any(attr => attr.AttributeType.Is(t));
}

public static void AddCustomAttribute(this ICustomAttributeProvider attributeProvider, ModuleDefinition module, Type t)
{
ConstructorInfo constructor = t.GetConstructor(new Type[0]);
var customAttribute = new CustomAttribute(module.ImportReference(constructor));
attributeProvider.CustomAttributes.Add(customAttribute);
}

public static T GetField<T>(this CustomAttribute ca, string field, T defaultValue)
{
foreach (CustomAttributeNamedArgument customField in ca.Fields)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Weaver/Serialization/Readers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Readers : SerializeFunctionBase
public Readers(ModuleDefinition module, IWeaverLogger logger) : base(module, logger) { }

protected override string FunctionTypeLog => "read function";
protected override string GeneratedLabel => "__MirageReaderGenerated";
protected override Type GeneratedAttribute => typeof(Mirage.Serialization.Internal.Codegen.WeaverReaderGeneratedAttribute);
protected override Expression<Action> ArrayExpression => () => CollectionExtensions.ReadArray<byte>(default);
protected override Expression<Action> ListExpression => () => CollectionExtensions.ReadList<byte>(default);
protected override Expression<Action> SegmentExpression => () => CollectionExtensions.ReadArraySegment<byte>(default);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Weaver/Serialization/Writers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Writers : SerializeFunctionBase
public Writers(ModuleDefinition module, IWeaverLogger logger) : base(module, logger) { }

protected override string FunctionTypeLog => "write function";
protected override string GeneratedLabel => "__MirageWirterGenerated";
protected override Type GeneratedAttribute => typeof(Mirage.Serialization.Internal.Codegen.WeaverWriterGeneratedAttribute);
protected override Expression<Action> ArrayExpression => () => CollectionExtensions.WriteArray<byte>(default, default);
protected override Expression<Action> ListExpression => () => CollectionExtensions.WriteList<byte>(default, default);
protected override Expression<Action> SegmentExpression => () => CollectionExtensions.WriteArraySegment<byte>(default, default);
Expand Down
16 changes: 10 additions & 6 deletions Assets/Mirage/Weaver/SerializeFunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class SerializeFunctionBase
/// <summary>
/// Name for const that will tell other asmdef's that type has already generated function
/// </summary>
protected abstract string GeneratedLabel { get; }
protected abstract Type GeneratedAttribute { get; }

protected SerializeFunctionBase(ModuleDefinition module, IWeaverLogger logger)
{
Expand Down Expand Up @@ -63,10 +63,14 @@ private void MarkAsGenerated(TypeReference typeReference)
private void MarkAsGenerated(TypeDefinition typeDefinition)
{
// if in this module, then mark as generated
if (typeDefinition.Module == module)
{
typeDefinition.SetConst(GeneratedLabel, true);
}
if (typeDefinition.Module != module)
return;

// dont add twice
if (typeDefinition.HasCustomAttribute(GeneratedAttribute))
return;

typeDefinition.AddCustomAttribute(module, GeneratedAttribute);
}

/// <summary>
Expand All @@ -80,7 +84,7 @@ private bool HasGeneratedFunctionInAnotherModule(TypeReference typeReference)
if (def.Module == module)
return false;

return def.GetConst<bool>(GeneratedLabel);
return def.HasCustomAttribute(GeneratedAttribute);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void SendValue_None(MessageWithNoWriter value)
}
}

public class RpcWithTypeInAnotherAssembly : ClientServerSetup<RpcWithTypeInAnotherAssemblyBehaviour>
public class GeneratedFunctionsInOtherAssembly : ClientServerSetup<RpcWithTypeInAnotherAssemblyBehaviour>
{
[UnityTest]
[Description("This has custom writer in other Assembly, we need to make sure we use it")]
Expand Down

0 comments on commit de6c97c

Please sign in to comment.