-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpineInfoParser.ttinclude
252 lines (224 loc) · 9.18 KB
/
SpineInfoParser.ttinclude
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
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#+
private string FilePath
{
get
{
IServiceProvider serviceProvider = (IServiceProvider)this.Host;
DTE dte = serviceProvider.GetService(typeof(DTE)) as DTE;
string path = new FileInfo(dte.Solution.FullName).Directory.FullName;
path = Path.Combine(path,"SpineDataInfo.txt");
return path;
}
}
private StringReader GetStringReader()
{
return new StringReader(File.ReadAllText(FilePath));
}
private List<string> GetAllLines()
{
var stringReader = GetStringReader();
var lines = new List<string>();
var line = stringReader.ReadLine();
lines.Add(line);
while (line != null)
{
line = stringReader.ReadLine();
if(!string.IsNullOrEmpty(line))
lines.Add(line.Replace("\"",""));
}
stringReader.Close();
return lines;
}
public string MakeFirstCharUpper(string str)
{
var firstChar = str.Substring(0, 1).ToUpper();
var result = str.Substring(1);
return firstChar + result;
}
/// <summary>
/// enumName
/// enumValues
/// </summary>
public Dictionary<string, List<string>> GetAllEnums(bool includeEditor)
{
var enums = new Dictionary<string, List<string>>();
var categories = GetAllClasses(true,includeEditor);
var prefix = GetPrefix();
var postfix= GetEnumPostfix();
foreach (var category in categories)
{
var categoryName = category.Key.Item1;
foreach (var tuple in category.Value)
{
if(!tuple.Item1.Contains("enum"))
continue;
var enumName = GetEnumType(categoryName,prefix,postfix,tuple.Item2);
var enumValues = //vnizu tabulyaciya
tuple.Item1.Replace("enum[", "").Replace("]", "").Replace(" ","").Split(',').Select(e => MakeFirstCharUpper(e));
enums.Add(enumName,enumValues.ToList());
}
}
return enums;
}
private string GetEnumType(string categoryName, string prefix, string postfix, string propName)
{
var enumName = prefix + categoryName + MakeFirstCharUpper(propName) + postfix;
return enumName;
}
/// <summary>
/// objectName
/// prop - defaultValue
/// </summary>
public Dictionary<Tuple<string,string>, List<Tuple<string, string, string>>> GetAllClasses(bool raw,bool includeEditor)
{
var lines = GetAllLines();
var categoriesRaw = new Dictionary<Tuple<string,string>,List<string>>();
var categoryRaw = new List<string>();
var prefix = "";
var postfix = "";
var postfixEnum = "";
if(!raw)
{
prefix = GetPrefix();
postfix = GetPostfix();
postfixEnum = GetEnumPostfix();
}
foreach (var line in lines)
{
if(string.IsNullOrEmpty(line))
continue;
var lineFormated = line.Replace(" ", "").Replace(" ","");//remove whiteSpace
if (lineFormated.Contains("={") || lineFormated.Contains("=<")) //is begin of data
{
if(lineFormated.Contains("={"))
{
var categoryName = lineFormated.Substring(0, lineFormated.IndexOf("={"));
categoriesRaw.Add(new Tuple<string,string>(categoryName,""), categoryRaw);
}
else
{
var categoryName = lineFormated.Substring(0, lineFormated.IndexOf("=<"));
var firstIndex = lineFormated.IndexOf("=<")+2;
var count = lineFormated.IndexOf(">") - firstIndex;
var baseType = lineFormated.Substring(firstIndex,count);
categoriesRaw.Add(new Tuple<string,string>(categoryName,baseType), categoryRaw);
categoryRaw = new List<string>();
}
}
else if (lineFormated.Contains("}")) //is end of data
{
categoryRaw = new List<string>();
}
else // props
{
if(lineFormated.Contains(":"))
categoryRaw.Add(lineFormated);
}
}
var categories = new Dictionary<Tuple<string,string>, List<Tuple<string,string,string>>>();
foreach (var category in categoriesRaw)
{
var categoryName = category.Key.Item1;
if(categoryName.Contains("Settings") && !raw)
continue;
var categoryProps = new List<Tuple<string,string,string>>();
var categoryNameNotRaw = categoryName;
var baseTypeNotRaw = category.Key.Item2;
if(!raw)
{
categoryNameNotRaw = prefix+categoryNameNotRaw+postfix;
if(!string.IsNullOrEmpty(baseTypeNotRaw))
baseTypeNotRaw = ApplyBaseTypes(prefix,postfix,postfixEnum,baseTypeNotRaw,categoryName,categoryName);
}
categories.Add(new Tuple<string,string>(categoryNameNotRaw,baseTypeNotRaw), categoryProps);
foreach (var prop in category.Value)
{
var propFormated = prop.Replace(":", "#").Replace("=", "#").Split('#');
var propType = propFormated[0];
var propName = propFormated[1];
var isString = propType.Contains("string");
if(propName.Contains("(editor)") && !includeEditor)
continue;
propName = propName.Replace("(editor)","");
if(!raw)
propType = ApplyBaseType(prefix,postfix,postfixEnum,propType,propName,categoryName);
categoryProps.Add(new Tuple<string, string, string>(propType, propName, (propFormated.Length > 2 ? string.Format((isString?"\"{0}\"":"{0}"),propFormated[2]) : null)));
}
}
return categories;
}
public string ApplyPrefixAndPostfix(string str)
{
if(str.Contains("[]"))
return GetPrefix()+str.Replace("[]","")+GetPostfix()+"[]";
return GetPrefix()+str+GetPostfix()+"[]";
}
private string ApplyBaseTypes(string prefix,string postfix,string postfixEnum,string str,string propName,string categoryName)
{
str = str.Replace(" ","");
var first = ApplyBaseType(prefix,postfix,postfixEnum,str.Substring(0,str.IndexOf(",")),propName,categoryName);
var second = ApplyBaseType(prefix,postfix,postfixEnum,str.Substring(str.IndexOf(",")+1),propName,categoryName);
return first+","+second;
}
private string ApplyBaseType(string prefix,string postfix,string postfixEnum,string propType,string propName,string categoryName)
{
if(propType != ("int")
&& propType != ("float")
&& propType != ("bool")
&& propType != ("string")
&& propType != ("Color")
&& propType != ("int[]")
&& propType != ("float[]")
&& propType != ("bool[]")
&& propType != ("string[]")
&& propType != ("Color[]")
&& !propType.StartsWith("enum"))
{
if(propType.Contains("[]") && !propType.Contains("enum"))
propType = prefix + propType.Replace("[]","") + postfix + "[]";
else
propType = prefix + propType + postfix;
} else if(propType.Contains("enum"))
{
propType = GetEnumType(categoryName,prefix,postfixEnum,propName);
}else{
}
return propType;
}
private string GetPrefix()
{
var categories = GetAllClasses(true,true);
if(categories.Any(e=>e.Key.Item1 == "Settings"))
{
var category = categories.FirstOrDefault(e=>e.Key.Item1 == "Settings");
return category.Value.FirstOrDefault(e => e.Item2 == "Prefix").Item3.Replace("\"","");
}
return "";
}
private string GetPostfix()
{
var categories = GetAllClasses(true,true);
if(categories.Any(e=>e.Key.Item1 == "Settings"))
{
var category = categories.FirstOrDefault(e=>e.Key.Item1 == "Settings");
return category.Value.FirstOrDefault(e => e.Item2 == "Postfix").Item3.Replace("\"","");
}
return "";
}
private string GetEnumPostfix()
{
var categories = GetAllClasses(true,true);
if(categories.Any(e=>e.Key.Item1 == "Settings"))
{
var category = categories.FirstOrDefault(e=>e.Key.Item1 == "Settings");
return category.Value.FirstOrDefault(e => e.Item2 == "EnumPostfix").Item3.Replace("\"","");
}
return "";
}
#>