Facepunch.Steamworks/Generator/CodeParser/ParseClasses.cs

156 lines
4.2 KiB
C#
Raw Normal View History

2019-04-12 17:41:40 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Generator
{
partial class CodeParser
{
public List<Class> Classes = new List<Class>();
public void ParseClasses()
{
var source = RemoveAnnotations( Content );
{
var r = new Regex( @"class ([a-zA-Z]+)[\r|\n]+{[\r|\n]((?s).*?)};" );
var ma = r.Matches( source );
foreach ( Match m in ma )
{
ProcessClass( m.Groups[0].Value.Trim(), m.Groups[1].Value.Trim(), m.Groups[2].Value.Trim() );
//def.CallbackIds.Add( m.Groups[1].Value.Substring( 3 ).Replace( "Callbacks", "" ), int.Parse( m.Groups[2].Value ) );
}
}
Console.WriteLine( "OKay" );
}
public void ProcessClass( string fulldef, string classname, string inner )
{
Console.WriteLine( $"Class: {classname} " );
var lines = inner.Split( new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries );
var func = new Regex( @"virtual (.+[\t |\*])([a-z0-9A-Z]+?)\((.+?)?\) = 0 ?;$" );
2019-04-12 17:41:40 +03:00
var c = new Class();
c.Name = classname;
2019-04-13 00:52:55 +03:00
var interfaceMatch = Regex.Match( Content, $"#define {classname.ToUpper().Substring( 1 )}_INTERFACE_VERSION \"(.+?)\"" );
if ( interfaceMatch.Success )
{
c.InterfaceString = interfaceMatch.Groups[1].Value;
}
var lastCallResult = "";
2019-04-16 23:13:55 +03:00
var partialLine = "";
var needsEndIf = false;
foreach ( var linestr in lines )
2019-04-12 17:41:40 +03:00
{
var line = linestr.Trim();
var commentPos = line.IndexOf( "//" );
if ( commentPos > 0 )
line = line.Substring( 0, commentPos-1 ).Trim();
2019-04-16 23:13:55 +03:00
2019-04-12 17:41:40 +03:00
if ( line.Trim().Length < 4 ) continue;
if ( line.Trim().StartsWith( "public:" ) ) continue;
if ( line.Trim().StartsWith( "//" ) ) continue;
2019-04-27 16:25:54 +03:00
if ( line.Trim().StartsWith( "#ifdef _PS3" ) || line.Trim().StartsWith( "#if defined(_PS3)" ) )
2019-04-16 23:13:55 +03:00
{
needsEndIf = true;
continue;
}
if ( needsEndIf )
{
needsEndIf = !line.Trim().StartsWith( "#endif" );
continue;
}
2019-04-16 18:50:05 +03:00
var callresult = Regex.Match( line, @"STEAM_CALL_RESULT\((.+?)\)" );
if ( callresult.Success )
{
2019-04-16 23:13:55 +03:00
partialLine = "";
lastCallResult = callresult.Groups[1].Value.Trim();
continue;
}
2019-04-16 23:13:55 +03:00
if ( !string.IsNullOrEmpty( partialLine ) )
2019-04-12 17:41:40 +03:00
{
2019-04-16 23:13:55 +03:00
partialLine += " " + line.Trim();
2019-04-15 16:19:40 +03:00
2019-04-16 23:13:55 +03:00
if ( !partialLine.Trim().EndsWith( ";" ) )
continue;
line = partialLine;
partialLine = "";
}
2019-04-15 16:19:40 +03:00
2019-04-16 23:13:55 +03:00
var f = func.Match( line );
if ( f.Success )
{
2019-04-12 17:41:40 +03:00
var returnType = f.Groups[1].Value.Trim();
var funcName = f.Groups[2].Value.Trim();
var args = f.Groups[3].Value.Trim();
// Console.WriteLine( $"Function: {funcName} returns {returnType} with args {args}" );
2019-04-17 16:07:58 +03:00
if ( funcName == "RequestUGCDetails" ) lastCallResult = "SteamUGCRequestUGCDetailsResult_t";
2019-04-15 16:19:40 +03:00
if ( funcName == "DownloadClanActivityCounts" ) lastCallResult = "DownloadClanActivityCountsResult_t";
2019-04-12 17:41:40 +03:00
if ( funcName.Contains( ' ' ) || funcName.Contains( '*' ) )
throw new System.Exception( "Parsing Error!" );
var fnc = c.AddFunction( funcName, returnType, args );
fnc.CallResult = lastCallResult;
lastCallResult = null;
2019-04-16 23:13:55 +03:00
partialLine = "";
2019-04-12 17:41:40 +03:00
}
else
{
2019-04-16 23:13:55 +03:00
if ( line.Trim().StartsWith( "virtual " ) )
{
partialLine = line;
}
2019-04-12 17:41:40 +03:00
Console.WriteLine( $"Unknown Line: {line}" );
}
}
2019-04-16 18:50:05 +03:00
c.PostProcess();
2019-04-12 17:41:40 +03:00
Classes.Add( c );
}
public string RemoveAnnotations( string str )
{
str = Regex.Replace( str, @"STEAM_OUT_ARRAY_CALL\((.+?)\)", "" );
str = Regex.Replace( str, @"STEAM_PRIVATE_API\((.+)\)", "$1" );
2019-04-15 13:41:19 +03:00
str = Regex.Replace( str, @"STEAM_ARRAY_COUNT\((.+?)\) ", "" );
2019-04-15 17:26:52 +03:00
str = Regex.Replace( str, @"STEAM_OUT_STRUCT\(\) ", "" );
2019-04-15 16:19:40 +03:00
str = Regex.Replace( str, @"STEAM_OUT_STRUCT\((.+?)\) ", "" );
2019-04-16 23:13:55 +03:00
str = Regex.Replace( str, @"STEAM_OUT_ARRAY_COUNT\((.+?)\)", "" );
str = Regex.Replace( str, @"STEAM_ARRAY_COUNT_D\((.+?)\)", "" );
2019-04-17 16:07:58 +03:00
str = Regex.Replace( str, @"STEAM_OUT_STRING_COUNT\((.+?)\)", "" );
2019-04-27 16:25:54 +03:00
str = Regex.Replace( str, @"STEAM_OUT_STRING\(\) ", "" );
2019-04-27 17:46:20 +03:00
str = Regex.Replace( str, @"STEAM_OUT_BUFFER_COUNT\((.+?)\) ", "" );
str = Regex.Replace( str, @"STEAM_BUFFER_COUNT\((.+?)\) ", "" );
str = Regex.Replace( str, @"STEAM_DESC\((.+?)\) ", "" );
2019-04-15 16:19:40 +03:00
2019-04-12 17:41:40 +03:00
return str;
}
}
}