summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/ProtoBuf/NetObjectCache.cs
blob: be43372e1504377e9098fee1b5ff96c3047c0319 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using ProtoBuf.Meta;

namespace ProtoBuf
{
	internal sealed class NetObjectCache
	{
		private MutableList List
		{
			get
			{
				bool flag = this.underlyingList == null;
				if (flag)
				{
					this.underlyingList = new MutableList();
				}
				return this.underlyingList;
			}
		}

		internal const int Root = 0;

		private MutableList underlyingList;

		private object rootObject;

		private int trapStartIndex;

		private Dictionary<string, int> stringKeys;

		private Dictionary<object, int> objectKeys;

		private sealed class ReferenceComparer : IEqualityComparer<object>
		{
			public static readonly NetObjectCache.ReferenceComparer Default = new NetObjectCache.ReferenceComparer();

			private ReferenceComparer()
			{
			}

			bool IEqualityComparer<object>.Equals(object x, object y)
			{
				return x == y;
			}

			int IEqualityComparer<object>.GetHashCode(object obj)
			{
				return RuntimeHelpers.GetHashCode(obj);
			}
		}

		internal object GetKeyedObject(int key)
		{
			bool flag = key-- == 0;
			object result;
			if (flag)
			{
				bool flag2 = this.rootObject == null;
				if (flag2)
				{
					throw new ProtoException("No root object assigned");
				}
				result = this.rootObject;
			}
			else
			{
				BasicList list = this.List;
				bool flag3 = key < 0 || key >= list.Count;
				if (flag3)
				{
					Helpers.DebugWriteLine("Missing key: " + key);
					throw new ProtoException("Internal error; a missing key occurred");
				}
				object obj = list[key];
				bool flag4 = obj == null;
				if (flag4)
				{
					throw new ProtoException("A deferred key does not have a value yet");
				}
				result = obj;
			}
			return result;
		}

		internal void SetKeyedObject(int key, object value)
		{
			bool flag = key-- == 0;
			if (flag)
			{
				bool flag2 = value == null;
				if (flag2)
				{
					throw new ArgumentNullException("value");
				}
				bool flag3 = this.rootObject != null && this.rootObject != value;
				if (flag3)
				{
					throw new ProtoException("The root object cannot be reassigned");
				}
				this.rootObject = value;
			}
			else
			{
				MutableList list = this.List;
				bool flag4 = key < list.Count;
				if (flag4)
				{
					object obj = list[key];
					bool flag5 = obj == null;
					if (flag5)
					{
						list[key] = value;
					}
					else
					{
						bool flag6 = obj != value;
						if (flag6)
						{
							throw new ProtoException("Reference-tracked objects cannot change reference");
						}
					}
				}
				else
				{
					bool flag7 = key != list.Add(value);
					if (flag7)
					{
						throw new ProtoException("Internal error; a key mismatch occurred");
					}
				}
			}
		}

		internal int AddObjectKey(object value, out bool existing)
		{
			bool flag = value == null;
			if (flag)
			{
				throw new ArgumentNullException("value");
			}
			bool flag2 = value == this.rootObject;
			int result;
			if (flag2)
			{
				existing = true;
				result = 0;
			}
			else
			{
				string text = value as string;
				BasicList list = this.List;
				bool flag3 = text == null;
				int num;
				if (flag3)
				{
					bool flag4 = this.objectKeys == null;
					if (flag4)
					{
						this.objectKeys = new Dictionary<object, int>(NetObjectCache.ReferenceComparer.Default);
						num = -1;
					}
					else
					{
						bool flag5 = !this.objectKeys.TryGetValue(value, out num);
						if (flag5)
						{
							num = -1;
						}
					}
				}
				else
				{
					bool flag6 = this.stringKeys == null;
					if (flag6)
					{
						this.stringKeys = new Dictionary<string, int>();
						num = -1;
					}
					else
					{
						bool flag7 = !this.stringKeys.TryGetValue(text, out num);
						if (flag7)
						{
							num = -1;
						}
					}
				}
				bool flag8 = !(existing = (num >= 0));
				if (flag8)
				{
					num = list.Add(value);
					bool flag9 = text == null;
					if (flag9)
					{
						this.objectKeys.Add(value, num);
					}
					else
					{
						this.stringKeys.Add(text, num);
					}
				}
				result = num + 1;
			}
			return result;
		}

		internal void RegisterTrappedObject(object value)
		{
			bool flag = this.rootObject == null;
			if (flag)
			{
				this.rootObject = value;
			}
			else
			{
				bool flag2 = this.underlyingList != null;
				if (flag2)
				{
					for (int i = this.trapStartIndex; i < this.underlyingList.Count; i++)
					{
						this.trapStartIndex = i + 1;
						bool flag3 = this.underlyingList[i] == null;
						if (flag3)
						{
							this.underlyingList[i] = value;
							break;
						}
					}
				}
			}
		}

		internal void Clear()
		{
			this.trapStartIndex = 0;
			this.rootObject = null;
			bool flag = this.underlyingList != null;
			if (flag)
			{
				this.underlyingList.Clear();
			}
			bool flag2 = this.stringKeys != null;
			if (flag2)
			{
				this.stringKeys.Clear();
			}
			bool flag3 = this.objectKeys != null;
			if (flag3)
			{
				this.objectKeys.Clear();
			}
		}
	}
}