1using System.CodeDom.Compiler;
2using System.Collections.Generic;
9using Microsoft.CodeAnalysis;
13public class BadInteropApiSourceGenerator
15 private readonly SourceProductionContext m_Context;
17 public BadInteropApiSourceGenerator(SourceProductionContext context)
22 private string GenerateInvocation(
MethodModel method)
24 StringBuilder sb =
new StringBuilder();
32 sb.Append(
"BadObject.Wrap(");
35 sb.Append($
"{method.MethodName}(");
36 bool hasCtx = method.
Parameters.Any(x => x.IsContext);
37 List<string> args =
new List<string>();
38 for (
int i = 0; i < method.
Parameters.Length; i++)
47 int index = i - (hasCtx ? 1 : 0);
48 string suppressNullable = parameter.IsNullable ?
"" :
"!";
53 args.Add($
"new BadArray(args.ToList()).Unwrap<{parameter.CsharpType}>()");
57 args.Add($
"new BadArray(args.Skip({index}).ToList()).Unwrap<{parameter.CsharpType}>()");
62 args.Add($
"GetParameter<{parameter.CsharpType}>(args, {index}, {parameter.DefaultValue ?? $"default({parameter.
CsharpType})
"}){suppressNullable}");
66 args.Add($
"GetParameter<{parameter.CsharpType}>(args, {index}){suppressNullable}");
71 sb.Append(
string.Join(
", ", args));
77 sb.Append(
"; return BadObject.Null; }");
81 sb.Append($
", {method.AllowNativeReturn.ToString().ToLower()})");
90 $
"new BadFunctionParameter(\"{model.Name}\", {model.HasDefaultValue.ToString().ToLower()}, {(!model.IsNullable).ToString().ToLower()}, {model.IsRestArgs.ToString().ToLower()}, null, BadNativeClassBuilder.GetNative(\"{model.Type}\"))";
93 private void GenerateMethodSource(IndentedTextWriter sb,
MethodModel method)
95 sb.WriteLine(
"target.SetProperty(");
97 sb.WriteLine($
"\"{method.ApiMethodName}\",");
98 sb.WriteLine(
"new BadInteropFunction(");
100 sb.WriteLine($
"\"{method.ApiMethodName}\",");
101 sb.WriteLine($
"(ctx, args) => {GenerateInvocation(method)},");
102 sb.WriteLine(
"false,");
103 sb.Write($
"BadNativeClassBuilder.GetNative(\"{method.ReturnType}\")");
114 for (
int i = 0; i < method.
Parameters.Length; i++)
122 sb.WriteLine(GenerateParameterSource(parameter) + (i == method.
Parameters.Length - 1 ?
"" :
","));
126 sb.WriteLine(
").SetMetaData(");
128 sb.WriteLine(
"new BadMetaData(");
130 sb.WriteLine($
"\"{method.Description}\",");
131 sb.WriteLine($
"\"{method.ReturnDescription}\",");
132 sb.WriteLine($
"\"{method.ReturnType}\",");
133 sb.WriteLine(
"new Dictionary<string, BadParameterMetaData>");
146 $
"{{\"{parameter.Name}\", new BadParameterMetaData(\"{parameter.Type}\", \"{parameter.Description}\\nDefault Value: {parameter.DefaultValue!.Replace("\
"",
"\\\"")}\
")}},"
151 sb.WriteLine($
"{{\"{parameter.Name}\", new BadParameterMetaData(\"{parameter.Type}\", \"{parameter.Description}\")}},");
165 public string GenerateModelSource(SourceProductionContext context,
ApiModel apiModel,
bool isError)
167 IndentedTextWriter tw =
new IndentedTextWriter(
new StringWriter());
168 tw.WriteLine(
"#nullable enable");
169 tw.WriteLine(
"using System.Collections.Generic;");
170 tw.WriteLine(
"using BadScript2.Parser;");
171 tw.WriteLine(
"using BadScript2.Runtime.Interop.Reflection;");
172 tw.WriteLine(
"using BadScript2.Runtime.Objects;");
173 tw.WriteLine(
"using BadScript2.Runtime.Interop.Functions;");
174 tw.WriteLine(
"using BadScript2.Runtime.Objects.Functions;");
175 tw.WriteLine(
"using BadScript2.Runtime.Objects.Types;");
176 tw.WriteLine(
"using BadScript2.Runtime.Interop;");
178 tw.WriteLine($
"namespace {apiModel.Namespace};");
179 tw.WriteLine($
"partial class {apiModel.ClassName} : BadScript2.Interop.BadAutoGeneratedInteropApi");
182 tw.WriteLine($
"{(apiModel.ConstructorPrivate ? "private" : "public")} {apiModel.ClassName}() : base(\"{apiModel.ApiName}\") {{ }}");
184 tw.WriteLine(
"protected override void LoadApi(BadTable target)");
187 tw.WriteLine(
"AdditionalData(target);");
189 if (!isError && apiModel.
Methods.Length != 0)
191 tw.WriteLine(
"T? GetParameter<T>(BadObject[] args, int i, T? defaultValue = default(T)) => args.Length>i?args[i].Unwrap<T>():defaultValue;");
194 GenerateMethodSource(tw, method);
206 string str = tw.InnerWriter.ToString();
readonly MethodModel[] Methods
readonly bool IsVoidReturn
readonly ParameterModel[] Parameters
readonly bool HasDefaultValue
readonly? string CsharpType