public bool CheckExistMethod(CLRSharp.ICLRType rType, string rFuncName, params object[] rArgs) { var rList = MatchParamList(rArgs); var method = rType.GetMethod(rFuncName, rList); if (method != null) { return(true); } else { return(false); } }
public bool TryGetMethod(CLRSharp.ICLRType rType, string rFuncName, out CLRSharp.IMethod outMethod, params object[] rArgs) { var rList = MatchParamList(rArgs); outMethod = rType.GetMethod(rFuncName, rList); if (outMethod != null) { return(true); } else { return(false); } }
public object CreateScriptObject(CLRSharp.ICLRType rType, params object[] rArgs) { var rList = MatchParamList(rArgs); CLRSharp.IMethod rCtor = rType.GetMethod(".ctor", rList); if (rCtor == null) { Debug.LogError("Create Script Object failed, check full type name and param list"); return(null); } object rObj = rCtor.Invoke(context, null, null); return(rObj); }
public bool TryGetType(string rName, out CLRSharp.ICLRType outType) { string fullTypeName = "HotFixCode." + rName; outType = env.GetType(fullTypeName); if (outType != null) { return(true); } else { Debug.LogError("Get CLRType failed, check full type name:>" + rName); return(false); } }
// Use this for initialization void Start() { //创建CLRSharp环境 env = new CLRSharp.CLRSharp_Environment(new LSharpLogger()); //加载L#模块 TextAsset dll = Resources.Load("Controller.dll") as TextAsset; TextAsset pdb = Resources.Load("Controller.pdb") as TextAsset; System.IO.MemoryStream msDll = new System.IO.MemoryStream(dll.bytes); System.IO.MemoryStream msPdb = new System.IO.MemoryStream(pdb.bytes); //env.LoadModule (msDll);//如果无符号是pdb的话,第二个参数传null env.LoadModule(msDll, msPdb, new Mono.Cecil.Pdb.PdbReaderProvider()); //Pdb //env.LoadModule(msDll, msMdb, new Mono.Cecil.Mdb.MdbReaderProvider());//如果符号是Mdb格式 Debug.Log("LoadModule Controller.dll done."); //step01建立一个线程上下文,用来模拟L#的线程模型,每个线程创建一个即可。 CLRSharp.ThreadContext context = new CLRSharp.ThreadContext(env); Debug.Log("Create ThreadContext for L#."); //step02取得想要调用的L#类型 CLRSharp.ICLRType wantType = env.GetType("Controller.Entry"); //用全名称,包括命名空间 Debug.Log("GetType:" + wantType.Name); //和反射代码中的Type.GetType相对应 //step03 静态调用 //得到类型上的一个函数,第一个参数是函数名字,第二个参数是函数的参数表,这是一个没有参数的函数 CLRSharp.IMethod method01 = wantType.GetMethod("Log", CLRSharp.MethodParamList.constEmpty()); method01.Invoke(context, null, null); //第三个参数是object[] 参数表,这个例子不需要参数 //这是个静态函数调用,对应到代码他就是HotFixCode.TestClass.Test1(); ////step04 成员调用 ////第二个测试程序是一个成员变量,所以先要创建实例 //CLRSharp.IMethod methodctor = wantType.GetMethod(".ctor", CLRSharp.MethodParamList.constEmpty());//取得构造函数 //object typeObj = methodctor.Invoke(context, null, null);//执行构造函数 ////这几行的作用对应到代码就约等于 HotFixCode.TestClass typeObj =new HotFixCode.TestClass(); //CLRSharp.IMethod method02 = wantType.GetMethod("Test2", CLRSharp.MethodParamList.constEmpty()); //method02.Invoke(context, typeObj, null); ////这两行的作用就相当于 typeOBj.Test2(); // //var list = CLRSharp.MethodParamList.Make(env.GetType(typeof(int)), env.GetType(typeof(string))); //CLRSharp.IMethod method03 = wantType.GetMethod("Test2", list); //method03.Invoke(context, typeObj, new object[] { 1234, "abcddd" }); ////这两行的作用就相当于 typeOBj.Test2(1234,"abcddd"); }
CLRSharp.MethodParamList MatchParamList(params object[] rArgs) { CLRSharp.MethodParamList rList; if (rArgs.Length > 0) { CLRSharp.ICLRType[] rClrTypes = new CLRSharp.ICLRType[rArgs.Length]; for (int i = 0; i < rArgs.Length; i++) { var arg = rArgs[i]; var clrType = env.GetType(arg.GetType()); rClrTypes[i] = clrType; } rList = CLRSharp.MethodParamList.Make(rClrTypes); } else { rList = CLRSharp.MethodParamList.constEmpty(); } return(rList); }
private void Form1_Load(object sender, EventArgs e) { //创建CLRSharp环境 env = new CLRSharp.CLRSharp_Environment(this); //加载L#模块 var dll = System.IO.File.ReadAllBytes("testdll01.dll"); var pdb = System.IO.File.ReadAllBytes("testdll01.pdb"); System.IO.MemoryStream msDll = new System.IO.MemoryStream(dll); System.IO.MemoryStream msPdb = new System.IO.MemoryStream(pdb); //env.LoadModule (msDll, null);//不需要pdb的话,第二个参数传null env.LoadModule(msDll, msPdb); Log("LoadModule HotFixCode.dll done."); //step01建立一个线程上下文,用来模拟L#的线程模型,每个线程创建一个即可。 CLRSharp.ThreadContext context = new CLRSharp.ThreadContext(env); Log("Create ThreadContext for L#."); //step02取得想要调用的L#类型 CLRSharp.ICLRType wantType = env.GetType("HoxFixCode.TestClass", null);//用全名称,包括命名空间 Log("GetType:" + wantType.Name); //和反射代码中的Type.GetType相对应 //step03 静态调用 //得到类型上的一个函数,第一个参数是函数名字,第二个参数是函数的参数表,这是一个没有参数的函数 CLRSharp.IMethod method01 = wantType.GetMethod("Test1", CLRSharp.MethodParamList.MakeEmpty()); method01.Invoke(context, null, null);//第三个参数是object[] 参数表,这个例子不需要参数 //这是个静态函数调用,对应到代码他就是HotFixCode.TestClass.Test1(); //step04 成员调用 //第二个测试程序是一个成员变量,所以先要创建实例 CLRSharp.CLRSharp_Instance typeObj = new CLRSharp.CLRSharp_Instance(wantType as CLRSharp.ICLRType_Sharp); //创建实例 CLRSharp.IMethod methodctor = wantType.GetMethod(".ctor", CLRSharp.MethodParamList.MakeEmpty()); //取得构造函数 methodctor.Invoke(context, typeObj, null); //执行构造函数 //这几行的作用对应到代码就约等于 HotFixCode.TestClass typeObj =new HotFixCode.TestClass(); CLRSharp.IMethod method02 = wantType.GetMethod("Test2", CLRSharp.MethodParamList.MakeEmpty()); method02.Invoke(context, typeObj, null); //这两行的作用就相当于 typeOBj.Test2(); }
protected virtual void Awake() { CLRSharp.ICLRType rType = null; var rName = name.Replace("(Clone)", ""); bool rGot = Global.ScriptManager.TryGetType(rName, out rType); if (rGot) { if (_scriptObject == null) { _scriptObject = Global.ScriptManager.CreateScriptObject(rType); } isNeedStart = Global.ScriptManager.CheckExistMethod(rType, startName); isNeedUpdate = Global.ScriptManager.CheckExistMethod(rType, updateName); isNeedLateUpdate = Global.ScriptManager.CheckExistMethod(rType, lateUpdateName); isNeedFixedUpdate = Global.ScriptManager.CheckExistMethod(rType, fixedUpdateName); isNeedOnDestroy = Global.ScriptManager.CheckExistMethod(rType, onDestroyName); CallMethod("Awake", gameObject); } else { DebugConsole.Log("未找到" + name + "相应的脚本类"); } }
public TestItem(CLRSharp.ICLRType type, string method) { this.type = type; this.method = method; }
protected virtual void Awake() { if (!LShapClient.Instance.IsScriptInited) { LShapClient.Instance.Initialize(); } CLRSharp.ICLRType rType = null; string typeName = name; var i = name.IndexOf(" ", 0); if (i >= 0) { typeName = name.Remove(i); } i = typeName.IndexOf("(", 0); if (i >= 0) { typeName = name.Remove(i); } bool rGot = LShapClient.Instance.TryGetType(typeName, out rType); if (rGot) { if (lShapObject == null) { SetLShapObject(LShapClient.Instance.CreateScriptObject(rType)); } isNeedAwake = LShapClient.Instance.CheckExistMethod(rType, awakeName, gameObject); isNeedStart = LShapClient.Instance.CheckExistMethod(rType, startName); isNeedOnDestroy = LShapClient.Instance.CheckExistMethod(rType, onDestroyName); LShapClient.Instance.TryGetMethod(rType, updateName, out update); LShapClient.Instance.TryGetMethod(rType, lateUpdateName, out lateUpdate); LShapClient.Instance.TryGetMethod(rType, fixedUpdateName, out fixedUpdate); LShapClient.Instance.TryGetMethod(rType, onTriggerEnterName, out onTriggerEnter, new Collider()); LShapClient.Instance.TryGetMethod(rType, onTriggerExitName, out onTriggerExit, new Collider()); LShapClient.Instance.TryGetMethod(rType, onTriggerStayName, out onTriggerStay, new Collider()); LShapClient.Instance.TryGetMethod(rType, onCollisionEnterName, out onCollisionEnter, new Collision()); LShapClient.Instance.TryGetMethod(rType, onCollisionExitName, out onCollisionExit, new Collision()); LShapClient.Instance.TryGetMethod(rType, onCollisionStayName, out onCollisionStay, new Collision()); LShapClient.Instance.TryGetMethod(rType, onTriggerEnter2DName, out onTriggerEnter2D, new Collider2D()); LShapClient.Instance.TryGetMethod(rType, onTriggerExit2DName, out onTriggerExit2D, new Collider2D()); LShapClient.Instance.TryGetMethod(rType, onTriggerStay2DName, out onTriggerStay2D, new Collider2D()); LShapClient.Instance.TryGetMethod(rType, onCollisionEnter2DName, out onCollisionEnter2D, new Collision2D()); LShapClient.Instance.TryGetMethod(rType, onCollisionExit2DName, out onCollisionExit2D, new Collision2D()); LShapClient.Instance.TryGetMethod(rType, onCollisionStay2DName, out onCollisionStay2D, new Collision2D()); if (isNeedAwake) { CallMethod("Awake", gameObject); } } else { DebugConsole.Log("未找到" + name + "相应的脚本类"); } }
private void Form1_Load(object sender, EventArgs e) { //创建CLRSharp环境 env = new CLRSharp.CLRSharp_Environment(this); //加载L#模块 var dll = System.IO.File.ReadAllBytes("testdll01.dll"); var pdb = System.IO.File.ReadAllBytes("testdll01.pdb"); System.IO.MemoryStream msDll = new System.IO.MemoryStream(dll); System.IO.MemoryStream msPdb = new System.IO.MemoryStream(pdb); //env.LoadModule (msDll);//不需要pdb的话 bool useSystemAssm = false; if (useSystemAssm) { byte[] dllbytes = msDll.ToArray(); byte[] pdbbytes = msPdb.ToArray(); var assem = System.Reflection.Assembly.Load(dllbytes, pdbbytes); //用系统反射加载 env.AddSerachAssembly(assem); //直接搜索系统反射,此时L#不发挥作用,为了调试的功能 } else { env.LoadModule(msDll, msPdb, new Mono.Cecil.Pdb.PdbReaderProvider()); } Log("LoadModule HotFixCode.dll done."); env.RegCrossBind(new MyCrossBind()); //step01建立一个线程上下文,用来模拟L#的线程模型,每个线程创建一个即可。 CLRSharp.ThreadContext context = new CLRSharp.ThreadContext(env); context.SetNoTry = true; Log("Create ThreadContext for L#."); //step02取得想要调用的L#类型 CLRSharp.ICLRType wantType = env.GetType("HoxFixCode.TestClass");//用全名称,包括命名空间 Log("GetType:" + wantType.Name); //和反射代码中的Type.GetType相对应 //step03 静态调用 //得到类型上的一个函数,第一个参数是函数名字,第二个参数是函数的参数表,这是一个没有参数的函数 CLRSharp.IMethod method01 = wantType.GetMethod("Test1", CLRSharp.MethodParamList.constEmpty()); method01.Invoke(context, null, null);//第三个参数是object[] 参数表,这个例子不需要参数 //这是个静态函数调用,对应到代码他就是HotFixCode.TestClass.Test1(); //step04 成员调用 //第二个测试程序是一个成员变量,所以先要创建实例 //CLRSharp.CLRSharp_Instance typeObj = new CLRSharp.CLRSharp_Instance(wantType as CLRSharp.ICLRType_Sharp);//创建实例 //上一句写的有问题,执行构造函数返回的才是 new出来的对象 CLRSharp.IMethod methodctor = wantType.GetMethod(".ctor", CLRSharp.MethodParamList.constEmpty()); //取得构造函数 //这里用object 就可以脚本和反射通用了 object typeObj = methodctor.Invoke(context, null, null); //执行构造函数 //这几行的作用对应到代码就约等于 HotFixCode.TestClass typeObj =new HotFixCode.TestClass(); CLRSharp.IMethod method02 = wantType.GetMethod("Test2", CLRSharp.MethodParamList.constEmpty()); for (int i = 0; i < 5; i++) { method02.Invoke(context, typeObj, null); } //这两行的作用就相当于 typeOBj.Test2(); //请注意,不要在初始化之后,再修改ParamList的内容 CLRSharp.MethodParamList list = CLRSharp.MethodParamList.Make( env.GetType(typeof(int)), env.GetType(typeof(string)) ); CLRSharp.IMethod method03 = wantType.GetMethod("Test3", list); CallMethod(method03, typeObj, 345, "abbc"); //获取TestClass.CreateMyI函数 //public static Interface.IMyType CreateMyI() //{ // return new myi(); //} CLRSharp.IMethod method04 = wantType.GetMethod("CreateMyI", CLRSharp.MethodParamList.constEmpty()); var obj = method04.Invoke(context, null, null); Interface.IMyType got = obj as Interface.IMyType; //执行他其实返回的是脚本的类型,但是可以让他自动转型成 程序认识的类型 }