Skip to content

Commit

Permalink
Merge pull request #26 from penev92/fixDescHandling
Browse files Browse the repository at this point in the history
Fix DescAttribute handling
  • Loading branch information
penev92 authored Oct 15, 2022
2 parents e771048 + 68e39fa commit fe2abca
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions server/Oraide.Csharp/CodeParsers/RoslynCodeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static IEnumerable<TraitInfo> ParseTraitInfo(string filePath, string fileText, C
yield break;

// Get trait's DescAttribute.
var traitDesc = ParseClassDescAttribute(classDeclaration);
var traitDesc = ParseDescAttribute(classDeclaration);

// Get TraitInfo property (actually field) list.
foreach (var member in classDeclaration.Members)
Expand Down Expand Up @@ -213,7 +213,7 @@ static SimpleClassInfo ParseSimpleClass(string filePath, string fileText, ClassD
var projectileInfoName = classDeclaration.Identifier.ValueText;
var projectileName = projectileInfoName.EndsWith("Info") ? projectileInfoName.Substring(0, projectileInfoName.Length - 4) : projectileInfoName;
projectileName = projectileName.EndsWith("Warhead") ? projectileName.Substring(0, projectileName.Length - 7) : projectileName;
var description = ParseClassDescAttribute(classDeclaration);
var description = ParseDescAttribute(classDeclaration);
var isAbstract = classDeclaration.Modifiers.Any(x => x.ValueText == "abstract");

// Some manual string nonsense to determine the class name location inside the file.
Expand Down Expand Up @@ -241,7 +241,6 @@ static IEnumerable<ClassFieldInfo> ParseClassField(string filePath, string fileT
{
foreach (var variableDeclaratorSyntax in fieldDeclarationSyntax.Declaration.Variables)
{
var fieldDesc = "";
var otherAttributes = new List<(string Name, string Value)>();
foreach (var attributeList in fieldDeclarationSyntax.AttributeLists)
{
Expand All @@ -250,29 +249,18 @@ static IEnumerable<ClassFieldInfo> ParseClassField(string filePath, string fileT
var attributeName = attribute.Name.GetText().ToString();
var attributeValue = attribute.ArgumentList?.Arguments.ToString();

if (attributeName == "Desc")
{
var strings = attribute.ArgumentList?.Arguments
.Select(x => x.GetText().ToString())
.Select(x => x.Substring(x.IndexOf('"') + 1))
.Select(x => x.Substring(0, x.Length - 1));

if (strings != null)
fieldDesc = string.Join(" ", strings);

// Resolve `nameof(...)`.
fieldDesc = Regex.Replace(fieldDesc, "(\"\\s*\\+\\s*nameof\\(([A-Za-z0-9.\\S]*)\\)\\s*\\+\\s*\")", "$2");
}

else if (attributeName == "FieldLoader.Ignore")
// Break because we shouldn't be parsing this field.
if (attributeName == "FieldLoader.Ignore")
yield break;

else if (attributeName == "FieldLoader.LoadUsing")
// Continue so we don't run into the "unknown field attribute" case.
if (attributeName == "Desc"
|| attributeName == "FieldLoader.LoadUsing")
continue;

// Full set of attributes on trait properties for future reference.
// The TranslateAttribute was present in `release-20210321` and was removed some time later.
else if (attributeName == "FieldLoader.Require"
if (attributeName == "FieldLoader.Require"
|| attributeName == "Translate"
|| attributeName == "ActorReference"
|| attributeName == "VoiceReference"
Expand Down Expand Up @@ -305,34 +293,40 @@ static IEnumerable<ClassFieldInfo> ParseClassField(string filePath, string fileT
var userFriendlyType = UserFriendlyTypeName(propertyType);
var defaultValue = HumanReadablePropertyDefaultValue(variableDeclaratorSyntax);
var location = FindPropertyLocationInText(filePath, fileText, variableDeclaratorSyntax.GetLocation().SourceSpan.Start);
var description = ParseDescAttribute(fieldDeclarationSyntax);

// Using "???" as class name here as a temporary placeholder. That should be replaced later when resolving inheritance and inherited fields.
yield return new ClassFieldInfo(propertyName, propertyType, userFriendlyType, defaultValue, "???", location, fieldDesc, otherAttributes.ToArray());
yield return new ClassFieldInfo(propertyName, propertyType, userFriendlyType, defaultValue, "???", location, description, otherAttributes.ToArray());
}
}

static string ParseClassDescAttribute(ClassDeclarationSyntax classDeclaration)
static string ParseDescAttribute(MemberDeclarationSyntax memberDeclarationSyntax)
{
// NOTE: Intentionally leaving the two cycles finish iterating and keeping the final result
// because I can't be bothered to check what AttributeLists are at present.
var description = "";
foreach (var attributeList in classDeclaration.AttributeLists)
foreach (var attributeList in memberDeclarationSyntax.AttributeLists)
{
foreach (var attribute in attributeList.Attributes)
{
if (attribute.Name.GetText().ToString() == "Desc")
{
var strings = attribute.ArgumentList.Arguments
var strings = attribute.ArgumentList?.Arguments
.Select(x => x.GetText().ToString())
.Select(x => x.Substring(x.IndexOf('"') + 1))
.Select(x => x.Substring(0, x.Length - 1));

description = string.Join(" ", strings);
description = string.Join(" ", strings ?? Array.Empty<string>());
}
}
}

// Resolve `nameof(...)`.
description = Regex.Replace(description, "(\"\\s*\\+\\s*nameof\\(([A-Za-z0-9.\\S]*)\\)\\s*\\+\\s*\")", "$2");

// Resolve (multi-line) string concatenation.
description = Regex.Replace(description, "(\"\\s*\\+\\s*\")", "");

return description;
}

Expand Down

0 comments on commit fe2abca

Please sign in to comment.