167 lines
4.9 KiB
C#
Raw Normal View History

2016-10-25 10:29:35 +01: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 16:11:29 +01:00
public partial class CodeWriter
2016-10-25 10:29:35 +01:00
{
2020-02-22 20:16:04 +00:00
public static CodeWriter Current { get; private set; }
2016-10-25 10:29:35 +01:00
private SteamApiDefinition def;
2020-02-22 20:16:04 +00:00
public CodeWriter( SteamApiDefinition def )
2016-10-25 10:29:35 +01:00
{
this.def = def;
2020-02-22 20:16:04 +00:00
Current = this;
2016-10-25 10:29:35 +01:00
WorkoutTypes();
}
public void ToFolder( string folder )
{
{
sb = new StringBuilder();
Header();
Enums();
Footer();
2019-04-16 11:45:44 +01:00
System.IO.File.WriteAllText( $"{folder}SteamEnums.cs", sb.ToString() );
2020-02-22 20:16:04 +00:00
}
{
sb = new StringBuilder();
Header();
CustomEnums();
Footer();
System.IO.File.WriteAllText( $"{folder}CustomEnums.cs", sb.ToString() );
2016-10-25 10:29:35 +01:00
}
{
sb = new StringBuilder();
2019-04-16 15:05:19 +01:00
Header( "Steamworks.Data" );
2016-10-25 10:29:35 +01:00
Types();
Footer();
2019-04-16 11:45:44 +01:00
System.IO.File.WriteAllText( $"{folder}SteamTypes.cs", sb.ToString() );
2016-10-25 10:29:35 +01:00
}
2020-02-22 20:16:04 +00:00
{
sb = new StringBuilder();
Header( "Steamworks.Data" );
2020-02-24 10:14:31 +00:00
Callbacks();
2020-02-22 20:16:04 +00:00
Footer();
System.IO.File.WriteAllText( $"{folder}SteamCallbacks.cs", sb.ToString() );
}
2016-10-25 10:29:35 +01:00
{
sb = new StringBuilder();
2019-04-16 15:00:22 +01:00
Header( "Steamworks.Data" );
2016-10-25 10:29:35 +01:00
Structs();
Footer();
2019-04-16 11:45:44 +01:00
System.IO.File.WriteAllText( $"{folder}SteamStructs.cs", sb.ToString() );
2016-10-25 10:29:35 +01:00
}
2016-10-29 20:28:16 +01:00
{
sb = new StringBuilder();
2019-04-16 15:05:19 +01:00
Header( "Steamworks.Data" );
2016-10-29 20:28:16 +01:00
Constants();
Footer();
2019-04-16 11:45:44 +01:00
System.IO.File.WriteAllText( $"{folder}SteamConstants.cs", sb.ToString() );
2020-02-24 10:14:31 +00:00
}
{
sb = new StringBuilder();
Header( "Steamworks.Data" );
StructFunctions();
Footer();
System.IO.File.WriteAllText( $"{folder}SteamStructFunctions.cs", sb.ToString() );
2016-10-25 10:29:35 +01:00
}
2019-04-29 13:28:20 +01:00
{
2020-02-22 20:16:04 +00:00
// GenerateGlobalFunctions( "SteamAPI", $"{folder}../Generated/SteamAPI.cs" );
// GenerateGlobalFunctions( "SteamGameServer", $"{folder}../Generated/SteamGameServer.cs" );
// GenerateGlobalFunctions( "SteamInternal", $"{folder}../Generated/SteamInternal.cs" );
2019-04-29 13:28:20 +01:00
}
2020-02-22 20:16:04 +00:00
foreach ( var iface in def.Interfaces )
{
GenerateInterface( iface, $"{folder}../Generated/Interfaces/" );
}
}
2016-10-25 10:29:35 +01: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 12:17:24 +01:00
TypeDefs.Add( c.Name, new TypeDef()
2016-10-25 10:29:35 +01:00
{
Name = c.Name,
NativeType = c.Type,
ManagedType = type,
} );
}
}
2019-04-16 11:45:44 +01:00
private void Header( string NamespaceName = "Steamworks" )
2016-10-25 10:29:35 +01:00
{
WriteLine( "using System;" );
WriteLine( "using System.Runtime.InteropServices;" );
WriteLine( "using System.Linq;" );
2019-04-16 14:38:10 +01:00
WriteLine( "using Steamworks.Data;" );
WriteLine( "using System.Threading.Tasks;" );
2016-10-25 10:29:35 +01:00
WriteLine();
2016-11-11 14:51:04 +00:00
StartBlock( "namespace " + NamespaceName );
2016-10-25 10:29:35 +01:00
}
2016-10-29 13:02:36 +01:00
private void Footer()
{
EndBlock();
}
2020-02-22 20:16:04 +00: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 13:02:36 +01:00
}
2016-10-25 10:29:35 +01:00
}