2019-04-12 17:43:11 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Generator
|
|
|
|
|
{
|
|
|
|
|
public partial class CodeWriter
|
|
|
|
|
{
|
2020-02-12 17:55:36 +03:00
|
|
|
|
public void GenerateVTableClass( SteamApiDefinition.Interface iface, string folder )
|
2019-04-12 17:43:11 +03:00
|
|
|
|
{
|
|
|
|
|
sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
WriteLine( $"using System;" );
|
|
|
|
|
WriteLine( $"using System.Runtime.InteropServices;" );
|
|
|
|
|
WriteLine( $"using System.Text;" );
|
2019-04-13 23:20:07 +03:00
|
|
|
|
WriteLine( $"using System.Threading.Tasks;" );
|
2019-04-16 16:38:10 +03:00
|
|
|
|
WriteLine( $"using Steamworks.Data;" );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
WriteLine();
|
|
|
|
|
|
|
|
|
|
WriteLine();
|
|
|
|
|
|
2019-04-16 16:38:10 +03:00
|
|
|
|
StartBlock( $"namespace Steamworks" );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
{
|
2020-02-12 17:55:36 +03:00
|
|
|
|
StartBlock( $"internal class {iface.Name} : SteamInterface" );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
{
|
|
|
|
|
WriteLine();
|
2020-02-19 14:14:50 +03:00
|
|
|
|
StartBlock( $"internal {iface.Name}( bool IsGameServer )" );
|
2020-02-11 14:28:30 +03:00
|
|
|
|
{
|
2020-02-19 14:14:50 +03:00
|
|
|
|
WriteLine( $"SetupInterface( IsGameServer );" );
|
2020-02-11 14:28:30 +03:00
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
WriteLine();
|
2019-04-12 17:43:11 +03:00
|
|
|
|
|
2020-02-19 14:14:50 +03:00
|
|
|
|
if ( iface.Accessors != null )
|
|
|
|
|
{
|
|
|
|
|
foreach ( var func in iface.Accessors )
|
|
|
|
|
{
|
2020-02-19 14:38:24 +03:00
|
|
|
|
WriteLine( $"[DllImport( Platform.LibraryName, EntryPoint = \"{func.Name_Flat}\", CallingConvention = Platform.CC)]" );
|
2020-02-19 14:14:50 +03:00
|
|
|
|
WriteLine( $"internal static extern IntPtr {func.Name_Flat}();" );
|
|
|
|
|
|
|
|
|
|
if ( func.Kind == "user" )
|
|
|
|
|
{
|
|
|
|
|
WriteLine( $"public override IntPtr GetUserInterfacePointer() => {func.Name_Flat}();" );
|
|
|
|
|
}
|
|
|
|
|
else if ( func.Kind == "gameserver" )
|
|
|
|
|
{
|
|
|
|
|
WriteLine( $"public override IntPtr GetServerInterfacePointer() => {func.Name_Flat}();" );
|
|
|
|
|
}
|
|
|
|
|
else if ( func.Kind == "global" )
|
|
|
|
|
{
|
|
|
|
|
WriteLine( $"public override IntPtr GetGlobalInterfacePointer() => {func.Name_Flat}();" );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception( $"unknown Kind {func.Kind}" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WriteLine();
|
|
|
|
|
WriteLine();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 17:55:36 +03:00
|
|
|
|
foreach ( var func in iface.Methods )
|
2019-04-12 17:43:11 +03:00
|
|
|
|
{
|
2020-02-12 17:55:36 +03:00
|
|
|
|
if ( Cleanup.IsDeprecated( $"{iface.Name}.{func.Name}" ) )
|
2019-04-27 16:25:54 +03:00
|
|
|
|
continue;
|
|
|
|
|
|
2020-02-12 17:55:36 +03:00
|
|
|
|
WriteFunction( iface, func );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
WriteLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
|
2020-02-12 17:55:36 +03:00
|
|
|
|
System.IO.File.WriteAllText( $"{folder}{iface.Name}.cs", sb.ToString() );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
}
|
2020-02-19 14:17:55 +03:00
|
|
|
|
|
2020-02-12 17:55:36 +03:00
|
|
|
|
private void WriteFunction( SteamApiDefinition.Interface iface, SteamApiDefinition.Interface.Method func )
|
2019-04-12 17:43:11 +03:00
|
|
|
|
{
|
2020-02-12 18:27:30 +03:00
|
|
|
|
var returnType = BaseType.Parse( func.ReturnType, null, func.CallResult );
|
2019-04-17 15:20:32 +03:00
|
|
|
|
returnType.Func = func.Name;
|
2019-04-12 17:43:11 +03:00
|
|
|
|
|
2020-02-10 22:21:52 +03:00
|
|
|
|
if ( func.Params == null )
|
2020-02-12 17:55:36 +03:00
|
|
|
|
func.Params = new SteamApiDefinition.Interface.Method.Param[0];
|
2020-02-10 22:21:52 +03:00
|
|
|
|
|
|
|
|
|
var args = func.Params.Select( x =>
|
2019-04-17 15:20:32 +03:00
|
|
|
|
{
|
2020-02-12 17:55:36 +03:00
|
|
|
|
var bt = BaseType.Parse( x.ParamType, x.ParamName );
|
2019-04-17 15:20:32 +03:00
|
|
|
|
bt.Func = func.Name;
|
|
|
|
|
return bt;
|
|
|
|
|
} ).ToArray();
|
2019-08-14 16:39:04 +03:00
|
|
|
|
|
|
|
|
|
for( int i=0; i<args.Length; i++ )
|
|
|
|
|
{
|
|
|
|
|
if ( args[i] is StringType )
|
|
|
|
|
{
|
2020-02-12 18:27:30 +03:00
|
|
|
|
if ( args[i + 1] is IntType || args[i + 1] is UIntType || args[i + 1] is UIntPtrType )
|
2019-08-14 16:39:04 +03:00
|
|
|
|
{
|
|
|
|
|
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() ) );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
|
2020-02-12 18:27:30 +03:00
|
|
|
|
if ( returnType is SteamApiCallType sap )
|
|
|
|
|
{
|
|
|
|
|
sap.CallResult = func.CallResult;
|
|
|
|
|
argstr = string.Join( ", ", args.Select( x => x.AsArgument().Replace( "ref ", " /* ref */ " ) ) );
|
|
|
|
|
}
|
2019-04-12 17:43:11 +03:00
|
|
|
|
|
|
|
|
|
WriteLine( $"#region FunctionMeta" );
|
|
|
|
|
|
2020-02-19 14:38:24 +03:00
|
|
|
|
WriteLine( $"[DllImport( Platform.LibraryName, EntryPoint = \"{func.FlatName}\", CallingConvention = Platform.CC)]" );
|
2020-02-10 22:21:52 +03:00
|
|
|
|
|
|
|
|
|
if ( returnType.ReturnAttribute != null )
|
2019-04-12 17:43:11 +03:00
|
|
|
|
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 )" ) );
|
2019-06-25 13:39:17 +03:00
|
|
|
|
|
2019-04-12 17:43:11 +03:00
|
|
|
|
WriteLine();
|
|
|
|
|
WriteLine( $"#endregion" );
|
|
|
|
|
|
2020-02-19 14:17:55 +03:00
|
|
|
|
|
|
|
|
|
if ( !string.IsNullOrEmpty( func.Desc ) )
|
|
|
|
|
{
|
|
|
|
|
WriteLine( "/// <summary>" );
|
|
|
|
|
WriteLine( $"/// {func.Desc}" );
|
|
|
|
|
WriteLine( "/// </summary>" );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-16 14:17:24 +03:00
|
|
|
|
StartBlock( $"internal {returnType.ReturnType} {func.Name}( {argstr} )".Replace( "( )", "()" ) );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
{
|
|
|
|
|
var callargs = string.Join( ", ", args.Select( x => x.AsCallArgument() ) );
|
|
|
|
|
|
2019-08-14 16:39:04 +03:00
|
|
|
|
//
|
|
|
|
|
// Code before any calls
|
|
|
|
|
//
|
|
|
|
|
foreach ( var arg in args )
|
|
|
|
|
{
|
|
|
|
|
if ( arg is StringType sb )
|
|
|
|
|
{
|
|
|
|
|
WriteLine( $"IntPtr mem{sb.VarName} = Helpers.TakeMemory();" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 17:47:56 +03:00
|
|
|
|
if ( returnType.IsVoid )
|
2019-04-12 17:43:11 +03:00
|
|
|
|
{
|
2019-04-17 15:36:11 +03:00
|
|
|
|
WriteLine( $"_{func.Name}( Self, {callargs} );".Replace( "( Self, )", "( Self )" ) );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-08-14 16:39:04 +03:00
|
|
|
|
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} );" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-12 17:43:11 +03:00
|
|
|
|
|
2019-08-14 16:39:04 +03:00
|
|
|
|
//
|
|
|
|
|
// Return
|
|
|
|
|
//
|
|
|
|
|
if ( !returnType.IsVoid )
|
|
|
|
|
{
|
|
|
|
|
WriteLine( returnType.Return( "returnValue" ) );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|