mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2025-04-27 23:39:36 +03:00
Struct cleanup
This commit is contained in:
parent
48cb001fb3
commit
561f185551
File diff suppressed because it is too large
Load Diff
@ -8,15 +8,23 @@ namespace Generator
|
|||||||
{
|
{
|
||||||
public partial class CodeWriter
|
public partial class CodeWriter
|
||||||
{
|
{
|
||||||
|
//
|
||||||
|
// Don't give a fuck about these classes, they just cause us trouble
|
||||||
|
//
|
||||||
|
public readonly static string[] SkipStructs = new string[]
|
||||||
|
{
|
||||||
|
"CSteamID",
|
||||||
|
"CSteamAPIContext",
|
||||||
|
"CCallResult",
|
||||||
|
"CCallback",
|
||||||
|
"ValvePackingSentinel_t"
|
||||||
|
};
|
||||||
|
|
||||||
void Structs()
|
void Structs()
|
||||||
{
|
{
|
||||||
foreach ( var c in def.structs )
|
foreach ( var c in def.structs )
|
||||||
{
|
{
|
||||||
if ( c.Name == "CSteamID" ||
|
if ( SkipStructs.Contains( c.Name ) )
|
||||||
c.Name == "CSteamAPIContext" ||
|
|
||||||
c.Name == "CCallResult" ||
|
|
||||||
c.Name == "CCallback" ||
|
|
||||||
c.Name == "ValvePackingSentinel_t" )
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if ( c.Name.Contains( "::" ) )
|
if ( c.Name.Contains( "::" ) )
|
||||||
@ -27,39 +35,73 @@ namespace Generator
|
|||||||
if ( c.Fields.Any( x => x.Type.Contains( "class CSteamID" ) ) )
|
if ( c.Fields.Any( x => x.Type.Contains( "class CSteamID" ) ) )
|
||||||
defaultPack = 4;
|
defaultPack = 4;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Main struct
|
||||||
|
//
|
||||||
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = {defaultPack} )]" );
|
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = {defaultPack} )]" );
|
||||||
StartBlock( $"public struct {c.Name}" );
|
StartBlock( $"public struct {c.Name}" );
|
||||||
|
|
||||||
StructFields( c.Fields );
|
|
||||||
|
|
||||||
WriteLine( $"public static {c.Name} FromPointer( IntPtr p ) {{ return ({c.Name}) Marshal.PtrToStructure( p, typeof({c.Name}) ); }}" );
|
|
||||||
|
|
||||||
|
|
||||||
if ( defaultPack == 8 )
|
|
||||||
defaultPack = 4;
|
|
||||||
|
|
||||||
WriteLine();
|
|
||||||
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = {defaultPack} )]" );
|
|
||||||
StartBlock( $"public struct PackSmall" );
|
|
||||||
StructFields( c.Fields );
|
|
||||||
|
|
||||||
WriteLine();
|
|
||||||
|
|
||||||
//
|
|
||||||
// Implicit convert from PackSmall to regular
|
|
||||||
//
|
|
||||||
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 )}," );
|
//
|
||||||
|
// The fields
|
||||||
|
//
|
||||||
|
StructFields( c.Fields );
|
||||||
|
|
||||||
|
WriteLine();
|
||||||
|
WriteLine( "//" );
|
||||||
|
WriteLine( "// Read this struct from a pointer, usually from Native" );
|
||||||
|
WriteLine( "//" );
|
||||||
|
StartBlock( $"public static {c.Name} FromPointer( IntPtr p )" );
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
|
||||||
|
WriteLine();
|
||||||
|
WriteLine( "//" );
|
||||||
|
WriteLine( "// Read this struct from a pointer, usually from Native" );
|
||||||
|
WriteLine( "//" );
|
||||||
|
StartBlock( $"public static PackSmall FromPointer( IntPtr p )" );
|
||||||
|
{
|
||||||
|
WriteLine( $"return (PackSmall) Marshal.PtrToStructure( p, typeof(PackSmall) );" );
|
||||||
|
}
|
||||||
|
EndBlock();
|
||||||
|
|
||||||
|
}
|
||||||
|
EndBlock();
|
||||||
|
|
||||||
}
|
}
|
||||||
EndBlock( ";" );
|
|
||||||
EndBlock();
|
|
||||||
|
|
||||||
EndBlock();
|
|
||||||
|
|
||||||
|
|
||||||
EndBlock();
|
EndBlock();
|
||||||
WriteLine();
|
WriteLine();
|
||||||
}
|
}
|
||||||
@ -123,8 +165,6 @@ namespace Generator
|
|||||||
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.R4)]" );
|
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.R4)]" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ( t == "const char **" )
|
if ( t == "const char **" )
|
||||||
{
|
{
|
||||||
t = "IntPtr";
|
t = "IntPtr";
|
||||||
@ -133,34 +173,5 @@ namespace Generator
|
|||||||
WriteLine( $"public {t} {CleanMemberName( m.Name )}; // {m.Name} {m.Type}" );
|
WriteLine( $"public {t} {CleanMemberName( m.Name )}; // {m.Name} {m.Type}" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string CleanMemberName( string m )
|
|
||||||
{
|
|
||||||
if ( m == "m_pubParam" ) return "ParamPtr";
|
|
||||||
if ( m == "m_cubParam" ) return "ParamCount";
|
|
||||||
if ( m == "m_itemId" ) return "ItemId";
|
|
||||||
|
|
||||||
|
|
||||||
var cleanName = m.Replace( "m_un", "" )
|
|
||||||
.Replace( "m_us", "" )
|
|
||||||
.Replace( "m_sz", "" )
|
|
||||||
.Replace( "m_h", "" )
|
|
||||||
.Replace( "m_pp", "" )
|
|
||||||
.Replace( "m_e", "" )
|
|
||||||
.Replace( "m_un", "" )
|
|
||||||
.Replace( "m_ul", "" )
|
|
||||||
.Replace( "m_fl", "" )
|
|
||||||
.Replace( "m_u", "" )
|
|
||||||
.Replace( "m_b", "" )
|
|
||||||
.Replace( "m_i", "" )
|
|
||||||
.Replace( "m_pub", "" )
|
|
||||||
.Replace( "m_cub", "" )
|
|
||||||
.Replace( "m_n", "" )
|
|
||||||
.Replace( "m_rgch", "" )
|
|
||||||
.Replace( "m_r", "" )
|
|
||||||
.Replace( "m_", "" );
|
|
||||||
|
|
||||||
return cleanName.Substring( 0, 1 ).ToUpper() + cleanName.Substring( 1 );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -30,5 +30,32 @@ namespace Generator
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string CleanMemberName( string m )
|
||||||
|
{
|
||||||
|
if ( m == "m_pubParam" ) return "ParamPtr";
|
||||||
|
if ( m == "m_cubParam" ) return "ParamCount";
|
||||||
|
if ( m == "m_itemId" ) return "ItemId";
|
||||||
|
|
||||||
|
var cleanName = m.Replace( "m_un", "" )
|
||||||
|
.Replace( "m_us", "" )
|
||||||
|
.Replace( "m_sz", "" )
|
||||||
|
.Replace( "m_h", "" )
|
||||||
|
.Replace( "m_pp", "" )
|
||||||
|
.Replace( "m_e", "" )
|
||||||
|
.Replace( "m_un", "" )
|
||||||
|
.Replace( "m_ul", "" )
|
||||||
|
.Replace( "m_fl", "" )
|
||||||
|
.Replace( "m_u", "" )
|
||||||
|
.Replace( "m_b", "" )
|
||||||
|
.Replace( "m_i", "" )
|
||||||
|
.Replace( "m_pub", "" )
|
||||||
|
.Replace( "m_cub", "" )
|
||||||
|
.Replace( "m_n", "" )
|
||||||
|
.Replace( "m_rgch", "" )
|
||||||
|
.Replace( "m_r", "" )
|
||||||
|
.Replace( "m_", "" );
|
||||||
|
|
||||||
|
return cleanName.Substring( 0, 1 ).ToUpper() + cleanName.Substring( 1 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@
|
|||||||
<Compile Include="CodeWriter\Class.cs" />
|
<Compile Include="CodeWriter\Class.cs" />
|
||||||
<Compile Include="CodeWriter.cs" />
|
<Compile Include="CodeWriter.cs" />
|
||||||
<Compile Include="CodeWriter\Enums.cs" />
|
<Compile Include="CodeWriter\Enums.cs" />
|
||||||
<Compile Include="CodeWriter.Struct.cs" />
|
<Compile Include="CodeWriter\Struct.cs" />
|
||||||
<Compile Include="CodeWriter.Types.cs" />
|
<Compile Include="CodeWriter.Types.cs" />
|
||||||
<Compile Include="CodeWriter\Utility.cs" />
|
<Compile Include="CodeWriter\Utility.cs" />
|
||||||
<Compile Include="CodeWriter\Interface.cs" />
|
<Compile Include="CodeWriter\Interface.cs" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user