mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-08 23:43:18 +02:00
Use source generator instead of reflection
This commit is contained in:
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
using YesNt.Interpreter.Attributes;
|
||||
using YesNt.Interpreter.Enums;
|
||||
@@ -49,47 +48,9 @@ public class YesNtInterpreter
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
Type[] types = assembly.GetTypes();
|
||||
|
||||
IEnumerable<Type> allStatementRuntimeInfo = types.Where(t => t.IsSubclassOf(typeof(StatementRuntimeInformation)));
|
||||
|
||||
statements.Clear();
|
||||
|
||||
foreach (Type type in allStatementRuntimeInfo)
|
||||
{
|
||||
object statementInfo = Activator.CreateInstance(type);
|
||||
|
||||
MethodInfo[] allMethodInfo = statementInfo.GetType().GetMethods();
|
||||
|
||||
StatementRuntimeInformation statementRuntimeInfo = statementInfo as StatementRuntimeInformation;
|
||||
statementRuntimeInfo.RuntimeInfo = runtimeInfo;
|
||||
|
||||
foreach (MethodInfo methodInfo in allMethodInfo)
|
||||
{
|
||||
StatementAttribute statementAttribute = methodInfo.GetCustomAttribute<StatementAttribute>();
|
||||
if (statementAttribute is not null)
|
||||
{
|
||||
Action<string> method = methodInfo.CreateDelegate(typeof(Action<string>), statementInfo) as Action<string>;
|
||||
statements.Add(statementAttribute, method);
|
||||
}
|
||||
|
||||
StaticStatementAttribute staticStatementAttribute = methodInfo.GetCustomAttribute<StaticStatementAttribute>();
|
||||
if (staticStatementAttribute is not null)
|
||||
{
|
||||
Action method = methodInfo.CreateDelegate(typeof(Action), statementInfo) as Action;
|
||||
staticStatements.Add(new(staticStatementAttribute, method));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
statements = statements
|
||||
.OrderBy(s => s.Key.Priority)
|
||||
.ThenByDescending(s => s.Key.Name.Length)
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
staticStatements = staticStatements
|
||||
.OrderBy(s => s.Key.Priority)
|
||||
.ToList();
|
||||
staticStatements.Clear();
|
||||
GeneratedStatementRegistry.Register(runtimeInfo, out statements, out staticStatements);
|
||||
|
||||
runtimeInfo.OnDebugOutput += (s) => OnDebugOutput?.Invoke(s);
|
||||
runtimeInfo.OnLineExecuted += (DebugEventArgs e) => OnLineExecuted?.Invoke(e);
|
||||
@@ -283,4 +244,4 @@ public class YesNtInterpreter
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
@@ -9,4 +9,10 @@
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\YesNt.Interpreter.Generator\YesNt.Interpreter.Generator.csproj"
|
||||
OutputItemType="Analyzer"
|
||||
ReferenceOutputAssembly="false" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user