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()); var gatheringSchema = JsonSchema.FromText(jsonSchemaFile.GetText()!.ToString());
List<(ushort, GatheringRoot)> gatheringLocations = []; 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>()!; var gatheringLocation = node.Deserialize<GatheringRoot>()!;
gatheringLocations.Add((id, gatheringLocation)); gatheringLocations.Add((id, gatheringLocation));

View File

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

View File

@ -74,6 +74,15 @@ public static class RoslynShortcuts
SingletonSeparatedList( SingletonSeparatedList(
Argument(LiteralValue(leveId.Value))))); 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) else if (value is Vector3 vector)
{ {
return ObjectCreationExpression( return ObjectCreationExpression(

View File

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

View File

@ -9,9 +9,9 @@ namespace Questionable.QuestPaths;
[SuppressMessage("ReSharper", "PartialTypeWithSinglePart", Justification = "Required for RELEASE")] [SuppressMessage("ReSharper", "PartialTypeWithSinglePart", Justification = "Required for RELEASE")]
public static partial class AssemblyQuestLoader 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) if (_quests == null)
{ {
@ -28,5 +28,5 @@ public static partial class AssemblyQuestLoader
typeof(AssemblyQuestLoader).Assembly.GetManifestResourceStream("Questionable.QuestPaths.QuestSchema")!; typeof(AssemblyQuestLoader).Assembly.GetManifestResourceStream("Questionable.QuestPaths.QuestSchema")!;
[SuppressMessage("ReSharper", "UnusedMember.Local")] [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"); _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() Quest quest = new()
{ {
Id = new QuestId(questId), Id = questId,
Root = questRoot, Root = questRoot,
Info = _questData.GetQuestInfo(new QuestId(questId)), Info = _questData.GetQuestInfo(questId),
ReadOnly = true, ReadOnly = true,
}; };
_quests[quest.Id] = quest; _quests[quest.Id] = quest;