Ourpalm / ILRuntime

Pure C# IL Intepreter Runtime, which is fast and reliable for scripting requirement on enviorments, where jitting isn't possible.
Other
2.99k stars 656 forks source link

class ILType #556

Open qn9663 opened 3 years ago

qn9663 commented 3 years ago

appdomain.RegisterCrossBindingAdaptor(new IComparerAdapter());

`using System; using ILRuntime.CLR.Method; using ILRuntime.Runtime.Enviorment; using ILRuntime.Runtime.Intepreter;

namespace ProjectAdapter { public class IComparerAdapter : CrossBindingAdaptor { static CrossBindingFunctionInfo<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance,int> mExampleMethod_0 = new CrossBindingFunctionInfo<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance,int>("Compare"); public override Type BaseCLRType { get { return typeof(System.Collections.Generic.IComparer); } }

    public override Type AdaptorType
    {
        get
        {
            return typeof(Adapter);
        }
    }

    public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
    {
        return new Adapter(appdomain, instance);
    }

    public class Adapter : System.Collections.Generic.IComparer<ILRuntime.Runtime.Intepreter.ILTypeInstance>, CrossBindingAdaptorType
    {
        ILTypeInstance instance;
        ILRuntime.Runtime.Enviorment.AppDomain appdomain;

        public Adapter()
        {

        }

        public Adapter(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
        {
            this.appdomain = appdomain;
            this.instance = instance;
        }

        public ILTypeInstance ILInstance { get { return instance; } }

        public int Compare(ILTypeInstance x, ILTypeInstance y)
        {
            return mExampleMethod_0.Invoke(instance,x, y);
        }

        public override string ToString()
        {
            IMethod m = appdomain.ObjectType.GetMethod("ToString", 0);
            m = instance.Type.GetVirtualMethod(m);
            if (m == null || m is ILMethod)
            {
                return instance.ToString();
            }
            else
                return instance.Type.FullName;
        }
    }
}

}

`

//热更新无法识别,需要修改如下才行.

public IMethod GetMethod(string name, List<IType> param, IType[] genericArguments, IType returnType = null, bool declaredOnly = false) { if (methods == null) InitializeMethods(); List<ILMethod> lst; IMethod genericMethod = null; if (methods.TryGetValue(name, out lst)) { for (var idx = 0; idx < lst.Count; idx++) { var i = lst[idx]; int pCnt = param != null ? param.Count : 0; if (i.ParameterCount == pCnt) { bool match = true; if (genericArguments != null && i.GenericParameterCount == genericArguments.Length && genericMethod == null) { genericMethod = CheckGenericParams(i, param, ref match); } else { match = CheckGenericArguments(i, genericArguments); if (!match) continue; for (int j = 0; j < pCnt; j++) { **if (i.Parameters[j].TypeForCLR == typeof(ILTypeInstance))//修改 { if (param[j].TypeForCLR != i.Parameters[j].TypeForCLR) { match = false; break; } }** else { if (param[j] != i.Parameters[j]) { match = false; break; } } } if (match) { match = returnType == null || i.ReturnType == returnType; } if (match) return i; } } } } if (genericArguments != null && genericMethod != null) { var m = genericMethod.MakeGenericMethod(genericArguments); lst.Add((ILMethod)m); return m; } if (declaredOnly) return null; else { if (BaseType != null) return BaseType.GetMethod(name, param, genericArguments, returnType, false); else return null; } }

qn9663 commented 3 years ago

自己写了一个List,主要是自带的list clear和remove时候产生gc

liiir1985 commented 3 years ago

热更层的源码能麻烦提供一下吗?

qn9663 commented 3 years ago

热更: public class SortComparer : IComparer<AchievementInstance> { public SortComparer() { } public int Compare(AchievementInstance a, AchievementInstance b) { return a.MissionId.CompareTo(b.MissionId); } } FastList list = player.Achievements.GetActiveAchievementInstances(); list.Sort(mSortComparer);

底层: public class FastList : IList { private T[] mItems; //篇幅,不给代码了, 测试时候初始化一下T[] 数值吧. public void Sort(IComparer comparer) { if (mCount > 0) { Array.Sort(mItems, 0, mCount, comparer); } }

} public void Sort(IComparer<T> comparer) { if (mCount > 0) { Array.Sort<T>(mItems, 0, mCount, comparer); } }

qn9663 commented 3 years ago

是不是我的用法问题? 我如果是比较的话,我测试过确实是判断不到这个接口类型.