Facepunch.Steamworks/Generator/CodeWriter/Enums.cs

100 lines
3.0 KiB
C#
Raw Normal View History

2016-10-29 14:38:27 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Generator
{
public partial class CodeWriter
{
private void Enums()
{
foreach ( var o in def.enums )
{
WriteLine( $"//" );
WriteLine( $"// {o.Name}" );
WriteLine( $"//" );
var name = o.Name;
// We're not interested in namespacing
if ( name.Contains( "::" ) )
name = o.Name.Substring( o.Name.LastIndexOf( ":" ) + 1 );
// Skip the E
if ( name[0] == 'E' )
name = name.Substring( 1 );
2020-02-19 12:00:56 +03:00
name = Cleanup.ConvertType( name );
2019-04-16 14:17:24 +03:00
2020-02-19 12:00:56 +03:00
if ( !Cleanup.ShouldCreate( name ) )
continue;
2019-04-17 10:40:58 +03:00
2020-02-12 18:43:53 +03:00
var lowest = o.Values.Min( x => long.Parse( x.Value ) );
var highest = o.Values.Max( x => long.Parse( x.Value ) );
var t = "int";
if ( highest > int.MaxValue )
t = "uint";
2020-02-19 12:00:56 +03:00
WriteEnum( o, name, t );
}
}
2020-02-12 18:43:53 +03:00
2020-02-19 12:00:56 +03:00
private void WriteEnum( SteamApiDefinition.EnumDef o, string name, string t = "int" )
{
StartBlock( $"{Cleanup.Expose( name )} enum {name} : {t}" );
{
//
// If all the enum values start with the same
// string, remove it. This converts
// "k_EUserHasLicenseResultHasLicense" to "HasLicense" etc
//
int iFinished = int.MaxValue;
for ( int i = 0; i < 4096; i++ )
2016-10-29 14:38:27 +03:00
{
2020-02-19 12:00:56 +03:00
var c = o.Values.First().Name[i];
foreach ( var entry in o.Values )
2016-10-29 14:38:27 +03:00
{
2020-02-19 12:00:56 +03:00
if ( entry.Name[i] != c )
2016-10-29 14:38:27 +03:00
{
2020-02-19 12:00:56 +03:00
iFinished = i;
2016-10-29 14:38:27 +03:00
break;
2020-02-19 12:00:56 +03:00
}
2016-10-29 14:38:27 +03:00
}
2020-02-19 12:00:56 +03:00
if ( iFinished != int.MaxValue )
break;
}
2016-10-29 14:38:27 +03:00
2020-02-19 12:00:56 +03:00
foreach ( var entry in o.Values )
{
var ename = entry.Name;
2016-10-29 14:38:27 +03:00
2020-02-19 12:00:56 +03:00
if ( iFinished != int.MaxValue )
ename = ename.Substring( iFinished );
2020-02-12 18:43:53 +03:00
2020-02-19 12:00:56 +03:00
//
// Names aren't allowed to start with a number
// So just stick the enum name on the front
//
if ( char.IsNumber( ename[0] ) )
{
var p = name;
2020-02-12 18:43:53 +03:00
2020-02-19 12:00:56 +03:00
if ( p == "HTTPStatusCode" ) p = "Code";
if ( p == "SteamIPType" ) p = "Type";
2016-10-29 14:38:27 +03:00
2020-02-19 12:00:56 +03:00
ename = p + ename;
2016-10-29 14:38:27 +03:00
}
2020-02-19 12:00:56 +03:00
2020-02-19 12:46:39 +03:00
WriteLine( $"{ename.Trim( ' ', '_' )} = {entry.Value}," );
2016-10-29 14:38:27 +03:00
}
}
2020-02-19 12:00:56 +03:00
EndBlock();
WriteLine();
2016-10-29 14:38:27 +03:00
}
}
}