Facepunch.Steamworks/Generator/CodeWriter/Struct.cs

151 lines
5.2 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>();
public readonly static string[] ForceLargePackStructs = new string[]
{
"LeaderboardEntry_t"
};
2016-10-25 12:29:35 +03:00
void Structs()
{
foreach ( var c in def.structs )
2016-10-25 12:29:35 +03:00
{
2019-04-16 13:45:44 +03:00
var name = Cleanup.ConvertType( c.Name );
if ( !Cleanup.ShouldCreate( name ) )
continue;
if ( name.Contains( "::" ) )
2016-10-25 12:29:35 +03:00
continue;
int defaultPack = c.IsPack4OnWindows ? 4 : 8;
2016-10-25 12:29:35 +03:00
2020-02-10 22:21:52 +03:00
//
// Main struct
//
WriteLine( $"[StructLayout( LayoutKind.Sequential, Pack = Platform.{(c.IsPack4OnWindows?"StructPackSize": "StructPlatformPackSize")} )]" );
StartBlock( $"{Cleanup.Expose( name )} struct {name}" );
2016-10-25 12:29:35 +03:00
{
2019-04-14 00:56:06 +03:00
//
// The fields
//
StructFields( c.Fields );
WriteLine();
2020-02-19 12:00:23 +03:00
if ( c.Enums != null )
{
foreach ( var e in c.Enums )
{
WriteEnum( e, e.Name );
}
}
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 );
2019-04-16 13:45:44 +03:00
t = Cleanup.ConvertType( t );
if ( TypeDefs.ContainsKey( t ) )
2016-10-25 12:29:35 +03:00
{
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( "[" ) )
{
WriteLine( $"internal string {CleanMemberName( m.Name )}UTF8() => System.Text.Encoding.UTF8.GetString( {CleanMemberName( m.Name )}, 0, System.Array.IndexOf<byte>( {CleanMemberName( m.Name )}, 0 ) );" );
var num = t.Replace( "char", "" ).Trim( '[', ']', ' ' );
t = "byte[]";
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num})] // {t} {m.Name}" );
2016-10-25 12:29:35 +03:00
}
2016-10-26 18:10:20 +03:00
if ( t.StartsWith( "uint8 " ) && t.Contains( "[" ) )
{
var num = t.Replace( "uint8", "" ).Trim( '[', ']', ' ' );
t = "byte[]";
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num})] // {m.Name}" );
2016-10-26 18:10:20 +03:00
}
2019-04-16 13:45:44 +03:00
if ( t.StartsWith( "SteamId" ) && t.Contains( "[" ) )
2016-10-25 12:29:35 +03:00
{
2019-04-16 13:45:44 +03:00
var num = t.Replace( "SteamId", "" ).Trim( '[', ']', ' ' );
2016-10-25 12:29:35 +03:00
t = $"ulong[]";
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.U8)]" );
}
2019-04-16 18:37:49 +03:00
if ( t.StartsWith( "PublishedFileId " ) && t.Contains( "[" ) )
2016-10-25 12:29:35 +03:00
{
2019-04-16 18:37:49 +03:00
var num = t.Replace( "PublishedFileId", "" ).Trim( '[', ']', ' ' );
t = $"PublishedFileId[]";
2016-10-25 12:29:35 +03:00
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.U4)]" );
2020-02-19 12:11:36 +03:00
}
if ( t.StartsWith( "uint " ) && t.Contains( "[" ) )
{
var num = t.Replace( "uint", "" ).Trim( '[', ']', ' ' );
t = $"uint[]";
WriteLine( $"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.U4)]" );
2016-10-25 12:29:35 +03:00
}
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";
}
2019-04-26 16:29:36 +03:00
if (t.StartsWith("AppId ") && t.Contains("["))
2018-01-23 14:00:14 +03:00
{
2019-04-26 16:29:36 +03:00
var num = t.Replace("AppId", "").Trim('[', ']', ' ');
t = $"AppId[]";
2018-01-23 14:00:14 +03:00
WriteLine($"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.U4)]");
}
2018-02-14 17:15:02 +03:00
WriteLine( $"internal {t} {CleanMemberName( m.Name )}; // {m.Name} {m.Type}" );
2016-10-25 12:29:35 +03:00
}
}
}
}