Skip to content

Commit d4f7a5f

Browse files
committed
FenGen cleanup
1 parent 6db1a76 commit d4f7a5f

File tree

2 files changed

+20
-45
lines changed

2 files changed

+20
-45
lines changed

FenGen/Generators.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,10 @@ internal void WL(string str = "")
6868
}
6969
}
7070

71-
string indent = strT.IsWhiteSpace() || strT.StartsWith("#") ? "" : Indent(curIndent);
71+
bool noIndent = strT.IsWhiteSpace() || (strT.StartsWith("#") && !strT.StartsWith("#region ") && strT != "#region");
72+
string indent = noIndent ? "" : Indent(curIndent);
7273
_sb.AppendLine(indent + strT);
7374
}
7475
}
75-
76-
internal sealed class SectionedIniFile
77-
{
78-
private int _indent = 0;
79-
80-
internal SectionedIniFile(StringBuilder sb, int startingIndent)
81-
{
82-
_indent = startingIndent;
83-
}
84-
}
8576
}
8677
}

FenGen/LanguageGen.cs

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,25 @@ internal static void
4040
private static (string LangClassName, List<NamedDictionary> Dict)
4141
ReadSource(string file)
4242
{
43-
var retDict = new List<NamedDictionary>();
44-
45-
var classInstanceDict = new Dictionary<string, string>();
46-
4743
string code = File.ReadAllText(file);
4844
var tree = ParseTextFast(code);
4945

5046
var (markedMember, _) = GetAttrMarkedItem(tree, SyntaxKind.ClassDeclaration, GenAttributes.FenGenLocalizationSourceClass);
51-
var LTextClass = (ClassDeclarationSyntax)markedMember;
47+
var lTextClass = (ClassDeclarationSyntax)markedMember;
5248

53-
var childNodes = LTextClass.ChildNodes().ToArray();
49+
var childNodes = lTextClass.ChildNodes().ToArray();
5450

51+
var classInstanceDict = new Dictionary<string, string>();
5552
// Once through first to get the instance types and names
5653
for (int i = 0; i < childNodes.Length; i++)
5754
{
5855
if (childNodes[i] is FieldDeclarationSyntax field)
5956
{
60-
classInstanceDict.Add(field.Declaration.Type.ToString(),
61-
field.Declaration.Variables[0].Identifier.Text);
57+
classInstanceDict.Add(field.Declaration.Type.ToString(), field.Declaration.Variables[0].Identifier.Text);
6258
}
6359
}
6460

61+
var retDict = new List<NamedDictionary>();
6562
// Now through again to get the language string names from the nested classes
6663
foreach (SyntaxNode cn in childNodes)
6764
{
@@ -85,8 +82,6 @@ private static (string LangClassName, List<NamedDictionary> Dict)
8582
}
8683

8784
var member = (MemberDeclarationSyntax)m;
88-
89-
string fName, fValue = "";
9085
foreach (AttributeListSyntax attrList in member.AttributeLists)
9186
{
9287
foreach (AttributeSyntax attr in attrList.Attributes)
@@ -98,6 +93,7 @@ private static (string LangClassName, List<NamedDictionary> Dict)
9893
var argList = attr.ArgumentList;
9994
if (argList != null)
10095
{
96+
string fValue = "";
10197
var args = argList.Arguments;
10298
for (int i = 0; i < args.Count; i++)
10399
{
@@ -126,6 +122,7 @@ private static (string LangClassName, List<NamedDictionary> Dict)
126122
}
127123
}
128124

125+
string fName;
129126
EqualsValueClauseSyntax? initializer;
130127
if (m.IsKind(SyntaxKind.FieldDeclaration))
131128
{
@@ -143,18 +140,16 @@ private static (string LangClassName, List<NamedDictionary> Dict)
143140
if (initializer == null)
144141
{
145142
string type = m.IsKind(SyntaxKind.FieldDeclaration) ? "field" : "property";
146-
ThrowErrorAndTerminate(nameof(Language) + ":\r\n" +
147-
"Found a " + type + " without an initializer in " + file);
143+
ThrowErrorAndTerminate(nameof(Language) + ":\r\n" + "Found a " + type + " without an initializer in " + file);
148144
}
149145

150-
fValue = ((LiteralExpressionSyntax)initializer!.Value).Token.ValueText;
151-
dict.Add(new IniItem { Key = fName, Value = fValue });
146+
dict.Add(new IniItem { Key = fName, Value = ((LiteralExpressionSyntax)initializer!.Value).Token.ValueText });
152147
}
153148

154149
retDict.Add(dict);
155150
}
156151

157-
string lTextClassId = LTextClass.Identifier.ToString();
152+
string lTextClassId = lTextClass.Identifier.ToString();
158153
return (lTextClassId, retDict);
159154
}
160155

@@ -248,42 +243,31 @@ private static void WriteDest(string langClassName, List<NamedDictionary> dictLi
248243
private static void WriteIniFile(string langIniFile, List<NamedDictionary> dictList, bool test = false)
249244
{
250245
var sb = new StringBuilder();
251-
252-
string testPrefix = test ? "█" : "";
253-
254246
sb.AppendLine("; This is an AngelLoader language file.");
255247
sb.AppendLine("; This file MUST be saved with UTF8 encoding in order to guarantee correct display of strings.");
256248
sb.AppendLine();
257249

250+
string testPrefix = test ? "█" : "";
258251
for (int i = 0; i < dictList.Count; i++)
259252
{
260253
var dict = dictList[i];
261254

262255
sb.AppendLine("[" + dict.Name + "]");
263256
foreach (IniItem item in dict)
264257
{
265-
if (item.IsComment)
258+
if (item.Key.IsEmpty() && item.Value.IsEmpty())
266259
{
267-
if (!item.Value.IsEmpty())
268-
{
269-
string[] comments = item.Value.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
270-
foreach (string c in comments) sb.AppendLine("; " + c);
271-
}
260+
sb.AppendLine();
272261
}
273-
else if (item.Key.IsEmpty() && item.Value.IsEmpty())
262+
else if (item.IsComment && !item.Value.IsEmpty())
274263
{
275-
sb.AppendLine();
264+
string[] comments = item.Value.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
265+
foreach (string c in comments) sb.AppendLine("; " + c);
276266
}
277267
else
278268
{
279-
if (test && item.Key == "TranslatedLanguageName")
280-
{
281-
sb.AppendLine(item.Key + "=" + "TéstLang");
282-
}
283-
else
284-
{
285-
sb.AppendLine(item.Key + "=" + testPrefix + item.Value);
286-
}
269+
string val = test && item.Key == "TranslatedLanguageName" ? "TéstLang" : testPrefix + item.Value;
270+
sb.AppendLine(item.Key + "=" + val);
287271
}
288272
}
289273
if (i < dictList.Count - 1) sb.AppendLine();

0 commit comments

Comments
 (0)