This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: changed the way to create the code.
Use the Roslyn way to create the code in the RS. Issues: #572, #565
- Loading branch information
1 parent
7dd7ba3
commit 84af732
Showing
13 changed files
with
498 additions
and
837 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Newtonsoft.Json.Linq; | ||
using System.Net; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
using static RotationSolver.GameData.SyntaxHelper; | ||
|
||
namespace RotationSolver.GameData; | ||
internal static class OpCodeGetter | ||
{ | ||
public static async Task GetOpCode(DirectoryInfo dirInfo) | ||
{ | ||
using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(30) }; | ||
using var result = await client.GetAsync("https://raw.githubusercontent.com/karashiiro/FFXIVOpcodes/master/opcodes.json"); | ||
|
||
if (result.StatusCode != HttpStatusCode.OK) return; | ||
var responseStream = await result.Content.ReadAsStringAsync(); | ||
|
||
var enums = JToken.Parse(responseStream)[0]!["lists"]!.Children() | ||
.SelectMany(i => i.Children()).SelectMany(i => i.Children()).Cast<JObject>() | ||
.Select(i => | ||
{ | ||
var name = (string)((JValue)i["name"]!).Value!; | ||
var value = (ushort)(long)((JValue)i["opcode"]!).Value!; | ||
var description = name!.Space(); | ||
|
||
var desc = AttributeList(SingletonSeparatedList(DescriptionAttribute(description))).WithXmlComment($"/// <summary> {description} </summary>"); | ||
|
||
return EnumMember(name, value).AddAttributeLists(desc); | ||
}); | ||
|
||
var enumDefinition = EnumDeclaration("OpCode") | ||
.AddBaseListTypes(SimpleBaseType(PredefinedType(Token(SyntaxKind.UShortKeyword)))) | ||
.AddAttributeLists(GeneratedCodeAttribute(typeof(OpCodeGetter)).WithXmlComment($"/// <summary> The OpCode </summary>")) | ||
.AddModifiers(Token(SyntaxKind.PublicKeyword)) | ||
.AddMembers([EnumMember("None", 0).WithXmlComment($"/// <summary/>") | ||
, ..enums]); | ||
|
||
var majorNameSpace = NamespaceDeclaration("RotationSolver.Basic.Data").AddMembers(enumDefinition); | ||
|
||
await SaveNode(majorNameSpace, dirInfo, "OpCode"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System.Security.Cryptography; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace RotationSolver.GameData; | ||
|
||
public static class SyntaxHelper | ||
{ | ||
public static AttributeListSyntax GeneratedCodeAttribute(Type generator) | ||
{ | ||
return AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")) | ||
.AddArgumentListArguments( | ||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(generator.FullName ?? generator.Name))), | ||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(generator.Assembly.GetName().Version?.ToString() ?? "1.0.0")))))); | ||
} | ||
|
||
public static EnumMemberDeclarationSyntax EnumMember(string name, ushort value) | ||
{ | ||
return EnumMemberDeclaration(name) | ||
.WithEqualsValue(EqualsValueClause(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(value)))); | ||
} | ||
|
||
public static AttributeSyntax DescriptionAttribute(string description) | ||
{ | ||
var attributeArgument = AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(description))); | ||
return Attribute(IdentifierName("global::System.ComponentModel.Description"), AttributeArgumentList(SingletonSeparatedList(attributeArgument))); | ||
} | ||
|
||
public static TSyntax WithXmlComment<TSyntax>(this TSyntax node, string comment) | ||
where TSyntax : SyntaxNode | ||
{ | ||
return node.WithLeadingTrivia(TriviaList([.. comment.Split('\n').Select(Comment)])); | ||
} | ||
|
||
public static BaseNamespaceDeclarationSyntax NamespaceDeclaration(string name) | ||
{ | ||
return FileScopedNamespaceDeclaration(ParseName(name)) | ||
.WithLeadingTrivia(TriviaList( | ||
Comment("// <auto-generated/>"), | ||
Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true)), | ||
Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)))); | ||
} | ||
|
||
public static async Task SaveNode(SyntaxNode node, DirectoryInfo dirInfo, string name) | ||
{ | ||
await using var streamWriter = new StreamWriter(dirInfo.FullName + $"\\RotationSolver.SourceGenerators\\Resources\\{name}.txt", false); | ||
node.NormalizeWhitespace().WriteTo(streamWriter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
RotationSolver.SourceGenerators/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.