Facepunch.Steamworks/Generator/CodeWriter/CodeWriter.cs

159 lines
4.7 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
{
2020-02-20 15:09:23 +03:00
public static CodeWriter Current { get; private set; }
2016-10-25 12:29:35 +03:00
private SteamApiDefinition def;
2020-02-12 17:55:36 +03:00
public CodeWriter( SteamApiDefinition def )
2016-10-25 12:29:35 +03:00
{
this.def = def;
2020-02-20 15:09:23 +03:00
Current = this;
2016-10-25 12:29:35 +03:00
WorkoutTypes();
}
public void ToFolder( string folder )
{
{
sb = new StringBuilder();
Header();
Enums();
Footer();
2019-04-16 13:45:44 +03:00
System.IO.File.WriteAllText( $"{folder}SteamEnums.cs", sb.ToString() );
2020-02-19 16:11:40 +03:00
}
{
sb = new StringBuilder();
Header();
CustomEnums();
Footer();
System.IO.File.WriteAllText( $"{folder}CustomEnums.cs", sb.ToString() );
2016-10-25 12:29:35 +03:00
}
{
sb = new StringBuilder();
2019-04-16 17:05:19 +03:00
Header( "Steamworks.Data" );
2016-10-25 12:29:35 +03:00
Types();
Footer();
2019-04-16 13:45:44 +03:00
System.IO.File.WriteAllText( $"{folder}SteamTypes.cs", sb.ToString() );
2016-10-25 12:29:35 +03:00
}
{
sb = new StringBuilder();
Header( "Steamworks.Data" );
StructCallbacks();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamCallbacks.cs", sb.ToString() );
}
2016-10-25 12:29:35 +03:00
{
sb = new StringBuilder();
2019-04-16 17:00:22 +03:00
Header( "Steamworks.Data" );
2016-10-25 12:29:35 +03:00
Structs();
Footer();
2019-04-16 13:45:44 +03:00
System.IO.File.WriteAllText( $"{folder}SteamStructs.cs", sb.ToString() );
2016-10-25 12:29:35 +03:00
}
2016-10-29 22:28:16 +03:00
{
sb = new StringBuilder();
2019-04-16 17:05:19 +03:00
Header( "Steamworks.Data" );
2016-10-29 22:28:16 +03:00
Constants();
Footer();
2019-04-16 13:45:44 +03:00
System.IO.File.WriteAllText( $"{folder}SteamConstants.cs", sb.ToString() );
2016-10-25 12:29:35 +03:00
}
2019-04-29 15:28:20 +03:00
{
2020-02-12 17:55:36 +03:00
// GenerateGlobalFunctions( "SteamAPI", $"{folder}../Generated/SteamAPI.cs" );
// GenerateGlobalFunctions( "SteamGameServer", $"{folder}../Generated/SteamGameServer.cs" );
// GenerateGlobalFunctions( "SteamInternal", $"{folder}../Generated/SteamInternal.cs" );
2019-04-29 15:28:20 +03:00
}
2020-02-12 17:55:36 +03:00
foreach ( var iface in def.Interfaces )
{
2020-02-19 20:00:26 +03:00
GenerateInterface( iface, $"{folder}../Generated/Interfaces/" );
2020-02-12 17:55:36 +03:00
}
}
2016-10-25 12:29:35 +03:00
void WorkoutTypes()
{
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 );
2019-04-16 14:17:24 +03:00
TypeDefs.Add( c.Name, new TypeDef()
2016-10-25 12:29:35 +03:00
{
Name = c.Name,
NativeType = c.Type,
ManagedType = type,
} );
}
}
2019-04-16 13:45:44 +03:00
private void Header( string NamespaceName = "Steamworks" )
2016-10-25 12:29:35 +03:00
{
WriteLine( "using System;" );
WriteLine( "using System.Runtime.InteropServices;" );
WriteLine( "using System.Linq;" );
2019-04-16 16:38:10 +03:00
WriteLine( "using Steamworks.Data;" );
WriteLine( "using System.Threading.Tasks;" );
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();
}
2020-02-20 15:09:23 +03:00
public bool IsStruct( string name )
{
if ( def.structs.Any( x => x.Name == name || Cleanup.ConvertType( x.Name ) == name ) )
return true;
return false;
}
public bool IsTypeDef( string name )
{
if ( def.typedefs.Any( x => x.Name == name || Cleanup.ConvertType( x.Name ) == name ) )
return true;
return false;
}
public bool IsCallback( string name )
{
if ( def.callback_structs.Any( x => x.Name == name || Cleanup.ConvertType( x.Name ) == name ) )
return true;
return false;
}
public bool IsEnum( string name )
{
if ( def.enums.Any( x => x.Name == name || Cleanup.ConvertType( x.Name ) == name ) )
return true;
return false;
}
2016-10-29 15:02:36 +03:00
}
2016-10-25 12:29:35 +03:00
}