Facepunch.Steamworks/Generator/CodeWriter.Struct.cs

153 lines
5.3 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
{
public partial class CSharpGenerator
{
void Structs()
{
foreach ( var c in def.structs )
{
if ( c.Name == "CSteamID" ||
c.Name == "CSteamAPIContext" ||
c.Name == "CCallResult" ||
2016-10-25 17:57:46 +03:00
c.Name == "CCallback" ||
c.Name == "ValvePackingSentinel_t" )
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
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = {defaultPack} )]" );
StartBlock( $"public struct {c.Name}" );
StructFields( c.Fields );
2016-10-25 13:51:24 +03:00
WriteLine( $"public static {c.Name} FromPointer( IntPtr p ) {{ return ({c.Name}) Marshal.PtrToStructure( p, typeof({c.Name}) ); }}" );
2016-10-25 12:29:35 +03:00
if ( defaultPack == 8 )
defaultPack = 4;
WriteLine();
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = {defaultPack} )]" );
StartBlock( $"public struct PackSmall" );
StructFields( c.Fields );
WriteLine();
2016-10-25 17:57:46 +03:00
//
// Implicit convert from PackSmall to regular
//
StartBlock( $"public static implicit operator {c.Name} ( {c.Name}.PackSmall d )" );
2016-10-25 12:29:35 +03:00
StartBlock( $"return new {c.Name}()" );
foreach ( var f in c.Fields )
{
2016-10-25 17:57:46 +03:00
WriteLine( $"{CleanMemberName( f.Name )} = d.{CleanMemberName( f.Name )}," );
2016-10-25 12:29:35 +03:00
}
EndBlock( ";" );
EndBlock();
EndBlock();
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})]" );
}
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)]" );
}
2016-10-25 17:57:46 +03:00
2016-10-25 12:29:35 +03:00
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-25 17:57:46 +03:00
string CleanMemberName( string m )
{
if ( m == "m_pubParam" ) return "ParamPtr";
if ( m == "m_cubParam" ) return "ParamCount";
var cleanName = m.Replace( "m_un", "" )
.Replace( "m_us", "" )
.Replace( "m_sz", "" )
.Replace( "m_h", "" )
.Replace( "m_e", "" )
.Replace( "m_un", "" )
.Replace( "m_ul", "" )
.Replace( "m_u", "" )
.Replace( "m_b", "" )
.Replace( "m_i", "" )
.Replace( "m_pub", "" )
.Replace( "m_cub", "" )
.Replace( "m_", "" );
return cleanName.Substring( 0, 1 ).ToUpper() + cleanName.Substring( 1 );
}
2016-10-25 12:29:35 +03:00
}
}