Skip to content

Commit 0e4c73d

Browse files
committed
Add mpu --internal option to generate source code asset friendly serializer sources.
1 parent 5002fee commit 0e4c73d

8 files changed

Lines changed: 76 additions & 22 deletions

File tree

src/MsgPack/Serialization/CodeDomSerializers/CodeDomContext.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ public string GetUniqueVariableName( string prefix )
163163
return prefix + counter.ToString( CultureInfo.InvariantCulture );
164164
}
165165

166+
/// <summary>
167+
/// Gets a value indicating whether the generated serializers will be internal to MsgPack library itself.
168+
/// </summary>
169+
/// <value>
170+
/// <c>true</c> if the generated serializers are internal to MsgPack library itself; otherwise, <c>false</c>.
171+
/// </value>
172+
/// <remarks>
173+
/// When you use MsgPack in Unity3D, you can import the library in source code form to your assets.
174+
/// And, you may also import generated serializers together, then the generated serializers and MsgPack library will be same assembly ultimately.
175+
/// It causes compilation error because some of overriding members have accessbility <c>FamilyOrAssembly</c>(<c>protected internal</c> in C#),
176+
/// so the generated source code must have the accessibility when and only when they will be same assembly as MsgPack library itself.
177+
/// </remarks>
178+
public bool IsInternalToMsgPackLibrary { get { return this._configuration.IsInternalToMsgPackLibrary; } }
179+
166180
/// <summary>
167181
/// Resets internal states for new type.
168182
/// </summary>

src/MsgPack/Serialization/CodeDomSerializers/CodeDomSerializerBuilder`1.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ protected override void EmitMethodEpilogue( CodeDomContext context, SerializerMe
5858
{
5959
return;
6060
}
61-
6261
CodeMemberMethod codeMethod;
6362
switch ( method )
6463
{
@@ -68,9 +67,6 @@ protected override void EmitMethodEpilogue( CodeDomContext context, SerializerMe
6867
new CodeMemberMethod
6968
{
7069
Name = "PackToCore",
71-
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags
72-
Attributes = MemberAttributes.Family | MemberAttributes.Override
73-
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
7470
};
7571
codeMethod.Parameters.Add( context.Packer.AsParameter() );
7672
codeMethod.Parameters.Add( context.PackToTarget.AsParameter() );
@@ -83,9 +79,6 @@ protected override void EmitMethodEpilogue( CodeDomContext context, SerializerMe
8379
new CodeMemberMethod
8480
{
8581
Name = "UnpackFromCore",
86-
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags
87-
Attributes = MemberAttributes.Family | MemberAttributes.Override,
88-
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
8982
ReturnType = new CodeTypeReference( typeof( TObject ) )
9083
};
9184
codeMethod.Parameters.Add( context.Unpacker.AsParameter() );
@@ -98,9 +91,6 @@ protected override void EmitMethodEpilogue( CodeDomContext context, SerializerMe
9891
new CodeMemberMethod
9992
{
10093
Name = "UnpackToCore",
101-
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags
102-
Attributes = MemberAttributes.Family | MemberAttributes.Override
103-
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
10494
};
10595
codeMethod.Parameters.Add( context.Unpacker.AsParameter() );
10696
codeMethod.Parameters.Add( context.UnpackToTarget.AsParameter() );
@@ -114,7 +104,7 @@ protected override void EmitMethodEpilogue( CodeDomContext context, SerializerMe
114104
}
115105

116106
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags
117-
codeMethod.Attributes = MemberAttributes.Family | MemberAttributes.Override;
107+
codeMethod.Attributes = ( context.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family ) | MemberAttributes.Override;
118108
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
119109
codeMethod.Statements.AddRange( construct.AsStatements().ToArray() );
120110

@@ -138,9 +128,6 @@ protected override void EmitMethodEpilogue( CodeDomContext context, EnumSerializ
138128
new CodeMemberMethod
139129
{
140130
Name = "PackUnderlyingValueTo",
141-
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags
142-
Attributes = MemberAttributes.Family | MemberAttributes.Override
143-
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
144131
};
145132
codeMethod.Parameters.Add( context.Packer.AsParameter() );
146133
codeMethod.Parameters.Add( new CodeParameterDeclarationExpression( typeof( TObject ), "enumValue" ) );
@@ -153,9 +140,6 @@ protected override void EmitMethodEpilogue( CodeDomContext context, EnumSerializ
153140
new CodeMemberMethod
154141
{
155142
Name = "UnpackFromUnderlyingValue",
156-
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags
157-
Attributes = MemberAttributes.Family | MemberAttributes.Override,
158-
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
159143
ReturnType = new CodeTypeReference( typeof( TObject ) )
160144
};
161145
codeMethod.Parameters.Add( new CodeParameterDeclarationExpression( typeof( MessagePackObject ), "messagePackObject" ) );
@@ -169,7 +153,7 @@ protected override void EmitMethodEpilogue( CodeDomContext context, EnumSerializ
169153
}
170154

171155
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags
172-
codeMethod.Attributes = MemberAttributes.Family | MemberAttributes.Override;
156+
codeMethod.Attributes = ( context.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family ) | MemberAttributes.Override;
173157
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
174158
codeMethod.Statements.AddRange( construct.AsStatements().ToArray() );
175159

src/MsgPack/Serialization/SerializerCodeGenerationConfiguration.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace MsgPack.Serialization
2525
{
2626
/// <summary>
27-
/// Represents configuration for serializer code generation.
27+
/// Represents configuration for serializer code generation.
2828
/// </summary>
2929
public sealed class SerializerCodeGenerationConfiguration : ISerializerGeneratorConfiguration
3030
{
@@ -146,6 +146,20 @@ public SerializationMethod SerializationMethod
146146
}
147147
}
148148

149+
/// <summary>
150+
/// Gets or sets a value indicating whether the generated serializers will be internal to MsgPack library itself.
151+
/// </summary>
152+
/// <value>
153+
/// <c>true</c> if the generated serializers are internal to MsgPack library itself; otherwise, <c>false</c>.
154+
/// </value>
155+
/// <remarks>
156+
/// When you use MsgPack in Unity3D, you can import the library in source code form to your assets.
157+
/// And, you may also import generated serializers together, then the generated serializers and MsgPack library will be same assembly ultimately.
158+
/// It causes compilation error because some of overriding members have accessbility <c>FamilyOrAssembly</c>(<c>protected internal</c> in C#),
159+
/// so the generated source code must have the accessibility when and only when they will be same assembly as MsgPack library itself.
160+
/// </remarks>
161+
public bool IsInternalToMsgPackLibrary { get; set; } // This is also convinience to Unittest -- which is intern with InternalsVisibleTo
162+
149163
/// <summary>
150164
/// Initializes a new instance of the <see cref="SerializerCodeGenerationConfiguration"/> class.
151165
/// </summary>

src/mpu/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ private static int Execute( IEnumerable<string> args )
118118
"n|namespace=", "[serializer, optional] Specify namespace for generated serializer types.",
119119
value => configuration.Namespace = value
120120
},
121+
{
122+
"internal", "[serializer, optional] Specify generated source code will be internal to MsgPack library itself. This option is required if you import MsgPack sources instead of an assembly to your Assets.",
123+
_ => configuration.IsInternalToMsgPackLibrary = true
124+
},
121125
{
122126
"method=", "[serializer, optional] Specify serialization method for generated serializers. Valid value is Array or Map. Default is 'Array'.",
123127
(SerializationMethod value) => configuration.SerializationMethod = value

test/MsgPack.UnitTest/Serialization/PreGeneratedSerializerGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ public void GenerateFiles()
3535
new SerializerCodeGenerationConfiguration
3636
{
3737
Namespace = "MsgPack.Serialization.GeneratedSerializers.ArrayBased",
38-
SerializationMethod = SerializationMethod.Array
38+
SerializationMethod = SerializationMethod.Array,
39+
IsInternalToMsgPackLibrary = true // because of InternalsVisibleTo
3940
},
4041
PreGeneratedSerializerActivator.KnownTypes
4142
);
4243
SerializerGenerator.GenerateCode(
4344
new SerializerCodeGenerationConfiguration
4445
{
4546
Namespace = "MsgPack.Serialization.GeneratedSerializers.MapBased",
46-
SerializationMethod = SerializationMethod.Map
47+
SerializationMethod = SerializationMethod.Map,
48+
IsInternalToMsgPackLibrary = true // because of InternalsVisibleTo
4749
},
4850
PreGeneratedSerializerActivator.KnownTypes
4951
);

tools/mpu/bin/MsgPack.dll

512 Bytes
Binary file not shown.

tools/mpu/bin/MsgPack.xml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5073,6 +5073,20 @@
50735073
The current <see cref="T:System.CodeDom.CodeTypeDeclaration"/>.
50745074
</value>
50755075
</member>
5076+
<member name="P:MsgPack.Serialization.CodeDomSerializers.CodeDomContext.IsInternalToMsgPackLibrary">
5077+
<summary>
5078+
Gets a value indicating whether the generated serializers will be internal to MsgPack library itself.
5079+
</summary>
5080+
<value>
5081+
<c>true</c> if the generated serializers are internal to MsgPack library itself; otherwise, <c>false</c>.
5082+
</value>
5083+
<remarks>
5084+
When you use MsgPack in Unity3D, you can import the library in source code form to your assets.
5085+
And, you may also import generated serializers together, then the generated serializers and MsgPack library will be same assembly ultimately.
5086+
It causes compilation error because some of overriding members have accessbility <c>FamilyOrAssembly</c>(<c>protected internal</c> in C#),
5087+
so the generated source code must have the accessibility when and only when they will be same assembly as MsgPack library itself.
5088+
</remarks>
5089+
</member>
50765090
<member name="T:MsgPack.Serialization.DataMemberContract">
50775091
<summary>
50785092
Represents member's data contract.
@@ -6199,6 +6213,14 @@
61996213
The ID of the member.
62006214
</value>
62016215
</member>
6216+
<member name="P:MsgPack.Serialization.MessagePackMemberAttribute.Name">
6217+
<summary>
6218+
Gets or sets the name of this member.
6219+
</summary>
6220+
<value>
6221+
The name which will be used in map key on serialized MessagePack stream.
6222+
</value>
6223+
</member>
62026224
<member name="P:MsgPack.Serialization.MessagePackMemberAttribute.NilImplication">
62036225
<summary>
62046226
Gets or sets the implication of the nil value.
@@ -7674,7 +7696,7 @@
76747696
</member>
76757697
<member name="T:MsgPack.Serialization.SerializerCodeGenerationConfiguration">
76767698
<summary>
7677-
Represents configuration for serializer code generation.
7699+
Represents configuration for serializer code generation.
76787700
</summary>
76797701
</member>
76807702
<member name="M:MsgPack.Serialization.SerializerCodeGenerationConfiguration.#ctor">
@@ -7740,6 +7762,20 @@
77407762
</value>
77417763
<exception cref="T:System.ArgumentOutOfRangeException">Specified value is not valid <see cref="P:MsgPack.Serialization.SerializerCodeGenerationConfiguration.SerializationMethod"/>.</exception>
77427764
</member>
7765+
<member name="P:MsgPack.Serialization.SerializerCodeGenerationConfiguration.IsInternalToMsgPackLibrary">
7766+
<summary>
7767+
Gets or sets a value indicating whether the generated serializers will be internal to MsgPack library itself.
7768+
</summary>
7769+
<value>
7770+
<c>true</c> if the generated serializers are internal to MsgPack library itself; otherwise, <c>false</c>.
7771+
</value>
7772+
<remarks>
7773+
When you use MsgPack in Unity3D, you can import the library in source code form to your assets.
7774+
And, you may also import generated serializers together, then the generated serializers and MsgPack library will be same assembly ultimately.
7775+
It causes compilation error because some of overriding members have accessbility <c>FamilyOrAssembly</c>(<c>protected internal</c> in C#),
7776+
so the generated source code must have the accessibility when and only when they will be same assembly as MsgPack library itself.
7777+
</remarks>
7778+
</member>
77437779
<member name="T:MsgPack.Serialization.SerializerDebugging">
77447780
<summary>
77457781
Holds debugging support information.

tools/mpu/bin/mpu.exe

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)