Facepunch.Steamworks/Generator/CodeWriter/GlobalFunctions.cs

133 lines
3.5 KiB
C#
Raw Normal View History

2019-04-29 15:28:20 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Generator
{
2020-02-12 17:55:36 +03:00
/*
2019-04-29 15:28:20 +03:00
public partial class CodeWriter
{
public void GenerateGlobalFunctions( string startingWith, string filename )
{
var functions = def.methods.Where( x => x.Name.StartsWith( startingWith ) );
sb = new StringBuilder();
WriteLine( $"using System;" );
WriteLine( $"using System.Runtime.InteropServices;" );
WriteLine( $"using System.Text;" );
WriteLine( $"using System.Threading.Tasks;" );
WriteLine( $"using Steamworks.Data;" );
WriteLine();
WriteLine();
StartBlock( $"namespace Steamworks" );
{
StartBlock( $"internal static class {startingWith}" );
{
2019-06-25 14:13:04 +03:00
StartBlock( $"internal static class Native" );
2019-04-29 15:28:20 +03:00
{
foreach ( var func in functions )
{
2019-06-25 14:13:04 +03:00
WriteMarshalledFunction( func );
2019-04-29 17:47:23 +03:00
}
}
EndBlock();
2019-04-29 15:28:20 +03:00
foreach ( var func in functions )
{
WriteGlobalFunction( startingWith, func );
WriteLine();
}
}
EndBlock();
}
EndBlock();
System.IO.File.WriteAllText( $"{filename}", sb.ToString().Replace( "( )", "()" ) );
}
private void WriteGlobalFunction( string cname, SteamApiDefinition.MethodDef func )
{
var cleanName = func.Name.Substring( cname.Length ).Trim( '_' );
var returnType = BaseType.Parse( func.ReturnType );
returnType.Func = func.Name;
if ( func.Params == null )
func.Params = new SteamApiDefinition.MethodDef.ParamType[0];
var args = func.Params.Select( x =>
{
var bt = BaseType.Parse( x.Type, x.Name );
bt.Func = func.Name;
return bt;
} ).ToArray();
var argstr = string.Join( ", ", args.Select( x => x.AsArgument() ) );
var delegateargstr = string.Join( ", ", args.Select( x => x.AsArgument() ) );
if ( returnType.IsReturnedWeird )
{
throw new System.Exception( "TODO" );
}
StartBlock( $"static internal {returnType.ReturnType} {cleanName}( {argstr} )" );
{
var callargs = string.Join( ", ", args.Select( x => x.AsCallArgument() ) );
2019-06-25 14:13:04 +03:00
if ( returnType.IsReturnedWeird )
2019-04-29 15:28:20 +03:00
{
2019-06-25 14:13:04 +03:00
WriteLine( $"var retVal = default( {returnType.TypeName} );" );
WriteLine( $"Native.{func.Name}( ref retVal, {callargs} );" );
WriteLine( $"{returnType.Return( "retVal" )}" );
2019-04-29 15:28:20 +03:00
}
2019-06-25 14:13:04 +03:00
else if ( returnType.IsVoid )
2019-04-29 15:28:20 +03:00
{
2019-06-25 14:13:04 +03:00
WriteLine( $"Native.{func.Name}( {callargs} );" );
2019-04-29 15:28:20 +03:00
}
2019-06-25 14:13:04 +03:00
else
2019-04-29 15:28:20 +03:00
{
2019-06-25 14:13:04 +03:00
var v = $"Native.{func.Name}( {callargs} )";
WriteLine( returnType.Return( v ) );
2019-04-29 15:28:20 +03:00
}
}
EndBlock();
}
2019-06-25 14:13:04 +03:00
private void WriteMarshalledFunction( SteamApiDefinition.MethodDef func )
2019-04-29 15:28:20 +03:00
{
var returnType = BaseType.Parse( func.ReturnType );
returnType.Func = func.Name;
if ( func.Params == null )
func.Params = new SteamApiDefinition.MethodDef.ParamType[0];
var args = func.Params.Select( x =>
{
var bt = BaseType.Parse( x.Type, x.Name );
bt.Func = func.Name;
return bt;
} ).ToArray();
var argstr = string.Join( ", ", args.Select( x => x.AsArgument() ) );
var delegateargstr = string.Join( ", ", args.Select( x => x.AsArgument() ) );
2019-06-25 14:13:04 +03:00
WriteLine( $"[DllImport( Platform.LibraryName, EntryPoint = \"{func.Name}\", CallingConvention = CallingConvention.Cdecl )]" );
2019-04-29 15:28:20 +03:00
if ( returnType.ReturnAttribute != null )
WriteLine( returnType.ReturnAttribute );
WriteLine( $"public static extern {(returnType.IsReturnedWeird ? "void" : returnType.TypeNameFrom)} {func.Name}( {delegateargstr} );" );
WriteLine();
}
2020-02-12 17:55:36 +03:00
2019-04-29 15:28:20 +03:00
}
2020-02-12 17:55:36 +03:00
*/
2019-04-29 15:28:20 +03:00
}