-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathScriptGenerator.cs
More file actions
317 lines (283 loc) · 10.4 KB
/
ScriptGenerator.cs
File metadata and controls
317 lines (283 loc) · 10.4 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
///////////////////////////////////////////////////////////////////////////////
///
/// ScriptGenerator.cs
///
/// (c)2013 Kim, Hyoun Woo
///
///////////////////////////////////////////////////////////////////////////////
using System;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Globalization;
using Object = UnityEngine.Object;
namespace UnityQuickSheet
{
internal class ScriptGenerator
{
private const int CommentWrapLength = 35;
private TextWriter m_Writer;
private string m_Text;
private ScriptPrescription m_ScriptPrescription;
private string m_Indentation;
private int m_IndentLevel = 0;
private int IndentLevel
{
get
{
return m_IndentLevel;
}
set
{
m_IndentLevel = value;
m_Indentation = String.Empty;
for (int i=0; i<m_IndentLevel; i++)
m_Indentation += " ";
}
}
private string ClassName
{
get
{
if (!string.IsNullOrEmpty(m_ScriptPrescription.className))
return m_ScriptPrescription.className;
return "Error_Empty_ClassName";
}
}
private string SpreadSheetName
{
get
{
if (!string.IsNullOrEmpty(m_ScriptPrescription.spreadsheetName))
return m_ScriptPrescription.spreadsheetName;
return "Error_Empty_SpreadSheetName";
}
}
private string WorkSheetClassName
{
get
{
if (!string.IsNullOrEmpty(m_ScriptPrescription.worksheetClassName))
return m_ScriptPrescription.worksheetClassName;
return "Error_Empty_WorkSheet_ClassName";
}
}
private string DataClassName
{
get
{
if (!string.IsNullOrEmpty(m_ScriptPrescription.dataClassName))
return m_ScriptPrescription.dataClassName;
return "Error_Empty_DataClassName";
}
}
private string AssetFileCreateFuncName
{
get
{
if (!string.IsNullOrEmpty(m_ScriptPrescription.assetFileCreateFuncName))
return m_ScriptPrescription.assetFileCreateFuncName;
return "Error_Empty_AssetFileCreateFunc_Name";
}
}
private string ImportedFilePath
{
get
{
if (!string.IsNullOrEmpty(m_ScriptPrescription.importedFilePath))
return m_ScriptPrescription.importedFilePath;
return "Error_Empty_FilePath";
}
}
private string AssetFilePath
{
get
{
if (!string.IsNullOrEmpty(m_ScriptPrescription.assetFilepath))
return m_ScriptPrescription.assetFilepath;
return "Error_Empty_AssetFilePath";
}
}
private string AssetPostprocessorClass
{
get
{
if (!string.IsNullOrEmpty(m_ScriptPrescription.assetPostprocessorClass))
return m_ScriptPrescription.assetPostprocessorClass;
return "Error_Empty_AssetPostprocessorClass";
}
}
/// <summary>
/// Constructor.
/// </summary>
public ScriptGenerator (ScriptPrescription scriptPrescription)
{
m_ScriptPrescription = scriptPrescription;
}
/// <summary>
/// Replace markdown keywords in the template text file which is currently read in.
/// </summary>
public override string ToString ()
{
m_Text = m_ScriptPrescription.template;
m_Writer = new StringWriter ();
m_Writer.NewLine = "\n";
// Make sure all line endings to be Unix (Mac OSX) format.
m_Text = Regex.Replace (m_Text, @"\r\n?", delegate(Match m) { return "\n"; });
// Class Name
m_Text = m_Text.Replace ("$ClassName", ClassName);
m_Text = m_Text.Replace ("$SpreadSheetName", SpreadSheetName);
m_Text = m_Text.Replace ("$WorkSheetClassName", WorkSheetClassName);
m_Text = m_Text.Replace ("$DataClassName", DataClassName);
m_Text = m_Text.Replace ("$AssetFileCreateFuncName", AssetFileCreateFuncName);
m_Text = m_Text.Replace ("$AssetPostprocessorClass", AssetPostprocessorClass);
m_Text = m_Text.Replace ("$IMPORT_PATH", ImportedFilePath);
m_Text = m_Text.Replace ("$ASSET_PATH", AssetFilePath);
// Other replacements
foreach (KeyValuePair<string, string> kvp in m_ScriptPrescription.mStringReplacements)
m_Text = m_Text.Replace (kvp.Key, kvp.Value);
// Do not change tabs to spcaes of the .txt template files.
Match match = Regex.Match (m_Text, @"(\t*)\$MemberFields");
if (match.Success)
{
// Set indent level to number of tabs before $Functions keyword
IndentLevel = match.Groups[1].Value.Length;
if (m_ScriptPrescription.memberFields != null)
{
foreach(var field in m_ScriptPrescription.memberFields)
{
WriteMemberField(field);
WriteProperty(field);
WriteBlankLine();
}
m_Text = m_Text.Replace (match.Value + "\n", m_Writer.ToString ());
}
}
// Return the text of the script
return m_Text;
}
private void PutCurveBracesOnNewLine ()
{
m_Text = Regex.Replace (m_Text, @"(\t*)(.*) {\n((\t*)\n(\t*))?", delegate(Match match)
{
return match.Groups[1].Value + match.Groups[2].Value + "\n" + match.Groups[1].Value + "{\n" +
(match.Groups[4].Value == match.Groups[5].Value ? match.Groups[4].Value : match.Groups[3].Value);
});
}
///
/// Write a member field of a data class.
///
private void WriteMemberField(MemberFieldData field)
{
m_Writer.WriteLine (m_Indentation + "[SerializeField]");
var fieldName = GetFieldNameForField(field);
string tmp;
if (field.type == CellType.Enum)
{
if (field.IsArrayType)
{
tmp = field.Name + "[] " + fieldName + ";";
}
else
{
tmp = field.Name + " " + fieldName + ";";
}
}
else
{
if (field.IsArrayType)
tmp = field.Type + "[]" + " " + fieldName + " = new " + field.Type + "[0]" +";";
else
tmp = field.Type + " " + fieldName + ";";
}
m_Writer.WriteLine (m_Indentation + tmp);
}
///
/// Write a property of a data class.
///
private void WriteProperty(MemberFieldData field)
{
string tmp = string.Empty;
var propertyName = GetPropertyNameForField(field);
var fieldName = GetFieldNameForField(field);
if (field.type == CellType.Enum)
{
if (field.IsArrayType)
{
tmp += "public " + field.Name + "[]" + " " + propertyName + " ";
}
else
{
tmp += "public " + field.Name + " " + propertyName + " ";
}
}
else
{
if (field.IsArrayType)
tmp += "public " + field.Type + "[]" + " " + propertyName + " ";
else
tmp += "public " + field.Type + " " + propertyName + " ";
}
tmp += "{ get {return " + fieldName + "; } set { this." + fieldName + " = value;} }";
m_Writer.WriteLine (m_Indentation + tmp);
}
/// <summary>
/// Override to implement your own field name format.
/// </summary>
protected virtual string GetFieldNameForField(MemberFieldData field)
{
return field.Name.ToLower();
}
/// <summary>
/// Override to implement your own property name format.
/// </summary>
protected virtual string GetPropertyNameForField(MemberFieldData field)
{
if (field.type == CellType.Enum)
return field.Name.ToUpper();
// To prevent an error can happen when the name of the column header has all lower case characters.
TextInfo ti = new CultureInfo("en-US", false).TextInfo;
return ti.ToTitleCase(field.Name);
}
/// <summary>
/// Write a blank line.
/// </summary>
private void WriteBlankLine ()
{
m_Writer.WriteLine (m_Indentation);
}
/// <summary>
/// Write comment.
/// </summary>
/// <param name="comment"></param>
private void WriteComment (string comment)
{
int index = 0;
while (true)
{
if (comment.Length <= index + CommentWrapLength)
{
m_Writer.WriteLine (m_Indentation + "// " + comment.Substring (index));
break;
}
else
{
int wrapIndex = comment.IndexOf (' ', index + CommentWrapLength);
if (wrapIndex < 0)
{
m_Writer.WriteLine (m_Indentation + "// " + comment.Substring (index));
break;
}
else
{
m_Writer.WriteLine (m_Indentation + "// " + comment.Substring (index, wrapIndex-index));
index = wrapIndex + 1;
}
}
}
}
}
}