/
CodeGenerator.cs
168 lines (144 loc) · 5.78 KB
/
CodeGenerator.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
using AltoFramework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
namespace AltoLib
{
public class CodeGenerator
{
//----------------------------------------------------------------------
// virtual (Please override in subclasses)
//----------------------------------------------------------------------
public virtual string outputDirPath { get; set; } = "Assets/Generated";
public virtual string outputFileName { get; set; } = "Dummy.gen.cs";
public virtual string fileHeader { get; set; } =
@"//------------------------------------------------------------------------------
// This file is AUTO-GENERATED by {0}.
//------------------------------------------------------------------------------
";
public virtual string namespaceName { get; set; } = "DummySpace";
public virtual string className { get; set; } = "DummyClass";
public virtual string classHeader { get; set; } =
@"namespace {0}
{{
public static class {1}
{{
";
public virtual string classFooter { get; set; } =
@" }
}
";
public virtual string indentUnit { get; set; } = " ";
protected virtual void WriteOuter(StringBuilder builder)
{
string generatorName = this.GetType().FullName;
builder.AppendFormat(fileHeader, generatorName);
builder.AppendFormat(classHeader, namespaceName, className);
WriteInner(builder);
builder.Append(classFooter);
}
protected virtual void WriteInner(StringBuilder builder)
{
// implement in subclasses.
}
//----------------------------------------------------------------------
// helper method
//----------------------------------------------------------------------
protected string MakeIndent(int indentLevel)
{
return StringUtil.Repeat(indentUnit, indentLevel);
}
/// <summary>
/// 文字列の HashSet を「Symbol = "Symbol";」形式に変換して追記。(文字列順でソート)
/// </summary>
protected void AppendSymbols(
StringBuilder builder, HashSet<string> labelSet,
int indentLevel = 2, bool alignEqual = true
)
{
if (labelSet.Count == 0) { return; }
var labels = labelSet.ToList();
var symbols = new List<string>();
var symbolToLabel = new Dictionary<string, string>();
foreach (string label in labels)
{
string symbol = MakeSafeSymbol(label);
symbols.Add(symbol);
symbolToLabel.Add(symbol, label);
}
int maxSymbolLength = symbols.Select(s => s.Length).Max();
string indent = MakeIndent(indentLevel);
symbols.Sort();
foreach (string symbol in symbols)
{
string label;
if (symbolToLabel.TryGetValue(symbol, out label))
{
string spacing = alignEqual ? StringUtil.Repeat(" ", maxSymbolLength - symbol.Length) : "";
builder.AppendLine($"{ indent }public const string { symbol }{ spacing } = \"{ label }\";");
}
}
}
/// <summary>
/// { 文字列 : 数値 } を「Symbol = 123;」形式で追記。(数値昇順でソート)
/// </summary>
protected void AppendSymbols(
StringBuilder builder, Dictionary<string, int> symbolToNum,
int indentLevel = 2, bool alignEqual = true
)
{
if (symbolToNum.Count == 0) { return; }
var numToSymbol = symbolToNum.Reverse().ToDictionary(x => x.Value, x => MakeSafeSymbol(x.Key));
var numbers = numToSymbol.Keys.ToList();
var symbols = numToSymbol.Values.ToList();
int maxSymbolLength = symbols.Select(s => s.Length).Max();
string indent = MakeIndent(indentLevel);
numbers.Sort();
foreach (int num in numbers)
{
string symbol;
if (numToSymbol.TryGetValue(num, out symbol))
{
string spacing = alignEqual ? StringUtil.Repeat(" ", maxSymbolLength - symbol.Length) : "";
builder.AppendLine($"{ indent }public const int { symbol }{ spacing } = { num };");
}
}
}
protected string MakeSafeSymbol(string label)
{
string symbol = Regex.Replace(label, "[^a-zA-Z0-9]", "_");
if ('0' <= symbol[0] && symbol[0] <= '9')
{
symbol = '_' + symbol;
}
return symbol;
}
//----------------------------------------------------------------------
// public
//----------------------------------------------------------------------
public void Generate()
{
if (!Directory.Exists(outputDirPath))
{
Directory.CreateDirectory(outputDirPath);
}
string outPath = $"{ outputDirPath }/{ outputFileName }";
AltoLog.FW($"Generate Code : { outPath }");
using (var fileStream = File.Open(outPath, FileMode.Create, FileAccess.Write))
{
using (var streamWriter = new StreamWriter(fileStream))
{
var stringBuilder = new StringBuilder();
WriteOuter(stringBuilder);
streamWriter.Write(stringBuilder.ToString());
}
}
AssetDatabase.Refresh();
AltoLog.Success("Generate Code Completed.");
}
}
}