-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpGenerator.cs
More file actions
209 lines (174 loc) · 8.06 KB
/
CSharpGenerator.cs
File metadata and controls
209 lines (174 loc) · 8.06 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace MavLinkGenerator
{
public class CSharpGenerator : GeneratorBase
{
private static Dictionary<string, Tuple<string, int>> _typeMap = new Dictionary<string, Tuple<string, int>>() {
{"float" , new Tuple<string, int>("float", 4)},
{"double" , new Tuple<string, int>("double", 8)},
{"char" , new Tuple<string, int>("byte", 1)},
{"int8_t" , new Tuple<string, int>("sbyte", 1)},
{"uint8_t" , new Tuple<string, int>("byte", 1)},
{"uint8_t_mavlink_version" , new Tuple<string, int>("byte", 1)}, // Make something special?
{"int16_t" , new Tuple<string, int>("Int16", 2)},
{"uint16_t" , new Tuple<string, int>("UInt16", 2)},
{"int32_t" , new Tuple<string, int>("Int32", 4)},
{"uint32_t" , new Tuple<string, int>("UInt32", 4)},
{"int64_t" , new Tuple<string, int>("Int64", 8)},
{"uint64_t" , new Tuple<string, int>("UInt64", 8)},
};
private static Dictionary<string, string> _reservedWords = new Dictionary<string, string>() {
{"fixed", "fixedField"},
};
private void AddComment(StringBuilder sb, string description, int indent = 2)
{
description = description.Replace("\n", "\n" + Indent(indent) + "/// ");
sb.AppendLine(Indent(indent) + "/// <summary>");
sb.AppendLine(Indent(indent) + "/// " + description);
sb.AppendLine(Indent(indent) + "/// </summary>");
}
public override string GetArrayDefinitionForType(string type, int arrayLength, int typeSize)
{
// don't care for the size
return type + "[]";
}
protected override string LoadTemplate()
{
return LoadTemplate("CSharpTemplate.cs");
}
protected override void GenerateEnums(XmlDocument document, ref string template)
{
StringBuilder sb = new StringBuilder();
XmlNodeList nodes = document.SelectNodes("/mavlink/enums/enum");
foreach (XmlNode node in nodes)
{
string enumName = node.GetAttribute("name");
if (string.IsNullOrEmpty(enumName))
continue;
enumName = CamelCase(enumName);
sb.AppendLine();
string description = node.ChildValue("description");
if (!string.IsNullOrEmpty(description))
AddComment(sb, description);
sb.AppendLine(string.Format(Indent(2) + "public enum {0}", enumName));
sb.AppendLine(Indent(2) + "{");
XmlNodeList enumValues = node.SelectNodes("entry");
foreach (XmlNode enumValue in enumValues)
{
string value = enumValue.GetAttribute("value");
string name = enumValue.GetAttribute("name");
string valueDescription = enumValue.ChildValue("description");
if (string.IsNullOrEmpty(name))
continue;
name = CamelCase(name);
if (!string.IsNullOrEmpty(valueDescription))
AddComment(sb, valueDescription, 3);
if (value != null)
sb.AppendLine(Indent(3) + name + " = " + value + ",");
else
sb.AppendLine(Indent(3) + name + ",");
}
sb.AppendLine("\t\t}");
}
template = template.Replace("/*ENUMS*/", sb.ToString());
}
protected override void GenerateMessages(XmlDocument document, ref string template)
{
StringBuilder sb = new StringBuilder();
StringBuilder enumSb = new StringBuilder();
XmlNodeList messageNodes = document.SelectNodes("/mavlink/messages/message");
foreach (XmlNode messageNode in messageNodes)
{
string messageId = messageNode.GetAttribute("id");
string name = messageNode.GetAttribute("name");
if (string.IsNullOrEmpty(messageId) || string.IsNullOrEmpty(name))
continue;
name = CamelCase(name);
sb.AppendLine();
string description = messageNode.ChildValue("description");
if (!string.IsNullOrEmpty(description))
AddComment(sb, description);
List<FieldDefinition> fields = GetFields(messageNode);
fields.ForEach(f => f.Name = lowerCamelCase(f.Name));
sb.AppendLine(string.Format("{0}public class {1} : MessageBase", Indent(2), name));
sb.AppendLine(Indent(2) + "{");
//sb.AppendLine(string.Join("\n", fields.Select(f => Indent(3) + "private " + f.Type + " _" + f.Name + ";").ToArray()));
//sb.AppendLine(string.Format("{0}private object[] _fields = new object[{1}];", Indent(3), fields.Count));
//sb.AppendLine();
AddComment(sb, "Static Constructor to register type", 3);
sb.AppendLine(string.Format("{0}static {1}()", Indent(3), name));
sb.AppendLine(Indent(3) + "{");
sb.AppendLine(string.Format("{0}MessageBase._messageTypes.Add({1}, typeof({2}));", Indent(4), messageId, name));
sb.AppendLine(Indent(3) + "}");
sb.AppendLine();
enumSb.AppendLine(string.Format("{0}{2} = {1},", Indent(4), messageId, name));
AddComment(sb, "Default Constructor to initialize type fields", 3);
sb.AppendLine(string.Format("{0}public {1}()", Indent(3), name));
sb.AppendLine(Indent(3) + "{");
sb.AppendLine(string.Format("{0}_fieldTypes = new Type[{1}];", Indent(4), fields.Count));
sb.AppendLine(string.Format("{0}_arrayLengths = new int[{1}];", Indent(4), fields.Count));
int index = 0;
sb.AppendLine(string.Join("\n", fields.Select(f => Indent(4) + "_fieldTypes[" + (index++) + "] = typeof(" + f.Type + ");")));
index = 0;
sb.AppendLine(string.Join("\n", fields.Select(f => string.Format("{0}_arrayLengths[{1}] = {2};", Indent(4), index++, f.IsArray ? f.ArrayLength : 0))));
sb.AppendLine(Indent(3) + "}");
AddComment(sb, "Constructor", 3);
sb.AppendLine(string.Join("\n", fields.Where(f => !string.IsNullOrEmpty(f.Description)).Select(f => string.Format("{0}/// <param name=\"{1}\">{2}</param>", Indent(3), f.Name, f.Description))));
sb.AppendLine(string.Format("{0}public {1}({2})", Indent(3), name, string.Join(", ", fields.Select(f => f.Type + " " + f.Name).ToArray())));
sb.AppendLine(Indent(4) + ": this()");
sb.AppendLine(Indent(3) + "{");
sb.AppendLine(string.Format("{0}_messageId = {1};", Indent(4), messageId));
sb.AppendLine(string.Format("{0}_fields = new object[{1}];", Indent(4), fields.Count));
sb.AppendLine(string.Format("{0}_payloadLength = {1};", Indent(4), fields.Select(f => {
if (f.IsArray)
return f.TypeSize * f.ArrayLength;
else
return f.TypeSize;
}).Sum()));
sb.AppendLine();
index = 0;
sb.AppendLine(string.Join("\n", fields.Select(f => Indent(4) + "_fields[" + (index++) + "] = " + f.Name + ";")));
sb.AppendLine(Indent(3) + "}");
for (int i = 0; i < fields.Count; ++i)
{
GenerateProperty(sb, fields[i], i, 3);
}
sb.AppendLine(Indent(2) + "}");
}
template = template.Replace("/*MESSAGES*/", sb.ToString());
template = template.Replace("/*MESSAGETYPEENUM*/", enumSb.ToString());
}
private void GenerateProperty(StringBuilder sb, FieldDefinition field, int index, int indent)
{
sb.AppendLine();
AddComment(sb, field.Description, indent);
sb.AppendLine(string.Format("{0}public {1} {2}", Indent(indent), field.Type, field.Name));
sb.AppendLine(Indent(indent) + "{");
sb.AppendLine(Indent(indent + 1) + "get");
sb.AppendLine(Indent(indent + 1) + "{");
sb.AppendLine(string.Format("{0}return ({2})_fields[{1}];", Indent(indent + 2), index, field.Type));
sb.AppendLine(Indent(indent + 1) + "}");
sb.AppendLine(Indent(indent + 1) + "set");
sb.AppendLine(Indent(indent + 1) + "{");
sb.AppendLine(string.Format("{0}if (({2})_fields[{1}] != value)", Indent(indent + 2), index, field.Type));
sb.AppendLine(Indent(indent + 2) + "{");
sb.AppendLine(string.Format("{0}_fields[{1}] = value;", Indent(indent + 3), index));
sb.AppendLine(string.Format("{0}OnPropertyChanged(\"{1}\");", Indent(indent + 3), field.Name));
sb.AppendLine(Indent(indent + 2) + "}");
sb.AppendLine(Indent(indent + 1) + "}");
sb.AppendLine(Indent(indent) + "}");
}
public override Dictionary<string, string> ReservedWords
{
get { return _reservedWords; }
}
public override Dictionary<string, Tuple<string, int>> TypeMap
{
get { return _typeMap; }
}
}
}