From 1a3394b8f5bda791f82faadcf87bbc99eecdcb9b Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 5 Jul 2026 08:53:45 +0900 Subject: [PATCH] =?UTF-8?q?=E5=A4=89=E6=95=B0=E6=9B=B4=E6=96=B0=E3=81=AE?= =?UTF-8?q?=E7=99=BA=E8=A1=8C=E3=82=92=E5=9E=8B=E4=BB=98=E3=81=8DDynValue?= =?UTF-8?q?=E3=81=B8=E5=A4=89=E6=9B=B4=E3=81=97=E3=83=9C=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=B9=E5=8C=96=E3=82=92=E9=99=A4=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Generator/LuaApiGenerator.cs | 49 +++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/LuaScript.Generator/LuaApiGenerator.cs b/LuaScript.Generator/LuaApiGenerator.cs index 48ec2e6..e0a5d38 100644 --- a/LuaScript.Generator/LuaApiGenerator.cs +++ b/LuaScript.Generator/LuaApiGenerator.cs @@ -39,7 +39,15 @@ namespace LuaScript.Generator OptionalReference, } - internal sealed record LuaUpdateModel(string LuaName, string MethodName, LuaUpdateKind Kind); + internal enum LuaValueKind + { + Number, + Boolean, + String, + Other, + } + + internal sealed record LuaUpdateModel(string LuaName, string MethodName, LuaUpdateKind Kind, LuaValueKind ValueKind); internal sealed record LuaCatalogEntry( string Table, @@ -309,12 +317,30 @@ namespace LuaScript.Generator var returnType = method.ReturnType; LuaUpdateKind kind; + ITypeSymbol valueType; if (returnType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) + { kind = LuaUpdateKind.OptionalValue; + valueType = ((INamedTypeSymbol)returnType).TypeArguments[0]; + } else if (returnType.IsReferenceType) + { kind = LuaUpdateKind.OptionalReference; + valueType = returnType; + } else + { kind = LuaUpdateKind.Direct; + valueType = returnType; + } + + var valueKind = valueType.SpecialType switch + { + SpecialType.System_Double or SpecialType.System_Int32 => LuaValueKind.Number, + SpecialType.System_Boolean => LuaValueKind.Boolean, + SpecialType.System_String => LuaValueKind.String, + _ => LuaValueKind.Other, + }; bool inCatalog = true; foreach (var argument in attribute.NamedArguments) @@ -325,7 +351,7 @@ namespace LuaScript.Generator if (inCatalog) entry = new LuaCatalogEntry(tableName, luaName, false, EquatableArray.Empty); - return new LuaUpdateModel(luaName, method.Name, kind); + return new LuaUpdateModel(luaName, method.Name, kind, valueKind); } private static string FormatDouble(double value) => @@ -387,13 +413,19 @@ namespace LuaScript.Generator switch (update.Kind) { case LuaUpdateKind.OptionalValue: - builder.AppendLine($"{body}{{ var value = {update.MethodName}(context); if (value.HasValue) table[\"{update.LuaName}\"] = value.Value; }}"); + builder.AppendLine(update.ValueKind == LuaValueKind.Other + ? $"{body}{{ var value = {update.MethodName}(context); if (value.HasValue) table[\"{update.LuaName}\"] = value.Value; }}" + : $"{body}{{ var value = {update.MethodName}(context); if (value.HasValue) table.Set(\"{update.LuaName}\", {WrapValue(update.ValueKind, "value.Value")}); }}"); break; case LuaUpdateKind.OptionalReference: - builder.AppendLine($"{body}{{ var value = {update.MethodName}(context); if (value is not null) table[\"{update.LuaName}\"] = value; }}"); + builder.AppendLine(update.ValueKind == LuaValueKind.Other + ? $"{body}{{ var value = {update.MethodName}(context); if (value is not null) table[\"{update.LuaName}\"] = value; }}" + : $"{body}{{ var value = {update.MethodName}(context); if (value is not null) table.Set(\"{update.LuaName}\", {WrapValue(update.ValueKind, "value")}); }}"); break; default: - builder.AppendLine($"{body}table[\"{update.LuaName}\"] = {update.MethodName}(context);"); + builder.AppendLine(update.ValueKind == LuaValueKind.Other + ? $"{body}table[\"{update.LuaName}\"] = {update.MethodName}(context);" + : $"{body}table.Set(\"{update.LuaName}\", {WrapValue(update.ValueKind, $"{update.MethodName}(context)")});"); break; } } @@ -409,6 +441,13 @@ namespace LuaScript.Generator return builder.ToString(); } + private static string WrapValue(LuaValueKind kind, string expression) => kind switch + { + LuaValueKind.Number => $"global::MoonSharp.Interpreter.DynValue.NewNumber({expression})", + LuaValueKind.Boolean => $"global::MoonSharp.Interpreter.DynValue.NewBoolean({expression})", + _ => $"global::MoonSharp.Interpreter.DynValue.NewString({expression})", + }; + private static string EmitArguments(EquatableArray parameters) { var rendered = new List();