Skip to content

Commit a28f372

Browse files
committed
Add PreserveAttribute to generic formatters for Unity IL2CPP
1 parent 528e6a7 commit a28f372

15 files changed

+143
-0
lines changed

sandbox/DynamicCodeDumper/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,13 @@ public EntityBase()
467467
//// }
468468
////}
469469
}
470+
471+
472+
#pragma warning disable
473+
474+
namespace MessagePack.Internal
475+
{
476+
internal sealed class PreserveAttribute : System.Attribute
477+
{
478+
}
479+
}

src/MessagePack.UnityClient/Assets/Scripts/Tests/IL2CPPTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#nullable enable
55

66
using System;
7+
using System.Collections.Concurrent;
8+
using System.Collections.Generic;
79
using System.Linq;
810
using MessagePack;
911
using NUnit.Framework;
@@ -73,6 +75,33 @@ public void LZ4BlockArray()
7375
var ys = MessagePackSerializer.Deserialize<MyClass[]>(bin, lz4Option);
7476
CollectionAssert.AreEqual(xs, ys);
7577
}
78+
79+
[Test]
80+
public void EnumSerialize()
81+
{
82+
var bin = MessagePackSerializer.Serialize(MessagePackCompression.Lz4BlockArray);
83+
var v2 = MessagePackSerializer.Deserialize<MessagePackCompression>(bin);
84+
85+
Assert.AreEqual(MessagePackCompression.Lz4BlockArray, v2);
86+
}
87+
88+
[Test]
89+
public void ListSerialize()
90+
{
91+
var bin = MessagePackSerializer.Serialize(new List<ulong> { 10UL });
92+
var v2 = MessagePackSerializer.Deserialize<List<ulong>>(bin);
93+
94+
CollectionAssert.AreEqual(new List<ulong> { 10UL }, v2);
95+
}
96+
97+
[Test]
98+
public void ConcurrentBagSerialize()
99+
{
100+
var bin = MessagePackSerializer.Serialize(new ConcurrentBag<ulong> { 10UL });
101+
var v2 = MessagePackSerializer.Deserialize<ConcurrentBag<ulong>>(bin);
102+
103+
CollectionAssert.AreEqual(new ConcurrentBag<ulong> { 10UL }, v2);
104+
}
76105
}
77106

78107
[MessagePackObject]

src/MessagePack/Formatters/CollectionFormatter.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
using System.Diagnostics;
1111
using System.Linq;
1212
using System.Runtime.InteropServices;
13+
using MessagePack.Internal;
1314

1415
#pragma warning disable SA1402 // File may only contain a single type
1516
#pragma warning disable SA1649 // File name should match first type name
1617

1718
namespace MessagePack.Formatters
1819
{
20+
[Preserve]
1921
public sealed class ArrayFormatter<T> : IMessagePackFormatter<T[]?>
2022
{
2123
public void Serialize(ref MessagePackWriter writer, T[]? value, MessagePackSerializerOptions options)
@@ -158,6 +160,7 @@ public ArraySegment<byte> Deserialize(ref MessagePackReader reader, MessagePackS
158160
}
159161
}
160162

163+
[Preserve]
161164
public sealed class MemoryFormatter<T> : IMessagePackFormatter<Memory<T>>
162165
{
163166
public void Serialize(ref MessagePackWriter writer, Memory<T> value, MessagePackSerializerOptions options)
@@ -172,6 +175,7 @@ public Memory<T> Deserialize(ref MessagePackReader reader, MessagePackSerializer
172175
}
173176
}
174177

178+
[Preserve]
175179
public sealed class ReadOnlyMemoryFormatter<T> : IMessagePackFormatter<ReadOnlyMemory<T>>
176180
{
177181
public void Serialize(ref MessagePackWriter writer, ReadOnlyMemory<T> value, MessagePackSerializerOptions options)
@@ -194,6 +198,7 @@ public ReadOnlyMemory<T> Deserialize(ref MessagePackReader reader, MessagePackSe
194198
}
195199
}
196200

201+
[Preserve]
197202
public sealed class ReadOnlySequenceFormatter<T> : IMessagePackFormatter<ReadOnlySequence<T>>
198203
{
199204
public void Serialize(ref MessagePackWriter writer, ReadOnlySequence<T> value, MessagePackSerializerOptions options)
@@ -218,6 +223,7 @@ public ReadOnlySequence<T> Deserialize(ref MessagePackReader reader, MessagePack
218223
}
219224
}
220225

226+
[Preserve]
221227
public sealed class ArraySegmentFormatter<T> : IMessagePackFormatter<ArraySegment<T>>
222228
{
223229
public void Serialize(ref MessagePackWriter writer, ArraySegment<T> value, MessagePackSerializerOptions options)
@@ -248,6 +254,7 @@ public ArraySegment<T> Deserialize(ref MessagePackReader reader, MessagePackSeri
248254
}
249255

250256
// List<T> is popular format, should avoid abstraction.
257+
[Preserve]
251258
public sealed class ListFormatter<T> : IMessagePackFormatter<List<T>?>
252259
{
253260
public void Serialize(ref MessagePackWriter writer, List<T>? value, MessagePackSerializerOptions options)
@@ -464,6 +471,7 @@ protected sealed override TCollection Complete(TCollection intermediateCollectio
464471
}
465472
}
466473

474+
[Preserve]
467475
public sealed class GenericCollectionFormatter<TElement, TCollection> : CollectionFormatterBase<TElement, TCollection>
468476
where TCollection : ICollection<TElement>, new()
469477
{
@@ -478,6 +486,7 @@ protected override void Add(TCollection collection, int index, TElement value, M
478486
}
479487
}
480488

489+
[Preserve]
481490
public sealed class GenericEnumerableFormatter<TElement, TCollection> : CollectionFormatterBase<TElement, TElement[], TCollection>
482491
where TCollection : IEnumerable<TElement>
483492
{
@@ -497,6 +506,7 @@ protected override TCollection Complete(TElement[] intermediateCollection)
497506
}
498507
}
499508

509+
[Preserve]
500510
public sealed class LinkedListFormatter<T> : CollectionFormatterBase<T, LinkedList<T>, LinkedList<T>.Enumerator, LinkedList<T>>
501511
{
502512
protected override void Add(LinkedList<T> collection, int index, T value, MessagePackSerializerOptions options)
@@ -520,6 +530,7 @@ protected override LinkedList<T>.Enumerator GetSourceEnumerator(LinkedList<T> so
520530
}
521531
}
522532

533+
[Preserve]
523534
public sealed class QueueFormatter<T> : CollectionFormatterBase<T, Queue<T>, Queue<T>.Enumerator, Queue<T>>
524535
{
525536
protected override int? GetCount(Queue<T> sequence)
@@ -640,6 +651,7 @@ public void Serialize(ref MessagePackWriter writer, PriorityQueue<TElement, TPri
640651
#endif
641652

642653
// should deserialize reverse order.
654+
[Preserve]
643655
public sealed class StackFormatter<T> : CollectionFormatterBase<T, T[], Stack<T>.Enumerator, Stack<T>>
644656
{
645657
protected override int? GetCount(Stack<T> sequence)
@@ -669,6 +681,7 @@ protected override Stack<T> Complete(T[] intermediateCollection)
669681
}
670682
}
671683

684+
[Preserve]
672685
public sealed class HashSetFormatter<T> : CollectionFormatterBase<T, HashSet<T>, HashSet<T>.Enumerator, HashSet<T>>
673686
{
674687
protected override int? GetCount(HashSet<T> sequence)
@@ -697,6 +710,7 @@ protected override HashSet<T>.Enumerator GetSourceEnumerator(HashSet<T> source)
697710
}
698711
}
699712

713+
[Preserve]
700714
public sealed class ReadOnlyCollectionFormatter<T> : CollectionFormatterBase<T, T[], ReadOnlyCollection<T>>
701715
{
702716
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
@@ -753,6 +767,7 @@ protected override ICollection<T> Complete(T[] intermediateCollection)
753767
}
754768
}
755769

770+
[Preserve]
756771
public sealed class InterfaceListFormatter2<T> : CollectionFormatterBase<T, List<T>, IList<T>>
757772
{
758773
protected override void Add(List<T> collection, int index, T value, MessagePackSerializerOptions options)
@@ -771,6 +786,7 @@ protected override IList<T> Complete(List<T> intermediateCollection)
771786
}
772787
}
773788

789+
[Preserve]
774790
public sealed class InterfaceCollectionFormatter2<T> : CollectionFormatterBase<T, List<T>, ICollection<T>>
775791
{
776792
protected override void Add(List<T> collection, int index, T value, MessagePackSerializerOptions options)
@@ -789,6 +805,7 @@ protected override ICollection<T> Complete(List<T> intermediateCollection)
789805
}
790806
}
791807

808+
[Preserve]
792809
public sealed class InterfaceEnumerableFormatter<T> : CollectionFormatterBase<T, T[], IEnumerable<T>>
793810
{
794811
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
@@ -808,6 +825,7 @@ protected override IEnumerable<T> Complete(T[] intermediateCollection)
808825
}
809826

810827
// [Key, [Array]]
828+
[Preserve]
811829
public sealed class InterfaceGroupingFormatter<TKey, TElement> : IMessagePackFormatter<IGrouping<TKey, TElement>?>
812830
{
813831
public void Serialize(ref MessagePackWriter writer, IGrouping<TKey, TElement>? value, MessagePackSerializerOptions options)
@@ -854,6 +872,7 @@ public void Serialize(ref MessagePackWriter writer, IGrouping<TKey, TElement>? v
854872
}
855873
}
856874

875+
[Preserve]
857876
public sealed class InterfaceLookupFormatter<TKey, TElement> : CollectionFormatterBase<IGrouping<TKey, TElement>, Dictionary<TKey, IGrouping<TKey, TElement>>, ILookup<TKey, TElement>>
858877
where TKey : notnull
859878
{
@@ -947,6 +966,7 @@ IEnumerator IEnumerable.GetEnumerator()
947966

948967
/* NonGenerics */
949968

969+
[Preserve]
950970
public sealed class NonGenericListFormatter<T> : IMessagePackFormatter<T?>
951971
where T : class, IList, new()
952972
{
@@ -998,6 +1018,7 @@ public void Serialize(ref MessagePackWriter writer, T? value, MessagePackSeriali
9981018
}
9991019
}
10001020

1021+
[Preserve]
10011022
public sealed class NonGenericInterfaceCollectionFormatter : IMessagePackFormatter<ICollection?>
10021023
{
10031024
public static readonly IMessagePackFormatter<ICollection?> Instance = new NonGenericInterfaceCollectionFormatter();
@@ -1058,6 +1079,7 @@ public void Serialize(ref MessagePackWriter writer, ICollection? value, MessageP
10581079
}
10591080
}
10601081

1082+
[Preserve]
10611083
public sealed class NonGenericInterfaceEnumerableFormatter : IMessagePackFormatter<IEnumerable?>
10621084
{
10631085
public static readonly IMessagePackFormatter<IEnumerable?> Instance = new NonGenericInterfaceEnumerableFormatter();
@@ -1199,6 +1221,7 @@ public void Serialize(ref MessagePackWriter writer, IList? value, MessagePackSer
11991221
}
12001222
}
12011223

1224+
[Preserve]
12021225
public sealed class NonGenericDictionaryFormatter<T> : IMessagePackFormatter<T?>
12031226
where T : class, IDictionary, new()
12041227
{
@@ -1312,6 +1335,7 @@ public void Serialize(ref MessagePackWriter writer, IDictionary? value, MessageP
13121335
}
13131336
}
13141337

1338+
[Preserve]
13151339
public sealed class ObservableCollectionFormatter<T> : CollectionFormatterBase<T, ObservableCollection<T>>
13161340
{
13171341
protected override void Add(ObservableCollection<T> collection, int index, T value, MessagePackSerializerOptions options)
@@ -1325,6 +1349,7 @@ protected override ObservableCollection<T> Create(int count, MessagePackSerializ
13251349
}
13261350
}
13271351

1352+
[Preserve]
13281353
public sealed class ReadOnlyObservableCollectionFormatter<T> : CollectionFormatterBase<T, ObservableCollection<T>, ReadOnlyObservableCollection<T>>
13291354
{
13301355
protected override void Add(ObservableCollection<T> collection, int index, T value, MessagePackSerializerOptions options)
@@ -1343,6 +1368,7 @@ protected override ReadOnlyObservableCollection<T> Complete(ObservableCollection
13431368
}
13441369
}
13451370

1371+
[Preserve]
13461372
public sealed class InterfaceReadOnlyListFormatter<T> : CollectionFormatterBase<T, T[], IReadOnlyList<T>>
13471373
{
13481374
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
@@ -1361,6 +1387,7 @@ protected override IReadOnlyList<T> Complete(T[] intermediateCollection)
13611387
}
13621388
}
13631389

1390+
[Preserve]
13641391
public sealed class InterfaceReadOnlyCollectionFormatter<T> : CollectionFormatterBase<T, T[], IReadOnlyCollection<T>>
13651392
{
13661393
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
@@ -1379,6 +1406,7 @@ protected override IReadOnlyCollection<T> Complete(T[] intermediateCollection)
13791406
}
13801407
}
13811408

1409+
[Preserve]
13821410
public sealed class InterfaceSetFormatter<T> : CollectionFormatterBase<T, HashSet<T>, ISet<T>>
13831411
{
13841412
protected override void Add(HashSet<T> collection, int index, T value, MessagePackSerializerOptions options)
@@ -1441,6 +1469,7 @@ protected override HashSet<T> Create(int count, MessagePackSerializerOptions opt
14411469

14421470
#endif
14431471

1472+
[Preserve]
14441473
public sealed class ConcurrentBagFormatter<T> : CollectionFormatterBase<T, System.Collections.Concurrent.ConcurrentBag<T>>
14451474
{
14461475
protected override int? GetCount(ConcurrentBag<T> sequence)
@@ -1459,6 +1488,7 @@ protected override ConcurrentBag<T> Create(int count, MessagePackSerializerOptio
14591488
}
14601489
}
14611490

1491+
[Preserve]
14621492
public sealed class ConcurrentQueueFormatter<T> : CollectionFormatterBase<T, System.Collections.Concurrent.ConcurrentQueue<T>>
14631493
{
14641494
protected override int? GetCount(ConcurrentQueue<T> sequence)
@@ -1477,6 +1507,7 @@ protected override ConcurrentQueue<T> Create(int count, MessagePackSerializerOpt
14771507
}
14781508
}
14791509

1510+
[Preserve]
14801511
public sealed class ConcurrentStackFormatter<T> : CollectionFormatterBase<T, T[], ConcurrentStack<T>>
14811512
{
14821513
protected override int? GetCount(ConcurrentStack<T> sequence)

src/MessagePack/Formatters/DictionaryFormatter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Collections.ObjectModel;
88
using System.Reflection;
9+
using MessagePack.Internal;
910

1011
#pragma warning disable SA1402 // File may only contain a single type
1112
#pragma warning disable SA1649 // File name should match first type name
@@ -141,6 +142,7 @@ protected override TDictionary Complete(TDictionary intermediateCollection)
141142
}
142143
}
143144

145+
[Preserve]
144146
public sealed class DictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, Dictionary<TKey, TValue>.Enumerator, Dictionary<TKey, TValue>>
145147
where TKey : notnull
146148
{
@@ -165,6 +167,7 @@ protected override Dictionary<TKey, TValue>.Enumerator GetSourceEnumerator(Dicti
165167
}
166168
}
167169

170+
[Preserve]
168171
public sealed class GenericDictionaryFormatter<TKey, TValue, TDictionary> : DictionaryFormatterBase<TKey, TValue, TDictionary>
169172
where TDictionary : class?, IDictionary<TKey, TValue>, new()
170173
where TKey : notnull
@@ -180,6 +183,7 @@ protected override TDictionary Create(int count, MessagePackSerializerOptions op
180183
}
181184
}
182185

186+
[Preserve]
183187
public sealed class GenericReadOnlyDictionaryFormatter<TKey, TValue, TDictionary> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, TDictionary>
184188
where TDictionary : class?, IReadOnlyDictionary<TKey, TValue>
185189
where TKey : notnull
@@ -200,6 +204,7 @@ protected override TDictionary Complete(Dictionary<TKey, TValue> intermediateCol
200204
}
201205
}
202206

207+
[Preserve]
203208
public sealed class InterfaceDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, IDictionary<TKey, TValue>>
204209
where TKey : notnull
205210
{
@@ -219,6 +224,7 @@ protected override IDictionary<TKey, TValue> Complete(Dictionary<TKey, TValue> i
219224
}
220225
}
221226

227+
[Preserve]
222228
public sealed class SortedListFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, SortedList<TKey, TValue>>
223229
where TKey : notnull
224230
{
@@ -233,6 +239,7 @@ protected override SortedList<TKey, TValue> Create(int count, MessagePackSeriali
233239
}
234240
}
235241

242+
[Preserve]
236243
public sealed class SortedDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, SortedDictionary<TKey, TValue>, SortedDictionary<TKey, TValue>.Enumerator, SortedDictionary<TKey, TValue>>
237244
where TKey : notnull
238245
{
@@ -257,6 +264,7 @@ protected override SortedDictionary<TKey, TValue>.Enumerator GetSourceEnumerator
257264
}
258265
}
259266

267+
[Preserve]
260268
public sealed class ReadOnlyDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, ReadOnlyDictionary<TKey, TValue>>
261269
where TKey : notnull
262270
{
@@ -276,6 +284,7 @@ protected override Dictionary<TKey, TValue> Create(int count, MessagePackSeriali
276284
}
277285
}
278286

287+
[Preserve]
279288
public sealed class InterfaceReadOnlyDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>>
280289
where TKey : notnull
281290
{
@@ -295,6 +304,7 @@ protected override Dictionary<TKey, TValue> Create(int count, MessagePackSeriali
295304
}
296305
}
297306

307+
[Preserve]
298308
public sealed class ConcurrentDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>>
299309
where TKey : notnull
300310
{

src/MessagePack/Formatters/EnumAsStringFormatter`1.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace MessagePack.Formatters
1111
{
1212
// Note:This implementation is 'not' fastest, should more improve.
13+
[Preserve]
1314
public sealed class EnumAsStringFormatter<T> : IMessagePackFormatter<T>
1415
where T : struct, Enum
1516
{

0 commit comments

Comments
 (0)