deukpack 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,256 @@
1
+ /**
2
+ * DeukPackSerializer — 직렬화 헬퍼. DpProtocolLibrary 모듈화.
3
+ */
4
+
5
+ using System;
6
+ using System.Collections.Generic;
7
+ using System.Linq;
8
+ using System.Reflection;
9
+
10
+ namespace DeukPack.Protocol
11
+ {
12
+ /// <summary>
13
+ /// DeukPack serialization helper - modularized to reduce code duplication
14
+ /// Recursive serialization support for nested structures
15
+ /// </summary>
16
+ public static class DeukPackSerializer
17
+ {
18
+ /// <summary>
19
+ /// Write a value recursively based on its type
20
+ /// </summary>
21
+ public static void WriteValue(DpProtocol oprot, DpWireType type, object value)
22
+ {
23
+ if (value == null)
24
+ return;
25
+
26
+ switch (type)
27
+ {
28
+ case DpWireType.Bool:
29
+ oprot.WriteBool((bool)value);
30
+ break;
31
+ case DpWireType.Byte:
32
+ oprot.WriteByte((byte)value);
33
+ break;
34
+ case DpWireType.I16:
35
+ oprot.WriteI16(value is short s16 ? s16 : Convert.ToInt16(value));
36
+ break;
37
+ case DpWireType.I32:
38
+ oprot.WriteI32(value is int i32 ? i32 : Convert.ToInt32(value));
39
+ break;
40
+ case DpWireType.I64:
41
+ oprot.WriteI64(value is long i64 ? i64 : Convert.ToInt64(value));
42
+ break;
43
+ case DpWireType.Double:
44
+ oprot.WriteDouble((double)value);
45
+ break;
46
+ case DpWireType.String:
47
+ if (value is byte[] bytes)
48
+ oprot.WriteBinary(bytes);
49
+ else
50
+ oprot.WriteString((string)value);
51
+ break;
52
+ case DpWireType.Struct:
53
+ if (value is IDpSerializable serializable)
54
+ serializable.Write(oprot);
55
+ break;
56
+ default:
57
+ if (value is IDpSerializable serializableValue)
58
+ serializableValue.Write(oprot);
59
+ break;
60
+ }
61
+ }
62
+
63
+ /// <summary>
64
+ /// Read a value recursively based on its type
65
+ /// </summary>
66
+ public static object ReadValue(DpProtocol iprot, DpWireType type, Type targetType = null)
67
+ {
68
+ switch (type)
69
+ {
70
+ case DpWireType.Bool:
71
+ return iprot.ReadBool();
72
+ case DpWireType.Byte:
73
+ return iprot.ReadByte();
74
+ case DpWireType.I16:
75
+ {
76
+ short v = iprot.ReadI16();
77
+ return (targetType != null && targetType.IsEnum) ? Enum.ToObject(targetType, v) : (object)v;
78
+ }
79
+ case DpWireType.I32:
80
+ {
81
+ int v = iprot.ReadI32();
82
+ return (targetType != null && targetType.IsEnum) ? Enum.ToObject(targetType, v) : (object)v;
83
+ }
84
+ case DpWireType.I64:
85
+ {
86
+ long v = iprot.ReadI64();
87
+ return (targetType != null && targetType.IsEnum) ? Enum.ToObject(targetType, v) : (object)v;
88
+ }
89
+ case DpWireType.Double:
90
+ return iprot.ReadDouble();
91
+ case DpWireType.String:
92
+ if (targetType == typeof(string))
93
+ return iprot.ReadString();
94
+ return iprot.ReadBinary();
95
+ case DpWireType.Struct:
96
+ if (targetType != null)
97
+ {
98
+ var instance = Activator.CreateInstance(targetType);
99
+ if (instance is IDpSerializable serializable)
100
+ {
101
+ serializable.Read(iprot);
102
+ return instance;
103
+ }
104
+ }
105
+ return null;
106
+ default:
107
+ if (targetType != null)
108
+ {
109
+ var instance = Activator.CreateInstance(targetType);
110
+ if (instance is IDpSerializable serializable)
111
+ {
112
+ serializable.Read(iprot);
113
+ return instance;
114
+ }
115
+ }
116
+ return null;
117
+ }
118
+ }
119
+
120
+ /// <summary>
121
+ /// Write a list recursively
122
+ /// </summary>
123
+ public static void WriteList<T>(DpProtocol oprot, DpWireType elementType, IEnumerable<T> list)
124
+ {
125
+ var count = list is ICollection<T> collection ? collection.Count : list.Count();
126
+ oprot.WriteListBegin(new DpList { ElementType = elementType, Count = count });
127
+ foreach (var item in list)
128
+ {
129
+ WriteValue(oprot, elementType, item);
130
+ }
131
+ oprot.WriteListEnd();
132
+ }
133
+
134
+ /// <summary>
135
+ /// Read a list recursively
136
+ /// </summary>
137
+ public static List<T> ReadList<T>(DpProtocol iprot, DpWireType elementType, Func<DpProtocol, T> reader = null)
138
+ {
139
+ var listInfo = iprot.ReadListBegin();
140
+ var list = new List<T>(listInfo.Count);
141
+ for (int i = 0; i < listInfo.Count; i++)
142
+ {
143
+ if (reader != null)
144
+ {
145
+ list.Add(reader(iprot));
146
+ }
147
+ else
148
+ {
149
+ var value = ReadValue(iprot, elementType, typeof(T));
150
+ if (value is T item)
151
+ {
152
+ list.Add(item);
153
+ }
154
+ }
155
+ }
156
+ iprot.ReadListEnd();
157
+ return list;
158
+ }
159
+
160
+ /// <summary>
161
+ /// Write a set recursively
162
+ /// </summary>
163
+ public static void WriteSet<T>(DpProtocol oprot, DpWireType elementType, IEnumerable<T> set)
164
+ {
165
+ var count = set is ICollection<T> collection ? collection.Count : set.Count();
166
+ oprot.WriteSetBegin(new DpSet { ElementType = elementType, Count = count });
167
+ foreach (var item in set)
168
+ {
169
+ WriteValue(oprot, elementType, item);
170
+ }
171
+ oprot.WriteSetEnd();
172
+ }
173
+
174
+ /// <summary>
175
+ /// Read a set recursively
176
+ /// </summary>
177
+ public static HashSet<T> ReadSet<T>(DpProtocol iprot, DpWireType elementType, Func<DpProtocol, T> reader = null)
178
+ {
179
+ var setInfo = iprot.ReadSetBegin();
180
+ var set = new HashSet<T>();
181
+ for (int i = 0; i < setInfo.Count; i++)
182
+ {
183
+ if (reader != null)
184
+ {
185
+ set.Add(reader(iprot));
186
+ }
187
+ else
188
+ {
189
+ var value = ReadValue(iprot, elementType, typeof(T));
190
+ if (value is T item)
191
+ {
192
+ set.Add(item);
193
+ }
194
+ }
195
+ }
196
+ iprot.ReadSetEnd();
197
+ return set;
198
+ }
199
+
200
+ /// <summary>
201
+ /// Write a map recursively
202
+ /// </summary>
203
+ public static void WriteMap<TKey, TValue>(DpProtocol oprot, DpWireType keyType, DpWireType valueType, IDictionary<TKey, TValue> map)
204
+ {
205
+ oprot.WriteMapBegin(new DpDict { KeyType = keyType, ValueType = valueType, Count = map.Count });
206
+ foreach (var kvp in map)
207
+ {
208
+ WriteValue(oprot, keyType, kvp.Key);
209
+ WriteValue(oprot, valueType, kvp.Value);
210
+ }
211
+ oprot.WriteMapEnd();
212
+ }
213
+
214
+ /// <summary>
215
+ /// Read a map recursively
216
+ /// </summary>
217
+ public static Dictionary<TKey, TValue> ReadMap<TKey, TValue>(
218
+ DpProtocol iprot,
219
+ DpWireType keyType,
220
+ DpWireType valueType,
221
+ Func<DpProtocol, TKey> keyReader = null,
222
+ Func<DpProtocol, TValue> valueReader = null)
223
+ {
224
+ var mapInfo = iprot.ReadMapBegin();
225
+ var map = new Dictionary<TKey, TValue>(mapInfo.Count);
226
+ for (int i = 0; i < mapInfo.Count; i++)
227
+ {
228
+ TKey key;
229
+ if (keyReader != null)
230
+ {
231
+ key = keyReader(iprot);
232
+ }
233
+ else
234
+ {
235
+ var keyValue = ReadValue(iprot, keyType, typeof(TKey));
236
+ key = keyValue is TKey k ? k : default(TKey);
237
+ }
238
+
239
+ TValue value;
240
+ if (valueReader != null)
241
+ {
242
+ value = valueReader(iprot);
243
+ }
244
+ else
245
+ {
246
+ var valueObj = ReadValue(iprot, valueType, typeof(TValue));
247
+ value = valueObj is TValue v ? v : default(TValue);
248
+ }
249
+
250
+ map[key] = value;
251
+ }
252
+ iprot.ReadMapEnd();
253
+ return map;
254
+ }
255
+ }
256
+ }