summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/ProtoBuf/Serializers/EnumSerializer.cs
blob: 3136c29efc6a2245b0a725e20d8d5fd5efbf111a (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using XUtliPoolLib;

namespace ProtoBuf.Serializers
{
	internal sealed class EnumSerializer : IProtoSerializer
	{
		public Type ExpectedType
		{
			get
			{
				return this.enumType;
			}
		}

		bool IProtoSerializer.RequiresOldValue
		{
			get
			{
				return false;
			}
		}

		bool IProtoSerializer.ReturnsValue
		{
			get
			{
				return true;
			}
		}

		private readonly Type enumType;

		private readonly EnumSerializer.EnumPair[] map;

		public struct EnumPair
		{
			public readonly object RawValue;

			public readonly Enum TypedValue;

			public readonly int WireValue;

			public EnumPair(int wireValue, object raw, Type type)
			{
				this.WireValue = wireValue;
				this.RawValue = raw;
				this.TypedValue = (Enum)Enum.ToObject(type, raw);
			}
		}

		public EnumSerializer(Type enumType, EnumSerializer.EnumPair[] map)
		{
			bool flag = enumType == null;
			if (flag)
			{
				throw new ArgumentNullException("enumType");
			}
			this.enumType = enumType;
			this.map = map;
			bool flag2 = map != null;
			if (flag2)
			{
				for (int i = 1; i < map.Length; i++)
				{
					for (int j = 0; j < i; j++)
					{
						bool flag3 = map[i].WireValue == map[j].WireValue && !object.Equals(map[i].RawValue, map[j].RawValue);
						if (flag3)
						{
							throw new ProtoException("Multiple enums with wire-value " + map[i].WireValue.ToString());
						}
						bool flag4 = object.Equals(map[i].RawValue, map[j].RawValue) && map[i].WireValue != map[j].WireValue;
						if (flag4)
						{
							throw new ProtoException("Multiple enums with deserialized-value " + map[i].RawValue);
						}
					}
				}
			}
		}

		private ProtoTypeCode GetTypeCode()
		{
			Type underlyingType = Helpers.GetUnderlyingType(this.enumType);
			bool flag = underlyingType == null;
			if (flag)
			{
				underlyingType = this.enumType;
			}
			return Helpers.GetTypeCode(underlyingType);
		}

		private int EnumToWire(object value)
		{
			int result;
			switch (this.GetTypeCode())
			{
			case ProtoTypeCode.SByte:
				result = (int)((sbyte)value);
				break;
			case ProtoTypeCode.Byte:
				result = (int)((byte)value);
				break;
			case ProtoTypeCode.Int16:
				result = (int)((short)value);
				break;
			case ProtoTypeCode.UInt16:
				result = (int)((ushort)value);
				break;
			case ProtoTypeCode.Int32:
				result = (int)value;
				break;
			case ProtoTypeCode.UInt32:
				result = (int)((uint)value);
				break;
			case ProtoTypeCode.Int64:
				result = (int)((long)value);
				break;
			case ProtoTypeCode.UInt64:
				result = (int)((ulong)value);
				break;
			default:
				throw new InvalidOperationException();
			}
			return result;
		}

		private object WireToEnum(int value)
		{
			object result;
			switch (this.GetTypeCode())
			{
			case ProtoTypeCode.SByte:
				result = Enum.ToObject(this.enumType, (sbyte)value);
				break;
			case ProtoTypeCode.Byte:
				result = Enum.ToObject(this.enumType, (byte)value);
				break;
			case ProtoTypeCode.Int16:
				result = Enum.ToObject(this.enumType, (short)value);
				break;
			case ProtoTypeCode.UInt16:
				result = Enum.ToObject(this.enumType, (ushort)value);
				break;
			case ProtoTypeCode.Int32:
				result = Enum.ToObject(this.enumType, value);
				break;
			case ProtoTypeCode.UInt32:
				result = Enum.ToObject(this.enumType, (uint)value);
				break;
			case ProtoTypeCode.Int64:
				result = Enum.ToObject(this.enumType, (long)value);
				break;
			case ProtoTypeCode.UInt64:
				result = Enum.ToObject(this.enumType, (ulong)((long)value));
				break;
			default:
				throw new InvalidOperationException();
			}
			return result;
		}

		public object Read(object value, ProtoReader source)
		{
			Helpers.DebugAssert(value == null);
			int num = source.ReadInt32();
			bool flag = this.map == null;
			object result;
			if (flag)
			{
				result = this.WireToEnum(num);
			}
			else
			{
				for (int i = 0; i < this.map.Length; i++)
				{
					bool flag2 = this.map[i].WireValue == num;
					if (flag2)
					{
						return this.map[i].TypedValue;
					}
				}
				XSingleton<XDebug>.singleton.AddWarningLog("Warning: No ", (this.ExpectedType == null) ? "<null>" : this.ExpectedType.FullName, " enum is mapped to the wire-value ", num.ToString(), null, null);
				result = this.WireToEnum(num);
			}
			return result;
		}

		public void Write(object value, ProtoWriter dest)
		{
			bool flag = this.map == null;
			if (flag)
			{
				ProtoWriter.WriteInt32(this.EnumToWire(value), dest);
			}
			else
			{
				for (int i = 0; i < this.map.Length; i++)
				{
					bool flag2 = object.Equals(this.map[i].TypedValue, value);
					if (flag2)
					{
						ProtoWriter.WriteInt32(this.map[i].WireValue, dest);
						return;
					}
				}
				XSingleton<XDebug>.singleton.AddErrorLog("Warning: No wire-value is mapped to the enum ", (value == null) ? "<null>" : (value.GetType().FullName + "." + value.ToString()), " at position ", (dest == null) ? "unknown" : ProtoWriter.GetPosition(dest).ToString(), null, null);
				ProtoWriter.WriteInt32(this.EnumToWire(value), dest);
			}
		}
	}
}