Skip to content

Commit

Permalink
.NET: Fix assembly generation in DD2
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed May 11, 2024
1 parent 4e258f3 commit cdb9c92
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions csharp-api/AssemblyGenerator/ClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ private TypeDeclarationSyntax GenerateMethods(List<SimpleBaseTypeSyntax> baseTyp
);

bool anyOutParams = false;
System.Collections.Generic.List<string> paramNames = [];

if (method.Parameters.Count > 0) {
// If any of the params have ! in them, skip this method
Expand Down Expand Up @@ -356,10 +357,13 @@ private TypeDeclarationSyntax GenerateMethods(List<SimpleBaseTypeSyntax> baseTyp
var paramType = param.get_ParameterType();

if (paramType == null) {
paramNames.Add(paramName);
parameters.Add(SyntaxFactory.Parameter(SyntaxFactory.Identifier(paramName)).WithType(SyntaxFactory.ParseTypeName("object")));
continue;
}

var parsedParamName = new string(paramName as string);

/*if (param.get_IsGenericParameter() == true) {
return null; // no generic parameters.
}*/
Expand Down Expand Up @@ -391,10 +395,14 @@ private TypeDeclarationSyntax GenerateMethods(List<SimpleBaseTypeSyntax> baseTyp
simpleMethodSignature += "ptr " + paramTypeSyntax.GetText().ToString();
parameters.Add(SyntaxFactory.Parameter(SyntaxFactory.Identifier(paramName)).WithType(SyntaxFactory.ParseTypeName(paramTypeSyntax.ToString() + "*")).AddModifiers(modifiers.ToArray()));
anyUnsafeParams = true;

parsedParamName = "(global::System.IntPtr) " + parsedParamName;
} else {
simpleMethodSignature += paramTypeSyntax.GetText().ToString();
parameters.Add(SyntaxFactory.Parameter(SyntaxFactory.Identifier(paramName)).WithType(paramTypeSyntax).AddModifiers(modifiers.ToArray()));
}

paramNames.Add(parsedParamName);
}

methodDeclaration = methodDeclaration.AddParameterListParameters([.. parameters]);
Expand Down Expand Up @@ -427,15 +435,15 @@ private TypeDeclarationSyntax GenerateMethods(List<SimpleBaseTypeSyntax> baseTyp
if (method.Parameters.Count == 0) {
bodyStatements.Add(SyntaxFactory.ParseStatement(internalFieldName + ".Invoke(null, null);"));
} else if (!anyOutParams) {
bodyStatements.Add(SyntaxFactory.ParseStatement(internalFieldName + ".Invoke(null, new object[] {" + string.Join(", ", method.Parameters.Select(param => param.Name)) + "});"));
bodyStatements.Add(SyntaxFactory.ParseStatement(internalFieldName + ".Invoke(null, new object[] {" + string.Join(", ", paramNames) + "});"));
} else {
bodyStatements.Add(SyntaxFactory.ParseStatement("throw new System.NotImplementedException();")); // TODO: Implement this
}
} else {
if (method.Parameters.Count == 0) {
bodyStatements.Add(SyntaxFactory.ParseStatement("return (" + returnType.GetText().ToString() + ")" + internalFieldName + ".InvokeBoxed(typeof(" + returnType.GetText().ToString() + "), null, null);"));
} else if (!anyOutParams) {
bodyStatements.Add(SyntaxFactory.ParseStatement("return (" + returnType.GetText().ToString() + ")" + internalFieldName + ".InvokeBoxed(typeof(" + returnType.GetText().ToString() + "), null, new object[] {" + string.Join(", ", method.Parameters.Select(param => param.Name)) + "});"));
bodyStatements.Add(SyntaxFactory.ParseStatement("return (" + returnType.GetText().ToString() + ")" + internalFieldName + ".InvokeBoxed(typeof(" + returnType.GetText().ToString() + "), null, new object[] {" + string.Join(", ", paramNames) + "});"));
} else {
bodyStatements.Add(SyntaxFactory.ParseStatement("throw new System.NotImplementedException();")); // TODO: Implement this
}
Expand Down

0 comments on commit cdb9c92

Please sign in to comment.