Facepunch.Steamworks/Generator/CodeParser/CodeParser.cs

76 lines
2.3 KiB
C#
Raw Normal View History

2016-10-29 22:28:16 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Generator
{
class CodeParser
{
public string Content;
public CodeParser( string folder )
{
foreach ( var file in System.IO.Directory.GetFiles( folder, "*.h", System.IO.SearchOption.AllDirectories ) )
{
Content += System.IO.File.ReadAllText( file );
}
Content = Content.Replace( "\r\n", "\n" );
Content = Content.Replace( "\n\r", "\n" );
}
internal void ExtendDefinition( SteamApiDefinition def )
{
2016-10-31 12:00:47 +03:00
//
// Get a list of CallbackIds
//
2016-10-29 22:28:16 +03:00
def.CallbackIds = new Dictionary<string, int>();
{
var r = new Regex( @"enum { (k_i(?:.+)) = ([0-9]+) };" );
var ma = r.Matches( Content );
foreach ( Match m in ma )
{
def.CallbackIds.Add( m.Groups[1].Value.Replace( "k_i", "" ).Replace( "Callbacks", "" ), int.Parse( m.Groups[2].Value ) );
}
}
2016-10-31 12:00:47 +03:00
//
// Associate callbackIds with structs
//
2016-10-29 22:28:16 +03:00
foreach ( var t in def.structs )
{
2016-10-29 22:50:40 +03:00
var r = new Regex( @"struct "+t.Name+@"\n{ ?\n(?:.)+enum { k_iCallback = (.+) \+ ([0-9]+)", RegexOptions.Multiline | RegexOptions.IgnoreCase );
2016-10-29 22:28:16 +03:00
var m = r.Match( Content );
if ( m.Success )
{
var kName = m.Groups[1].Value;
var num = m.Groups[2].Value;
kName = kName.Replace( "k_i", "CallbackIdentifiers." ).Replace( "Callbacks", "" );
t.CallbackId = $"{kName} + {num}";
}
}
2016-10-31 12:00:47 +03:00
//
// Find defines
//
2016-10-29 23:05:48 +03:00
def.Defines = new Dictionary<string, string>();
{
var r = new Regex( @"#define ([a-zA-Z_]+) ""(.+)""" );
var ma = r.Matches( Content );
foreach ( Match m in ma )
{
def.Defines.Add( m.Groups[1].Value.Replace( "Callbacks", "" ), m.Groups[2].Value );
}
}
2016-10-29 22:28:16 +03:00
}
}
}