Facepunch.Steamworks/Generator/CodeWriter/CodeWriter.cs

164 lines
5.0 KiB
C#
Raw Normal View History

2016-10-25 12:29:35 +03:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Generator;
namespace Generator
{
2016-10-25 18:11:29 +03:00
public partial class CodeWriter
2016-10-25 12:29:35 +03:00
{
private SteamApiDefinition def;
public CodeWriter( SteamApiDefinition def )
2016-10-25 12:29:35 +03:00
{
this.def = def;
WorkoutTypes();
}
public void ToFolder( string folder )
{
{
sb = new StringBuilder();
Header();
Enums();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamNative.Enums.cs", sb.ToString() );
}
{
sb = new StringBuilder();
Header();
Types();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamNative.Types.cs", sb.ToString() );
}
{
sb = new StringBuilder();
Header();
Structs();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamNative.Structs.cs", sb.ToString() );
}
2016-10-29 22:28:16 +03:00
{
sb = new StringBuilder();
Header();
Constants();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamNative.Constants.cs", sb.ToString() );
}
2016-10-25 15:22:59 +03:00
{
sb = new StringBuilder();
Header();
PlatformInterface();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamNative.Platform.Interface.cs", sb.ToString() );
}
2016-10-25 12:29:35 +03:00
{
sb = new StringBuilder();
Header();
2016-10-25 17:35:48 +03:00
PlatformClass( "Win32", "steam_api.dll", true );
2016-10-25 12:29:35 +03:00
Footer();
System.IO.File.WriteAllText( $"{folder}SteamNative.Platform.Win32.cs", sb.ToString() );
}
{
sb = new StringBuilder();
Header();
2016-10-25 17:35:48 +03:00
PlatformClass( "Win64", "steam_api64.dll", true );
2016-10-25 12:29:35 +03:00
Footer();
System.IO.File.WriteAllText( $"{folder}SteamNative.Platform.Win64.cs", sb.ToString() );
}
{
sb = new StringBuilder();
Header();
2016-10-25 17:35:48 +03:00
PlatformClass( "Linux32", "libsteam_api.so", false );
2016-10-25 15:22:59 +03:00
Footer();
System.IO.File.WriteAllText( $"{folder}SteamNative.Platform.Linux32.cs", sb.ToString() );
}
{
sb = new StringBuilder();
Header();
2016-10-25 17:35:48 +03:00
PlatformClass( "Linux64", "libsteam_api64.so", false );
2016-10-25 12:29:35 +03:00
Footer();
2016-10-25 15:22:59 +03:00
System.IO.File.WriteAllText( $"{folder}SteamNative.Platform.Linux64.cs", sb.ToString() );
2016-10-25 12:29:35 +03:00
}
{
sb = new StringBuilder();
Header();
2016-10-25 17:35:48 +03:00
PlatformClass( "Mac", "libsteam_api.dylib", false );
2016-10-25 12:29:35 +03:00
Footer();
System.IO.File.WriteAllText( $"{folder}SteamNative.Platform.Mac.cs", sb.ToString() );
}
{
2016-10-29 15:02:36 +03:00
GenerateClasses( $"{folder}SteamNative." );
2016-10-25 12:29:35 +03:00
}
}
void WorkoutTypes()
{
def.typedefs.Add( new SteamApiDefinition.TypeDef() { Name = "CGameID", Type = "ulong" } );
def.typedefs.Add( new SteamApiDefinition.TypeDef() { Name = "CSteamID", Type = "ulong" } );
foreach ( var c in def.typedefs )
{
if ( c.Name.StartsWith( "uint" ) || c.Name.StartsWith( "int" ) || c.Name.StartsWith( "lint" ) || c.Name.StartsWith( "luint" ) || c.Name.StartsWith( "ulint" ) )
continue;
// Unused, messers
if ( c.Name == "Salt_t" || c.Name == "compile_time_assert_type" || c.Name == "ValvePackingSentinel_t" || c.Name.Contains( "::" ) || c.Type.Contains( "(*)" ) )
continue;
var type = c.Type;
type = ToManagedType( type );
TypeDefs.Add( c.Name, new TypeDef()
{
Name = c.Name,
NativeType = c.Type,
ManagedType = type,
} );
}
}
private List<Argument> BuildArguments( SteamApiDefinition.MethodDef.ParamType[] ps )
{
var args = new List<Argument>();
if ( ps == null ) return args;
foreach ( var p in ps )
{
2016-10-25 18:16:02 +03:00
var a = new Argument( p.Name, p.Type, TypeDefs );
2016-10-25 12:29:35 +03:00
args.Add( a );
}
return args;
}
2016-11-11 17:51:04 +03:00
private void Header( string NamespaceName = "SteamNative" )
2016-10-25 12:29:35 +03:00
{
WriteLine( "using System;" );
WriteLine( "using System.Runtime.InteropServices;" );
WriteLine( "using System.Linq;" );
2016-10-25 12:29:35 +03:00
WriteLine();
2016-11-11 17:51:04 +03:00
StartBlock( "namespace " + NamespaceName );
2016-10-25 12:29:35 +03:00
}
2016-10-29 15:02:36 +03:00
private void Footer()
{
EndBlock();
}
}
2016-10-25 12:29:35 +03:00
}