Update source gen

This commit is contained in:
Liza 2024-08-05 07:51:34 +02:00
parent d63d372fe2
commit 837ee7b368
Signed by: liza
GPG Key ID: 7199F8D727D55F67
6 changed files with 31 additions and 22 deletions

View File

@ -44,7 +44,8 @@ public class GatheringSourceGenerator : ISourceGenerator
var gatheringSchema = JsonSchema.FromText(jsonSchemaFile.GetText()!.ToString());
List<(ushort, GatheringRoot)> gatheringLocations = [];
foreach (var (id, node) in Utils.GetAdditionalFiles(context, jsonSchemaFile, gatheringSchema, InvalidJson))
foreach (var (id, node) in Utils.GetAdditionalFiles(context, jsonSchemaFile, gatheringSchema, InvalidJson,
ushort.Parse))
{
var gatheringLocation = node.Deserialize<GatheringRoot>()!;
gatheringLocations.Add((id, gatheringLocation));

View File

@ -47,8 +47,9 @@ public class QuestSourceGenerator : ISourceGenerator
{
var questSchema = JsonSchema.FromText(jsonSchemaFile.GetText()!.ToString());
List<(ushort, QuestRoot)> quests = [];
foreach (var (id, node) in Utils.GetAdditionalFiles(context, jsonSchemaFile, questSchema, InvalidJson))
List<(ElementId, QuestRoot)> quests = [];
foreach (var (id, node) in Utils.GetAdditionalFiles(context, jsonSchemaFile, questSchema, InvalidJson,
ElementId.FromString))
{
var quest = node.Deserialize<QuestRoot>()!;
if (quest.Disabled)
@ -65,8 +66,8 @@ public class QuestSourceGenerator : ISourceGenerator
return;
var partitionedQuests = quests
.OrderBy(x => x.Item1)
.GroupBy(x => $"LoadQuests{x.Item1 / 50}")
.OrderBy(x => x.Item1.Value)
.GroupBy(x => $"LoadQuests{x.Item1.Value / 50}")
.ToList();
var methods = Utils.CreateMethods("LoadQuests", partitionedQuests, CreateInitializer);
@ -123,7 +124,7 @@ public class QuestSourceGenerator : ISourceGenerator
context.AddSource("AssemblyQuestLoader.g.cs", code.ToFullString());
}
private static StatementSyntax[] CreateInitializer(List<(ushort QuestId, QuestRoot Root)> quests)
private static StatementSyntax[] CreateInitializer(List<(ElementId QuestId, QuestRoot Root)> quests)
{
List<StatementSyntax> statements = [];
@ -138,9 +139,7 @@ public class QuestSourceGenerator : ISourceGenerator
SeparatedList<ArgumentSyntax>(
new SyntaxNodeOrToken[]
{
Argument(
LiteralExpression(SyntaxKind.NumericLiteralExpression,
Literal(quest.QuestId))),
Argument(LiteralValue(quest.QuestId)),
Token(SyntaxKind.CommaToken),
Argument(CreateQuestRootExpression(quest.QuestId, quest.Root))
})))));
@ -149,7 +148,7 @@ public class QuestSourceGenerator : ISourceGenerator
return statements.ToArray();
}
private static ObjectCreationExpressionSyntax CreateQuestRootExpression(ushort questId, QuestRoot quest)
private static ObjectCreationExpressionSyntax CreateQuestRootExpression(ElementId questId, QuestRoot quest)
{
try
{

View File

@ -74,6 +74,15 @@ public static class RoslynShortcuts
SingletonSeparatedList(
Argument(LiteralValue(leveId.Value)))));
}
else if (value is SatisfactionSupplyNpcId satisfactionSupplyNpcId)
{
return ObjectCreationExpression(
IdentifierName(nameof(SatisfactionSupplyNpcId)))
.WithArgumentList(
ArgumentList(
SingletonSeparatedList(
Argument(LiteralValue(satisfactionSupplyNpcId.Value)))));
}
else if (value is Vector3 vector)
{
return ObjectCreationExpression(

View File

@ -14,8 +14,8 @@ namespace Questionable.QuestPathGenerator;
public static class Utils
{
public static IEnumerable<(ushort, JsonNode)> GetAdditionalFiles(GeneratorExecutionContext context,
AdditionalText jsonSchemaFile, JsonSchema jsonSchema, DiagnosticDescriptor invalidJson)
public static IEnumerable<(T, JsonNode)> GetAdditionalFiles<T>(GeneratorExecutionContext context,
AdditionalText jsonSchemaFile, JsonSchema jsonSchema, DiagnosticDescriptor invalidJson, Func<string, T> idParser)
{
var commonSchemaFile = context.AdditionalFiles.Single(x => Path.GetFileName(x.Path) == "common-schema.json");
List<AdditionalText> jsonSchemaFiles = [jsonSchemaFile, commonSchemaFile];
@ -36,7 +36,7 @@ public static class Utils
if (!name.Contains("_"))
continue;
ushort id = ushort.Parse(name.Substring(0, name.IndexOf('_')));
T id = idParser(name.Substring(0, name.IndexOf('_')));
var text = additionalFile.GetText();
if (text == null)
@ -68,9 +68,9 @@ public static class Utils
}
}
public static List<MethodDeclarationSyntax> CreateMethods<T>(string prefix,
List<IGrouping<string, (ushort, T)>> partitions,
Func<List<(ushort, T)>, StatementSyntax[]> toInitializers)
public static List<MethodDeclarationSyntax> CreateMethods<TId, TQuest>(string prefix,
List<IGrouping<string, (TId, TQuest)>> partitions,
Func<List<(TId, TQuest)>, StatementSyntax[]> toInitializers)
{
List<MethodDeclarationSyntax> methods =
[

View File

@ -9,9 +9,9 @@ namespace Questionable.QuestPaths;
[SuppressMessage("ReSharper", "PartialTypeWithSinglePart", Justification = "Required for RELEASE")]
public static partial class AssemblyQuestLoader
{
private static Dictionary<ushort, QuestRoot>? _quests;
private static Dictionary<ElementId, QuestRoot>? _quests;
public static IReadOnlyDictionary<ushort, QuestRoot> GetQuests()
public static IReadOnlyDictionary<ElementId, QuestRoot> GetQuests()
{
if (_quests == null)
{
@ -28,5 +28,5 @@ public static partial class AssemblyQuestLoader
typeof(AssemblyQuestLoader).Assembly.GetManifestResourceStream("Questionable.QuestPaths.QuestSchema")!;
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private static void AddQuest(ushort questId, QuestRoot root) => _quests![questId] = root;
private static void AddQuest(ElementId questId, QuestRoot root) => _quests![questId] = root;
}

View File

@ -87,13 +87,13 @@ internal sealed class QuestRegistry
{
_logger.LogInformation("Loading quests from assembly");
foreach ((ushort questId, QuestRoot questRoot) in AssemblyQuestLoader.GetQuests())
foreach ((ElementId questId, QuestRoot questRoot) in AssemblyQuestLoader.GetQuests())
{
Quest quest = new()
{
Id = new QuestId(questId),
Id = questId,
Root = questRoot,
Info = _questData.GetQuestInfo(new QuestId(questId)),
Info = _questData.GetQuestInfo(questId),
ReadOnly = true,
};
_quests[quest.Id] = quest;