Facepunch.Steamworks/Generator/Argument.cs

295 lines
9.0 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
{
class Argument
{
public string Name;
public string NativeType;
public string ManagedType;
2016-10-29 15:02:36 +03:00
public CodeWriter.TypeDef TypeDef;
2016-10-25 12:29:35 +03:00
2016-10-29 15:02:36 +03:00
public Argument( string Name, string ManagedType, Dictionary<string, CodeWriter.TypeDef> typeDefs )
2016-10-25 18:16:02 +03:00
{
this.Name = Name;
this.NativeType = ManagedType;
Build( typeDefs );
}
2016-10-29 15:02:36 +03:00
private void Build( Dictionary<string, CodeWriter.TypeDef> typeDefs )
2016-10-25 12:29:35 +03:00
{
var cleanNative = NativeType.Trim( '*', ' ' ).Replace( "class ", "" ).Replace( "const ", "" );
2016-10-31 14:46:53 +03:00
if ( typeDefs != null && typeDefs.ContainsKey( cleanNative ) )
2016-10-25 12:29:35 +03:00
{
TypeDef = typeDefs[cleanNative];
}
ManagedType = ToManagedType( NativeType );
if ( ManagedType.EndsWith( "*" ) )
{
ManagedType = ToManagedType( ManagedType.Trim( '*', ' ' ) ) + "*";
}
}
2016-11-08 16:51:24 +03:00
bool IsInputArray
{
get
{
if ( !NativeType.Contains( "const" ) ) return false;
if ( !NativeType.EndsWith( "*" ) ) return false;
if ( NativeType.Contains( "const char" ) ) return false;
if ( NativeType.Contains( "const void" ) ) return false;
if ( NativeType.Contains( "SteamParamStringArray_t" ) ) return false;
return true;
}
}
bool IsStruct
{
get
{
return ManagedType.Contains( "_t" );
}
}
2016-10-25 12:29:35 +03:00
bool IsStructShouldBePassedAsRef
{
get
{
2016-10-26 12:20:14 +03:00
if ( ManagedType.Contains( "bool*" ) )
return true;
2016-10-25 12:29:35 +03:00
return ManagedType.EndsWith( "*" ) && ManagedType.Contains( "_t" ) && Name.StartsWith( "p" ) && !Name.StartsWith( "pvec" );
}
}
bool ShouldBePassedAsOut
{
get
{
2016-10-26 13:08:32 +03:00
2016-10-26 12:20:14 +03:00
return ManagedType.EndsWith( "*" ) && !ManagedType.Contains( "_t" ) && !ManagedType.Contains( "char" ) && !ManagedType.Contains( "bool" );
2016-10-25 12:29:35 +03:00
}
}
bool ShouldBeIntPtr
{
get
{
2016-11-08 16:51:24 +03:00
if ( IsInputArray )
return false;
2016-11-11 13:43:49 +03:00
if ( Name.Contains( "Flags" ) )
return false;
2016-10-26 19:27:17 +03:00
if ( ManagedType.Contains( "SteamUGCDetails_t" ) || ManagedType.Contains( "SteamParamStringArray_t" ) )
2016-10-25 13:16:05 +03:00
return false;
2016-10-26 13:08:32 +03:00
if ( Name == "pOutItemsArray" || Name == "handlesOut" )
2016-10-25 12:29:35 +03:00
return true;
if ( Name.Contains( "Dest" ) && ManagedType.EndsWith( "*" ) )
return true;
2017-06-09 13:36:53 +03:00
if ( ManagedType.EndsWith( "*" ) )
{
if ( Name.EndsWith( "s" ) && !Name.EndsWith( "Bytes" ) ) return true;
}
2016-10-25 12:29:35 +03:00
return false;
}
}
2016-10-26 13:08:32 +03:00
bool PassedToNativeAsValue
{
get
{
if ( Name.StartsWith( "pvec" ) ) return false;
if ( TypeDef == null ) return false;
if ( ManagedType.Contains( "IntPtr" ) ) return false;
if ( Name.Contains( "IntPtr" ) ) return false;
return true;
}
}
2016-10-25 12:29:35 +03:00
private static string ToManagedType( string type )
{
type = type.Replace( "ISteamHTMLSurface::", "" );
type = type.Replace( "class ", "" );
type = type.Replace( "struct ", "" );
type = type.Replace( "const void", "void" );
switch ( type )
{
case "uint64": return "ulong";
case "uint32": return "uint";
case "int32": return "int";
case "int64": return "long";
case "void *": return "IntPtr";
case "int16": return "short";
case "uint8": return "byte";
case "int8": return "char";
case "unsigned short": return "ushort";
case "unsigned int": return "uint";
case "uint16": return "ushort";
case "const char *": return "string";
case "SteamAPIWarningMessageHook_t": return "IntPtr";
}
//type = type.Trim( '*', ' ' );
// Enums - skip the 'E'
if ( type[0] == 'E' )
{
return type.Substring( 1 );
}
if ( type.StartsWith( "const " ) )
return ToManagedType( type.Replace( "const ", "" ) );
if ( type.StartsWith( "ISteamMatchmak" ) )
return "IntPtr";
return type;
}
internal string ManagedParameter()
{
2016-11-08 16:51:24 +03:00
if ( IsInputArray )
return $"{ManagedType.Trim( '*', ' ' )}[] {Name} /*{NativeType}*/";
2016-10-25 12:29:35 +03:00
if ( ShouldBeIntPtr )
return $"IntPtr {Name} /*{NativeType}*/";
if ( IsStructShouldBePassedAsRef )
return $"ref {ManagedType.Trim( '*', ' ' )} {Name} /*{NativeType}*/";
if ( ShouldBePassedAsOut )
return $"out {ManagedType.Trim( '*', ' ' )} {Name} /*{NativeType}*/";
2016-10-26 19:02:57 +03:00
return $"{ManagedType} {Name} /*{NativeType}*/";
2016-10-25 12:29:35 +03:00
}
2016-10-26 13:08:32 +03:00
internal string InteropVariable( bool AsRawValues )
2016-10-25 12:29:35 +03:00
{
2016-11-08 16:51:24 +03:00
if ( IsInputArray )
{
if ( AsRawValues && IsStruct ) return $"{Name}.Select( x => x.Value ).ToArray()";
2016-11-08 16:51:24 +03:00
return $"{Name}";
}
2016-10-25 12:29:35 +03:00
if ( ShouldBeIntPtr )
return $"{Name}";
2016-10-26 13:08:32 +03:00
var value = (PassedToNativeAsValue && AsRawValues) ? ".Value" : "";
2016-10-25 12:29:35 +03:00
if ( IsStructShouldBePassedAsRef )
2016-10-26 13:08:32 +03:00
return $"ref {Name}{value}";
2016-10-25 12:29:35 +03:00
if ( ShouldBePassedAsOut )
2016-10-26 13:08:32 +03:00
return $"out {Name}{value}";
2016-10-25 12:29:35 +03:00
2016-10-26 13:08:32 +03:00
return $"{Name}{value}";
2016-10-25 12:29:35 +03:00
}
2016-10-26 12:32:00 +03:00
internal string InteropParameter( bool LargePack, bool includeMarshalling = false )
2016-10-25 12:29:35 +03:00
{
2016-10-25 17:35:48 +03:00
var ps = LargePack ? "" : ".PackSmall";
2016-10-26 12:43:08 +03:00
var marshalling = "";
2016-10-25 17:35:48 +03:00
if ( !NativeType.Contains( "_t" ) )
ps = string.Empty;
if ( TypeDef != null )
ps = string.Empty;
2016-10-26 12:43:08 +03:00
if ( includeMarshalling )
2016-10-26 12:32:00 +03:00
{
2016-10-26 12:43:08 +03:00
if ( NativeType == "bool" ) marshalling = "[MarshalAs(UnmanagedType.U1)]";
if ( NativeType == "bool *" ) marshalling = "[MarshalAs(UnmanagedType.U1)]";
2016-10-26 12:32:00 +03:00
2016-10-26 13:08:32 +03:00
if ( PassedToNativeAsValue && !ShouldBeIntPtr )
2016-10-26 12:43:08 +03:00
{
2016-11-08 16:51:24 +03:00
if ( IsInputArray )
return $"{TypeDef.ManagedType}[] {Name}";
else if ( IsStructShouldBePassedAsRef )
2016-10-26 13:08:32 +03:00
return $"ref {TypeDef.ManagedType} {Name}";
else if ( ShouldBePassedAsOut )
return $"out {TypeDef.ManagedType} {Name}";
else
return $"{TypeDef.ManagedType} {Name}";
2016-10-26 12:43:08 +03:00
}
2016-10-26 12:32:00 +03:00
}
2016-10-25 12:29:35 +03:00
if ( ShouldBeIntPtr )
2016-11-08 16:51:24 +03:00
return $"IntPtr /*{NativeType}*/ {Name}".Trim();
2016-10-25 12:29:35 +03:00
if ( IsStructShouldBePassedAsRef )
2016-10-26 12:43:08 +03:00
return $"{marshalling} ref {ManagedType.Trim( '*', ' ' )}{ps} /*{NativeType}*/ {Name}".Trim();
2016-10-25 12:29:35 +03:00
2016-11-08 16:51:24 +03:00
if ( IsInputArray )
return $"{marshalling} {ManagedType.Trim( '*', ' ' )}[] /*{NativeType}*/ {Name}".Trim();
2016-10-25 12:29:35 +03:00
if ( ShouldBePassedAsOut )
2016-10-26 12:43:08 +03:00
return $"{marshalling} out {ManagedType.Trim( '*', ' ' )} /*{NativeType}*/ {Name}".Trim();
2016-10-25 12:29:35 +03:00
2016-10-25 13:16:05 +03:00
if ( NativeType == "char *" || NativeType == "char **" )
{
2016-10-26 12:43:08 +03:00
return $"System.Text.StringBuilder /*{NativeType}*/ {Name}".Trim();
2016-10-25 13:16:05 +03:00
}
2016-10-25 12:29:35 +03:00
if ( TypeDef != null )
{
if ( NativeType.EndsWith( "*" ) )
{
2016-10-26 12:43:08 +03:00
return $"IntPtr /*{NativeType}*/ {Name}".Trim();
2016-10-25 12:29:35 +03:00
}
else
{
2016-10-26 12:43:08 +03:00
return $"{marshalling} {TypeDef.Name} /*{NativeType}*/ {Name}".Trim();
2016-10-25 12:29:35 +03:00
}
}
2016-10-26 12:32:00 +03:00
2016-10-25 12:29:35 +03:00
if ( NativeType.EndsWith( "*" ) && ManagedType.Contains( "_t" ) )
{
2016-10-26 12:43:08 +03:00
return $"IntPtr /*{NativeType}*/ {Name} ".Trim();
2016-10-25 12:29:35 +03:00
}
2016-10-26 12:43:08 +03:00
return $"{marshalling} {ManagedType} /*{NativeType}*/ {Name} ".Trim();
2016-10-25 12:29:35 +03:00
}
2016-10-25 15:22:59 +03:00
internal string Return()
2016-10-25 12:29:35 +03:00
{
if ( ManagedType.EndsWith( "*" ) )
{
return $"IntPtr /*{NativeType}*/";
}
if ( TypeDef != null )
{
2016-10-25 15:22:59 +03:00
return $"{TypeDef.Name} /*({NativeType})*/";
2016-10-25 12:29:35 +03:00
}
if ( ManagedType == "string" )
return "IntPtr";
return $"{ManagedType} /*{NativeType}*/";
}
}
}