rj-action-library/Runtime/Godot/Generated/GodotClassGenerator.cs

118 lines
2.6 KiB
C#
Raw Normal View History

2024-07-26 09:26:24 +00:00
using Godot;
using System.Text;
using System.Reflection;
namespace Rokojori
{
[Tool]
[GlobalClass]
public partial class GodotClassGenerator:Node
{
[Export]
public string outputPath;
[Export]
public bool exportFlag;
[Export]
public string testClass;
2024-08-04 09:08:12 +00:00
public override void _Ready()
{
ExportFiles();
}
2024-07-26 09:26:24 +00:00
public override void _Process( double delta )
{
if ( exportFlag )
{
exportFlag = false;
2024-08-04 09:08:12 +00:00
ExportFiles();
2024-07-26 09:26:24 +00:00
}
}
2024-08-04 09:08:12 +00:00
void ExportFiles()
{
RJLog.Log( "Exporting:", testClass );
var result = CreateClass( testClass );
FilesSync.SaveUTF8( outputPath + "RJ" + testClass + ".cs", result );
}
2024-07-26 09:26:24 +00:00
public string CreateClass( string name )
{
var output = new StringBuilder();
output.Append( "using Godot;\n" );
output.Append( "\n" );
output.Append( "namespace Rokojori\n" );
output.Append( "{\n" );
output.Append( "\n" );
output.Append( " [GlobalClass]\n");
output.Append( " public partial class RJ" + name + ":" + name + "\n");
output.Append( " {\n" );
2024-08-04 09:08:12 +00:00
var classType = ReflectionHelper.GetTypeByNameFromAssembly( "Godot." + name, typeof( Node ).Assembly );
2024-07-26 09:26:24 +00:00
if ( classType == null )
{
RJLog.Log( "Type is null" );
return "";
}
output.Append( " \n" );
var bindingFlags = BindingFlags.Instance | BindingFlags.Public;
EventInfo[] events = classType.GetEvents( bindingFlags );
for ( int i = 0; i < events.Length; i++ )
{
var ev = events[ i ];
var eventName = ev.Name;
2024-08-04 09:08:12 +00:00
output.Append( " \n" );
2024-07-26 09:26:24 +00:00
output.Append( " [Export]\n" );
output.Append( " public RJAction On" + eventName + ";\n" );
}
output.Append( " \n" );
output.Append( " public override void _Ready()\n" );
output.Append( " {\n" );
for ( int i = 0; i < events.Length; i++ )
{
var ev = events[ i ];
var eventName = ev.Name;
2024-08-04 09:08:12 +00:00
var parameters = classType.GetEvent( eventName ).EventHandlerType.GetMethod( "Invoke" ).GetParameters();
var p = "( ";
2024-07-26 09:26:24 +00:00
for ( int pn = 0; pn < parameters.Length; pn ++ )
{
2024-08-04 09:08:12 +00:00
if ( pn != 0 )
{
p += ", ";
}
2024-07-26 09:26:24 +00:00
p += "p" + pn;
}
2024-08-04 09:08:12 +00:00
p += " )";
2024-07-26 09:26:24 +00:00
2024-08-04 09:08:12 +00:00
output.Append( " " + eventName + " += " + p + " => { Actions.Trigger( On" + eventName +" ); }; \n" );
2024-07-26 09:26:24 +00:00
}
output.Append( " }\n" );
output.Append( " }\n" );
output.Append( "}\n" );
2024-08-04 09:08:12 +00:00
RJLog.Log( output.ToString() );
2024-07-26 09:26:24 +00:00
return output.ToString();
}
}
}