-
Notifications
You must be signed in to change notification settings - Fork 3
/
PyObjectEx.cs
71 lines (59 loc) · 2.25 KB
/
PyObjectEx.cs
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
using System.Collections.Generic;
using System.IO;
namespace eveMarshal
{
public class PyObjectEx : PyObject
{
private const byte PackedTerminator = 0x2D;
public bool IsType2 { get; private set; }
public PyObject Header { get; private set; }
public Dictionary<PyObject, PyObject> Dictionary { get; private set; }
public List<PyObject> List { get; private set; }
public PyObjectEx(bool isType2, PyObject header)
: base(PyObjectType.ObjectEx)
{
IsType2 = isType2;
Header = header;
Dictionary = new Dictionary<PyObject, PyObject>();
}
public PyObjectEx()
: base(PyObjectType.ObjectEx)
{
}
public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
{
if (op == MarshalOpcode.ObjectEx2)
IsType2 = true;
Dictionary = new Dictionary<PyObject, PyObject>();
List = new List<PyObject>();
Header = context.ReadObject(source);
while (source.BaseStream.Position < source.BaseStream.Length)
{
var b = source.ReadByte();
if (b == PackedTerminator)
break;
source.BaseStream.Seek(-1, SeekOrigin.Current);
List.Add(context.ReadObject(source));
}
while (source.BaseStream.Position < source.BaseStream.Length)
{
var b = source.ReadByte();
if (b == PackedTerminator)
break;
source.BaseStream.Seek(-1, SeekOrigin.Current);
var key = context.ReadObject(source);
var value = context.ReadObject(source);
Dictionary.Add(key, value);
}
}
protected override void EncodeInternal(BinaryWriter output)
{
output.WriteOpcode(IsType2 ? MarshalOpcode.ObjectEx2 : MarshalOpcode.ObjectEx1);
Header.Encode(output);
// list
output.Write(PackedTerminator);
// dict
output.Write(PackedTerminator);
}
}
}