forked from Kragrathea/TelloLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
153 lines (132 loc) · 6 KB
/
Program.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatConToCsv
{
class Program
{
public class FieldSpec
{
public string name;
public int offset;
public string type;
}
class RecordSpec
{
public string definedIn;
public string name, id, len;
public List<FieldSpec> fields= new List<FieldSpec>();
}
static void Main(string[] args)
{
var srcPath = "C:/Users/v-chph/Downloads/DatCon-master/DatCon/src/DatConRecs/";
var fileNames = Directory.GetFiles(srcPath, "*.java", SearchOption.AllDirectories);
var typeLookup = new Dictionary<string, string>
{
{ ".getByte(", "byte" },
{ ".getUnsignedByte(", "byte" },
{ ".getUnsignedInt(", "UInt32" },
{ ".getUnsignedShort(", "UInt16" },
{ ".getFloat(", "float" },
{ ".getShort(", "short" },
{ ".getInt(", "int" },
{ ".getDouble(", "double" },
{ ".getString(", "string" },
{ ".getCleanString(", "string" }
};
var specString = "new RecClassSpec(";
var recSpecs = new Dictionary<string, RecordSpec>();
foreach (var fn in fileNames)
{
var lines = File.ReadAllLines(fn);
foreach (var l in lines)
{
if (l.Contains(specString))
{
if (l.Trim().StartsWith("//"))
continue;
var ia = l.IndexOf(specString) + specString.Length;
var ib = l.IndexOf(".class");
if (ib < 0)
continue;
var name = l.Substring(ia, ib - ia);
var parts = l.Substring(ib).Split(new char[] { ',', ')' });
if (parts.Length < 3)
continue;
var id = parts[1].Trim();
var len = parts[2].Trim();
Console.WriteLine("{0},{1},{2}", name, id, len);
var cleanName = name.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_' });
var dirName = Path.GetFileName(Directory.GetParent(fn).ToString());
recSpecs[name] = new RecordSpec { name = cleanName, id = id, len = len,
definedIn = dirName
};
}
}
}
var allLines = new List<string>();
var validLines = new List<string>();
var invalidLines = new List<string>();
validLines.Add("definedIn,groupName,groupId,groupLen,name,offset,type,validParse,sourceLine");
foreach (var fn in fileNames)
{
var lines = File.ReadAllLines(fn);
foreach (var l in lines)
{
foreach (var key in typeLookup.Keys)
{
if (l.Trim().StartsWith("//"))
continue;
if (l.Contains(key))
{
var name = l.Split('=')[0].Trim();
var ia = l.IndexOf(key) + key.Length;
var ib = l.IndexOf(')', ia);
var offStr = l.Substring(ia, ib - ia);
var valid = true;
int off;
if(typeLookup[key]=="string")
{
off = 0;
}
else if (!int.TryParse(offStr, out off))
{
valid = false;
}
var groupName = Path.GetFileNameWithoutExtension(fn);
var recSpec = new RecordSpec { name = "unk", id = "0", len = "0" };
if (recSpecs.Keys.Contains(groupName))
recSpec = recSpecs[groupName];
else
valid = false;
if (name.StartsWith("double "))//special case for a few lines that are defined like this.
name = name.Substring("double ".Length);
//groupName = groupName.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_' });
var outLine = String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}", Path.GetFileName(Directory.GetParent(fn).ToString()), recSpec.name, recSpec.id, recSpec.len, name, off, typeLookup[key], valid, l.Trim());
if (recSpec.id == "65533")
{
}
if (valid)
{
var field = new FieldSpec() { name = name, offset = off,type= typeLookup[key] };
recSpec.fields.Add(field);
validLines.Add(outLine);
}
else
invalidLines.Add(outLine);
}
}
}
}
var destPath = "../../../TelloLib/";
File.WriteAllLines(destPath + "parsedRecSpecs.csv", validLines.Concat(invalidLines).ToArray());
string json = JsonConvert.SerializeObject(recSpecs.Values.ToArray(),Formatting.Indented);
File.WriteAllText(destPath + "parsedRecSpecs.json", json);
//var xx = JsonConvert.DeserializeObject(json);
}
}
}