Skip to content

Commit 8bebc56

Browse files
Replace BinaryFormatter with DataContractSerializer (#641)
* Replace BinaryFormatter with DataContractSerializer BinaryFormatter is deprecated in future .NET versions and may prevent assemblies from unloading or return incorrect results. Replaced with System.Runtime.Serialization.DataContractSerializer as recommended for compatibility. Changes: - KdTree.cs: Replaced BinaryFormatter with DataContractSerializer in SaveToFile and LoadFromFile methods - KdTreeNode.cs: Added DataContract and DataMember attributes - Added [DataContract] and [DataMember] attributes to all serialized classes Related to SCP-1555 * Use binary XML format for DataContractSerializer Switch from text XML to binary XML format using XmlDictionaryWriter.CreateBinaryWriter() and XmlDictionaryReader.CreateBinaryReader(). This provides: - Binary format similar to BinaryFormatter (more compact and efficient) - Better performance for large KdTree data structures - Still uses DataContractSerializer for CoreCLR compatibility Note: This changes the file format. Existing serialized KdTree files will need to be regenerated. --------- Co-authored-by: thomas-tu <45040865+thomas-tu@users.noreply.github.com>
1 parent 41c5767 commit 8bebc56

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

External/KdTree/KdTreeLib/KdTree.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6-
using System.Runtime.Serialization.Formatters.Binary;
6+
using System.Runtime.Serialization;
77
using System.Text;
8+
using System.Xml;
89

910
namespace UnityEngine.ProBuilder.KdTree
1011
{
@@ -25,6 +26,7 @@ public DuplicateNodeError()
2526
}
2627

2728
[Serializable]
29+
[DataContract]
2830
class KdTree<TKey, TValue> : IKdTree<TKey, TValue>
2931
{
3032
public KdTree(int dimensions, ITypeMath<TKey> typeMath)
@@ -40,12 +42,16 @@ public KdTree(int dimensions, ITypeMath<TKey> typeMath, AddDuplicateBehavior add
4042
AddDuplicateBehavior = addDuplicateBehavior;
4143
}
4244

45+
[DataMember]
4346
private int dimensions;
4447

48+
[DataMember]
4549
private ITypeMath<TKey> typeMath = null;
4650

51+
[DataMember]
4752
private KdTreeNode<TKey, TValue> root = null;
4853

54+
[DataMember]
4955
public AddDuplicateBehavior AddDuplicateBehavior { get; private set; }
5056

5157
public bool Add(TKey[] point, TValue value)
@@ -370,7 +376,8 @@ public KdTreeNode<TKey, TValue>[] RadialSearch(TKey[] center, TKey radius, int c
370376
return neighbourArray;
371377
}
372378

373-
public int Count { get; private set; }
379+
[DataMember]
380+
public int Count { get; private set; }
374381

375382
public bool TryFindValueAt(TKey[] point, out TValue value)
376383
{
@@ -578,20 +585,22 @@ public void Clear()
578585

579586
public void SaveToFile(string filename)
580587
{
581-
BinaryFormatter formatter = new BinaryFormatter();
588+
var serializer = new DataContractSerializer(typeof(KdTree<TKey, TValue>));
582589
using (FileStream stream = File.Create(filename))
590+
using (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream))
583591
{
584-
formatter.Serialize(stream, this);
585-
stream.Flush();
592+
serializer.WriteObject(writer, this);
593+
writer.Flush();
586594
}
587595
}
588596

589597
public static KdTree<TKey, TValue> LoadFromFile(string filename)
590598
{
591-
BinaryFormatter formatter = new BinaryFormatter();
599+
var serializer = new DataContractSerializer(typeof(KdTree<TKey, TValue>));
592600
using (FileStream stream = File.Open(filename, FileMode.Open))
601+
using (var reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max))
593602
{
594-
return (KdTree<TKey, TValue>)formatter.Deserialize(stream);
603+
return (KdTree<TKey, TValue>)serializer.ReadObject(reader);
595604
}
596605

597606
}

External/KdTree/KdTreeLib/KdTreeNode.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
using System.Text;
33
using System.Linq;
44
using System.Collections.Generic;
5+
using System.Runtime.Serialization;
56

67
namespace UnityEngine.ProBuilder.KdTree
78
{
89
[Serializable]
10+
[DataContract]
911
class KdTreeNode<TKey, TValue>
1012
{
1113
public KdTreeNode()
@@ -18,11 +20,16 @@ public KdTreeNode(TKey[] point, TValue value)
1820
Value = value;
1921
}
2022

23+
[DataMember]
2124
public TKey[] Point;
25+
[DataMember]
2226
public TValue Value = default(TValue);
27+
[DataMember]
2328
public List<TValue> Duplicates = null;
2429

30+
[DataMember]
2531
internal KdTreeNode<TKey, TValue> LeftChild = null;
32+
[DataMember]
2633
internal KdTreeNode<TKey, TValue> RightChild = null;
2734

2835
internal KdTreeNode<TKey, TValue> this[int compare]

0 commit comments

Comments
 (0)