object DoStaticMethod(CLRSharp.ThreadContext context, string type, string method) { CLRSharp.ICLRType wt = env.GetType(type); CLRSharp.IMethod methodctor = wt.GetMethod(method, CLRSharp.MethodParamList.constEmpty());//取得构造函数 return(methodctor.Invoke(context, null, null)); }
void InvokeMethod(CLRSharp.ThreadContext context, string type, string method) { CLRSharp.ICLRType wt = env.GetType(type); //用全名称,包括命名空间 CLRSharp.CLRSharp_Instance to = new CLRSharp.CLRSharp_Instance(wt as CLRSharp.ICLRType_Sharp); //创建实例 CLRSharp.IMethod methodctor = wt.GetMethod(".ctor", CLRSharp.MethodParamList.constEmpty()); //取得构造函数 methodctor.Invoke(context, to, null); //执行构造函数 CLRSharp.IMethod mt = wt.GetMethod(method, CLRSharp.MethodParamList.constEmpty()); mt.Invoke(context, to, null); }
public ThreadContext(ICLRSharp_Environment env, int DebugLevel) { this.environment = env; this.DebugLevel = DebugLevel; if (_activeContext != null) { env.logger.Log_Error("在同一线程上多次创建ThreadContext"); } _activeContext = this; }
object TestOne(CLRSharp.IMethod method, bool LogStep = false, bool notry = false) { int debug = LogStep ? 9 : 0; //if (CLRSharp.ThreadContext.activeContext == null) { CLRSharp.ThreadContext context = new CLRSharp.ThreadContext(env, debug); } CLRSharp.ThreadContext.activeContext.SetNoTry = notry; return(method.Invoke(CLRSharp.ThreadContext.activeContext, null, null)); }
public Delegate GetDelegate(ThreadContext context, Type deleType, IMethod method) { Delegate dele = null; if (!Delegates.TryGetValue(method, out dele)) { dele = Delegate_Binder.MakeDelegate(deleType, this, method); Delegates[method] = dele; //需要从Delegate转换成实际类型赋值的帮助类 } return dele; }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { if (context == null) { context = ThreadContext.activeContext; } if (context == null) { throw new Exception("这个线程上没有CLRSharp:ThreadContext"); } if (bVisual && method_CLRSharp.IsVirtual) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type != this.DeclaringType) { IMethod impl = inst.type.GetVMethod(this);// .GetMethod(this.Name, this.ParamList); if (impl != this) { return(impl.Invoke(context, _this, _params)); } } } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst == null) { inst = new CLRSharp_Instance(_DeclaringType); } //if (_DeclaringType.BaseType is ICLRType_System) context.ExecuteFunc(this, inst, _params); return(inst); } var obj = context.ExecuteFunc(this, _this, _params); if (obj is CLRSharp_Instance && ReturnType is ICLRType_System) { var bind = context.environment.GetCrossBind((ReturnType as ICLRType_System).TypeForSystem); if (bind != null) { obj = bind.CreateBind(obj as CLRSharp_Instance); } } return(obj); }
public void Init(ICLRSharp_Environment env) { bool flag = this.bInited; if (!flag) { bool hasVariables = this.bodyNative.HasVariables; if (hasVariables) { this.typelistForLoc = new MethodParamList(env, this.bodyNative.Variables); } for (int i = 0; i < this.bodyNative.Instructions.Count; i++) { Instruction instruction = this.bodyNative.Instructions[i]; CodeBody.OpCode opCode = new CodeBody.OpCode(); opCode.code = (CodeEx)instruction.OpCode.Code; opCode.addr = instruction.Offset; bool flag2 = instruction.SequencePoint != null; if (flag2) { bool flag3 = this.debugDoc == null; if (flag3) { this.debugDoc = new Dictionary <string, int>(); } bool flag4 = !this.debugDoc.ContainsKey(instruction.SequencePoint.Document.Url); if (flag4) { this.debugDoc.Add(instruction.SequencePoint.Document.Url, instruction.SequencePoint.StartLine); } opCode.debugline = instruction.SequencePoint.StartLine; } this.opCodes.Add(opCode); this.addr[opCode.addr] = i; } ThreadContext activeContext = ThreadContext.activeContext; for (int j = 0; j < this.bodyNative.Instructions.Count; j++) { CodeBody.OpCode opCode2 = this.opCodes[j]; Instruction instruction2 = this.bodyNative.Instructions[j]; opCode2.InitToken(activeContext, this, instruction2.Operand); } this.bInited = true; } }
// 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"); }
// 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"); }
public object Invoke(ThreadContext context, object _this, object[] _params) { if (context == null) { context = ThreadContext.activeContext; } if (context == null) { throw new Exception("这个线程上没有CLRSharp:ThreadContext"); } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = new CLRSharp_Instance(_DeclaringType); context.ExecuteFunc(method_CLRSharp, inst, _params); return(inst); } return(context.ExecuteFunc(method_CLRSharp, _this, _params)); }
//寻址类 public void NewObj(ThreadContext context, IMethod _clrmethod) { //MethodParamList list = new MethodParamList(context.environment, method); object[] _pp = null; if (_clrmethod.ParamList != null && _clrmethod.ParamList.Count > 0) { _pp = new object[_clrmethod.ParamList.Count]; for (int i = 0; i < _pp.Length; i++) { _pp[_pp.Length - 1 - i] = stackCalc.Pop(); } } //var typesys = context.environment.GetType(method.DeclaringType.FullName, method.Module); object returnvar = _clrmethod.Invoke(context, null, _pp); stackCalc.Push(returnvar); _pos = _pos.Next; }
public FixUtil() { Debug.Log("Init Start"); list = new List <string>(); TextAsset txt = Resources.Load("HotFix.txt") as TextAsset; SetConfigData(txt.text); env = new CLRSharp.CLRSharp_Environment(new Logger()); TextAsset dll = Resources.Load("HotFix.dll") as TextAsset; TextAsset pdb = Resources.Load("HotFix.pdb") as TextAsset; System.IO.MemoryStream msDll = new System.IO.MemoryStream(dll.bytes); System.IO.MemoryStream msPdb = new System.IO.MemoryStream(pdb.bytes);//pdb可以不要。可以从streamingassets读入 env.LoadModule(msDll, msPdb, new Mono.Cecil.Pdb.PdbReaderProvider()); context = new CLRSharp.ThreadContext(env); Debug.Log("Init End"); }
public void Initialize() { env = new CLRSharp.CLRSharp_Environment(new Logger()); byte[] dll, pdb; if (Application.isEditor) { dll = FileUtil.ReadFileWithByte(AppPlatform.GetRawResourcesPath() + "Code/" + "HotFixCode.dll.bytes"); pdb = FileUtil.ReadFileWithByte(AppPlatform.GetRawResourcesPath() + "Code/" + "HotFixCode.pdb.bytes"); } else { dll = FileUtil.ReadFileWithByte(AppPlatform.GetRuntimePackagePath() + "Code/" + "HotFixCode.dll.bytes"); pdb = FileUtil.ReadFileWithByte(AppPlatform.GetRuntimePackagePath() + "Code/" + "HotFixCode.pdb.bytes"); } System.IO.MemoryStream msDll = new System.IO.MemoryStream(dll); System.IO.MemoryStream msPdb = new System.IO.MemoryStream(pdb); env.LoadModule(msDll, msPdb, new Mono.Cecil.Pdb.PdbReaderProvider()); context = new CLRSharp.ThreadContext(env); IsScriptInited = true; }
public Delegate CreateDele(Type deletype, ThreadContext context, CLRSharp_Instance _this, IMethod _method) { Action act = () => { var o = _method.Invoke(context, _this, new object[] { }, true, true); if (o is VBox) { o = (o as VBox).BoxDefine(); } try { return((TRet)o); } catch (Exception ex) { var type = typeof(TRet); if (type == typeof(bool) && o is int) { return((TRet)(object)((int)o == 1)); } else { context.environment.logger.Log_Error("Delegate_BindTool_Ret<TRet> can not cast from " + o.GetType() + " to " + type + " for return value."); throw; } } }; try { return(Delegate.CreateDelegate(deletype, act.Target, act.Method)); } catch (Exception ex) { context.environment.logger.Log_Error("CreateDelegate error. " + deletype + " method:" + act.Method); throw; } }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual, bool autoLogDump) { try { return(Invoke(context, _this, _params, bVisual)); } catch (Exception err) { if (context == null) { context = ThreadContext.activeContext; } if (context == null) { throw new Exception("当前线程没有创建ThreadContext,无法Dump", err); } else { context.environment.logger.Log_Error(context.Dump()); throw err; } } }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual, bool autoLogDump) { try { return(Invoke(context, _this, _params)); } catch (Exception err) { if (context == null) { context = ThreadContext.activeContext; } if (context == null) { throw new Exception("当前线程没有创建ThreadContext,无法Dump", err); } else { context.environment.logger.Log_Error("Error InSystemCall:" + this.DeclaringType.FullName + "::" + this.Name); throw err; } } }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { //对程序类型,其实我们做不到区分虚实调用。。。没办法 if (this.Name == "Concat" && this.DeclaringType.TypeForSystem == typeof(string)) { //这里有一个IL2CPP的问题 if (_params.Length == 1) { if (_params[0] == null) { return("null"); } return(_params[0].ToString()); } else { string outstr = "null"; if (_params[0] != null) { outstr = _params[0].ToString(); } for (int i = 1; i < _params.Length; i++) { if (_params[i] != null) { outstr += _params[i]; } else { outstr += "null"; } } return(outstr); } } return(Invoke(context, _this, _params)); }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { if (context == null) { context = ThreadContext.activeContext; } if (context == null) { throw new Exception("这个线程上没有CLRSharp:ThreadContext"); } if (bVisual && method_CLRSharp.IsVirtual) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type != this.DeclaringType) { IMethod impl = inst.type.GetVMethod(this);// .GetMethod(this.Name, this.ParamList); if (impl != this) { return(impl.Invoke(context, _this, _params)); } } } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst == null) { inst = new CLRSharp_Instance(_DeclaringType); } //if (_DeclaringType.BaseType is ICLRType_System) context.ExecuteFunc(this, inst, _params); return(inst); } return(context.ExecuteFunc(this, _this, _params)); }
public object Invoke(ThreadContext context, object _this, object[] _params) { //委托是很特殊的存在 //if(this.DeclaringType.IsDelegate) //{ //} if (method_System is System.Reflection.ConstructorInfo) { if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate))) {//创建委托 object src = _params[0]; RefFunc fun = _params[1] as RefFunc; ICLRType_Sharp clrtype = fun._method.DeclaringType as ICLRType_Sharp; if (clrtype != null)//onclr { CLRSharp_Instance inst = src as CLRSharp_Instance; if (fun._method.isStatic && clrtype != null) { inst = clrtype.staticInstance; } return(inst.GetDelegate(context, method_System.DeclaringType, fun._method)); } else//onsystem { ICLRType_System stype = fun._method.DeclaringType as ICLRType_System; return(stype.CreateDelegate(method_System.DeclaringType, src, fun._method as IMethod_System)); } } object[] _outp = null; if (_params != null && _params.Length > 0) { _outp = new object[_params.Length]; var _paramsdef = method_System.GetParameters(); for (int i = 0; i < _params.Length; i++) { if (_params[i] == null) { _outp[i] = null; } Type tsrc = _params[i].GetType(); Type ttarget = _paramsdef[i].ParameterType; if (tsrc == ttarget) { _outp[i] = _params[i]; } else if (tsrc.IsSubclassOf(ttarget)) { _outp[i] = _params[i]; } else { if (ttarget == typeof(byte)) { _outp[i] = (byte)Convert.ToDecimal(_params[i]); } else { _outp[i] = _params[i]; } //var ms =_params[i].GetType().GetMethods(); } } } var newobj = (method_System as System.Reflection.ConstructorInfo).Invoke(_outp); return(newobj); } else { //if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate)))//直接用Delegate.Invoke,会导致转到本机代码再回来 ////会导致错误堆栈不方便观察,但是也没办法直接调用,只能手写一些常用类型 //{ // //需要从Delegate转换成实际类型执行的帮助类 // Action<int> abc = _this as Action<int>; // abc((int)_params[0]); // return null; //} //else { return(method_System.Invoke(_this, _params)); } } }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { bool flag = context == null; if (flag) { context = ThreadContext.activeContext; } bool flag2 = context == null; if (flag2) { throw new Exception("这个线程上没有CLRSharp:ThreadContext"); } bool flag3 = bVisual && this.method_CLRSharp.IsVirtual; object result; if (flag3) { CLRSharp_Instance cLRSharp_Instance = _this as CLRSharp_Instance; bool flag4 = cLRSharp_Instance.type != this.DeclaringType; if (flag4) { IMethod vMethod = cLRSharp_Instance.type.GetVMethod(this); bool flag5 = vMethod != this; if (flag5) { result = vMethod.Invoke(context, _this, _params); return(result); } } } bool flag6 = this.method_CLRSharp.Name == ".ctor"; if (flag6) { CLRSharp_Instance cLRSharp_Instance2 = _this as CLRSharp_Instance; bool flag7 = cLRSharp_Instance2 == null; if (flag7) { cLRSharp_Instance2 = new CLRSharp_Instance(this._DeclaringType); } context.ExecuteFunc(this, cLRSharp_Instance2, _params); result = cLRSharp_Instance2; } else { object obj = context.ExecuteFunc(this, _this, _params); bool flag8 = obj is CLRSharp_Instance && this.ReturnType is ICLRType_System; if (flag8) { ICrossBind crossBind = context.environment.GetCrossBind((this.ReturnType as ICLRType_System).TypeForSystem); bool flag9 = crossBind != null; if (flag9) { obj = crossBind.CreateBind(obj as CLRSharp_Instance); } } result = obj; } return(result); }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual, bool autoLogDump) { try { return Invoke(context, _this, _params); } catch(Exception err) { if (context == null) context = ThreadContext.activeContext; if (context == null) throw new Exception("当前线程没有创建ThreadContext,无法Dump", err); else { context.environment.logger.Log_Error("Error InSystemCall:" + this.DeclaringType.FullName + "::" + this.Name); throw err; } } }
public object Invoke(ThreadContext context, object _this, object[] _params) { if (_this is CLRSharp_Instance) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type.HasSysBase) { var btype = inst.type.ContainBase(method_System.DeclaringType); if (btype) { var CrossBind = context.environment.GetCrossBind(method_System.DeclaringType); if (CrossBind != null) { _this = CrossBind.CreateBind(inst); } else { _this = (_this as CLRSharp_Instance).system_base; //如果没有绑定器,尝试直接使用System_base; } //context.environment.logger.Log("这里有一个需要映射的类型"); } } } //委托是很特殊的存在 //if(this.DeclaringType.IsDelegate) //{ //} if (method_System is System.Reflection.ConstructorInfo) { if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate))) {//创建委托 object src = _params[0]; RefFunc fun = _params[1] as RefFunc; ICLRType_Sharp clrtype = fun._method.DeclaringType as ICLRType_Sharp; if (clrtype != null)//onclr { CLRSharp_Instance inst = src as CLRSharp_Instance; if (fun._method.isStatic && clrtype != null) inst = clrtype.staticInstance; return inst.GetDelegate(context, method_System.DeclaringType, fun._method); } else//onsystem { ICLRType_System stype = fun._method.DeclaringType as ICLRType_System; return stype.CreateDelegate(method_System.DeclaringType, src, fun._method as IMethod_System); } } object[] _outp = null; if (_params != null && _params.Length > 0) { _outp = new object[_params.Length]; var _paramsdef = method_System.GetParameters(); for (int i = 0; i < _params.Length; i++) { if (_params[i] == null) { _outp[i] = null; continue; } Type tsrc = _params[i].GetType(); Type ttarget = _paramsdef[i].ParameterType; if (tsrc == ttarget) { _outp[i] = _params[i]; } else if (tsrc.IsSubclassOf(ttarget)) { _outp[i] = _params[i]; } else if (_paramsdef[i].ParameterType.IsEnum)//特殊处理枚举 { var ms = _paramsdef[i].ParameterType.GetMethods(); _outp[i] = Enum.ToObject(_paramsdef[i].ParameterType, _params[i]); } else { if (ttarget == typeof(byte)) _outp[i] = (byte)Convert.ToDecimal(_params[i]); else { _outp[i] = _params[i]; } //var ms =_params[i].GetType().GetMethods(); } } } var newobj = (method_System as System.Reflection.ConstructorInfo).Invoke(_outp); return newobj; } else { Dictionary<int, object> hasref = new Dictionary<int, object>(); object[] _outp = null; if (_params != null && _params.Length > 0) { _outp = new object[_params.Length]; var _paramsdef = method_System.GetParameters(); for (int i = 0; i < _params.Length; i++) { if (_params[i] is CLRSharp.StackFrame.RefObj)//特殊处理outparam { object v = (_params[i] as CLRSharp.StackFrame.RefObj).Get(); if (v is VBox) { v = (v as VBox).BoxDefine(); } hasref[i] = v; _outp[i] = v; } else if (_paramsdef[i].ParameterType.IsEnum)//特殊处理枚举 { var ms = _paramsdef[i].ParameterType.GetMethods(); _outp[i] = Enum.ToObject(_paramsdef[i].ParameterType, _params[i]); } else { if(_paramsdef[i].ParameterType==typeof(UInt64)&&_params[i] is Int64) { _outp[i] = (UInt64)(Int64)_params[i]; } else if (_paramsdef[i].ParameterType == typeof(Int64) && _params[i] is UInt64) { _outp[i] = (Int64)(UInt64)_params[i]; } else if (_paramsdef[i].ParameterType == typeof(UInt32) && _params[i] is Int32) { _outp[i] = (UInt32)(Int32)_params[i]; } else if (_paramsdef[i].ParameterType == typeof(Int32) && _params[i] is UInt32) { _outp[i] = (Int32)(UInt32)_params[i]; } else { _outp[i] = _params[i]; } } } } //if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate)))//直接用Delegate.Invoke,会导致转到本机代码再回来 ////会导致错误堆栈不方便观察,但是也没办法直接调用,只能手写一些常用类型 //{ // //需要从Delegate转换成实际类型执行的帮助类 // Action<int> abc = _this as Action<int>; // abc((int)_params[0]); // return null; //} //else { var _out = method_System.Invoke(_this, _outp); foreach (var _ref in hasref) { if (_ref.Value is VBox) { (_ref.Value as VBox).SetDirect(_outp[_ref.Key]); } else { (_params[_ref.Key] as CLRSharp.StackFrame.RefObj).Set(_outp[_ref.Key]); } } return _out; } } }
public object Invoke(ThreadContext context, object _this, object[] _params) { return(Invoke(context, _this, _params, true)); }
public void InitToken(ThreadContext context, CodeBody body, object _p) { CodeEx codeEx = this.code; switch (codeEx) { case CodeEx.Ldloc_0: this.tokenI32 = 0; return; case CodeEx.Ldloc_1: this.tokenI32 = 1; return; case CodeEx.Ldloc_2: this.tokenI32 = 2; return; case CodeEx.Ldloc_3: this.tokenI32 = 3; return; case CodeEx.Stloc_0: case CodeEx.Stloc_1: case CodeEx.Stloc_2: case CodeEx.Stloc_3: case CodeEx.Ldnull: case CodeEx.Dup: case CodeEx.Pop: case CodeEx.Jmp: case CodeEx.Calli: case CodeEx.Ret: case CodeEx.Stind_Ref: case CodeEx.Stind_I1: case CodeEx.Stind_I2: case CodeEx.Stind_I4: case CodeEx.Stind_I8: case CodeEx.Stind_R4: case CodeEx.Stind_R8: case CodeEx.Add: case CodeEx.Sub: case CodeEx.Mul: case CodeEx.Div: case CodeEx.Div_Un: case CodeEx.Rem: case CodeEx.Rem_Un: case CodeEx.And: case CodeEx.Or: case CodeEx.Xor: case CodeEx.Shl: case CodeEx.Shr: case CodeEx.Shr_Un: case CodeEx.Neg: case CodeEx.Not: case CodeEx.Conv_I1: case CodeEx.Conv_I2: case CodeEx.Conv_I4: case CodeEx.Conv_I8: case CodeEx.Conv_R4: case CodeEx.Conv_R8: case CodeEx.Conv_U4: case CodeEx.Conv_U8: case CodeEx.Cpobj: case CodeEx.Ldobj: case CodeEx.Conv_R_Un: case CodeEx.Unbox: case CodeEx.Throw: case CodeEx.Stobj: case CodeEx.Conv_Ovf_I1_Un: case CodeEx.Conv_Ovf_I2_Un: case CodeEx.Conv_Ovf_I4_Un: case CodeEx.Conv_Ovf_I8_Un: case CodeEx.Conv_Ovf_U1_Un: case CodeEx.Conv_Ovf_U2_Un: case CodeEx.Conv_Ovf_U4_Un: case CodeEx.Conv_Ovf_U8_Un: case CodeEx.Conv_Ovf_I_Un: case CodeEx.Conv_Ovf_U_Un: goto IL_4AA; case CodeEx.Ldarg_S: this.tokenI32 = (_p as ParameterReference).Index; return; case CodeEx.Ldarga_S: case CodeEx.Starg_S: goto IL_42C; case CodeEx.Ldloc_S: case CodeEx.Ldloca_S: case CodeEx.Stloc_S: goto IL_3D5; case CodeEx.Ldc_I4_M1: this.tokenI32 = -1; return; case CodeEx.Ldc_I4_0: this.tokenI32 = 0; return; case CodeEx.Ldc_I4_1: this.tokenI32 = 1; return; case CodeEx.Ldc_I4_2: this.tokenI32 = 2; return; case CodeEx.Ldc_I4_3: this.tokenI32 = 3; return; case CodeEx.Ldc_I4_4: this.tokenI32 = 4; return; case CodeEx.Ldc_I4_5: this.tokenI32 = 5; return; case CodeEx.Ldc_I4_6: this.tokenI32 = 6; return; case CodeEx.Ldc_I4_7: this.tokenI32 = 7; return; case CodeEx.Ldc_I4_8: this.tokenI32 = 8; return; case CodeEx.Ldc_I4_S: this.tokenI32 = (int)Convert.ToDecimal(_p); return; case CodeEx.Ldc_I4: this.tokenI32 = (int)_p; return; case CodeEx.Ldc_I8: this.tokenI64 = (long)_p; return; case CodeEx.Ldc_R4: this.tokenR32 = (float)_p; return; case CodeEx.Ldc_R8: this.tokenR64 = (double)_p; return; case CodeEx.Call: case CodeEx.Callvirt: case CodeEx.Newobj: goto IL_2E0; case CodeEx.Br_S: case CodeEx.Brfalse_S: case CodeEx.Brtrue_S: case CodeEx.Beq_S: case CodeEx.Bge_S: case CodeEx.Bgt_S: case CodeEx.Ble_S: case CodeEx.Blt_S: case CodeEx.Bne_Un_S: case CodeEx.Bge_Un_S: case CodeEx.Bgt_Un_S: case CodeEx.Ble_Un_S: case CodeEx.Blt_Un_S: case CodeEx.Br: case CodeEx.Brfalse: case CodeEx.Brtrue: case CodeEx.Beq: case CodeEx.Bge: case CodeEx.Bgt: case CodeEx.Ble: case CodeEx.Blt: case CodeEx.Bne_Un: case CodeEx.Bge_Un: case CodeEx.Bgt_Un: case CodeEx.Ble_Un: case CodeEx.Blt_Un: break; case CodeEx.Switch: { Instruction[] array = _p as Instruction[]; this.tokenAddr_Switch = new int[array.Length]; for (int i = 0; i < array.Length; i++) { this.tokenAddr_Switch[i] = body.addr[array[i].Offset]; } return; } case CodeEx.Ldind_I1: case CodeEx.Ldind_U1: case CodeEx.Ldind_I2: case CodeEx.Ldind_U2: case CodeEx.Ldind_I4: case CodeEx.Ldind_U4: case CodeEx.Ldind_I8: case CodeEx.Ldind_I: case CodeEx.Ldind_R4: case CodeEx.Ldind_R8: case CodeEx.Ldind_Ref: goto IL_4A8; case CodeEx.Ldstr: this.tokenStr = (_p as string); return; case CodeEx.Castclass: case CodeEx.Isinst: case CodeEx.Box: case CodeEx.Newarr: goto IL_2BC; case CodeEx.Ldfld: case CodeEx.Ldflda: case CodeEx.Stfld: case CodeEx.Ldsfld: case CodeEx.Ldsflda: case CodeEx.Stsfld: this.tokenField = context.GetField(_p); return; default: switch (codeEx) { case CodeEx.Leave: case CodeEx.Leave_S: break; case CodeEx.Stind_I: case CodeEx.Conv_U: case CodeEx.Arglist: case CodeEx.Ceq: case CodeEx.Cgt: case CodeEx.Cgt_Un: case CodeEx.Clt: case CodeEx.Clt_Un: case CodeEx.Localloc: case CodeEx.Endfilter: case CodeEx.Unaligned: case CodeEx.Tail: goto IL_4AA; case CodeEx.Ldftn: case CodeEx.Ldvirtftn: goto IL_2E0; case CodeEx.Ldarg: this.tokenI32 = (int)_p; return; case CodeEx.Ldarga: case CodeEx.Starg: goto IL_42C; case CodeEx.Ldloc: case CodeEx.Stloc: this.tokenI32 = (int)_p; return; case CodeEx.Ldloca: goto IL_3D5; case CodeEx.Volatile: goto IL_4A8; case CodeEx.Initobj: case CodeEx.Constrained: goto IL_2BC; default: goto IL_4AA; } break; } this.tokenAddr_Index = body.addr[((Instruction)_p).Offset]; return; IL_2BC: this.tokenType = context.GetType(_p); return; IL_2E0: this.tokenMethod = context.GetMethod(_p); return; IL_3D5: this.tokenI32 = ((VariableDefinition)_p).Index; return; IL_42C: this.tokenI32 = (_p as ParameterDefinition).Index; IL_4A8: return; IL_4AA: this.tokenUnknown = _p; }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual, bool autoLogDump) { try { return Invoke(context, _this, _params, bVisual); } catch (Exception err) { if (context == null) context = ThreadContext.activeContext; if (context == null) throw new Exception("当前线程没有创建ThreadContext,无法Dump", err); else { context.environment.logger.Log_Error(context.Dump()); throw err; } } }
public void Ldftn(ThreadContext context, IMethod method) { stackCalc.Push(new RefFunc(method, null)); //throw new NotImplementedException(); _pos = _pos.Next; }
public void Constrained(ThreadContext context, Mono.Cecil.TypeReference obj) { _pos = _pos.Next; }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { bool flag = this.Name == "Concat" && this.DeclaringType.TypeForSystem == typeof(string); object result; if (flag) { bool flag2 = _params.Length == 1; if (flag2) { bool flag3 = _params[0] == null; if (flag3) { result = "null"; } else { bool flag4 = _params[0] is string[]; if (flag4) { result = string.Concat(_params[0] as string[]); } else { bool flag5 = _params[0] is object[]; if (flag5) { result = string.Concat(_params[0] as object[]); } else { result = _params[0].ToString(); } } } } else { string text = "null"; bool flag6 = _params[0] != null; if (flag6) { text = _params[0].ToString(); } for (int i = 1; i < _params.Length; i++) { bool flag7 = _params[i] != null; if (flag7) { text += _params[i]; } else { text += "null"; } } result = text; } } else { result = this.Invoke(context, _this, _params); } return(result); }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { if (context == null) context = ThreadContext.activeContext; if (context == null) throw new Exception("这个线程上没有CLRSharp:ThreadContext"); if (bVisual && method_CLRSharp.IsVirtual) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type != this.DeclaringType) { IMethod impl = inst.type.GetVMethod(this);// .GetMethod(this.Name, this.ParamList); if (impl != this) { return impl.Invoke(context, _this, _params); } } } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst == null) inst = new CLRSharp_Instance(_DeclaringType); //if (_DeclaringType.BaseType is ICLRType_System) context.ExecuteFunc(this, inst, _params); return inst; } return context.ExecuteFunc(this, _this, _params); }
public object ExecuteFunc(IMethod_Sharp method, object _this, object[] _params) { _activeContext = this; if (this.DebugLevel >= 9) { environment.logger.Log("<Call>::" + method.DeclaringType.FullName + "::" + method.Name.ToString()); } StackFrame stack = new StackFrame(method.Name, method.isStatic); stacks.Push(stack); object[] _withp = null; bool isctor = method.Name == ".ctor"; if (isctor) { //CLRSharp_Instance pthis = new CLRSharp_Instance(GetType(func.ReturnType) as Type_Common_CLRSharp); //StackFrame.RefObj pthis = new StackFrame.RefObj(stack, 0, StackFrame.RefType.arg); _withp = new object[_params == null ? 1 : (_params.Length + 1)]; if (_params != null) _params.CopyTo(_withp, 1); _withp[0] = _this; } else { if (!method.isStatic) { _withp = new object[(_params == null) ? 1 : (_params.Length + 1)]; _withp[0] = _this; if (_params != null) _params.CopyTo(_withp, 1); } else { _withp = _params; } } stack.SetParams(_withp); if (method.body != null) { stack.Init(method.body); stack.SetCodePos(0); //._pos = method.body.bodyNative.Instructions[0]; stack._codepos = 0; if (method.body.bodyNative.HasExceptionHandlers && !SetNoTry) { RunCodeWithTry(method.body, stack); } else { RunCode(stack, method.body); } } if (this.DebugLevel >= 9) { environment.logger.Log("<CallEnd>"); } var ret = stacks.Pop().Return(); return isctor ? _this : ret; //if (func.HasBody) //{ // RunCode(stack, func.Body.Instructions); //} //var ret = stacks.Pop().Return(); //if (this.DebugLevel >= 9) //{ // environment.logger.Log("<CallEnd>"); //} //return ret; }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { //对程序类型,其实我们做不到区分虚实调用。。。没办法 if (this.Name == "Concat" && this.DeclaringType.TypeForSystem == typeof(string)) {//这里有一个IL2CPP的问题 if (_params.Length == 1) { if (_params[0] == null) return "null"; if (_params[0] is string[]) { return string.Concat(_params[0] as string[]); } else if (_params[0] is object[]) { return string.Concat(_params[0] as object[]); } else { return _params[0].ToString(); } } else { string outstr = "null"; if (_params[0] != null) outstr = _params[0].ToString(); for (int i = 1; i < _params.Length; i++) { if (_params[i] != null) outstr += _params[i]; else outstr += "null"; } return outstr; } } return Invoke(context, _this, _params); }
public object ExecuteFunc(IMethod_Sharp method, object _this, object[] _params) { _activeContext = this; if (this.DebugLevel >= 9) { environment.logger.Log("<Call>::" + method.DeclaringType.FullName + "::" + method.Name.ToString()); } StackFrame stack = new StackFrame(method.Name, method.isStatic); stacks.Push(stack); object[] _withp = null; bool isctor = method.Name == ".ctor"; if (isctor) { //CLRSharp_Instance pthis = new CLRSharp_Instance(GetType(func.ReturnType) as Type_Common_CLRSharp); //StackFrame.RefObj pthis = new StackFrame.RefObj(stack, 0, StackFrame.RefType.arg); _withp = new object[_params == null ? 1 : (_params.Length + 1)]; if (_params != null) { _params.CopyTo(_withp, 1); } _withp[0] = _this; } else { if (!method.isStatic) { _withp = new object[(_params == null) ? 1 : (_params.Length + 1)]; _withp[0] = _this; if (_params != null) { _params.CopyTo(_withp, 1); } } else { _withp = _params; } } stack.SetParams(_withp); if (method.body != null) { stack.Init(method.body); stack.SetCodePos(0); //._pos = method.body.bodyNative.Instructions[0]; stack._codepos = 0; if (method.body.bodyNative.HasExceptionHandlers && !SetNoTry) { RunCodeWithTry(method.body, stack); } else { RunCode(stack, method.body); } } if (this.DebugLevel >= 9) { environment.logger.Log("<CallEnd>"); } var ret = stacks.Pop().Return(); return(isctor ? _this : ret); //if (func.HasBody) //{ // RunCode(stack, func.Body.Instructions); //} //var ret = stacks.Pop().Return(); //if (this.DebugLevel >= 9) //{ // environment.logger.Log("<CallEnd>"); //} //return ret; }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { if (context == null) context = ThreadContext.activeContext; if (context == null) throw new Exception("这个线程上没有CLRSharp:ThreadContext"); if (bVisual && method_CLRSharp.IsVirtual) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type != this.DeclaringType) { IMethod impl = inst.type.GetVMethod(this);// .GetMethod(this.Name, this.ParamList); if (impl != this) { return impl.Invoke(context, _this, _params); } } } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst == null) inst = new CLRSharp_Instance(_DeclaringType); //if (_DeclaringType.BaseType is ICLRType_System) context.ExecuteFunc(this, inst, _params); return inst; } var obj = context.ExecuteFunc(this, _this, _params); if (obj is CLRSharp_Instance && ReturnType is ICLRType_System) { var bind = context.environment.GetCrossBind((ReturnType as ICLRType_System).TypeForSystem); if (bind != null) { obj = bind.CreateBind(obj as CLRSharp_Instance); } } return obj; }
public object Invoke(ThreadContext context, object _this, object[] _params) { return Invoke(context, _this, _params, true); }
public void InitToken(ThreadContext context, CodeBody body, object _p) { switch (code) { case CodeEx.Leave: case CodeEx.Leave_S: case CodeEx.Br: case CodeEx.Br_S: case CodeEx.Brtrue: case CodeEx.Brtrue_S: case CodeEx.Brfalse: case CodeEx.Brfalse_S: //比较流程控制 case CodeEx.Beq: case CodeEx.Beq_S: case CodeEx.Bne_Un: case CodeEx.Bne_Un_S: case CodeEx.Bge: case CodeEx.Bge_S: case CodeEx.Bge_Un: case CodeEx.Bge_Un_S: case CodeEx.Bgt: case CodeEx.Bgt_S: case CodeEx.Bgt_Un: case CodeEx.Bgt_Un_S: case CodeEx.Ble: case CodeEx.Ble_S: case CodeEx.Ble_Un: case CodeEx.Ble_Un_S: case CodeEx.Blt: case CodeEx.Blt_S: case CodeEx.Blt_Un: case CodeEx.Blt_Un_S: //this.tokenAddr = ((Mono.Cecil.Cil.Instruction)_p).Offset; this.tokenAddr_Index = body.addr[((Mono.Cecil.Cil.Instruction)_p).Offset]; break; case CodeEx.Isinst: case CodeEx.Constrained: case CodeEx.Box: case CodeEx.Initobj: case CodeEx.Castclass: case CodeEx.Newarr: this.tokenType = context.GetType(_p); //this.tokenUnknown = _p; break; case CodeEx.Ldfld: case CodeEx.Ldflda: case CodeEx.Ldsfld: case CodeEx.Ldsflda: case CodeEx.Stfld: case CodeEx.Stsfld: this.tokenField = context.GetField(_p); //this.tokenUnknown = _p; break; case CodeEx.Call: case CodeEx.Callvirt: case CodeEx.Newobj: case CodeEx.Ldftn: case CodeEx.Ldvirtftn: this.tokenMethod = context.GetMethod(_p); break; case CodeEx.Ldc_I4: this.tokenI32 = (int)_p; break; case CodeEx.Ldc_I4_S: this.tokenI32 = (int)Convert.ToDecimal(_p); break; case CodeEx.Ldc_I4_M1: this.tokenI32 = -1; break; case CodeEx.Ldc_I4_0: this.tokenI32 = 0; break; case CodeEx.Ldc_I4_1: this.tokenI32 = 1; break; case CodeEx.Ldc_I4_2: this.tokenI32 = 2; break; case CodeEx.Ldc_I4_3: this.tokenI32 = 3; break; case CodeEx.Ldc_I4_4: this.tokenI32 = 4; break; case CodeEx.Ldc_I4_5: this.tokenI32 = 5; break; case CodeEx.Ldc_I4_6: this.tokenI32 = 6; break; case CodeEx.Ldc_I4_7: this.tokenI32 = 7; break; case CodeEx.Ldc_I4_8: this.tokenI32 = 8; break; case CodeEx.Ldc_I8: this.tokenI64 = (Int64)_p; break; case CodeEx.Ldc_R4: this.tokenR32 = (float)_p; break; case CodeEx.Ldc_R8: this.tokenR64 = (double)_p; break; case CodeEx.Ldstr: this.tokenStr = _p as string; break; case CodeEx.Ldloca: case CodeEx.Ldloca_S: case CodeEx.Ldloc_S: case CodeEx.Stloc_S: this.tokenI32 = ((VariableDefinition)_p).Index; //this.tokenUnknown = _p; break; case CodeEx.Ldloc: case CodeEx.Stloc: this.tokenI32 = (int)_p; break; case CodeEx.Ldloc_0: this.tokenI32 = 0; break; case CodeEx.Ldloc_1: this.tokenI32 = 1; break; case CodeEx.Ldloc_2: this.tokenI32 = 2; break; case CodeEx.Ldloc_3: this.tokenI32 = 3; break; case CodeEx.Ldarga: case CodeEx.Ldarga_S: case CodeEx.Starg: case CodeEx.Starg_S: this.tokenI32 = (_p as Mono.Cecil.ParameterDefinition).Index; break; case CodeEx.Switch: { Mono.Cecil.Cil.Instruction[] e = _p as Mono.Cecil.Cil.Instruction[]; tokenAddr_Switch = new int[e.Length]; for (int i = 0; i < e.Length; i++) { tokenAddr_Switch[i] = body.addr[(e[i].Offset)]; } } break; case CodeEx.Ldarg: this.tokenI32 = (int)_p; break; case CodeEx.Ldarg_S: this.tokenI32 = (_p as Mono.Cecil.ParameterReference).Index; break; case CodeEx.Volatile: case CodeEx. Ldind_I1: case CodeEx. Ldind_U1: case CodeEx. Ldind_I2: case CodeEx. Ldind_U2: case CodeEx. Ldind_I4: case CodeEx. Ldind_U4: case CodeEx. Ldind_I8: case CodeEx. Ldind_I: case CodeEx. Ldind_R4: case CodeEx. Ldind_R8: case CodeEx. Ldind_Ref: break; default: this.tokenUnknown = _p; break; } }
public void InvokeCCtor(ThreadContext context) { NeedCCtor = false; this.GetMethod(".cctor", null).Invoke(context, this.staticInstance, new object[] { }); }
public void InitToken(ThreadContext context, CodeBody body, object _p) { switch (code) { case CodeEx.Leave: case CodeEx.Leave_S: case CodeEx.Br: case CodeEx.Br_S: case CodeEx.Brtrue: case CodeEx.Brtrue_S: case CodeEx.Brfalse: case CodeEx.Brfalse_S: //比较流程控制 case CodeEx.Beq: case CodeEx.Beq_S: case CodeEx.Bne_Un: case CodeEx.Bne_Un_S: case CodeEx.Bge: case CodeEx.Bge_S: case CodeEx.Bge_Un: case CodeEx.Bge_Un_S: case CodeEx.Bgt: case CodeEx.Bgt_S: case CodeEx.Bgt_Un: case CodeEx.Bgt_Un_S: case CodeEx.Ble: case CodeEx.Ble_S: case CodeEx.Ble_Un: case CodeEx.Ble_Un_S: case CodeEx.Blt: case CodeEx.Blt_S: case CodeEx.Blt_Un: case CodeEx.Blt_Un_S: //this.tokenAddr = ((Mono.Cecil.Cil.Instruction)_p).Offset; this.tokenAddr_Index = body.addr[((Mono.Cecil.Cil.Instruction)_p).Offset]; break; case CodeEx.Isinst: case CodeEx.Constrained: case CodeEx.Box: case CodeEx.Initobj: case CodeEx.Castclass: this.tokenType = context.GetType(_p); //this.tokenUnknown = _p; break; case CodeEx.Ldfld: case CodeEx.Ldflda: case CodeEx.Ldsfld: case CodeEx.Ldsflda: case CodeEx.Stfld: case CodeEx.Stsfld: this.tokenField = context.GetField(_p); //this.tokenUnknown = _p; break; case CodeEx.Call: case CodeEx.Callvirt: case CodeEx.Newobj: case CodeEx.Ldftn: case CodeEx.Ldvirtftn: this.tokenMethod = context.GetMethod(_p); break; case CodeEx.Ldc_I4: this.tokenI32 = (int)_p; break; case CodeEx.Ldc_I4_S: this.tokenI32 = (int)Convert.ToDecimal(_p); break; case CodeEx.Ldc_I4_M1: this.tokenI32 = -1; break; case CodeEx.Ldc_I4_0: this.tokenI32 = 0; break; case CodeEx.Ldc_I4_1: this.tokenI32 = 1; break; case CodeEx.Ldc_I4_2: this.tokenI32 = 2; break; case CodeEx.Ldc_I4_3: this.tokenI32 = 3; break; case CodeEx.Ldc_I4_4: this.tokenI32 = 4; break; case CodeEx.Ldc_I4_5: this.tokenI32 = 5; break; case CodeEx.Ldc_I4_6: this.tokenI32 = 6; break; case CodeEx.Ldc_I4_7: this.tokenI32 = 7; break; case CodeEx.Ldc_I4_8: this.tokenI32 = 8; break; case CodeEx.Ldc_I8: this.tokenI64 = (Int64)_p; break; case CodeEx.Ldc_R4: this.tokenR32 = (float)_p; break; case CodeEx.Ldc_R8: this.tokenR64 = (double)_p; break; case CodeEx.Ldstr: this.tokenStr = _p as string; break; case CodeEx.Ldloca: case CodeEx.Ldloca_S: case CodeEx.Ldloc_S: case CodeEx.Stloc_S: this.tokenI32 = ((VariableDefinition)_p).Index; //this.tokenUnknown = _p; break; case CodeEx.Ldloc: case CodeEx.Stloc: this.tokenI32 = (int)_p; break; case CodeEx.Ldloc_0: this.tokenI32 = 0; break; case CodeEx.Ldloc_1: this.tokenI32 = 1; break; case CodeEx.Ldloc_2: this.tokenI32 = 2; break; case CodeEx.Ldloc_3: this.tokenI32 = 3; break; case CodeEx.Ldarga: case CodeEx.Ldarga_S: case CodeEx.Starg: case CodeEx.Starg_S: this.tokenI32 = (_p as Mono.Cecil.ParameterDefinition).Index; break; case CodeEx.Switch: { Mono.Cecil.Cil.Instruction[] e = _p as Mono.Cecil.Cil.Instruction[]; tokenAddr_Switch = new int[e.Length]; for (int i = 0; i < e.Length; i++) { tokenAddr_Switch[i] = body.addr[(e[i].Offset)]; } } break; case CodeEx.Ldarg: this.tokenI32 = (int)_p; break; case CodeEx.Ldarg_S: this.tokenI32 = (_p as Mono.Cecil.ParameterReference).Index; break; case CodeEx.Volatile: break; default: this.tokenUnknown = _p; break; } }
public object Invoke(ThreadContext context, object _this, object[] _params) { if (_this is CLRSharp_Instance) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type.HasSysBase) { var btype = inst.type.ContainBase(method_System.DeclaringType); if (btype) { var CrossBind = context.environment.GetCrossBind(method_System.DeclaringType); if (CrossBind != null) { _this = CrossBind.CreateBind(inst); } else { _this = (_this as CLRSharp_Instance).system_base; //如果没有绑定器,尝试直接使用System_base; } //context.environment.logger.Log("这里有一个需要映射的类型"); } } } //委托是很特殊的存在 //if(this.DeclaringType.IsDelegate) //{ //} if (method_System is System.Reflection.ConstructorInfo) { if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate))) {//创建委托 object src = _params[0]; RefFunc fun = _params[1] as RefFunc; IMethod method = fun._method; MemoryPool.ReleaseRefFunc(fun); ICLRType_Sharp clrtype = method.DeclaringType as ICLRType_Sharp; if (clrtype != null)//onclr { CLRSharp_Instance inst = src as CLRSharp_Instance; if (method.isStatic && clrtype != null) { inst = clrtype.staticInstance; } return(inst.GetDelegate(context, method_System.DeclaringType, method)); } else//onsystem { ICLRType_System stype = method.DeclaringType as ICLRType_System; return(stype.CreateDelegate(method_System.DeclaringType, src, method as IMethod_System)); } } object[] _outp = null; if (_params != null && _params.Length > 0) { _outp = new object[_params.Length]; var _paramsdef = ParamList; for (int i = 0; i < _params.Length; i++) { if (_params[i] == null) { _outp[i] = null; continue; } Type tsrc = _params[i].GetType(); Type ttarget = _paramsdef[i].TypeForSystem; if (tsrc == ttarget) { _outp[i] = _params[i]; } else if (tsrc.IsSubclassOf(ttarget)) { _outp[i] = _params[i]; } else if (ttarget.IsEnum)//特殊处理枚举 { _outp[i] = Enum.ToObject(ttarget, _params[i]); } else if (tsrc.IsEnum) { _outp[i] = Enum.ToObject(ttarget, _params[i]); } else { if (ttarget == typeof(byte)) { _outp[i] = (byte)Convert.ToDecimal(_params[i]); } else { _outp[i] = _params[i]; } //var ms =_params[i].GetType().GetMethods(); } } } var newobj = (method_System as System.Reflection.ConstructorInfo).Invoke(_outp); return(newobj); } else { object[] hasref = MemoryPool.GetArray(_params.Length); long flag = 0; object[] _outp = null; if (_params != null && _params.Length > 0) { _outp = new object[_params.Length]; var _paramsdef = ParamList; for (int i = 0; i < _params.Length; i++) { var _targetType = _paramsdef[i].TypeForSystem; if (_targetType.GetElementType() != null) { _targetType = _targetType.GetElementType(); } if (_params[i] is CLRSharp.StackFrame.RefObj) //特殊处理outparam { object v = (_params[i] as CLRSharp.StackFrame.RefObj).Get(); MemoryPool.ReleaseRefObj(_params[i]); if (v is VBox) { v = (v as VBox).BoxDefine(); } else if (v == null) { v = null; } else if (v.GetType() != _targetType && _targetType != typeof(object)) { v = v.Convert(_targetType); } hasref[i] = v; flag |= (1 << i); _outp[i] = v; } else if (_targetType.IsEnum || _params[i] is Enum) //特殊处理枚举 { _outp[i] = Enum.ToObject(_targetType, _params[i]); } else { var v = _params[i]; if (v != null && v.GetType() != _targetType && _targetType != typeof(object)) { v = v.Convert(_targetType); } _outp[i] = v; } } } //if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate)))//直接用Delegate.Invoke,会导致转到本机代码再回来 ////会导致错误堆栈不方便观察,但是也没办法直接调用,只能手写一些常用类型 //{ // //需要从Delegate转换成实际类型执行的帮助类 // Action<int> abc = _this as Action<int>; // abc((int)_params[0]); // return null; //} //else { try { if (_this != null && _this.GetType() != method_System.DeclaringType) { _this = _this.Convert(method_System.DeclaringType); } var _out = method_System.Invoke(_this, _outp); { for (int i = 0; i < hasref.Length; i++) { var _ref = hasref[i]; if ((flag & (1 << i)) == 0) { continue; } if (_ref is VBox) { (_ref as VBox).SetDirect(_outp[i]); } else { (_params[i] as CLRSharp.StackFrame.RefObj).Set(_outp[i]); } } } return(_out); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); if (_outp != null) { sb.Append("real args: "); { var __array6 = _outp; var __arrayLength6 = __array6.Length; for (int __i6 = 0; __i6 < __arrayLength6; ++__i6) { var o = __array6[__i6]; { sb.Append(o.GetType().FullName); sb.Append("="); sb.Append(o); sb.Append(","); } } } } context.environment.logger.Log_Error(string.Format("name:{0} type:{1} ret:{2} {3} ex:{4}", method_System.ToString(), DeclaringType, ReturnType, sb, ex)); throw; } finally { MemoryPool.ReleaseArray(hasref); } } } }
/// <summary> /// 初始化 /// </summary> /// <param name="dllstr">dll文件</param> public void init(TextAsset dllstr) { if (isInit) { throw new Exception("CLRSharpManager试图重复初始化,如果确信要这么做请在clear()后调用此方法"); } env = new CLRSharp_Environment(new Logger()); MemoryStream msDll = new MemoryStream(dllstr.bytes); MemoryStream msPdb = null; PdbReaderProvider pdbReaderProvider = null; #if UNITY_EDITOR TextAsset pdb = Resources.Load<TextAsset>(GameConstant.MODULES + ".pdb"); msPdb = new MemoryStream(pdb.bytes); pdbReaderProvider = new PdbReaderProvider(); #endif env.LoadModule(msDll, msPdb, pdbReaderProvider); //step01建立一个线程上下文,用来模拟L#的线程模型,每个线程创建一个即可。 context = new ThreadContext(env); }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) {//对程序类型,其实我们做不到区分虚实调用。。。没办法 return(Invoke(context, _this, _params)); }
public void Reset() { ThreadContext activeContext = ThreadContext.activeContext; object obj = this._Reset.Invoke(activeContext, this.inst, null); }
public object Invoke(ThreadContext context, object _this, object[] _params) { if (_this is CLRSharp_Instance) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type.HasSysBase) { var btype = inst.type.ContainBase(method_System.DeclaringType); if (btype) { var CrossBind = context.environment.GetCrossBind(method_System.DeclaringType); if (CrossBind != null) { _this = CrossBind.CreateBind(inst); } else { _this = (_this as CLRSharp_Instance).system_base; //如果没有绑定器,尝试直接使用System_base; } //context.environment.logger.Log("这里有一个需要映射的类型"); } } } //委托是很特殊的存在 //if(this.DeclaringType.IsDelegate) //{ //} if (method_System is System.Reflection.ConstructorInfo) { if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate))) {//创建委托 object src = _params[0]; RefFunc fun = _params[1] as RefFunc; ICLRType_Sharp clrtype = fun._method.DeclaringType as ICLRType_Sharp; if (clrtype != null)//onclr { CLRSharp_Instance inst = src as CLRSharp_Instance; if (fun._method.isStatic && clrtype != null) inst = clrtype.staticInstance; return inst.GetDelegate(context, method_System.DeclaringType, fun._method); } else//onsystem { ICLRType_System stype = fun._method.DeclaringType as ICLRType_System; return stype.CreateDelegate(method_System.DeclaringType, src, fun._method as IMethod_System); } } object[] _outp = null; if (_params != null && _params.Length > 0) { _outp = new object[_params.Length]; var _paramsdef = method_System.GetParameters(); for (int i = 0; i < _params.Length; i++) { if (_params[i] == null) _outp[i] = null; Type tsrc = _params[i].GetType(); Type ttarget = _paramsdef[i].ParameterType; if (tsrc == ttarget) { _outp[i] = _params[i]; } else if (tsrc.IsSubclassOf(ttarget)) { _outp[i] = _params[i]; } else { if (ttarget == typeof(byte)) _outp[i] = (byte)Convert.ToDecimal(_params[i]); else { _outp[i] = _params[i]; } //var ms =_params[i].GetType().GetMethods(); } } } var newobj = (method_System as System.Reflection.ConstructorInfo).Invoke(_outp); return newobj; } else { //if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate)))//直接用Delegate.Invoke,会导致转到本机代码再回来 ////会导致错误堆栈不方便观察,但是也没办法直接调用,只能手写一些常用类型 //{ // //需要从Delegate转换成实际类型执行的帮助类 // Action<int> abc = _this as Action<int>; // abc((int)_params[0]); // return null; //} //else { return method_System.Invoke(_this, _params); } } }
public object Invoke(ThreadContext context, object _this, object[] _params) { if (_this is CLRSharp_Instance) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type.HasSysBase) { var btype = inst.type.ContainBase(method_System.DeclaringType); if (btype) { var CrossBind = context.environment.GetCrossBind(method_System.DeclaringType); if (CrossBind != null) { _this = CrossBind.CreateBind(inst); } else { _this = (_this as CLRSharp_Instance).system_base; //如果没有绑定器,尝试直接使用System_base; } //context.environment.logger.Log("这里有一个需要映射的类型"); } } } //委托是很特殊的存在 //if(this.DeclaringType.IsDelegate) //{ //} if (method_System is System.Reflection.ConstructorInfo) { if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate))) {//创建委托 object src = _params[0]; RefFunc fun = _params[1] as RefFunc; ICLRType_Sharp clrtype = fun._method.DeclaringType as ICLRType_Sharp; if (clrtype != null)//onclr { CLRSharp_Instance inst = src as CLRSharp_Instance; if (fun._method.isStatic && clrtype != null) { inst = clrtype.staticInstance; } return(inst.GetDelegate(context, method_System.DeclaringType, fun._method)); } else//onsystem { ICLRType_System stype = fun._method.DeclaringType as ICLRType_System; return(stype.CreateDelegate(method_System.DeclaringType, src, fun._method as IMethod_System)); } } object[] _outp = null; if (_params != null && _params.Length > 0) { _outp = new object[_params.Length]; var _paramsdef = method_System.GetParameters(); for (int i = 0; i < _params.Length; i++) { if (_params[i] == null) { _outp[i] = null; } Type tsrc = _params[i].GetType(); Type ttarget = _paramsdef[i].ParameterType; if (tsrc == ttarget) { _outp[i] = _params[i]; } else if (tsrc.IsSubclassOf(ttarget)) { _outp[i] = _params[i]; } else { if (ttarget == typeof(byte)) { _outp[i] = (byte)Convert.ToDecimal(_params[i]); } else { _outp[i] = _params[i]; } //var ms =_params[i].GetType().GetMethods(); } } } var newobj = (method_System as System.Reflection.ConstructorInfo).Invoke(_outp); return(newobj); } else { Dictionary <int, object> hasref = new Dictionary <int, object>(); object[] _outp = null; if (_params != null && _params.Length > 0) { _outp = new object[_params.Length]; var _paramsdef = method_System.GetParameters(); for (int i = 0; i < _params.Length; i++) { if (_params[i] is CLRSharp.StackFrame.RefObj)//特殊处理outparam { object v = (_params[i] as CLRSharp.StackFrame.RefObj).Get(); if (v is VBox) { v = (v as VBox).BoxDefine(); } hasref[i] = v; _outp[i] = v; } else if (_paramsdef[i].ParameterType.IsEnum)//特殊处理枚举 { var ms = _paramsdef[i].ParameterType.GetMethods(); _outp[i] = Enum.ToObject(_paramsdef[i].ParameterType, _params[i]); } else { _outp[i] = _params[i]; } } } //if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate)))//直接用Delegate.Invoke,会导致转到本机代码再回来 ////会导致错误堆栈不方便观察,但是也没办法直接调用,只能手写一些常用类型 //{ // //需要从Delegate转换成实际类型执行的帮助类 // Action<int> abc = _this as Action<int>; // abc((int)_params[0]); // return null; //} //else { var _out = method_System.Invoke(_this, _outp); foreach (var _ref in hasref) { if (_ref.Value is VBox) { (_ref.Value as VBox).SetDirect(_outp[_ref.Key]); } else { (_params[_ref.Key] as CLRSharp.StackFrame.RefObj).Set(_outp[_ref.Key]); } } return(_out); } } }
public object ExecuteFunc(Mono.Cecil.MethodDefinition func, object _this, object[] _params) { _activeContext = this; if (this.DebugLevel >= 9) { environment.logger.Log("<Call>::" + func.ToString()); } StackFrame stack = new StackFrame(func.Name, func.IsStatic); stacks.Push(stack); object[] _withp = null; bool isctor = func.Name == ".ctor"; if (isctor) { //CLRSharp_Instance pthis = new CLRSharp_Instance(GetType(func.ReturnType) as Type_Common_CLRSharp); //StackFrame.RefObj pthis = new StackFrame.RefObj(stack, 0, StackFrame.RefType.arg); _withp = new object[_params == null ? 1 : (_params.Length + 1)]; if (_params != null) { _params.CopyTo(_withp, 1); } _withp[0] = _this; } else { if (!func.IsStatic) { _withp = new object[(_params == null) ? 1 : (_params.Length + 1)]; _withp[0] = _this; if (_params != null) { _params.CopyTo(_withp, 1); } } else { _withp = _params; } } stack.SetParams(_withp); if (func.HasBody) { stack._pos = func.Body.Instructions[0]; if (func.Body.HasExceptionHandlers) { RunCodeWithTry(func, stack); } else { RunCode(stack, func.Body.Instructions); } } if (this.DebugLevel >= 9) { environment.logger.Log("<CallEnd>"); } var ret = stacks.Pop().Return(); return(isctor ? _this : ret); //if (func.HasBody) //{ // RunCode(stack, func.Body.Instructions); //} //var ret = stacks.Pop().Return(); //if (this.DebugLevel >= 9) //{ // environment.logger.Log("<CallEnd>"); //} //return ret; }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) {//对程序类型,其实我们做不到区分虚实调用。。。没办法 return Invoke(context, _this, _params); }
object TestOne(CLRSharp.IMethod method, bool LogStep = false, bool notry = false) { int debug = LogStep ? 9 : 0; //if (CLRSharp.ThreadContext.activeContext == null) { CLRSharp.ThreadContext context = new CLRSharp.ThreadContext(env, debug); } CLRSharp.ThreadContext.activeContext.SetNoTry = notry; return method.Invoke(CLRSharp.ThreadContext.activeContext, null, null); }