forked from Xamla/ROS.NET
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SrvFile.cs
273 lines (241 loc) · 11.8 KB
/
SrvFile.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using YAMLParser;
namespace FauxMessages
{
public class SrvFile
{
private string GUTS;
public string GeneratedDictHelper;
public bool HasHeader;
public string Name;
public string Namespace = "Messages";
public MsgFile Request;
public MsgFile Response;
public List<SingleType> Stuff = new List<SingleType>();
public string backhalf;
public string classname;
private List<string> def = new List<string>();
public string dimensions = "";
public string fronthalf;
private bool meta;
public string requestbackhalf;
public string requestfronthalf;
public string responsebackhalf;
public string resposonebackhalf;
internal MsgFileLocation msgfilelocation;
public SrvFile(MsgFileLocation filename)
{
msgfilelocation = filename;
// read in srv file
var lines = File.ReadAllLines(filename.Path);
classname = filename.basename;
Namespace += "." + filename.package;
Name = filename.package + "." + filename.basename;
// def is the list of all lines in the file
def = new List<string>();
int mid = 0;
bool found = false;
var request = new List<string>();
var response = new List<string>();
// Search through for the "---" separator between request and response
for (; mid < lines.Length; mid++)
{
lines[mid] = lines[mid].Replace("\"", "\\\"");
if (lines[mid].Contains('#'))
{
lines[mid] = lines[mid].Substring(0, lines[mid].IndexOf('#'));
}
lines[mid] = lines[mid].Trim();
if (lines[mid].Length == 0)
{
continue;
}
def.Add(lines[mid]);
if (lines[mid].Contains("---"))
{
found = true;
continue;
}
if (found)
response.Add(lines[mid]);
else
request.Add(lines[mid]);
}
// treat request and response like 2 message files, each with a partial definition and extra stuff tagged on to the classname
Request = new MsgFile(new MsgFileLocation(filename.Path.Replace(".srv", ".msg"), filename.searchroot), true, request, "\t");
Response = new MsgFile(new MsgFileLocation(filename.Path.Replace(".srv", ".msg"), filename.searchroot), false, response, "\t");
}
public void ParseAndResolveTypes()
{
this.Request.ParseAndResolveTypes();
this.Response.ParseAndResolveTypes();
}
public void Write(string outdir)
{
string[] chunks = Name.Split('.');
for (int i = 0; i < chunks.Length - 1; i++)
outdir = Path.Combine(outdir, chunks[i]);
if (!Directory.Exists(outdir))
Directory.CreateDirectory(outdir);
string contents = this.ToString();
if (contents != null)
File.WriteAllText(Path.Combine(outdir, msgfilelocation.basename + ".cs"), contents.Replace("FauxMessages", "Messages"));
}
public override string ToString()
{
if (requestfronthalf == null)
{
requestfronthalf = "";
requestbackhalf = "";
string[] lines = Templates.SrvPlaceHolder.Split('\n');
int section = 0;
for (int i = 0; i < lines.Length; i++)
{
//read until you find public class request... do everything once.
//then, do it again response
if (lines[i].Contains("$$REQUESTDOLLADOLLABILLS"))
{
section++;
continue;
}
if (lines[i].Contains("namespace"))
{
requestfronthalf +=
"\nusing Messages.std_msgs;\nusing String=System.String;\nusing Messages.geometry_msgs;\n\n"; //\nusing Messages.roscsharp;
requestfronthalf += "namespace " + Namespace + "\n";
continue;
}
if (lines[i].Contains("$$RESPONSEDOLLADOLLABILLS"))
{
section++;
continue;
}
switch (section)
{
case 0:
requestfronthalf += lines[i] + "\n";
break;
case 1:
requestbackhalf += lines[i] + "\n";
break;
case 2:
responsebackhalf += lines[i] + "\n";
break;
}
}
}
GUTS = requestfronthalf + Request.GetSrvHalf() + requestbackhalf + Response.GetSrvHalf() + "\n" +
responsebackhalf;
/***********************************/
/* CODE BLOCK DUMP */
/***********************************/
#region definitions
for (int i = 0; i < def.Count; i++)
{
while (def[i].Contains("\t"))
def[i] = def[i].Replace("\t", " ");
while (def[i].Contains("\n\n"))
def[i] = def[i].Replace("\n\n", "\n");
def[i] = def[i].Replace('\t', ' ');
while (def[i].Contains(" "))
def[i] = def[i].Replace(" ", " ");
def[i] = def[i].Replace(" = ", "=");
def[i] = def[i].Replace("\"", "\"\"");
}
StringBuilder md = new StringBuilder();
StringBuilder reqd = new StringBuilder();
StringBuilder resd = null;
foreach (string s in def)
{
if (s == "---")
{
//only put this string in md, because the subclass defs don't contain it
md.AppendLine(s);
//we've hit the middle... move from the request to the response by making responsedefinition not null.
resd = new StringBuilder();
continue;
}
//add every line to MessageDefinition for whole service
md.AppendLine(s);
//before we hit ---, add lines to request Definition. Otherwise, add them to response.
if (resd == null)
reqd.AppendLine(s);
else
resd.AppendLine(s);
}
string MessageDefinition = md.ToString().Trim();
string RequestDefinition = reqd.ToString().Trim();
string ResponseDefinition = "";
if (resd != null)
ResponseDefinition = resd.ToString().Trim();
#endregion
#region THE SERVICE
GUTS = GUTS.Replace("$WHATAMI", classname);
GUTS = GUTS.Replace("$MYSRVTYPE", Namespace.Replace("Messages.", "") + "/" + classname);
GUTS = GUTS.Replace("$MYSERVICEDEFINITION", "@\"" + MessageDefinition + "\"");
#endregion
#region request
string RequestDict = Request.GenFields();
meta = Request.meta;
GUTS = GUTS.Replace("$REQUESTMYISMETA", meta.ToString().ToLower());
GUTS = GUTS.Replace("$REQUESTMYMSGTYPE", Namespace.Replace("Messages.", "") + "/" + classname);
GUTS = GUTS.Replace("$REQUESTMYMESSAGEDEFINITION", "@\"" + RequestDefinition + "\"");
GUTS = GUTS.Replace("$REQUESTMYHASHEADER", Request.HasHeader.ToString().ToLower());
GUTS = GUTS.Replace("$REQUESTMYFIELDS", RequestDict.Length > 5 ? "{{" + RequestDict + "}}" : "()");
GUTS = GUTS.Replace("$REQUESTNULLCONSTBODY", "");
GUTS = GUTS.Replace("$REQUESTEXTRACONSTRUCTOR", "");
#endregion
#region response
string ResponseDict = Response.GenFields();
GUTS = GUTS.Replace("$RESPONSEMYISMETA", Response.meta.ToString().ToLower());
GUTS = GUTS.Replace("$RESPONSEMYMSGTYPE", Namespace.Replace("Messages.", "") + "/" + classname);
GUTS = GUTS.Replace("$RESPONSEMYMESSAGEDEFINITION", "@\"" + ResponseDefinition + "\"");
GUTS = GUTS.Replace("$RESPONSEMYHASHEADER", Response.HasHeader.ToString().ToLower());
GUTS = GUTS.Replace("$RESPONSEMYFIELDS", ResponseDict.Length > 5 ? "{{" + ResponseDict + "}}" : "()");
GUTS = GUTS.Replace("$RESPONSENULLCONSTBODY", "");
GUTS = GUTS.Replace("$RESPONSEEXTRACONSTRUCTOR", "");
#endregion
#region MD5
GUTS = GUTS.Replace("$REQUESTMYMD5SUM", MD5.Sum(Request));
GUTS = GUTS.Replace("$RESPONSEMYMD5SUM", MD5.Sum(Response));
string GeneratedReqDeserializationCode = "", GeneratedReqSerializationCode = "", GeneratedResDeserializationCode = "", GeneratedResSerializationCode = "", GeneratedReqRandomizationCode = "", GeneratedResRandomizationCode = "", GeneratedReqEqualizationCode = "", GeneratedResEqualizationCode = "";
//TODO: service support
GeneratedReqEqualizationCode += string.Format("{0}.Request other = (Messages.{0}.Request)____other;\n", Request.Name);
GeneratedResEqualizationCode += string.Format("{0}.Response other = (Messages.{0}.Response)____other;\n", Response.Name);
for (int i = 0; i < Request.Stuff.Count; i++)
{
GeneratedReqDeserializationCode += Request.GenerateDeserializationCode(Request.Stuff[i], 1);
GeneratedReqSerializationCode += Request.GenerateSerializationCode(Request.Stuff[i], 1);
GeneratedReqRandomizationCode += Request.GenerateRandomizationCode(Request.Stuff[i], 1);
GeneratedReqEqualizationCode += Request.GenerateEqualityCode(Request.Stuff[i], 1);
}
for (int i = 0; i < Response.Stuff.Count; i++)
{
GeneratedResDeserializationCode += Response.GenerateDeserializationCode(Response.Stuff[i], 1);
GeneratedResSerializationCode += Response.GenerateSerializationCode(Response.Stuff[i], 1);
GeneratedResRandomizationCode += Response.GenerateRandomizationCode(Response.Stuff[i], 1);
GeneratedResEqualizationCode += Response.GenerateEqualityCode(Response.Stuff[i], 1);
}
GUTS = GUTS.Replace("$REQUESTSERIALIZATIONCODE", GeneratedReqSerializationCode);
GUTS = GUTS.Replace("$REQUESTDESERIALIZATIONCODE", GeneratedReqDeserializationCode);
GUTS = GUTS.Replace("$REQUESTRANDOMIZATIONCODE", GeneratedReqRandomizationCode);
GUTS = GUTS.Replace("$REQUESTEQUALITYCODE", GeneratedReqEqualizationCode);
GUTS = GUTS.Replace("$RESPONSESERIALIZATIONCODE", GeneratedResSerializationCode);
GUTS = GUTS.Replace("$RESPONSEDESERIALIZATIONCODE", GeneratedResDeserializationCode);
GUTS = GUTS.Replace("$RESPONSERANDOMIZATIONCODE", GeneratedResRandomizationCode);
GUTS = GUTS.Replace("$RESPONSEEQUALITYCODE", GeneratedResEqualizationCode);
string md5 = MD5.Sum(this);
if (md5 == null)
return null;
GUTS = GUTS.Replace("$MYSRVMD5SUM", md5);
#endregion
/********END BLOCK**********/
return GUTS;
}
}
}