@@ -26,6 +26,8 @@ public CppClassGenerator(SourceText text, string className, Config config, strin
2626 List < CMethodGenerator > methods = [ ] ;
2727 StringBuilder ? builder = null ;
2828
29+ Dictionary < string , string > typeDefinitions = [ ] ;
30+
2931 foreach ( var line in text . Lines )
3032 {
3133 var lineText = line . ToString ( ) . Trim ( ) ;
@@ -46,6 +48,25 @@ public CppClassGenerator(SourceText text, string className, Config config, strin
4648 {
4749 builder . Append ( lineText . Trim ( ) ) ;
4850 }
51+ else if ( IsStartWith ( ref lineText , "typedef" ) )
52+ {
53+ var types = lineText . Substring ( 0 , lineText . Length - 1 ) . Split ( ' ' ) ;
54+ if ( types . Length is not 2 ) continue ;
55+ var replacedName = BaseParameterGenerator . GetTypeNameFromCpp ( types [ 0 ] , [ ] ) ;
56+ if ( replacedName
57+ is not "sbyte" and not "byte"
58+ and not "short" and not "ushort"
59+ and not "int" and not "uint"
60+ and not "long" and not "ulong"
61+ and not "global::System.IntPtr" and not "global::System.UIntPtr"
62+ and not "char"
63+ and not "float" and not "double" )
64+ {
65+ continue ;
66+ }
67+
68+ typeDefinitions [ types [ 1 ] ] = replacedName ;
69+ }
4970 }
5071
5172 if ( builder is not null ) CheckBuilder ( builder ) ;
@@ -60,7 +81,7 @@ void CheckBuilder(StringBuilder stringBuilder)
6081 var index = str . IndexOf ( '{' ) ;
6182 if ( index < 0 ) return ;
6283 var methodDeclare = str . Substring ( 0 , index ) ;
63- methods . Add ( new CMethodGenerator ( methodDeclare , className , privateIt , allClassNames ) ) ;
84+ methods . Add ( new CMethodGenerator ( methodDeclare , className , privateIt , allClassNames , typeDefinitions ) ) ;
6485 }
6586 }
6687
@@ -81,7 +102,8 @@ public ClassDeclarationSyntax Generate()
81102 ] ) )
82103 . WithAttributeLists ( [ GeneratedCodeAttribute ( typeof ( CppClassGenerator ) ) ] )
83104 . WithModifiers (
84- TokenList ( Token ( SyntaxKind . InternalKeyword ) , Token ( SyntaxKind . ReadOnlyKeyword ) ) ) ,
105+ TokenList ( Token ( _config . IsInternal ? SyntaxKind . InternalKeyword : SyntaxKind . PublicKeyword ) ,
106+ Token ( SyntaxKind . ReadOnlyKeyword ) ) ) ,
85107
86108 StructDeclaration ( "Data" )
87109 . WithAttributeLists (
@@ -164,7 +186,7 @@ private class CMethodGenerator
164186 private readonly IReadOnlyList < BaseParameterGenerator > _parameters ;
165187
166188 public CMethodGenerator ( string methodDeclare , string className , bool isPrivate ,
167- string [ ] allClassesName )
189+ string [ ] allClassesName , Dictionary < string , string > parameterTypeReplace )
168190 {
169191 _className = className ;
170192 var startIndex = methodDeclare . IndexOf ( '(' ) ;
@@ -174,7 +196,8 @@ public CMethodGenerator(string methodDeclare, string className, bool isPrivate,
174196 var endIndex = methodDeclare . LastIndexOf ( ')' ) ;
175197
176198 _parameters = BaseParameterGenerator . GenerateParameters ( _methodName ,
177- methodDeclare . Substring ( startIndex + 1 , endIndex - startIndex - 1 ) , allClassesName ) . ToArray ( ) ;
199+ methodDeclare . Substring ( startIndex + 1 , endIndex - startIndex - 1 ) , allClassesName ,
200+ parameterTypeReplace ) . ToArray ( ) ;
178201
179202 _isPrivate = isPrivate || _parameters . Any ( p => p . HasHandle ) ;
180203 }
@@ -191,21 +214,23 @@ public IEnumerable<MemberDeclarationSyntax> Generate()
191214 .._parameters . Take ( _parameters . Count - 1 ) . Select ( p => p . GenerateParameter ( ) )
192215 ] ) )
193216 . WithBody ( Block (
194- GetRunMethod ( Block (
195- GetFunctionPointer ( _parameters . Select ( p => FunctionPointerParameter ( p . InnerType ) ) ) ,
196- FixedStatement ( VariableDeclaration ( PointerType ( PointerType ( IdentifierName ( "Data" ) ) ) )
197- . WithVariables ( [
198- VariableDeclarator ( Identifier ( "ptr" ) )
199- . WithInitializer ( EqualsValueClause ( PrefixUnaryExpression (
200- SyntaxKind . AddressOfExpression , IdentifierName ( "Ptr" ) ) ) )
201- ] ) ,
202- ReturnStatement ( InvocationExpression ( IdentifierName ( "__method" ) )
203- . WithArgumentList ( ArgumentList (
204- [
205- .._parameters . Take ( _parameters . Count - 1 ) . Select ( p => p . GenerateArgument ( ) ) ,
206- Argument ( IdentifierName ( "ptr" ) )
207- ] ) ) ) )
208- ) ) ) ) ;
217+ GetFunctionPointer ( _parameters . Select ( p => FunctionPointerParameter ( p . InnerType ) ) ) ,
218+ FixedStatement ( VariableDeclaration ( PointerType ( PointerType ( IdentifierName ( "Data" ) ) ) )
219+ . WithVariables ( [
220+ VariableDeclarator ( Identifier ( "ptr" ) )
221+ . WithInitializer ( EqualsValueClause ( PrefixUnaryExpression (
222+ SyntaxKind . AddressOfExpression , IdentifierName ( "Ptr" ) ) ) )
223+ ] ) ,
224+ ExpressionStatement ( InvocationExpression ( IdentifierName ( "ThrowIfError" ) )
225+ . WithArgumentList ( ArgumentList ( [
226+ Argument ( InvocationExpression ( IdentifierName ( "__method" ) )
227+ . WithArgumentList ( ArgumentList (
228+ [
229+ .._parameters . Take ( _parameters . Count - 1 ) . Select ( p => p . GenerateArgument ( ) ) ,
230+ Argument ( IdentifierName ( "ptr" ) )
231+ ] ) ) )
232+ ] ) ) ) )
233+ ) ) ;
209234 yield break ;
210235 }
211236
@@ -215,14 +240,16 @@ public IEnumerable<MemberDeclarationSyntax> Generate()
215240 . WithModifiers ( TokenList ( Token ( SyntaxKind . ProtectedKeyword ) , Token ( SyntaxKind . OverrideKeyword ) ) )
216241 . WithAttributeLists ( [ GeneratedCodeAttribute ( typeof ( CMethodGenerator ) ) ] )
217242 . WithBody ( Block (
218- GetRunMethod ( Block (
219- GetFunctionPointer ( _parameters . Select ( p => FunctionPointerParameter ( p . InnerType ) ) ) ,
220- ReturnStatement ( InvocationExpression ( IdentifierName ( "__method" ) )
221- . WithArgumentList ( ArgumentList (
222- [
223- Argument ( IdentifierName ( "Ptr" ) )
224- ] ) ) )
225- ) ) ) ) ;
243+ GetFunctionPointer ( _parameters . Select ( p => FunctionPointerParameter ( p . InnerType ) ) ) ,
244+ ExpressionStatement ( InvocationExpression ( IdentifierName ( "ThrowIfError" ) )
245+ . WithArgumentList ( ArgumentList ( [
246+ Argument ( InvocationExpression ( IdentifierName ( "__method" ) )
247+ . WithArgumentList ( ArgumentList (
248+ [
249+ Argument ( IdentifierName ( "Ptr" ) ) ,
250+ ] ) ) )
251+ ] ) ) )
252+ ) ) ;
226253 yield break ;
227254 }
228255
@@ -242,28 +269,22 @@ public IEnumerable<MemberDeclarationSyntax> Generate()
242269 . WithBody ( Block ( ( StatementSyntax [ ] )
243270 [
244271 .._parameters . Skip ( 1 ) . Where ( p => p . IsRefOrOut ) . SelectMany ( p => p . GenerateLocalDeclaration ( ) ) ,
245- GetRunMethod ( Block (
246- GetFunctionPointer ( _parameters . Select ( p => FunctionPointerParameter ( p . InnerType ) ) ) ,
247- ReturnStatement ( InvocationExpression ( IdentifierName ( "__method" ) )
248- . WithArgumentList ( ArgumentList (
249- [
250- Argument ( IdentifierName ( "Ptr" ) ) ,
251- .._parameters . Skip ( 1 ) . Select ( p => p . GenerateArgument ( ) )
252- ] ) ) ) ) ) ,
272+
273+ GetFunctionPointer ( _parameters . Select ( ( p , i ) =>
274+ FunctionPointerParameter ( i == 0 ? p . InnerType : p . InnerCppType ) ) ) ,
275+ ExpressionStatement ( InvocationExpression ( IdentifierName ( "ThrowIfError" ) )
276+ . WithArgumentList ( ArgumentList ( [
277+ Argument ( InvocationExpression ( IdentifierName ( "__method" ) )
278+ . WithArgumentList ( ArgumentList (
279+ [
280+ Argument ( IdentifierName ( "Ptr" ) ) ,
281+ .._parameters . Skip ( 1 ) . Select ( p => p . GenerateArgument ( ) )
282+ ] ) ) )
283+ ] ) ) ) ,
253284 .._parameters . Skip ( 1 ) . Where ( p => p . IsRefOrOut ) . Select ( p => p . GenerateAssignment ( ) )
254285 ] ) ) ;
255286 yield break ;
256287
257- ExpressionStatementSyntax GetRunMethod ( BlockSyntax block )
258- {
259- return ExpressionStatement ( InvocationExpression ( IdentifierName ( "SafeRun" ) )
260- . WithArgumentList ( ArgumentList (
261- [
262- Argument ( SimpleLambdaExpression ( Parameter ( Identifier ( "loader" ) ) )
263- . WithBlock ( block ) )
264- ] ) ) ) ;
265- }
266-
267288 LocalDeclarationStatementSyntax GetFunctionPointer ( IEnumerable < FunctionPointerParameterSyntax > parameters )
268289 {
269290 return LocalDeclarationStatement ( VariableDeclaration ( IdentifierName ( "var" ) )
@@ -283,9 +304,7 @@ LocalDeclarationStatementSyntax GetFunctionPointer(IEnumerable<FunctionPointerPa
283304 ..parameters ,
284305 FunctionPointerParameter ( IdentifierName ( "global::System.IntPtr" ) )
285306 ] ) ) ,
286- InvocationExpression ( MemberAccessExpression (
287- SyntaxKind . SimpleMemberAccessExpression ,
288- IdentifierName ( "loader" ) , IdentifierName ( "GetFunctionPointer" ) ) )
307+ InvocationExpression ( IdentifierName ( "GetFunctionPointer" ) )
289308 . WithArgumentList ( ArgumentList (
290309 [
291310 Argument ( LiteralExpression ( SyntaxKind . StringLiteralExpression ,
0 commit comments