Facepunch.Steamworks/Generator/CodeWriter/ClassVTable.cs

151 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Generator
{
public partial class CodeWriter
{
public void GenerateVTableClass( string className, string filename )
{
sb = new StringBuilder();
WriteLine( $"using System;" );
WriteLine( $"using System.Runtime.InteropServices;" );
WriteLine( $"using System.Text;" );
WriteLine( $"using System.Threading.Tasks;" );
2019-04-16 16:38:10 +03:00
WriteLine( $"using Steamworks.Data;" );
WriteLine();
WriteLine();
2019-04-16 16:38:10 +03:00
StartBlock( $"namespace Steamworks" );
{
2020-02-10 22:21:52 +03:00
StartBlock( $"internal class {className} : SteamInterface" );
{
2020-02-10 23:22:17 +03:00
WriteLine( $"public override IntPtr GetInterfacePointer() => GetApi.{className.Substring( 1 )}();" );
WriteLine();
2020-02-10 22:21:52 +03:00
var functions = def.methods.Where( x => x.ClassName == className );
foreach ( var func in functions )
{
2020-02-10 22:21:52 +03:00
if ( Cleanup.IsDeprecated( $"{func.ClassName}.{func.Name}" ) )
2019-04-27 16:25:54 +03:00
continue;
2020-02-10 22:21:52 +03:00
WriteFunction( func );
WriteLine();
}
}
EndBlock();
}
EndBlock();
System.IO.File.WriteAllText( $"{filename}", sb.ToString() );
}
2020-02-10 22:21:52 +03:00
private void WriteFunction( SteamApiDefinition.MethodDef func )
{
var returnType = BaseType.Parse( func.ReturnType );
2019-04-17 15:20:32 +03:00
returnType.Func = func.Name;
2020-02-10 22:21:52 +03:00
if ( func.Params == null )
func.Params = new SteamApiDefinition.MethodDef.ParamType[0];
var args = func.Params.Select( x =>
2019-04-17 15:20:32 +03:00
{
2020-02-10 22:21:52 +03:00
var bt = BaseType.Parse( x.Type, x.Name );
2019-04-17 15:20:32 +03:00
bt.Func = func.Name;
return bt;
} ).ToArray();
for( int i=0; i<args.Length; i++ )
{
if ( args[i] is StringType )
{
if ( args[i + 1] is IntType || args[i + 1] is UIntType )
{
if ( args[i + 1].Ref == string.Empty )
{
args[i + 1] = new ConstValueType( args[i + 1], "(1024 * 32)" );
}
}
else
{
throw new System.Exception( $"String Builder Next Type Is {args[i+1].GetType()}" );
}
}
}
var argstr = string.Join( ", ", args.Where( x => !x.ShouldSkipAsArgument ).Select( x => x.AsArgument() ) ); ;
var delegateargstr = string.Join( ", ", args.Select( x => x.AsNativeArgument() ) );
if ( returnType is SteamApiCallType sap )
{
sap.CallResult = func.CallResult;
2019-05-01 23:33:10 +03:00
argstr = string.Join( ", ", args.Select( x => x.AsArgument().Replace( "ref ", " /* ref */ " ) ) );
}
WriteLine( $"#region FunctionMeta" );
2020-02-10 22:21:52 +03:00
WriteLine( $"[DllImport( Platform.LibraryName, EntryPoint = \"SteamAPI_{func.ClassName}_{func.Name}\")]" );
if ( returnType.ReturnAttribute != null )
WriteLine( returnType.ReturnAttribute );
2020-02-10 22:21:52 +03:00
WriteLine( $"private static extern {returnType.TypeNameFrom} _{func.Name}( IntPtr self, {delegateargstr} );".Replace( "( IntPtr self, )", "( IntPtr self )" ) );
WriteLine();
WriteLine( $"#endregion" );
2019-04-16 14:17:24 +03:00
StartBlock( $"internal {returnType.ReturnType} {func.Name}( {argstr} )".Replace( "( )", "()" ) );
{
var callargs = string.Join( ", ", args.Select( x => x.AsCallArgument() ) );
//
// Code before any calls
//
foreach ( var arg in args )
{
if ( arg is StringType sb )
{
WriteLine( $"IntPtr mem{sb.VarName} = Helpers.TakeMemory();" );
}
}
if ( returnType.IsVoid )
{
2019-04-17 15:36:11 +03:00
WriteLine( $"_{func.Name}( Self, {callargs} );".Replace( "( Self, )", "( Self )" ) );
}
else
{
WriteLine( $"var returnValue = _{func.Name}( Self, {callargs} );".Replace( "( Self, )", "( Self )" ) );
}
//
// Code after the call
//
foreach ( var arg in args )
{
if ( arg is StringType sb )
{
WriteLine( $"{sb.VarName} = Helpers.MemoryToString( mem{sb.VarName} );" );
}
}
//
// Return
//
if ( !returnType.IsVoid )
{
WriteLine( returnType.Return( "returnValue" ) );
}
}
EndBlock();
}
}
}