forked from jintiao/sproto-cs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpTypeManager.cs
115 lines (92 loc) · 3.02 KB
/
SpTypeManager.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
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
using System.Collections.Generic;
using System.IO;
public class SpTypeManager : SpProtoParserListener {
private Dictionary<string, SpProtocol> mProtocols = new Dictionary<string, SpProtocol> ();
private Dictionary<int, SpProtocol> mProtocolsByTag = new Dictionary<int, SpProtocol> ();
private Dictionary<string, SpType> mTypes = new Dictionary<string, SpType> ();
private Dictionary<string, SpType> mIncompleteTypes = new Dictionary<string, SpType> ();
private SpType mTypeInteger;
private SpType mTypeString;
private SpType mTypeBoolean;
private SpCodec mCodec;
public SpTypeManager () {
mTypeInteger = new SpType ("integer");
mTypeBoolean = new SpType ("boolean");
mTypeString = new SpType ("string");
OnNewType (mTypeInteger);
OnNewType (mTypeString);
OnNewType (mTypeBoolean);
mCodec = new SpCodec (this);
}
public void OnNewType (SpType type) {
if (GetType (type.Name) != null)
return;
if (IsTypeComplete (type))
mTypes.Add (type.Name, type);
else
mIncompleteTypes.Add (type.Name, type);
}
public void OnNewProtocol (SpProtocol protocol) {
if (GetProtocolByName (protocol.Name) != null || GetProtocolByTag (protocol.Tag) != null)
return;
mProtocols.Add (protocol.Name, protocol);
mProtocolsByTag.Add (protocol.Tag, protocol);
}
private bool IsTypeComplete (SpType type) {
return type.CheckAndUpdate ();
}
public SpProtocol GetProtocolByName (string name) {
if (mProtocols.ContainsKey (name)) {
return mProtocols[name];
}
return null;
}
public SpProtocol GetProtocolByTag (int tag) {
if (mProtocolsByTag.ContainsKey (tag)) {
return mProtocolsByTag[tag];
}
return null;
}
public SpType GetType (string name) {
if (mTypes.ContainsKey (name)) {
return mTypes[name];
}
if (mIncompleteTypes.ContainsKey (name)) {
SpType t = mIncompleteTypes[name];
if (IsTypeComplete (t)) {
mIncompleteTypes.Remove (name);
mTypes.Add (name, t);
}
return t;
}
return null;
}
public SpType GetTypeNoCheck (string name) {
if (mTypes.ContainsKey (name)) {
return mTypes[name];
}
if (mIncompleteTypes.ContainsKey (name)) {
return mIncompleteTypes[name];
}
return null;
}
public bool IsBuildinType (SpType type) {
if (type == mTypeInteger || type == mTypeBoolean || type == mTypeString)
return true;
return false;
}
public SpType Integer { get { return mTypeInteger; } }
public SpType String { get { return mTypeString; } }
public SpType Boolean { get { return mTypeBoolean; } }
public SpCodec Codec { get { return mCodec; } }
public static SpTypeManager Import (Stream stream) {
SpTypeManager instance = new SpTypeManager ();
new SpProtoParser (instance).Parse (stream);
return instance;
}
public static SpTypeManager Import (string proto) {
SpTypeManager instance = new SpTypeManager ();
new SpProtoParser (instance).Parse (proto);
return instance;
}
}