2024-06-14 09:37:33 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2024-06-15 21:32:58 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-06-14 09:37:33 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.Json;
|
2024-06-15 21:32:58 +00:00
|
|
|
using Json.Schema;
|
2024-06-14 09:37:33 +00:00
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
2024-08-02 16:30:21 +00:00
|
|
|
using Questionable.Model.Questing;
|
2024-06-14 09:37:33 +00:00
|
|
|
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
|
|
|
using static Questionable.QuestPathGenerator.RoslynShortcuts;
|
|
|
|
|
|
|
|
namespace Questionable.QuestPathGenerator;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A sample source generator that creates C# classes based on the text file (in this case, Domain Driven Design ubiquitous language registry).
|
|
|
|
/// When using a simple text file as a baseline, we can create a non-incremental source generator.
|
|
|
|
/// </summary>
|
|
|
|
[Generator]
|
2024-06-15 21:32:58 +00:00
|
|
|
[SuppressMessage("MicrosoftCodeAnalysisReleaseTracking", "RS2008")]
|
2024-06-14 09:37:33 +00:00
|
|
|
public class QuestSourceGenerator : ISourceGenerator
|
|
|
|
{
|
2024-06-15 21:32:58 +00:00
|
|
|
private static readonly DiagnosticDescriptor InvalidJson = new("QSG0001",
|
|
|
|
"Invalid JSON",
|
|
|
|
"Invalid quest file {0}",
|
|
|
|
nameof(QuestSourceGenerator),
|
|
|
|
DiagnosticSeverity.Error,
|
|
|
|
true);
|
|
|
|
|
2024-06-14 09:37:33 +00:00
|
|
|
public void Initialize(GeneratorInitializationContext context)
|
|
|
|
{
|
|
|
|
// No initialization required for this generator.
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Execute(GeneratorExecutionContext context)
|
|
|
|
{
|
2024-06-15 21:32:58 +00:00
|
|
|
// Find schema definition
|
2024-08-02 16:30:21 +00:00
|
|
|
AdditionalText? questSchema =
|
|
|
|
context.AdditionalFiles.SingleOrDefault(x => Path.GetFileName(x.Path) == "quest-v1.json");
|
|
|
|
if (questSchema != null)
|
|
|
|
GenerateQuestSource(context, questSchema);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void GenerateQuestSource(GeneratorExecutionContext context, AdditionalText jsonSchemaFile)
|
|
|
|
{
|
2024-06-15 21:32:58 +00:00
|
|
|
var questSchema = JsonSchema.FromText(jsonSchemaFile.GetText()!.ToString());
|
2024-08-16 20:52:43 +00:00
|
|
|
var jsonSchemaFiles = Utils.RegisterSchemas(context);
|
2024-06-15 21:32:58 +00:00
|
|
|
|
2024-08-05 05:51:34 +00:00
|
|
|
List<(ElementId, QuestRoot)> quests = [];
|
2024-08-16 20:52:43 +00:00
|
|
|
foreach (var (id, node) in Utils.GetAdditionalFiles(context, jsonSchemaFiles, questSchema, InvalidJson,
|
2024-08-05 05:51:34 +00:00
|
|
|
ElementId.FromString))
|
2024-06-14 09:37:33 +00:00
|
|
|
{
|
2024-08-02 16:30:21 +00:00
|
|
|
var quest = node.Deserialize<QuestRoot>()!;
|
2024-07-15 21:09:09 +00:00
|
|
|
if (quest.Disabled)
|
2024-07-20 12:34:54 +00:00
|
|
|
{
|
|
|
|
quest.Author = [];
|
|
|
|
quest.QuestSequence = [];
|
|
|
|
}
|
2024-07-15 21:09:09 +00:00
|
|
|
|
2024-06-14 09:37:33 +00:00
|
|
|
quests.Add((id, quest));
|
|
|
|
}
|
|
|
|
|
2024-06-15 12:12:32 +00:00
|
|
|
if (quests.Count == 0)
|
|
|
|
return;
|
|
|
|
|
2024-07-25 02:15:44 +00:00
|
|
|
var partitionedQuests = quests
|
2024-08-05 05:51:34 +00:00
|
|
|
.OrderBy(x => x.Item1.Value)
|
|
|
|
.GroupBy(x => $"LoadQuests{x.Item1.Value / 50}")
|
2024-07-25 02:15:44 +00:00
|
|
|
.ToList();
|
|
|
|
|
2024-08-02 16:30:21 +00:00
|
|
|
var methods = Utils.CreateMethods("LoadQuests", partitionedQuests, CreateInitializer);
|
2024-06-14 09:37:33 +00:00
|
|
|
|
|
|
|
var code =
|
|
|
|
CompilationUnit()
|
|
|
|
.WithUsings(
|
|
|
|
List(
|
|
|
|
new[]
|
|
|
|
{
|
|
|
|
UsingDirective(
|
|
|
|
IdentifierName("System")),
|
|
|
|
UsingDirective(
|
|
|
|
QualifiedName(
|
|
|
|
IdentifierName("System"),
|
|
|
|
IdentifierName("Numerics"))),
|
|
|
|
UsingDirective(
|
|
|
|
QualifiedName(
|
|
|
|
IdentifierName("System"),
|
|
|
|
IdentifierName("IO"))),
|
|
|
|
UsingDirective(
|
|
|
|
QualifiedName(
|
|
|
|
QualifiedName(
|
|
|
|
IdentifierName("System"), IdentifierName("Collections")),
|
|
|
|
IdentifierName("Generic"))),
|
|
|
|
UsingDirective(
|
|
|
|
QualifiedName(
|
|
|
|
QualifiedName(
|
|
|
|
IdentifierName("Questionable"),
|
|
|
|
IdentifierName("Model")),
|
2024-08-02 16:30:21 +00:00
|
|
|
IdentifierName("Questing"))),
|
|
|
|
UsingDirective(
|
|
|
|
QualifiedName(
|
|
|
|
QualifiedName(
|
|
|
|
IdentifierName("Questionable"),
|
|
|
|
IdentifierName("Model")),
|
|
|
|
IdentifierName("Common")))
|
2024-06-14 09:37:33 +00:00
|
|
|
}))
|
|
|
|
.WithMembers(
|
|
|
|
SingletonList<MemberDeclarationSyntax>(
|
|
|
|
FileScopedNamespaceDeclaration(
|
|
|
|
QualifiedName(
|
|
|
|
IdentifierName("Questionable"),
|
|
|
|
IdentifierName("QuestPaths")))
|
|
|
|
.WithMembers(
|
|
|
|
SingletonList<MemberDeclarationSyntax>(
|
|
|
|
ClassDeclaration("AssemblyQuestLoader")
|
|
|
|
.WithModifiers(
|
2024-07-25 02:15:44 +00:00
|
|
|
TokenList(Token(SyntaxKind.PartialKeyword)))
|
|
|
|
.WithMembers(List<MemberDeclarationSyntax>(methods))))))
|
2024-06-14 09:37:33 +00:00
|
|
|
.NormalizeWhitespace();
|
|
|
|
|
|
|
|
// Add the source code to the compilation.
|
|
|
|
context.AddSource("AssemblyQuestLoader.g.cs", code.ToFullString());
|
|
|
|
}
|
|
|
|
|
2024-08-05 05:51:34 +00:00
|
|
|
private static StatementSyntax[] CreateInitializer(List<(ElementId QuestId, QuestRoot Root)> quests)
|
2024-07-25 02:15:44 +00:00
|
|
|
{
|
|
|
|
List<StatementSyntax> statements = [];
|
|
|
|
|
|
|
|
foreach (var quest in quests)
|
|
|
|
{
|
|
|
|
statements.Add(
|
|
|
|
ExpressionStatement(
|
|
|
|
InvocationExpression(
|
|
|
|
IdentifierName("AddQuest"))
|
|
|
|
.WithArgumentList(
|
|
|
|
ArgumentList(
|
|
|
|
SeparatedList<ArgumentSyntax>(
|
|
|
|
new SyntaxNodeOrToken[]
|
|
|
|
{
|
2024-08-05 05:51:34 +00:00
|
|
|
Argument(LiteralValue(quest.QuestId)),
|
2024-07-25 02:15:44 +00:00
|
|
|
Token(SyntaxKind.CommaToken),
|
|
|
|
Argument(CreateQuestRootExpression(quest.QuestId, quest.Root))
|
|
|
|
})))));
|
|
|
|
}
|
|
|
|
|
|
|
|
return statements.ToArray();
|
|
|
|
}
|
|
|
|
|
2024-08-05 05:51:34 +00:00
|
|
|
private static ObjectCreationExpressionSyntax CreateQuestRootExpression(ElementId questId, QuestRoot quest)
|
2024-06-14 09:37:33 +00:00
|
|
|
{
|
2024-07-21 21:10:16 +00:00
|
|
|
try
|
2024-06-14 09:37:33 +00:00
|
|
|
{
|
2024-07-25 02:15:44 +00:00
|
|
|
return ObjectCreationExpression(
|
|
|
|
IdentifierName(nameof(QuestRoot)))
|
|
|
|
.WithInitializer(
|
|
|
|
InitializerExpression(
|
|
|
|
SyntaxKind.ObjectInitializerExpression,
|
|
|
|
SeparatedList<ExpressionSyntax>(
|
|
|
|
SyntaxNodeList(
|
|
|
|
AssignmentList(nameof(QuestRoot.Author), quest.Author)
|
|
|
|
.AsSyntaxNodeOrToken(),
|
2024-08-03 18:30:18 +00:00
|
|
|
Assignment(nameof(QuestRoot.Disabled), quest.Disabled, false).AsSyntaxNodeOrToken(),
|
2024-07-25 02:15:44 +00:00
|
|
|
Assignment(nameof(QuestRoot.Comment), quest.Comment, null)
|
|
|
|
.AsSyntaxNodeOrToken(),
|
|
|
|
AssignmentExpression(
|
|
|
|
SyntaxKind.SimpleAssignmentExpression,
|
|
|
|
IdentifierName(nameof(QuestRoot.QuestSequence)),
|
|
|
|
CreateQuestSequence(quest.QuestSequence))))));
|
2024-07-21 21:10:16 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
throw new Exception($"QuestGen[{questId}]: {e.Message}", e);
|
|
|
|
}
|
2024-06-14 09:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static ExpressionSyntax CreateQuestSequence(List<QuestSequence> sequences)
|
|
|
|
{
|
|
|
|
return CollectionExpression(
|
|
|
|
SeparatedList<CollectionElementSyntax>(
|
|
|
|
sequences.SelectMany(sequence => new SyntaxNodeOrToken[]
|
|
|
|
{
|
|
|
|
ExpressionElement(
|
|
|
|
ObjectCreationExpression(
|
|
|
|
IdentifierName(nameof(QuestSequence)))
|
|
|
|
.WithInitializer(
|
|
|
|
InitializerExpression(
|
|
|
|
SyntaxKind.ObjectInitializerExpression,
|
|
|
|
SeparatedList<ExpressionSyntax>(
|
|
|
|
SyntaxNodeList(
|
|
|
|
Assignment<int?>(nameof(QuestSequence.Sequence), sequence.Sequence, null)
|
|
|
|
.AsSyntaxNodeOrToken(),
|
|
|
|
Assignment(nameof(QuestSequence.Comment), sequence.Comment, null)
|
|
|
|
.AsSyntaxNodeOrToken(),
|
2024-08-16 20:52:43 +00:00
|
|
|
AssignmentList(nameof(QuestSequence.Steps), sequence.Steps)
|
2024-06-14 09:37:33 +00:00
|
|
|
.AsSyntaxNodeOrToken()))))),
|
|
|
|
Token(SyntaxKind.CommaToken),
|
|
|
|
}.ToArray())));
|
|
|
|
}
|
|
|
|
}
|