新增Enum支持

This commit is contained in:
2025-11-25 17:03:43 +08:00
parent 5b235a7f26
commit 2f24d94db2
3 changed files with 90 additions and 37 deletions

View File

@@ -116,7 +116,7 @@ namespace Convention.RScript.Variable.CStyle
protected override string WritePageHead(Type currentType)
{
return $"#include\"{GetFilename(currentType)}\"";
return $"#include\"{GetFilename(currentType.BaseType)}\"";
}
public override string GetFilename(Type currentType)

View File

@@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEditor;
namespace Convention.RScript.Variable
{
@@ -115,6 +116,20 @@ namespace Convention.RScript.Variable
StringBuilder builder = new();
builder.AppendLine(this.WritePageHead(targetType));
if (targetType.IsEnum)
{
// 绘制枚举
builder.AppendLine(this.WriteEnumHead(targetType));
builder.AppendLine(this.WriteEnumBodyEnter(targetType));
foreach (var enumName in targetType.GetEnumNames())
{
builder.AppendLine(this.WriteEnumName(enumName));
}
builder.AppendLine(this.WriteEnumBodyExit(targetType));
builder.AppendLine(this.WriteEnumTail(targetType));
}
else
{
builder.AppendLine(this.WriteClassHead(targetType));
builder.AppendLine(this.WriteClassBodyEnter(targetType));
// 绘制枚举
@@ -153,6 +168,7 @@ namespace Convention.RScript.Variable
}
builder.AppendLine(this.WriteClassBodyExit(targetType));
builder.AppendLine(this.WriteClassTail(targetType));
}
builder.AppendLine(this.WritePageEnd(targetType));
this.scriptIndicator = builder.ToString();
}
@@ -173,9 +189,24 @@ namespace Convention.RScript.Variable
return AllRScriptInjectVariables.Remove(name);
}
public class EnumGetter
{
public Type enumType;
public EnumGetter(Type enumType)
{
this.enumType = enumType;
}
public object Get(string name)
{
return Enum.Parse(enumType, name);
}
}
public static object GenerateRScriptVariable(string name)
{
if (AllRScriptInjectVariables.TryGetValue(name, out var variable))
{
if (variable.targetType.IsEnum)
{
if (variable.MyGenerater != null)
{
@@ -188,6 +219,25 @@ namespace Convention.RScript.Variable
else
throw new InvalidOperationException($"{name} target is not sub-class of it's target type");
}
else
{
return variable.targetType;
}
}
else
{
if (variable.MyGenerater != null)
{
var result = variable.Generate();
if (result.GetType().IsSubclassOf(variable.targetType) || result.GetType() == variable.targetType)
{
variable.GenerateObjects.Add(result);
return result;
}
else
throw new InvalidOperationException($"{name} target is not sub-class of it's target type");
}
}
}
throw new InvalidOperationException($"{name} target is not exist or abstract");
}

View File

@@ -6,6 +6,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using UnityEngine;
using static Convention.RScript.RScriptContext;
namespace Convention.RScript
@@ -276,10 +277,12 @@ namespace Convention.RScript
}
catch (RScriptException)
{
Debug.LogError($"current sentence: {sentence}");
throw;
}
catch (Exception ex)
{
Debug.LogError($"current sentence: {sentence}");
throw new RScriptRuntimeException($"Runtime error: {ex.Message}", CurrentRuntimePointer, ex);
}
}