From 807df7df8a66bac7c6f94bd84009bc0701eb2d32 Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 3 Jul 2026 14:05:10 +0900 Subject: [PATCH] =?UTF-8?q?Lua=20API=20=E3=82=BD=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E3=82=B8=E3=82=A7=E3=83=8D=E3=83=AC=E3=83=BC=E3=82=BF=E3=82=92?= =?UTF-8?q?=E6=96=B0=E8=A8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Generator/EquatableArray.cs | 45 ++ LuaScript.Generator/IsExternalInit.cs | 4 + LuaScript.Generator/LuaApiGenerator.cs | 486 ++++++++++++++++++ .../LuaScript.Generator.csproj | 21 + 4 files changed, 556 insertions(+) create mode 100644 LuaScript.Generator/EquatableArray.cs create mode 100644 LuaScript.Generator/IsExternalInit.cs create mode 100644 LuaScript.Generator/LuaApiGenerator.cs create mode 100644 LuaScript.Generator/LuaScript.Generator.csproj diff --git a/LuaScript.Generator/EquatableArray.cs b/LuaScript.Generator/EquatableArray.cs new file mode 100644 index 0000000..8a6012f --- /dev/null +++ b/LuaScript.Generator/EquatableArray.cs @@ -0,0 +1,45 @@ +using System.Collections; + +namespace LuaScript.Generator +{ + internal readonly struct EquatableArray(T[] items) : IEquatable>, IReadOnlyList + where T : IEquatable + { + private readonly T[] _items = items; + + public static readonly EquatableArray Empty = new([]); + + public int Count => _items?.Length ?? 0; + + public T this[int index] => _items[index]; + + public bool Equals(EquatableArray other) + { + var a = _items ?? []; + var b = other._items ?? []; + if (a.Length != b.Length) + return false; + for (int i = 0; i < a.Length; i++) + { + if (!a[i].Equals(b[i])) + return false; + } + return true; + } + + public override bool Equals(object? obj) => obj is EquatableArray other && Equals(other); + + public override int GetHashCode() + { + var items = _items ?? []; + int hash = 17; + foreach (var item in items) + hash = hash * 31 + (item?.GetHashCode() ?? 0); + return hash; + } + + public IEnumerator GetEnumerator() => ((IEnumerable)(_items ?? [])).GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } +} diff --git a/LuaScript.Generator/IsExternalInit.cs b/LuaScript.Generator/IsExternalInit.cs new file mode 100644 index 0000000..f9f3fbe --- /dev/null +++ b/LuaScript.Generator/IsExternalInit.cs @@ -0,0 +1,4 @@ +namespace System.Runtime.CompilerServices +{ + internal static class IsExternalInit; +} diff --git a/LuaScript.Generator/LuaApiGenerator.cs b/LuaScript.Generator/LuaApiGenerator.cs new file mode 100644 index 0000000..48ec2e6 --- /dev/null +++ b/LuaScript.Generator/LuaApiGenerator.cs @@ -0,0 +1,486 @@ +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Text; + +namespace LuaScript.Generator +{ + internal enum LuaParameterKind + { + ExecutionContext, + Arguments, + DynValue, + Double, + Int, + Bool, + } + + internal enum LuaReturnKind + { + DynValue, + Double, + Void, + } + + internal sealed record LuaParameterModel(string Name, LuaParameterKind Kind, string DefaultLiteral); + + internal sealed record LuaFunctionModel( + string LuaName, + string MethodName, + LuaReturnKind ReturnKind, + EquatableArray Parameters); + + internal sealed record LuaConstantModel(string LuaName, string MemberName); + + internal enum LuaUpdateKind + { + Direct, + OptionalValue, + OptionalReference, + } + + internal sealed record LuaUpdateModel(string LuaName, string MethodName, LuaUpdateKind Kind); + + internal sealed record LuaCatalogEntry( + string Table, + string Name, + bool IsFunction, + EquatableArray Parameters); + + internal sealed record LuaTypePart(string Name, bool IsStatic); + + internal sealed record LuaTableModel( + string Namespace, + EquatableArray TypeChain, + string TableName, + string ContextType, + EquatableArray Functions, + EquatableArray Constants, + EquatableArray Updates, + EquatableArray Entries); + + [Generator] + public sealed class LuaApiGenerator : IIncrementalGenerator + { + private const string TableAttributeName = "LuaScript.Api.LuaTableAttribute"; + private const string FunctionAttributeName = "LuaScript.Api.LuaFunctionAttribute"; + private const string ConstantAttributeName = "LuaScript.Api.LuaConstantAttribute"; + private const string VariableAttributeName = "LuaScript.Api.LuaVariableAttribute"; + private const string BuiltinAttributeName = "LuaScript.Api.LuaBuiltinAttribute"; + + public void Initialize(IncrementalGeneratorInitializationContext context) + { + var tables = context.SyntaxProvider + .ForAttributeWithMetadataName( + TableAttributeName, + static (node, _) => node is ClassDeclarationSyntax, + static (ctx, _) => CreateModel(ctx)) + .Where(static model => model is not null) + .Select(static (model, _) => model!); + + var assemblyEntries = context.CompilationProvider + .Select(static (compilation, _) => CollectAssemblyEntries(compilation)); + + context.RegisterSourceOutput(tables, static (ctx, model) => + { + if (model.Functions.Count == 0 && model.Constants.Count == 0 && model.Updates.Count == 0) + return; + ctx.AddSource(RegistrationHintName(model), SourceText.From(EmitRegistration(model), Encoding.UTF8)); + }); + + var catalog = tables.Collect().Combine(assemblyEntries); + context.RegisterSourceOutput(catalog, static (ctx, source) => + { + var (models, extra) = source; + var entries = models + .OrderBy(static m => m.Namespace, StringComparer.Ordinal) + .ThenBy(static m => string.Join("+", m.TypeChain.Select(static p => p.Name)), StringComparer.Ordinal) + .SelectMany(static m => m.Entries) + .Concat(extra) + .ToArray(); + if (entries.Length == 0) + return; + ctx.AddSource("LuaApiCatalog.g.cs", SourceText.From(EmitCatalog(entries), Encoding.UTF8)); + }); + } + + private static LuaTableModel? CreateModel(GeneratorAttributeSyntaxContext context) + { + if (context.TargetSymbol is not INamedTypeSymbol symbol) + return null; + + var tableArguments = context.Attributes[0].ConstructorArguments; + string tableName = tableArguments.Length > 0 && tableArguments[0].Value is string name ? name : string.Empty; + + var chain = new List(); + for (var type = symbol; type is not null; type = type.ContainingType) + chain.Insert(0, new LuaTypePart(type.Name, type.IsStatic)); + + var entries = new List<(int Position, LuaCatalogEntry Entry)>(); + foreach (var attribute in symbol.GetAttributes()) + { + string? attributeName = attribute.AttributeClass?.ToDisplayString(); + if (attributeName is not (VariableAttributeName or BuiltinAttributeName or FunctionAttributeName)) + continue; + var entry = CreateAttributeEntry(attribute, tableName); + if (entry is null) + continue; + int position = attribute.ApplicationSyntaxReference?.Span.Start ?? 0; + entries.Add((position, entry)); + } + var classEntries = entries.OrderBy(static e => e.Position).Select(static e => e.Entry).ToList(); + + var functions = new List<(int Position, LuaFunctionModel Function, LuaCatalogEntry Entry)>(); + var constants = new List<(int Position, LuaConstantModel Constant, LuaCatalogEntry Entry)>(); + var updates = new List<(int Position, LuaUpdateModel Update, LuaCatalogEntry? Entry)>(); + string contextType = string.Empty; + foreach (var member in symbol.GetMembers()) + { + int position = member.DeclaringSyntaxReferences.Length > 0 + ? member.DeclaringSyntaxReferences[0].Span.Start + : 0; + + if (member is IMethodSymbol method) + { + var functionAttribute = FindAttribute(method, FunctionAttributeName); + if (functionAttribute is not null) + { + var function = CreateFunctionModel(method, functionAttribute, out var catalogParameters); + if (function is null) + continue; + functions.Add((position, function, new LuaCatalogEntry(tableName, function.LuaName, true, catalogParameters))); + continue; + } + + var variableAttribute = FindAttribute(method, VariableAttributeName); + if (variableAttribute is not null) + { + var update = CreateUpdateModel(method, variableAttribute, tableName, ref contextType, out var entry); + if (update is not null) + updates.Add((position, update, entry)); + } + continue; + } + + if (member is IFieldSymbol or IPropertySymbol) + { + var attribute = FindAttribute(member, ConstantAttributeName); + if (attribute is null) + continue; + if (attribute.ConstructorArguments.Length == 0 || attribute.ConstructorArguments[0].Value is not string constantName) + continue; + constants.Add(( + position, + new LuaConstantModel(constantName, member.Name), + new LuaCatalogEntry(tableName, constantName, false, EquatableArray.Empty))); + } + } + + var orderedMembers = functions + .Select(static f => (Position: f.Position, Entry: f.Entry)) + .Concat(constants.Select(static c => (Position: c.Position, Entry: c.Entry))) + .Concat(updates.Where(static u => u.Entry is not null).Select(static u => (Position: u.Position, Entry: u.Entry!))) + .OrderBy(static e => e.Position) + .Select(static e => e.Entry); + + return new LuaTableModel( + symbol.ContainingNamespace.ToDisplayString(), + new EquatableArray([.. chain]), + tableName, + contextType, + new EquatableArray([.. functions.OrderBy(static f => f.Position).Select(static f => f.Function)]), + new EquatableArray([.. constants.OrderBy(static c => c.Position).Select(static c => c.Constant)]), + new EquatableArray([.. updates.OrderBy(static u => u.Position).Select(static u => u.Update)]), + new EquatableArray([.. classEntries, .. orderedMembers])); + } + + private static AttributeData? FindAttribute(ISymbol symbol, string attributeName) + { + foreach (var attribute in symbol.GetAttributes()) + { + if (attribute.AttributeClass?.ToDisplayString() == attributeName) + return attribute; + } + return null; + } + + private static LuaCatalogEntry? CreateAttributeEntry(AttributeData attribute, string defaultTable) + { + if (attribute.ConstructorArguments.Length == 0 || attribute.ConstructorArguments[0].Value is not string name) + return null; + + string table = defaultTable; + foreach (var argument in attribute.NamedArguments) + { + if (argument.Key == "Table" && argument.Value.Value is string overridden) + table = overridden; + } + + bool isFunction = attribute.AttributeClass?.ToDisplayString() is (BuiltinAttributeName or FunctionAttributeName); + var parameters = isFunction && attribute.ConstructorArguments.Length > 1 + ? ExtractStringArray(attribute.ConstructorArguments[1]) + : EquatableArray.Empty; + + return new LuaCatalogEntry(table, name, isFunction, parameters); + } + + private static EquatableArray ExtractStringArray(TypedConstant constant) + { + if (constant.Kind != TypedConstantKind.Array) + return EquatableArray.Empty; + return new EquatableArray([.. constant.Values.Select(static v => v.Value as string ?? string.Empty)]); + } + + private static LuaFunctionModel? CreateFunctionModel(IMethodSymbol method, AttributeData attribute, out EquatableArray catalogParameters) + { + catalogParameters = EquatableArray.Empty; + if (attribute.ConstructorArguments.Length == 0 || attribute.ConstructorArguments[0].Value is not string luaName) + return null; + + LuaReturnKind returnKind; + if (method.ReturnsVoid) + returnKind = LuaReturnKind.Void; + else if (method.ReturnType.ToDisplayString() == "MoonSharp.Interpreter.DynValue") + returnKind = LuaReturnKind.DynValue; + else if (method.ReturnType.SpecialType == SpecialType.System_Double) + returnKind = LuaReturnKind.Double; + else + return null; + + var parameters = new List(); + foreach (var parameter in method.Parameters) + { + LuaParameterKind kind; + string defaultLiteral = string.Empty; + switch (parameter.Type.ToDisplayString()) + { + case "MoonSharp.Interpreter.ScriptExecutionContext": + kind = LuaParameterKind.ExecutionContext; + break; + case "MoonSharp.Interpreter.CallbackArguments": + kind = LuaParameterKind.Arguments; + break; + case "MoonSharp.Interpreter.DynValue": + kind = LuaParameterKind.DynValue; + break; + case "double": + kind = LuaParameterKind.Double; + defaultLiteral = FormatDouble(parameter.HasExplicitDefaultValue ? (double)parameter.ExplicitDefaultValue! : 0d); + break; + case "int": + kind = LuaParameterKind.Int; + defaultLiteral = FormatDouble(parameter.HasExplicitDefaultValue ? (int)parameter.ExplicitDefaultValue! : 0); + break; + case "bool": + kind = LuaParameterKind.Bool; + defaultLiteral = parameter.HasExplicitDefaultValue && (bool)parameter.ExplicitDefaultValue! ? "true" : "false"; + break; + default: + return null; + } + parameters.Add(new LuaParameterModel(parameter.Name, kind, defaultLiteral)); + } + + var explicitParameters = attribute.ConstructorArguments.Length > 1 + ? ExtractStringArray(attribute.ConstructorArguments[1]) + : EquatableArray.Empty; + catalogParameters = explicitParameters.Count > 0 + ? explicitParameters + : new EquatableArray([.. parameters + .Where(static p => p.Kind is not (LuaParameterKind.ExecutionContext or LuaParameterKind.Arguments)) + .Select(static p => p.Name)]); + + return new LuaFunctionModel(luaName, method.Name, returnKind, new EquatableArray([.. parameters])); + } + + private static LuaUpdateModel? CreateUpdateModel(IMethodSymbol method, AttributeData attribute, string tableName, ref string contextType, out LuaCatalogEntry? entry) + { + entry = null; + if (attribute.ConstructorArguments.Length == 0 || attribute.ConstructorArguments[0].Value is not string luaName) + return null; + if (method.Parameters.Length != 1) + return null; + + string parameterType = method.Parameters[0].Type.ToDisplayString(); + if (contextType.Length == 0) + contextType = parameterType; + else if (contextType != parameterType) + return null; + + var returnType = method.ReturnType; + LuaUpdateKind kind; + if (returnType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) + kind = LuaUpdateKind.OptionalValue; + else if (returnType.IsReferenceType) + kind = LuaUpdateKind.OptionalReference; + else + kind = LuaUpdateKind.Direct; + + bool inCatalog = true; + foreach (var argument in attribute.NamedArguments) + { + if (argument.Key == "InCatalog" && argument.Value.Value is bool value) + inCatalog = value; + } + + if (inCatalog) + entry = new LuaCatalogEntry(tableName, luaName, false, EquatableArray.Empty); + return new LuaUpdateModel(luaName, method.Name, kind); + } + + private static string FormatDouble(double value) => + value.ToString("R", System.Globalization.CultureInfo.InvariantCulture) + "d"; + + private static string RegistrationHintName(LuaTableModel model) + { + var chain = string.Join(".", model.TypeChain.Select(static p => p.Name)); + return $"{model.Namespace}.{chain}.LuaRegistration.g.cs"; + } + + private static string EmitRegistration(LuaTableModel model) + { + var builder = new StringBuilder(); + builder.AppendLine("#nullable enable"); + builder.AppendLine($"namespace {model.Namespace}"); + builder.AppendLine("{"); + + string indent = " "; + foreach (var part in model.TypeChain) + { + builder.AppendLine($"{indent}{(part.IsStatic ? "static partial class " : "partial class ")}{part.Name}"); + builder.AppendLine($"{indent}{{"); + indent += " "; + } + + bool isStatic = model.TypeChain[model.TypeChain.Count - 1].IsStatic; + string body = indent + " "; + + if (model.Constants.Count > 0 || model.Functions.Count > 0) + { + builder.AppendLine($"{indent}internal {(isStatic ? "static " : "")}void RegisterLuaMembers(global::MoonSharp.Interpreter.Table table)"); + builder.AppendLine($"{indent}{{"); + + foreach (var constant in model.Constants) + builder.AppendLine($"{body}table[\"{constant.LuaName}\"] = {constant.MemberName};"); + + foreach (var function in model.Functions) + { + string call = $"{function.MethodName}({EmitArguments(function.Parameters)})"; + string lambda = function.ReturnKind switch + { + LuaReturnKind.DynValue => $"(execCtx, args) => {call}", + LuaReturnKind.Double => $"(execCtx, args) => global::MoonSharp.Interpreter.DynValue.NewNumber({call})", + _ => $"(execCtx, args) => {{ {call}; return global::MoonSharp.Interpreter.DynValue.Void; }}", + }; + builder.AppendLine($"{body}table[\"{function.LuaName}\"] = global::MoonSharp.Interpreter.DynValue.NewCallback({lambda});"); + } + + builder.AppendLine($"{indent}}}"); + } + + if (model.Updates.Count > 0) + { + builder.AppendLine($"{indent}internal {(isStatic ? "static " : "")}void UpdateLuaMembers(global::MoonSharp.Interpreter.Table table, global::{model.ContextType} context)"); + builder.AppendLine($"{indent}{{"); + foreach (var update in model.Updates) + { + switch (update.Kind) + { + case LuaUpdateKind.OptionalValue: + builder.AppendLine($"{body}{{ var value = {update.MethodName}(context); if (value.HasValue) table[\"{update.LuaName}\"] = value.Value; }}"); + break; + case LuaUpdateKind.OptionalReference: + builder.AppendLine($"{body}{{ var value = {update.MethodName}(context); if (value is not null) table[\"{update.LuaName}\"] = value; }}"); + break; + default: + builder.AppendLine($"{body}table[\"{update.LuaName}\"] = {update.MethodName}(context);"); + break; + } + } + builder.AppendLine($"{indent}}}"); + } + + for (int i = model.TypeChain.Count - 1; i >= 0; i--) + { + indent = new string(' ', 4 * (i + 1)); + builder.AppendLine($"{indent}}}"); + } + builder.AppendLine("}"); + return builder.ToString(); + } + + private static string EmitArguments(EquatableArray parameters) + { + var rendered = new List(); + int position = 0; + foreach (var parameter in parameters) + { + if (parameter.Kind == LuaParameterKind.ExecutionContext) + { + rendered.Add("execCtx"); + continue; + } + if (parameter.Kind == LuaParameterKind.Arguments) + { + rendered.Add("args"); + continue; + } + rendered.Add(parameter.Kind switch + { + LuaParameterKind.DynValue => $"args.Count > {position} ? args[{position}] : global::MoonSharp.Interpreter.DynValue.Nil", + LuaParameterKind.Double => $"args.Count > {position} ? args[{position}].CastToNumber() ?? {parameter.DefaultLiteral} : {parameter.DefaultLiteral}", + LuaParameterKind.Int => $"(int)(args.Count > {position} ? args[{position}].CastToNumber() ?? {parameter.DefaultLiteral} : {parameter.DefaultLiteral})", + _ => $"args.Count > {position} ? args[{position}].CastToBool() : {parameter.DefaultLiteral}", + }); + position++; + } + return string.Join(", ", rendered); + } + + private static string EmitCatalog(LuaCatalogEntry[] entries) + { + var builder = new StringBuilder(); + builder.AppendLine("#nullable enable"); + builder.AppendLine("namespace LuaScript.Generated"); + builder.AppendLine("{"); + builder.AppendLine(" internal static class LuaApiModuleInitializer"); + builder.AppendLine(" {"); + builder.AppendLine(" [global::System.Runtime.CompilerServices.ModuleInitializer]"); + builder.AppendLine(" internal static void Initialize()"); + builder.AppendLine(" {"); + builder.AppendLine(" global::LuaScript.Api.LuaApiCatalog.Register(new global::LuaScript.Api.LuaApiMember[]"); + builder.AppendLine(" {"); + foreach (var entry in entries) + { + string kind = entry.IsFunction + ? "global::LuaScript.Api.LuaApiMemberKind.Function" + : "global::LuaScript.Api.LuaApiMemberKind.Variable"; + string parameters = entry.Parameters.Count == 0 + ? "global::System.Array.Empty()" + : $"new string[] {{ {string.Join(", ", entry.Parameters.Select(static p => $"\"{p}\""))} }}"; + builder.AppendLine($" new(\"{entry.Table}\", \"{entry.Name}\", {kind}, {parameters}),"); + } + builder.AppendLine(" });"); + builder.AppendLine(" }"); + builder.AppendLine(" }"); + builder.AppendLine("}"); + return builder.ToString(); + } + + private static IReadOnlyList CollectAssemblyEntries(Compilation compilation) + { + var entries = new List<(int Position, LuaCatalogEntry Entry)>(); + foreach (var attribute in compilation.Assembly.GetAttributes()) + { + string? attributeName = attribute.AttributeClass?.ToDisplayString(); + if (attributeName is not (VariableAttributeName or BuiltinAttributeName or FunctionAttributeName)) + continue; + var entry = CreateAttributeEntry(attribute, string.Empty); + if (entry is null) + continue; + entries.Add((attribute.ApplicationSyntaxReference?.Span.Start ?? 0, entry)); + } + return [.. entries.OrderBy(static e => e.Position).Select(static e => e.Entry)]; + } + } +} diff --git a/LuaScript.Generator/LuaScript.Generator.csproj b/LuaScript.Generator/LuaScript.Generator.csproj new file mode 100644 index 0000000..94c3040 --- /dev/null +++ b/LuaScript.Generator/LuaScript.Generator.csproj @@ -0,0 +1,21 @@ + + + + netstandard2.0 + latest + enable + enable + 1.13.3 + true + true + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + +