|
|
@@ -12,59 +12,67 @@ namespace MoonSharp.Interpreter.Interop
|
|
|
public class CustomConvertersCollection
|
|
|
{
|
|
|
private Dictionary<Type, Func<DynValue, object>>[] m_Script2Clr = new Dictionary<Type, Func<DynValue, object>>[(int)LuaTypeExtensions.MaxConvertibleTypes + 1];
|
|
|
- private Dictionary<Type, Dictionary<Type, Func<object, object>>> m_Script2ClrUserData = new Dictionary<Type, Dictionary<Type, Func<object, object>>>();
|
|
|
private Dictionary<Type, Func<Script, object, DynValue>> m_Clr2Script = new Dictionary<Type, Func<Script, object, DynValue>>();
|
|
|
|
|
|
+
|
|
|
+
|
|
|
internal CustomConvertersCollection()
|
|
|
{
|
|
|
for (int i = 0; i < m_Script2Clr.Length; i++)
|
|
|
m_Script2Clr[i] = new Dictionary<Type, Func<DynValue, object>>();
|
|
|
}
|
|
|
|
|
|
- public void SetScriptToClrUserDataSpecificCustomConversion(Type destType, Type userDataType, Func<object, object> converter = null)
|
|
|
- {
|
|
|
- var destTypeMap = m_Script2ClrUserData.GetOrCreate(destType, () => new Dictionary<Type, Func<object, object>>());
|
|
|
- destTypeMap[userDataType] = converter;
|
|
|
+ // This needs to be evaluated further (doesn't work well with inheritance)
|
|
|
+ //
|
|
|
+ // private Dictionary<Type, Dictionary<Type, Func<object, object>>> m_Script2ClrUserData = new Dictionary<Type, Dictionary<Type, Func<object, object>>>();
|
|
|
+ //
|
|
|
+ //public void SetScriptToClrUserDataSpecificCustomConversion(Type destType, Type userDataType, Func<object, object> converter = null)
|
|
|
+ //{
|
|
|
+ // var destTypeMap = m_Script2ClrUserData.GetOrCreate(destType, () => new Dictionary<Type, Func<object, object>>());
|
|
|
+ // destTypeMap[userDataType] = converter;
|
|
|
|
|
|
- SetScriptToClrCustomConversion(DataType.UserData, destType, v => DispatchUserDataCustomConverter(destTypeMap, v));
|
|
|
- }
|
|
|
+ // SetScriptToClrCustomConversion(DataType.UserData, destType, v => DispatchUserDataCustomConverter(destTypeMap, v));
|
|
|
+ //}
|
|
|
|
|
|
- private object DispatchUserDataCustomConverter(Dictionary<Type, Func<object, object>> destTypeMap, DynValue v)
|
|
|
- {
|
|
|
- if (v.Type != DataType.UserData)
|
|
|
- return null;
|
|
|
+ //private object DispatchUserDataCustomConverter(Dictionary<Type, Func<object, object>> destTypeMap, DynValue v)
|
|
|
+ //{
|
|
|
+ // if (v.Type != DataType.UserData)
|
|
|
+ // return null;
|
|
|
|
|
|
- if (v.UserData.Object == null)
|
|
|
- return null;
|
|
|
+ // if (v.UserData.Object == null)
|
|
|
+ // return null;
|
|
|
|
|
|
- Type userDataType = v.UserData.Object.GetType();
|
|
|
+ // Func<object, object> converter;
|
|
|
|
|
|
- Func<object, object> converter;
|
|
|
+ // for (Type userDataType = v.UserData.Object.GetType();
|
|
|
+ // userDataType != typeof(object);
|
|
|
+ // userDataType = userDataType.BaseType)
|
|
|
+ // {
|
|
|
+ // if (destTypeMap.TryGetValue(userDataType, out converter))
|
|
|
+ // {
|
|
|
+ // return converter(v.UserData.Object);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
- if (destTypeMap.TryGetValue(userDataType, out converter))
|
|
|
- {
|
|
|
- return converter(v.UserData.Object);
|
|
|
- }
|
|
|
+ // return null;
|
|
|
+ //}
|
|
|
|
|
|
- return null;
|
|
|
- }
|
|
|
+ //public Func<object, object> GetScriptToClrUserDataSpecificCustomConversion(Type destType, Type userDataType)
|
|
|
+ //{
|
|
|
+ // Dictionary<Type, Func<object, object>> destTypeMap;
|
|
|
|
|
|
- public Func<object, object> GetScriptToClrUserDataSpecificCustomConversion(Type destType, Type userDataType)
|
|
|
- {
|
|
|
- Dictionary<Type, Func<object, object>> destTypeMap;
|
|
|
+ // if (m_Script2ClrUserData.TryGetValue(destType, out destTypeMap))
|
|
|
+ // {
|
|
|
+ // Func<object, object> converter;
|
|
|
|
|
|
- if (m_Script2ClrUserData.TryGetValue(destType, out destTypeMap))
|
|
|
- {
|
|
|
- Func<object, object> converter;
|
|
|
+ // if (destTypeMap.TryGetValue(userDataType, out converter))
|
|
|
+ // {
|
|
|
+ // return converter;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
- if (destTypeMap.TryGetValue(userDataType, out converter))
|
|
|
- {
|
|
|
- return converter;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
+ // return null;
|
|
|
+ //}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -168,36 +176,6 @@ namespace MoonSharp.Interpreter.Interop
|
|
|
}
|
|
|
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Registers a proxy handler
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="TProxy">The type of the proxy.</typeparam>
|
|
|
- /// <typeparam name="TTarget">The type of the target.</typeparam>
|
|
|
- /// <param name="proxyHandler">The proxy handler.</param>
|
|
|
- public void RegisterProxy<TProxy, TTarget>(IProxyHandler<TProxy, TTarget> proxyHandler)
|
|
|
- where TProxy : class
|
|
|
- where TTarget : class
|
|
|
- {
|
|
|
- SetClrToScriptCustomConversion<TTarget>((s, t) => DynValue.FromObject(s, proxyHandler.ProxyWrap(t)));
|
|
|
- SetScriptToClrCustomConversion(DataType.UserData, typeof(TTarget), v => proxyHandler.ProxyUnwrap((TProxy)v.UserData.Object));
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Registers a proxy handler using DelegateProxyHandler and the specified delegates.
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="TProxy">The type of the proxy.</typeparam>
|
|
|
- /// <typeparam name="TTarget">The type of the target.</typeparam>
|
|
|
- /// <param name="wrapDelegate">The wrap delegate.</param>
|
|
|
- /// <param name="unwrapDelegate">The unwrap delegate.</param>
|
|
|
- public void RegisterProxy<TProxy, TTarget>(Func<TTarget, TProxy> wrapDelegate, Func<TProxy, TTarget> unwrapDelegate)
|
|
|
- where TProxy : class
|
|
|
- where TTarget : class
|
|
|
- {
|
|
|
- RegisterProxy(new DelegateProxyHandler<TProxy, TTarget>(wrapDelegate, unwrapDelegate));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Removes all converters.
|
|
|
/// </summary>
|