using System; using MoonSharp.Interpreter; namespace WingsAPI.Scripting.LUA.Converter { /// /// Converter used to convert CSharp object to Lua object & Lua object to CSharp object /// public interface IConverter { /// /// Type of lua data /// DataType DataType { get; } /// /// Type of the object created by this converted /// Type ObjectType { get; } /// /// Create lua object from csharp object /// /// Script used to create this object /// Value to convert /// Converted value DynValue ToLuaObject(Script script, object value); /// /// Create csharp object from lua object /// /// Lua object to convert /// Converted value object ToCSharpObject(DynValue value); } public abstract class Converter : IConverter { /// /// Empty object used to get correct properties name using nameof(Object.MyProperty) /// protected static readonly T Object = Activator.CreateInstance(); public virtual DataType DataType { get; } = DataType.Table; public Type ObjectType { get; } = typeof(T); public DynValue ToLuaObject(Script script, object value) => ToLuaObject(script, (T)value); object IConverter.ToCSharpObject(DynValue value) => ToCSharpObject(value); protected abstract DynValue ToLuaObject(Script script, T value); protected abstract T ToCSharpObject(DynValue value); } }