Added StructFunctions

This commit is contained in:
Garry Newman 2020-02-24 10:14:31 +00:00
parent e7a6a00675
commit afd314627d
14 changed files with 339 additions and 14 deletions

View File

@ -0,0 +1,208 @@
using System;
using System.Runtime.InteropServices;
using System.Linq;
using Steamworks.Data;
using System.Threading.Tasks;
namespace Steamworks.Data
{
internal partial struct gameserveritem_t
{
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_gameserveritem_t_Construct", CallingConvention = Platform.CC)]
internal static extern void InternalConstruct( ref gameserveritem_t self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_gameserveritem_t_GetName", CallingConvention = Platform.CC)]
internal static extern Utf8StringPointer InternalGetName( ref gameserveritem_t self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_gameserveritem_t_SetName", CallingConvention = Platform.CC)]
internal static extern void InternalSetName( ref gameserveritem_t self, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pName );
}
internal partial struct MatchMakingKeyValuePair
{
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_MatchMakingKeyValuePair_t_Construct", CallingConvention = Platform.CC)]
internal static extern void InternalConstruct( ref MatchMakingKeyValuePair self );
}
internal partial struct servernetadr_t
{
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_Construct", CallingConvention = Platform.CC)]
internal static extern void InternalConstruct( ref servernetadr_t self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_Init", CallingConvention = Platform.CC)]
internal static extern void InternalInit( ref servernetadr_t self, uint ip, ushort usQueryPort, ushort usConnectionPort );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_GetQueryPort", CallingConvention = Platform.CC)]
internal static extern ushort InternalGetQueryPort( ref servernetadr_t self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_SetQueryPort", CallingConvention = Platform.CC)]
internal static extern void InternalSetQueryPort( ref servernetadr_t self, ushort usPort );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_GetConnectionPort", CallingConvention = Platform.CC)]
internal static extern ushort InternalGetConnectionPort( ref servernetadr_t self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_SetConnectionPort", CallingConvention = Platform.CC)]
internal static extern void InternalSetConnectionPort( ref servernetadr_t self, ushort usPort );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_GetIP", CallingConvention = Platform.CC)]
internal static extern uint InternalGetIP( ref servernetadr_t self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_SetIP", CallingConvention = Platform.CC)]
internal static extern void InternalSetIP( ref servernetadr_t self, uint unIP );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_GetConnectionAddressString", CallingConvention = Platform.CC)]
internal static extern Utf8StringPointer InternalGetConnectionAddressString( ref servernetadr_t self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_GetQueryAddressString", CallingConvention = Platform.CC)]
internal static extern Utf8StringPointer InternalGetQueryAddressString( ref servernetadr_t self );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_IsLessThan", CallingConvention = Platform.CC)]
internal static extern bool InternalIsLessThan( ref servernetadr_t self, ref servernetadr_t netadr );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_servernetadr_t_Assign", CallingConvention = Platform.CC)]
internal static extern void InternalAssign( ref servernetadr_t self, ref servernetadr_t that );
}
internal partial struct SteamDatagramHostedAddress
{
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamDatagramHostedAddress_Clear", CallingConvention = Platform.CC)]
internal static extern void InternalClear( ref SteamDatagramHostedAddress self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamDatagramHostedAddress_GetPopID", CallingConvention = Platform.CC)]
internal static extern SteamNetworkingPOPID InternalGetPopID( ref SteamDatagramHostedAddress self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamDatagramHostedAddress_SetDevAddress", CallingConvention = Platform.CC)]
internal static extern void InternalSetDevAddress( ref SteamDatagramHostedAddress self, uint nIP, ushort nPort, SteamNetworkingPOPID popid );
}
internal partial struct SteamIPAddress
{
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamIPAddress_t_IsSet", CallingConvention = Platform.CC)]
internal static extern bool InternalIsSet( ref SteamIPAddress self );
}
public partial struct NetIdentity
{
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_Clear", CallingConvention = Platform.CC)]
internal static extern void InternalClear( ref NetIdentity self );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_IsInvalid", CallingConvention = Platform.CC)]
internal static extern bool InternalIsInvalid( ref NetIdentity self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_SetSteamID", CallingConvention = Platform.CC)]
internal static extern void InternalSetSteamID( ref NetIdentity self, SteamId steamID );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_GetSteamID", CallingConvention = Platform.CC)]
internal static extern SteamId InternalGetSteamID( ref NetIdentity self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_SetSteamID64", CallingConvention = Platform.CC)]
internal static extern void InternalSetSteamID64( ref NetIdentity self, ulong steamID );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_GetSteamID64", CallingConvention = Platform.CC)]
internal static extern ulong InternalGetSteamID64( ref NetIdentity self );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_SetXboxPairwiseID", CallingConvention = Platform.CC)]
internal static extern bool InternalSetXboxPairwiseID( ref NetIdentity self, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pszString );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_GetXboxPairwiseID", CallingConvention = Platform.CC)]
internal static extern Utf8StringPointer InternalGetXboxPairwiseID( ref NetIdentity self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_SetIPAddr", CallingConvention = Platform.CC)]
internal static extern void InternalSetIPAddr( ref NetIdentity self, ref NetAddress addr );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_GetIPAddr", CallingConvention = Platform.CC)]
internal static extern IntPtr InternalGetIPAddr( ref NetIdentity self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_SetLocalHost", CallingConvention = Platform.CC)]
internal static extern void InternalSetLocalHost( ref NetIdentity self );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_IsLocalHost", CallingConvention = Platform.CC)]
internal static extern bool InternalIsLocalHost( ref NetIdentity self );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_SetGenericString", CallingConvention = Platform.CC)]
internal static extern bool InternalSetGenericString( ref NetIdentity self, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pszString );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_GetGenericString", CallingConvention = Platform.CC)]
internal static extern Utf8StringPointer InternalGetGenericString( ref NetIdentity self );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_SetGenericBytes", CallingConvention = Platform.CC)]
internal static extern bool InternalSetGenericBytes( ref NetIdentity self, IntPtr data, uint cbLen );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_GetGenericBytes", CallingConvention = Platform.CC)]
internal static extern byte InternalGetGenericBytes( ref NetIdentity self, ref int cbLen );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_IsEqualTo", CallingConvention = Platform.CC)]
internal static extern bool InternalIsEqualTo( ref NetIdentity self, ref NetIdentity x );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_ToString", CallingConvention = Platform.CC)]
internal static extern void InternalToString( ref NetIdentity self, IntPtr buf, uint cbBuf );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIdentity_ParseString", CallingConvention = Platform.CC)]
internal static extern bool InternalParseString( ref NetIdentity self, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pszStr );
}
public partial struct NetAddress
{
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_Clear", CallingConvention = Platform.CC)]
internal static extern void InternalClear( ref NetAddress self );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_IsIPv6AllZeros", CallingConvention = Platform.CC)]
internal static extern bool InternalIsIPv6AllZeros( ref NetAddress self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_SetIPv6", CallingConvention = Platform.CC)]
internal static extern void InternalSetIPv6( ref NetAddress self, ref byte ipv6, ushort nPort );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_SetIPv4", CallingConvention = Platform.CC)]
internal static extern void InternalSetIPv4( ref NetAddress self, uint nIP, ushort nPort );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_IsIPv4", CallingConvention = Platform.CC)]
internal static extern bool InternalIsIPv4( ref NetAddress self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_GetIPv4", CallingConvention = Platform.CC)]
internal static extern uint InternalGetIPv4( ref NetAddress self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_SetIPv6LocalHost", CallingConvention = Platform.CC)]
internal static extern void InternalSetIPv6LocalHost( ref NetAddress self, ushort nPort );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_IsLocalHost", CallingConvention = Platform.CC)]
internal static extern bool InternalIsLocalHost( ref NetAddress self );
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_ToString", CallingConvention = Platform.CC)]
internal static extern void InternalToString( ref NetAddress self, IntPtr buf, uint cbBuf, [MarshalAs( UnmanagedType.U1 )] bool bWithPort );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_ParseString", CallingConvention = Platform.CC)]
internal static extern bool InternalParseString( ref NetAddress self, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pszStr );
[return: MarshalAs( UnmanagedType.I1 )]
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingIPAddr_IsEqualTo", CallingConvention = Platform.CC)]
internal static extern bool InternalIsEqualTo( ref NetAddress self, ref NetAddress x );
}
internal partial struct NetMsg
{
[DllImport( Platform.LibraryName, EntryPoint = "SteamAPI_SteamNetworkingMessage_t_Release", CallingConvention = Platform.CC)]
internal static unsafe extern void InternalRelease( NetMsg* self );
}
}

View File

@ -26,7 +26,7 @@ internal struct FriendSessionStateInfo_t
}
[StructLayout( LayoutKind.Sequential, Pack = Platform.StructPlatformPackSize )]
internal struct servernetadr_t
internal partial struct servernetadr_t
{
internal ushort ConnectionPort; // m_usConnectionPort uint16
internal ushort QueryPort; // m_usQueryPort uint16
@ -35,7 +35,7 @@ internal struct servernetadr_t
}
[StructLayout( LayoutKind.Sequential, Pack = Platform.StructPackSize )]
internal struct gameserveritem_t
internal partial struct gameserveritem_t
{
internal servernetadr_t NetAdr; // m_NetAdr servernetadr_t
internal int Ping; // m_nPing int
@ -200,7 +200,7 @@ internal struct SteamNetworkingQuickConnectionStatus
}
[StructLayout( LayoutKind.Sequential, Pack = Platform.StructPlatformPackSize )]
internal struct SteamDatagramHostedAddress
internal partial struct SteamDatagramHostedAddress
{
internal int CbSize; // m_cbSize int
internal string DataUTF8() => System.Text.Encoding.UTF8.GetString( Data, 0, System.Array.IndexOf<byte>( Data, 0 ) );

View File

@ -4,7 +4,7 @@
namespace Steamworks.Data
{
[StructLayout( LayoutKind.Explicit, Size = 18, Pack = 1 )]
public struct NetAddress
public partial struct NetAddress
{
[FieldOffset( 0 )]
internal IPV4 ip;

View File

@ -3,7 +3,7 @@
namespace Steamworks.Data
{
[StructLayout( LayoutKind.Explicit, Size = 136, Pack = 1 )]
public struct NetIdentity
public partial struct NetIdentity
{
[FieldOffset( 0 )]
internal IdentityType type;

View File

@ -5,7 +5,7 @@
namespace Steamworks.Data
{
[StructLayout( LayoutKind.Sequential )]
internal struct NetMsg
internal partial struct NetMsg
{
internal IntPtr DataPtr;
internal int DataSize;

View File

@ -4,7 +4,7 @@
namespace Steamworks.Data
{
[StructLayout( LayoutKind.Sequential, Pack = Platform.StructPackSize )]
internal struct MatchMakingKeyValuePair
internal partial struct MatchMakingKeyValuePair
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
internal string Key;

View File

@ -7,7 +7,7 @@
namespace Steamworks.Data
{
[StructLayout( LayoutKind.Explicit, Pack = Platform.StructPlatformPackSize )]
internal struct SteamIPAddress
internal partial struct SteamIPAddress
{
[FieldOffset( 0 )]
public uint Ip4Address; // Host Order

View File

@ -134,6 +134,8 @@ internal static string Expose( string name )
if ( name == "SteamDeviceFormFactor" ) return "public";
if ( name == "DurationControlProgress" ) return "public";
if ( name == "NetConnectionEnd" ) return "public";
if ( name == "NetIdentity" ) return "public";
if ( name == "NetAddress" ) return "public";
return "internal";
}

View File

@ -8,7 +8,7 @@ namespace Generator
{
public partial class CodeWriter
{
void StructCallbacks()
void Callbacks()
{
var callbackList = new List<SteamApiDefinition.StructDef>();
@ -22,7 +22,10 @@ void StructCallbacks()
if ( name.Contains( "::" ) )
continue;
int defaultPack = c.IsPack4OnWindows ? 4 : 8;
var partial = "";
if ( c.Methods != null ) partial = " partial";
int defaultPack = c.IsPack4OnWindows ? 4 : 8;
var isCallback = true;
var iface = "";
@ -33,7 +36,7 @@ void StructCallbacks()
// Main struct
//
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = Platform.{(c.IsPack4OnWindows?"StructPackSize": "StructPlatformPackSize")} )]" );
StartBlock( $"{Cleanup.Expose( name )} struct {name}{iface}" );
StartBlock( $"{Cleanup.Expose( name )}{partial} struct {name}{iface}" );
{
//
// The fields

View File

@ -50,7 +50,7 @@ public void ToFolder( string folder )
{
sb = new StringBuilder();
Header( "Steamworks.Data" );
StructCallbacks();
Callbacks();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamCallbacks.cs", sb.ToString() );
}
@ -69,6 +69,14 @@ public void ToFolder( string folder )
Constants();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamConstants.cs", sb.ToString() );
}
{
sb = new StringBuilder();
Header( "Steamworks.Data" );
StructFunctions();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamStructFunctions.cs", sb.ToString() );
}
{

View File

@ -31,11 +31,14 @@ void Structs()
int defaultPack = c.IsPack4OnWindows ? 4 : 8;
var partial = "";
if ( c.Methods != null ) partial = " partial";
//
// Main struct
//
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = Platform.{(c.IsPack4OnWindows?"StructPackSize": "StructPlatformPackSize")} )]" );
StartBlock( $"{Cleanup.Expose( name )} struct {name}" );
StartBlock( $"{Cleanup.Expose( name )}{partial} struct {name}" );
{
//
// The fields

View File

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Generator
{
public partial class CodeWriter
{
void StructFunctions()
{
foreach ( var c in def.structs.Union( def.callback_structs.Select( x => x as SteamApiDefinition.StructDef ) ).OrderBy( x => x.Name ) )
{
var name = Cleanup.ConvertType( c.Name );
if ( name.Contains( "::" ) )
continue;
if ( c.Methods == null || c.Methods.Length == 0 )
continue;
//
// Main struct
//
StartBlock( $"{Cleanup.Expose( name )} partial struct {name}" );
{
foreach ( var func in c.Methods )
{
if ( func.Name.Contains( "operator" ) )
func.Name = func.FlatName.Substring( func.FlatName.LastIndexOf( '_' ) + 1 );
var returnType = BaseType.Parse( func.ReturnType, null, func.CallResult );
returnType.Func = func.Name;
var args = func.Params.Select( x =>
{
var bt = BaseType.Parse( x.ParamType, x.ParamName );
bt.Func = func.Name;
return bt;
} ).ToArray();
for ( int i = 0; i < args.Length; i++ )
{
if ( args[i] is FetchStringType )
{
if ( args[i + 1] is IntType || args[i + 1] is UIntType || args[i + 1] is UIntPtrType )
{
if ( args[i + 1].Ref == string.Empty )
{
args[i + 1] = new LiteralType( args[i + 1], "(1024 * 32)" );
}
}
else
{
throw new System.Exception( $"String Builder Next Type Is {args[i + 1].GetType()}" );
}
}
}
var delegateargstr = string.Join( ", ", args.Select( x => x.AsNativeArgument() ) );
if ( !string.IsNullOrEmpty( func.Desc ) )
{
WriteLine( "/// <summary>" );
WriteLine( $"/// {func.Desc}" );
WriteLine( "/// </summary>" );
}
if ( returnType.ReturnAttribute != null )
WriteLine( returnType.ReturnAttribute );
var _unsafe = "";
var firstArg = $"ref {name} self";
//
// If this is NetMsg then the ORIGINAL pointer address is important
// because we need to pass in the original pointer - not just the data
//
if ( name == "NetMsg" )
{
firstArg = $"{name}* self";
_unsafe = " unsafe";
}
WriteLine( $"[DllImport( Platform.LibraryName, EntryPoint = \"{func.FlatName}\", CallingConvention = Platform.CC)]" );
WriteLine( $"internal static{_unsafe} extern {returnType.TypeNameFrom} Internal{func.Name}( {firstArg}, {delegateargstr} );".Replace( $"( {firstArg}, )", $"( {firstArg} )" ) );
WriteLine();
}
}
EndBlock();
WriteLine();
}
}
}
}

View File

@ -50,7 +50,8 @@
<Compile Include="CodeWriter\Interface.cs" />
<Compile Include="CodeWriter\Constants.cs" />
<Compile Include="CodeWriter\CustomEnums.cs" />
<Compile Include="CodeWriter\StructCallbacks.cs" />
<Compile Include="CodeWriter\Callbacks.cs" />
<Compile Include="CodeWriter\StructFunctions.cs" />
<Compile Include="Types\FetchStringType.cs" />
<Compile Include="Types\ConstCharType.cs" />
<Compile Include="Types\LiteralType.cs" />

View File

@ -97,6 +97,7 @@ public class StructFields
public string Name { get; set; }
[JsonProperty( PropertyName = "fields" )]
public StructFields[] Fields { get; set; }
public Interface.Method[] Methods { get; set; }
public bool IsPack4OnWindows
{