Facepunch.Steamworks/Generator/CodeWriter/Struct.cs

345 lines
15 KiB
C#
Raw Normal View History

2016-10-25 12:29:35 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Generator
{
2016-10-25 18:11:29 +03:00
public partial class CodeWriter
2016-10-25 12:29:35 +03:00
{
2016-10-29 15:02:36 +03:00
public class TypeDef
{
public string Name;
public string NativeType;
public string ManagedType;
}
private Dictionary<string, TypeDef> TypeDefs = new Dictionary<string, TypeDef>();
2016-10-29 14:49:36 +03:00
//
// Don't give a fuck about these classes, they just cause us trouble
//
public readonly static string[] SkipStructs = new string[]
{
"CSteamID",
"CSteamAPIContext",
"CCallResult",
"CCallback",
2016-10-30 23:52:42 +03:00
"ValvePackingSentinel_t",
"CCallbackBase"
2016-10-29 14:49:36 +03:00
};
2016-10-25 12:29:35 +03:00
void Structs()
{
foreach ( var c in def.structs )
{
2016-10-29 14:49:36 +03:00
if ( SkipStructs.Contains( c.Name ) )
2016-10-25 12:29:35 +03:00
continue;
if ( c.Name.Contains( "::" ) )
continue;
int defaultPack = 8;
if ( c.Fields.Any( x => x.Type.Contains( "class CSteamID" ) ) )
2016-10-25 13:51:24 +03:00
defaultPack = 4;
2016-10-25 12:29:35 +03:00
2016-10-25 17:57:46 +03:00
//
2016-10-29 14:49:36 +03:00
// Main struct
2016-10-25 17:57:46 +03:00
//
2016-10-29 14:49:36 +03:00
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = {defaultPack} )]" );
StartBlock( $"public struct {c.Name}" );
2016-10-25 12:29:35 +03:00
{
2016-10-29 22:28:16 +03:00
if ( !string.IsNullOrEmpty( c.CallbackId ) )
{
WriteLine( "public const int CallbackId = " + c.CallbackId + ";" );
}
2016-10-29 14:49:36 +03:00
//
// The fields
//
StructFields( c.Fields );
WriteLine();
WriteLine( "//" );
2016-10-31 14:46:53 +03:00
WriteLine( "// Read this struct from a pointer, usually from Native. It will automatically do the awesome stuff." );
2016-10-29 14:49:36 +03:00
WriteLine( "//" );
StartBlock( $"public static {c.Name} FromPointer( IntPtr p )" );
{
2016-10-31 14:46:53 +03:00
WriteLine( $"if ( Platform.PackSmall ) return ({c.Name}.PackSmall) Marshal.PtrToStructure( p, typeof({c.Name}.PackSmall) );" );
2016-10-29 14:49:36 +03:00
WriteLine( $"return ({c.Name}) Marshal.PtrToStructure( p, typeof({c.Name}) );" );
}
EndBlock();
if ( defaultPack == 8 )
defaultPack = 4;
//
// Small packed struct (for osx, linux)
//
WriteLine();
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = {defaultPack} )]" );
StartBlock( $"public struct PackSmall" );
{
StructFields( c.Fields );
//
// Implicit convert from PackSmall to regular
//
WriteLine();
WriteLine( "//" );
WriteLine( $"// Easily convert from PackSmall to {c.Name}" );
WriteLine( "//" );
StartBlock( $"public static implicit operator {c.Name} ( {c.Name}.PackSmall d )" );
{
StartBlock( $"return new {c.Name}()" );
{
foreach ( var f in c.Fields )
{
WriteLine( $"{CleanMemberName( f.Name )} = d.{CleanMemberName( f.Name )}," );
}
}
EndBlock( ";" );
}
EndBlock();
}
EndBlock();
2016-10-25 12:29:35 +03:00
2016-10-31 14:46:53 +03:00
if ( c.IsCallResult )
{
CallResult( c );
}
else if ( !string.IsNullOrEmpty( c.CallbackId ) )
2016-10-30 23:52:42 +03:00
{
Callback( c );
}
2016-10-29 14:49:36 +03:00
}
2016-10-25 12:29:35 +03:00
EndBlock();
WriteLine();
}
}
private void StructFields( SteamApiDefinition.StructDef.StructFields[] fields )
{
foreach ( var m in fields )
{
var t = ToManagedType( m.Type );
if ( TypeDefs.ContainsKey( t ) )
{
t = TypeDefs[t].ManagedType;
}
2016-10-25 13:51:24 +03:00
if ( t == "bool" )
{
WriteLine( "[MarshalAs(UnmanagedType.I1)]" );
}
2016-10-25 12:29:35 +03:00
if ( t.StartsWith( "char " ) && t.Contains( "[" ) )
{
var num = t.Replace( "char", "" ).Trim( '[', ']', ' ' );
t = "string";
WriteLine( $"[MarshalAs(UnmanagedType.ByValTStr, SizeConst = {num})]" );
}
2016-10-26 18:10:20 +03:00
if ( t.StartsWith( "uint8 " ) && t.Contains( "[" ) )
{
var num = t.Replace( "uint8", "" ).Trim( '[', ']', ' ' );
t = "char";
WriteLine( $"[MarshalAs(UnmanagedType.ByValTStr, SizeConst = {num})]" );
}
2016-10-25 12:29:35 +03:00
if ( t.StartsWith( "CSteamID " ) && t.Contains( "[" ) )
{
var num = t.Replace( "CSteamID", "" ).Trim( '[', ']', ' ' );
t = $"ulong[]";
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.U8)]" );
}
if ( t.StartsWith( "PublishedFileId_t " ) && t.Contains( "[" ) )
{
var num = t.Replace( "PublishedFileId_t", "" ).Trim( '[', ']', ' ' );
t = $"ulong[]";
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.U8)]" );
}
if ( t.StartsWith( "uint32 " ) && t.Contains( "[" ) )
{
var num = t.Replace( "uint32", "" ).Trim( '[', ']', ' ' );
t = $"uint[]";
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.U8)]" );
}
if ( t.StartsWith( "float " ) && t.Contains( "[" ) )
{
var num = t.Replace( "float", "" ).Trim( '[', ']', ' ' );
t = $"float[]";
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.R4)]" );
}
if ( t == "const char **" )
{
t = "IntPtr";
}
2016-10-25 17:57:46 +03:00
WriteLine( $"public {t} {CleanMemberName( m.Name )}; // {m.Name} {m.Type}" );
2016-10-25 12:29:35 +03:00
}
}
2016-10-30 23:52:42 +03:00
private void Callback( SteamApiDefinition.StructDef c )
{
WriteLine();
StartBlock( $"public static void RegisterCallback( Facepunch.Steamworks.BaseSteamworks steamworks, Action<{c.Name}, bool> CallbackFunction )" );
{
2016-10-31 14:46:53 +03:00
WriteLine( $"var handle = new CallbackHandle();" );
WriteLine( $"handle.steamworks = steamworks;" );
2016-10-30 23:52:42 +03:00
WriteLine( $"" );
WriteLine( "//" );
WriteLine( "// Create the functions we need for the vtable" );
WriteLine( "//" );
WriteLine( $"Callback.Result funcA = ( _, p ) => {{ CallbackFunction( FromPointer( p ), false ); }};" );
2016-10-31 14:46:53 +03:00
WriteLine( $"Callback.ResultWithInfo funcB = ( _, p, bIOFailure, hSteamAPICall ) => {{ CallbackFunction( FromPointer( p ), bIOFailure ); }};" );
2016-10-30 23:52:42 +03:00
WriteLine( $"Callback.GetSize funcC = ( _ ) => {{ return Marshal.SizeOf( typeof( {c.Name} ) ); }};" );
WriteLine();
WriteLine( "//" );
WriteLine( "// If this platform is PackSmall, use PackSmall versions of everything instead" );
WriteLine( "//" );
StartBlock( "if ( Platform.PackSmall )" );
{
WriteLine( $"funcC = ( _ ) => {{ return Marshal.SizeOf( typeof( PackSmall ) ); }};" );
}
EndBlock();
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Allocate a handle to each function, so they don't get disposed" );
WriteLine( "//" );
WriteLine( "handle.FuncA = GCHandle.Alloc( funcA );" );
WriteLine( "handle.FuncB = GCHandle.Alloc( funcB );" );
WriteLine( "handle.FuncC = GCHandle.Alloc( funcC );" );
WriteLine();
WriteLine( "//" );
WriteLine( "// Create the VTable by manually allocating the memory and copying across" );
WriteLine( "//" );
WriteLine( "handle.vTablePtr = Marshal.AllocHGlobal( Marshal.SizeOf( typeof( Callback.VTable ) ) );" );
StartBlock( "var vTable = new Callback.VTable()" );
{
WriteLine( "ResultA = Marshal.GetFunctionPointerForDelegate( funcB ), // The order of these functions is a point of contention" );
WriteLine( "ResultB = Marshal.GetFunctionPointerForDelegate( funcA ), // Doesn't seem to matter win64, but win32 crashes if WithInfo not first" );
WriteLine( "GetSize = Marshal.GetFunctionPointerForDelegate( funcC ), // Which is the opposite of how they are in code, but whatever works" );
}
EndBlock( ";" );
WriteLine( "Marshal.StructureToPtr( vTable, handle.vTablePtr, false );" );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Create the callback object" );
WriteLine( "//" );
WriteLine( $"var cb = new Callback();" );
WriteLine( $"cb.vTablePtr = handle.vTablePtr;" );
WriteLine( $"cb.CallbackFlags = steamworks.IsGameServer ? (byte) SteamNative.Callback.Flags.GameServer : (byte) 0;" );
WriteLine( $"cb.CallbackId = CallbackId;" );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Pin the callback, so it doesn't get garbage collected and we can pass the pointer to native" );
WriteLine( "//" );
WriteLine( $"handle.PinnedCallback = GCHandle.Alloc( cb, GCHandleType.Pinned );" );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Register the callback with Steam" );
WriteLine( "//" );
WriteLine( $"steamworks.native.api.SteamAPI_RegisterCallback( handle.PinnedCallback.AddrOfPinnedObject(), CallbackId );" );
WriteLine();
WriteLine( "steamworks.RegisterCallbackHandle( handle );" );
}
EndBlock();
}
2016-10-31 14:46:53 +03:00
private void CallResult( SteamApiDefinition.StructDef c )
{
WriteLine();
StartBlock( $"public static CallbackHandle CallResult( Facepunch.Steamworks.BaseSteamworks steamworks, SteamAPICall_t call, Action<{c.Name}, bool> CallbackFunction )" );
{
WriteLine( $"var handle = new CallbackHandle();" );
WriteLine( $"handle.steamworks = steamworks;" );
WriteLine( $"handle.callHandle = call;" );
WriteLine( $"" );
WriteLine( "//" );
WriteLine( "// Create the functions we need for the vtable" );
WriteLine( "//" );
WriteLine( $"Callback.Result funcA = ( _, p ) => {{ CallbackFunction( FromPointer( p ), false ); handle.UnregisterCallResult(); }};" );
WriteLine( $"Callback.ResultWithInfo funcB = ( _, p, bIOFailure, hSteamAPICall ) => {{ CallbackFunction( FromPointer( p ), bIOFailure ); handle.UnregisterCallResult(); }};" );
WriteLine( $"Callback.GetSize funcC = ( _ ) => {{ return Marshal.SizeOf( typeof( {c.Name} ) ); }};" );
WriteLine();
WriteLine( "//" );
WriteLine( "// If this platform is PackSmall, use PackSmall versions of everything instead" );
WriteLine( "//" );
StartBlock( "if ( Platform.PackSmall )" );
{
WriteLine( $"funcC = ( _ ) => {{ return Marshal.SizeOf( typeof( PackSmall ) ); }};" );
}
EndBlock();
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Allocate a handle to each function, so they don't get disposed" );
WriteLine( "//" );
WriteLine( "handle.FuncA = GCHandle.Alloc( funcA );" );
WriteLine( "handle.FuncB = GCHandle.Alloc( funcB );" );
WriteLine( "handle.FuncC = GCHandle.Alloc( funcC );" );
WriteLine();
WriteLine( "//" );
WriteLine( "// Create the VTable by manually allocating the memory and copying across" );
WriteLine( "//" );
WriteLine( "handle.vTablePtr = Marshal.AllocHGlobal( Marshal.SizeOf( typeof( Callback.VTable ) ) );" );
StartBlock( "var vTable = new Callback.VTable()" );
{
WriteLine( "ResultA = Marshal.GetFunctionPointerForDelegate( funcB ), // The order of these functions is a point of contention" );
WriteLine( "ResultB = Marshal.GetFunctionPointerForDelegate( funcA ), // Doesn't seem to matter win64, but win32 crashes if WithInfo not first" );
WriteLine( "GetSize = Marshal.GetFunctionPointerForDelegate( funcC ), // Which is the opposite of how they are in code, but whatever works" );
}
EndBlock( ";" );
WriteLine( "Marshal.StructureToPtr( vTable, handle.vTablePtr, false );" );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Create the callback object" );
WriteLine( "//" );
WriteLine( $"var cb = new Callback();" );
WriteLine( $"cb.vTablePtr = handle.vTablePtr;" );
WriteLine( $"cb.CallbackFlags = steamworks.IsGameServer ? (byte) SteamNative.Callback.Flags.GameServer : (byte) 0;" );
WriteLine( $"cb.CallbackId = CallbackId;" );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Pin the callback, so it doesn't get garbage collected and we can pass the pointer to native" );
WriteLine( "//" );
WriteLine( $"handle.PinnedCallback = GCHandle.Alloc( cb, GCHandleType.Pinned );" );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Register the callback with Steam" );
WriteLine( "//" );
WriteLine( $"steamworks.native.api.SteamAPI_RegisterCallResult( handle.PinnedCallback.AddrOfPinnedObject(), call );" );
WriteLine();
WriteLine( "return handle;" );
}
EndBlock();
}
2016-10-25 12:29:35 +03:00
}
}