summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/ProtoBuf/Serializers/NullDecorator.cs
blob: 2285d847341988970e48fbd334f7b15f8f86b2b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using ProtoBuf.Meta;

namespace ProtoBuf.Serializers
{
	internal sealed class NullDecorator : ProtoDecoratorBase
	{
		public override Type ExpectedType
		{
			get
			{
				return this.expectedType;
			}
		}

		public override bool ReturnsValue
		{
			get
			{
				return true;
			}
		}

		public override bool RequiresOldValue
		{
			get
			{
				return true;
			}
		}

		private readonly Type expectedType;

		public const int Tag = 1;

		public NullDecorator(TypeModel model, IProtoSerializer tail) : base(tail)
		{
			bool flag = !tail.ReturnsValue;
			if (flag)
			{
				throw new NotSupportedException("NullDecorator only supports implementations that return values");
			}
			Type type = tail.ExpectedType;
			bool flag2 = Helpers.IsValueType(type);
			if (flag2)
			{
				this.expectedType = model.MapType(typeof(Nullable<>)).MakeGenericType(new Type[]
				{
					type
				});
			}
			else
			{
				this.expectedType = type;
			}
		}

		public override object Read(object value, ProtoReader source)
		{
			SubItemToken token = ProtoReader.StartSubItem(source);
			int num;
			while ((num = source.ReadFieldHeader()) > 0)
			{
				bool flag = num == 1;
				if (flag)
				{
					value = this.Tail.Read(value, source);
				}
				else
				{
					source.SkipField();
				}
			}
			ProtoReader.EndSubItem(token, source);
			return value;
		}

		public override void Write(object value, ProtoWriter dest)
		{
			SubItemToken token = ProtoWriter.StartSubItem(null, dest);
			bool flag = value != null;
			if (flag)
			{
				this.Tail.Write(value, dest);
			}
			ProtoWriter.EndSubItem(token, dest);
		}
	}
}