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
|
|
|
|
|
{
|
|
|
|
|
public void GenerateVTableClass( string className, string filename )
|
|
|
|
|
{
|
|
|
|
|
var clss = Parser.Classes.Single( x => x.Name == className );
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-04-16 16:54:05 +03:00
|
|
|
|
StartBlock( $"internal class {clss.Name} : SteamInterface" );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
{
|
2019-04-15 22:54:50 +03:00
|
|
|
|
StartBlock( $"public {clss.Name}( bool server = false ) : base( server )" );
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
WriteLine();
|
2019-04-13 00:53:46 +03:00
|
|
|
|
|
|
|
|
|
WriteLine( $"public override string InterfaceName => \"{clss.InterfaceString}\";" );
|
|
|
|
|
WriteLine();
|
|
|
|
|
|
2019-04-12 17:43:11 +03:00
|
|
|
|
WriteFunctionPointerReader( clss );
|
|
|
|
|
|
|
|
|
|
WriteLine();
|
|
|
|
|
|
|
|
|
|
foreach ( var func in clss.Functions )
|
|
|
|
|
{
|
2019-04-27 16:25:54 +03:00
|
|
|
|
if ( Cleanup.IsDeprecated( $"{clss.Name}.{func.Name}" ) )
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
2019-04-12 17:43:11 +03:00
|
|
|
|
WriteFunction( clss, func );
|
|
|
|
|
WriteLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
|
|
|
|
|
System.IO.File.WriteAllText( $"{filename}", sb.ToString() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteFunctionPointerReader( CodeParser.Class clss )
|
|
|
|
|
{
|
2019-04-17 15:53:54 +03:00
|
|
|
|
// TODO - we'll probably have to do this PER platform
|
|
|
|
|
|
|
|
|
|
int[] locations = new int[clss.Functions.Count];
|
|
|
|
|
|
|
|
|
|
for ( int i = 0; i < clss.Functions.Count; i++ )
|
|
|
|
|
{
|
|
|
|
|
locations[i] = i * 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// MSVC switches the order in the vtable of overloaded functions
|
|
|
|
|
// I'm not going to try to try to work out how to order shit
|
|
|
|
|
// so lets just manually fix shit here
|
|
|
|
|
//
|
|
|
|
|
if ( clss.Name == "ISteamUserStats" )
|
|
|
|
|
{
|
|
|
|
|
Swap( clss, "GetStat1", "GetStat2", locations );
|
|
|
|
|
Swap( clss, "SetStat1", "SetStat2", locations );
|
|
|
|
|
Swap( clss, "GetUserStat1", "GetUserStat2", locations );
|
|
|
|
|
Swap( clss, "GetGlobalStat1", "GetGlobalStat2", locations );
|
|
|
|
|
Swap( clss, "GetGlobalStatHistory1", "GetGlobalStatHistory2", locations );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 18:41:06 +03:00
|
|
|
|
if ( clss.Name == "ISteamUGC" )
|
|
|
|
|
{
|
|
|
|
|
Swap( clss, "CreateQueryAllUGCRequest1", "CreateQueryAllUGCRequest2", locations );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-04-12 17:43:11 +03:00
|
|
|
|
StartBlock( $"public override void InitInternals()" );
|
|
|
|
|
{
|
|
|
|
|
for (int i=0; i< clss.Functions.Count; i++ )
|
|
|
|
|
{
|
|
|
|
|
var func = clss.Functions[i];
|
2019-04-29 17:47:56 +03:00
|
|
|
|
var returnType = BaseType.Parse( func.ReturnType );
|
2019-04-27 16:25:54 +03:00
|
|
|
|
|
|
|
|
|
if ( Cleanup.IsDeprecated( $"{clss.Name}.{func.Name}" ) )
|
|
|
|
|
{
|
|
|
|
|
WriteLine( $" // {func.Name} is deprecated - {locations[i]}" );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
WriteLine( $"_{func.Name} = Marshal.GetDelegateForFunctionPointer<F{func.Name}>( Marshal.ReadIntPtr( VTable, {locations[i]}) );" );
|
2019-04-29 17:47:56 +03:00
|
|
|
|
|
|
|
|
|
if ( returnType.IsReturnedWeird )
|
|
|
|
|
{
|
|
|
|
|
WriteLine( $"_{func.Name}_Windows = Marshal.GetDelegateForFunctionPointer<F{func.Name}_Windows>( Marshal.ReadIntPtr( VTable, {locations[i]}) );" );
|
|
|
|
|
}
|
2019-04-27 16:25:54 +03:00
|
|
|
|
}
|
2019-04-12 17:43:11 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 15:53:54 +03:00
|
|
|
|
private void Swap( CodeParser.Class clss, string v1, string v2, int[] locations )
|
|
|
|
|
{
|
|
|
|
|
var a = clss.Functions.IndexOf( clss.Functions.Single( x => x.Name == v1 ) );
|
|
|
|
|
var b = clss.Functions.IndexOf( clss.Functions.Single( x => x.Name == v2 ) );
|
|
|
|
|
|
|
|
|
|
var s = locations[a];
|
|
|
|
|
locations[a] = locations[b];
|
|
|
|
|
locations[b] = s;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 17:43:11 +03:00
|
|
|
|
private void WriteFunction( CodeParser.Class clss, CodeParser.Class.Function func )
|
|
|
|
|
{
|
2019-04-27 16:25:54 +03:00
|
|
|
|
|
2019-04-12 17:43:11 +03:00
|
|
|
|
var returnType = BaseType.Parse( func.ReturnType );
|
2019-04-17 15:20:32 +03:00
|
|
|
|
returnType.Func = func.Name;
|
2019-04-12 17:43:11 +03:00
|
|
|
|
|
2019-04-17 15:20:32 +03:00
|
|
|
|
var args = func.Arguments.Select( x =>
|
|
|
|
|
{
|
|
|
|
|
var bt = BaseType.Parse( x.Value, x.Key );
|
|
|
|
|
bt.Func = func.Name;
|
|
|
|
|
return bt;
|
|
|
|
|
} ).ToArray();
|
2019-04-12 17:43:11 +03:00
|
|
|
|
var argstr = string.Join( ", ", args.Select( x => x.AsArgument() ) );
|
2019-04-15 17:19:33 +03:00
|
|
|
|
var delegateargstr = string.Join( ", ", args.Select( x => x.AsArgument() ) );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
|
2019-04-13 20:46:39 +03:00
|
|
|
|
if ( returnType is SteamApiCallType sap )
|
|
|
|
|
{
|
|
|
|
|
sap.CallResult = func.CallResult;
|
|
|
|
|
}
|
2019-04-12 17:43:11 +03:00
|
|
|
|
|
|
|
|
|
WriteLine( $"#region FunctionMeta" );
|
|
|
|
|
|
|
|
|
|
WriteLine( $"[UnmanagedFunctionPointer( CallingConvention.ThisCall )]" );
|
|
|
|
|
|
|
|
|
|
if ( returnType.ReturnAttribute != null)
|
|
|
|
|
WriteLine( returnType.ReturnAttribute );
|
|
|
|
|
|
2019-04-29 17:47:56 +03:00
|
|
|
|
WriteLine( $"private delegate {returnType.TypeNameFrom} F{func.Name}( IntPtr self, {delegateargstr} );".Replace( "( IntPtr self, )", "( IntPtr self )" ) );
|
2019-04-17 15:36:11 +03:00
|
|
|
|
WriteLine( $"private F{func.Name} _{func.Name};" );
|
2019-04-29 17:47:56 +03:00
|
|
|
|
|
|
|
|
|
if ( returnType.IsReturnedWeird )
|
|
|
|
|
{
|
|
|
|
|
var windelargs = $"ref {returnType.TypeName} retVal, {delegateargstr}".Trim( ',', ' ' );
|
|
|
|
|
|
|
|
|
|
WriteLine( $"private delegate void F{func.Name}_Windows( IntPtr self, {windelargs} );".Replace( "( IntPtr self, )", "( IntPtr self )" ) );
|
|
|
|
|
WriteLine( $"private F{func.Name}_Windows _{func.Name}_Windows;" );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 17:43:11 +03:00
|
|
|
|
WriteLine();
|
|
|
|
|
WriteLine( $"#endregion" );
|
|
|
|
|
|
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() ) );
|
|
|
|
|
|
|
|
|
|
if ( returnType.IsReturnedWeird )
|
|
|
|
|
{
|
2019-04-29 17:47:56 +03:00
|
|
|
|
StartBlock( "if ( Config.Os == OsType.Windows )" );
|
|
|
|
|
{
|
|
|
|
|
WriteLine( $"var retVal = default( {returnType.TypeName} );" );
|
|
|
|
|
WriteLine( $"_{func.Name}_Windows( Self, ref retVal, {callargs} );".Replace( ", );", " );" ) );
|
|
|
|
|
WriteLine( $"{returnType.Return( "retVal" )}" );
|
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
WriteLine();
|
2019-04-12 17:43:11 +03:00
|
|
|
|
}
|
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-04-17 15:36:11 +03:00
|
|
|
|
var v = $"_{func.Name}( Self, {callargs} )".Replace( "( Self, )", "( Self )" );
|
2019-04-12 17:43:11 +03:00
|
|
|
|
|
|
|
|
|
WriteLine( returnType.Return( v ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
EndBlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|