Facepunch.Steamworks/Generator/Argument.cs

208 lines
5.9 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;
public bool IsRef;
public TypeDef TypeDef;
internal void Build( SteamApiDefinition.MethodDef.ParamType[] ps, Dictionary<string, TypeDef> typeDefs )
{
var cleanNative = NativeType.Trim( '*', ' ' ).Replace( "class ", "" ).Replace( "const ", "" );
if ( typeDefs.ContainsKey( cleanNative ) )
{
TypeDef = typeDefs[cleanNative];
}
ManagedType = ToManagedType( NativeType );
if ( ManagedType.EndsWith( "*" ) )
{
ManagedType = ToManagedType( ManagedType.Trim( '*', ' ' ) ) + "*";
}
}
bool IsStructShouldBePassedAsRef
{
get
{
return ManagedType.EndsWith( "*" ) && ManagedType.Contains( "_t" ) && Name.StartsWith( "p" ) && !Name.StartsWith( "pvec" );
}
}
bool ShouldBePassedAsOut
{
get
{
return ManagedType.EndsWith( "*" ) && !ManagedType.Contains( "_t" ) && !ManagedType.Contains( "char" );
}
}
bool ShouldBeIntPtr
{
get
{
2016-10-25 13:16:05 +03:00
if ( ManagedType.Contains( "SteamUGCDetails_t" ) )
return false;
2016-10-25 12:29:35 +03:00
if ( Name == "pOutItemsArray" )
return true;
if ( Name.Contains( "Dest" ) && ManagedType.EndsWith( "*" ) )
return true;
if ( Name.EndsWith( "s" ) && ManagedType.EndsWith( "*" ) )
return true;
return false;
}
}
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()
{
if ( ShouldBeIntPtr )
return $"IntPtr {Name} /*{NativeType}*/";
if ( IsStructShouldBePassedAsRef )
return $"ref {ManagedType.Trim( '*', ' ' )} {Name} /*{NativeType}*/";
if ( ShouldBePassedAsOut )
return $"out {ManagedType.Trim( '*', ' ' )} {Name} /*{NativeType}*/";
var refv = IsRef ? "ref " : "";
return $"{refv}{ManagedType} {Name} /*{NativeType}*/";
}
internal string InteropVariable()
{
if ( ShouldBeIntPtr )
return $"{Name}";
if ( IsStructShouldBePassedAsRef )
return $"ref {Name}";
if ( ShouldBePassedAsOut )
return $"out {Name}";
return $"{Name}";
}
2016-10-25 17:35:48 +03:00
internal string InteropParameter( bool LargePack )
2016-10-25 12:29:35 +03:00
{
2016-10-25 17:35:48 +03:00
var ps = LargePack ? "" : ".PackSmall";
if ( !NativeType.Contains( "_t" ) )
ps = string.Empty;
if ( TypeDef != null )
ps = string.Empty;
2016-10-25 12:29:35 +03:00
if ( ShouldBeIntPtr )
return $"IntPtr /*{NativeType}*/ {Name}";
if ( IsStructShouldBePassedAsRef )
2016-10-25 17:35:48 +03:00
return $"ref {ManagedType.Trim( '*', ' ' )}{ps} /*{NativeType}*/ {Name}";
2016-10-25 12:29:35 +03:00
if ( ShouldBePassedAsOut )
return $"out {ManagedType.Trim( '*', ' ' )} /*{NativeType}*/ {Name}";
2016-10-25 13:16:05 +03:00
if ( NativeType == "char *" || NativeType == "char **" )
{
return $"System.Text.StringBuilder /*{NativeType}*/ {Name}";
}
2016-10-25 12:29:35 +03:00
if ( TypeDef != null )
{
if ( NativeType.EndsWith( "*" ) )
{
return $"IntPtr /*{NativeType}*/ {Name}";
}
else
{
return $"{TypeDef.Name} /*{NativeType}*/ {Name}";
}
}
if ( NativeType.EndsWith( "*" ) && ManagedType.Contains( "_t" ) )
{
return $"IntPtr /*{NativeType}*/ {Name} ";
}
return $"{ManagedType} /*{NativeType}*/ {Name} ";
}
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}*/";
}
}
}