Example #1
0
        static void InitTest()
        {
            tests.Clear();

            var bytes    = System.IO.File.ReadAllBytes("UnitTestDll.dll");
            var bytespdb = System.IO.File.ReadAllBytes("UnitTestDll.pdb");

            //var bytesmdb = System.IO.File.ReadAllBytes("UnitTestDll.dll.mdb");//现在支持mdb了
            System.IO.MemoryStream ms    = new System.IO.MemoryStream(bytes);
            System.IO.MemoryStream mspdb = new System.IO.MemoryStream(bytespdb);
            //System.IO.MemoryStream msmdb = new System.IO.MemoryStream(bytesmdb);

            env = new CLRSharp.CLRSharp_Environment(new Logger());
            logger.Log(" L# Ver:" + env.version);
            try
            {
                env.LoadModule(ms, mspdb, new Mono.Cecil.Pdb.PdbReaderProvider());
                //env.LoadModule(ms, msmdb, new Mono.Cecil.Mdb.MdbReaderProvider());
            }
            catch (Exception err)
            {
                logger.Log_Error(err.ToString());
                logger.Log_Error("模块未加载完成,请检查错误");
            }
            var types = env.GetAllTypes();

            foreach (var t in types)
            {
                var tclr = env.GetType(t) as CLRSharp.Type_Common_CLRSharp;
                if (tclr != null && tclr.type_CLRSharp.HasMethods)
                {
                    foreach (var m in tclr.type_CLRSharp.Methods)
                    {
                        var mm = tclr.GetMethod(m.Name, CLRSharp.MethodParamList.constEmpty());
                        if (mm != null)
                        {
                            if (mm.Name.Contains("UnitTest"))
                            {
                                tests.Add(mm);
                            }
                        }
                    }
                    if (tclr.type_CLRSharp.HasNestedTypes)
                    {
                        foreach (var ttt in tclr.type_CLRSharp.NestedTypes)
                        {
                            var tclr2 = env.GetType(ttt.FullName) as CLRSharp.Type_Common_CLRSharp;

                            foreach (var m in tclr2.GetAllMethods())
                            {
                                if (m.ParamList.Count == 0)
                                {
                                    if (m.Name.Contains("UnitTest"))
                                    {
                                        tests.Add(m);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            logger.Log(" Got Test:" + tests.Count);
        }
Example #2
0
        static void Dump(string file)
        {
            bool   bMDB = System.IO.File.Exists(file + ".mdb");
            string fpdb = file.Substring(0, file.Length - 4) + ".pdb";
            bool   bPDB = System.IO.File.Exists(fpdb);

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Begin Dump " + file);

            System.IO.Stream s  = null;
            System.IO.Stream sp = null;

            try
            {
                var env = new CLRSharp.CLRSharp_Environment(new Logger());
                if (bPDB)
                {
                    Console.WriteLine("Load dll and  pdb");

                    s  = System.IO.File.OpenRead(file);
                    sp = System.IO.File.OpenRead(fpdb);

                    env.LoadModule(s, sp, new Mono.Cecil.Pdb.PdbReaderProvider());
                }
                else if (bMDB)
                {
                    Console.WriteLine("Load dll and  mdb");

                    s  = System.IO.File.OpenRead(file);
                    sp = System.IO.File.OpenRead(file + ".mdb");
                    {
                        env.LoadModule(s, sp, new Mono.Cecil.Mdb.MdbReaderProvider());
                    }
                }
                else
                {
                    Console.WriteLine("Load dll");
                    s = System.IO.File.OpenRead(file);
                    {
                        env.LoadModule(s);
                    }
                }

                var types = env.GetAllTypes();
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("Got Type:" + types.Length);

                foreach (var t in types)
                {
                    var st = env.GetType(t) as CLRSharp.ICLRType_Sharp;
                    if (st == null)
                    {
                        continue;
                    }
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("dump type:" + st.FullName);
                    Console.ForegroundColor = ConsoleColor.Gray;

                    var ms = st.type_CLRSharp.Methods;
                    if (ms == null || ms.Count == 0)
                    {
                        continue;
                    }
                    foreach (var m in ms)
                    {
                        Console.WriteLine("==dump method:" + m);
                        if (m.HasBody)
                        {
                            foreach (Mono.Cecil.Cil.Instruction i in m.Body.Instructions)
                            {
                                string line = i.ToString();

                                //i.Offset
                                if (i.SequencePoint != null)
                                {
                                    line += "(" + i.SequencePoint.Document.Url + ")";
                                    line += " |(" + i.SequencePoint.StartLine + "," + i.SequencePoint.StartColumn + ")";

                                    //if (codes[i.SequencePoint.Document.Url] != null)
                                    //{
                                    //    int lines = i.SequencePoint.StartLine;
                                    //    if (lines - 1 < codes[i.SequencePoint.Document.Url].Length)
                                    //        line += "| " + codes[i.SequencePoint.Document.Url][i.SequencePoint.StartLine - 1];
                                    //}
                                }
                                Console.WriteLine(line);
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("error:" + err.ToString());
            }
            finally
            {
                if (s != null)
                {
                    s.Close();
                }
                if (sp != null)
                {
                    sp.Close();
                }
            }

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("End Dump " + file);
        }
Example #3
0
        static void InitTest()
        {
            tests.Clear();

            var bytes = System.IO.File.ReadAllBytes("UnitTestDll.dll");
            var bytespdb = System.IO.File.ReadAllBytes("UnitTestDll.pdb");
            //var bytesmdb = System.IO.File.ReadAllBytes("UnitTestDll.dll.mdb");//现在支持mdb了
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
            System.IO.MemoryStream mspdb = new System.IO.MemoryStream(bytespdb);
            //System.IO.MemoryStream msmdb = new System.IO.MemoryStream(bytesmdb);

            env = new CLRSharp.CLRSharp_Environment(new Logger());
            logger.Log(" L# Ver:" + env.version);
            try
            {
                env.LoadModule(ms, mspdb, new Mono.Cecil.Pdb.PdbReaderProvider());
                //env.LoadModule(ms, msmdb, new Mono.Cecil.Mdb.MdbReaderProvider());
            }
            catch (Exception err)
            {
                logger.Log_Error(err.ToString());
                logger.Log_Error("模块未加载完成,请检查错误");
            }
            var types = env.GetAllTypes();
            foreach (var t in types)
            {
                var tclr = env.GetType(t) as CLRSharp.Type_Common_CLRSharp;
                if (tclr != null && tclr.type_CLRSharp.HasMethods)
                {

                    foreach (var m in tclr.type_CLRSharp.Methods)
                    {
                        var mm = tclr.GetMethod(m.Name, CLRSharp.MethodParamList.constEmpty());
                        if (mm != null)
                        {
                            if (mm.Name.Contains("UnitTest"))
                                tests.Add(mm);
                        }
                    }
                    if (tclr.type_CLRSharp.HasNestedTypes)
                    {

                        foreach (var ttt in tclr.type_CLRSharp.NestedTypes)
                        {
                            var tclr2 = env.GetType(ttt.FullName) as CLRSharp.Type_Common_CLRSharp;

                            foreach (var m in tclr2.GetAllMethods())
                            {
                                if (m.ParamList.Count == 0)
                                {
                                    if (m.Name.Contains("UnitTest"))
                                        tests.Add(m);
                                }
                            }
                        }
                    }

                }

            }
            logger.Log(" Got Test:" + tests.Count);
        }
Example #4
0
        static void Dump(string file)
        {

            bool bMDB = System.IO.File.Exists(file + ".mdb");
            string fpdb = file.Substring(0, file.Length - 4) + ".pdb";
            bool bPDB = System.IO.File.Exists(fpdb);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Begin Dump " + file);

            System.IO.Stream s = null;
            System.IO.Stream sp = null;

            try
            {
                var env = new CLRSharp.CLRSharp_Environment(new Logger());
                if (bPDB)
                {
                    Console.WriteLine("Load dll and  pdb");

                    s = System.IO.File.OpenRead(file);
                    sp = System.IO.File.OpenRead(fpdb);

                    env.LoadModule(s, sp, new Mono.Cecil.Pdb.PdbReaderProvider());

                }
                else if (bMDB)
                {
                    Console.WriteLine("Load dll and  mdb");

                    s = System.IO.File.OpenRead(file);
                    sp = System.IO.File.OpenRead(file + ".mdb");
                    {
                        env.LoadModule(s, sp, new Mono.Cecil.Mdb.MdbReaderProvider());
                    }
                }
                else
                {
                    Console.WriteLine("Load dll");
                    s = System.IO.File.OpenRead(file);
                    {
                        env.LoadModule(s);
                    }
                }

                var types = env.GetAllTypes();
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("Got Type:" + types.Length);

                foreach (var t in types)
                {
                    var st = env.GetType(t) as CLRSharp.ICLRType_Sharp;
                    if (st == null) continue;
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("dump type:" + st.FullName);
                    Console.ForegroundColor = ConsoleColor.Gray;

                    var ms = st.type_CLRSharp.Methods;
                    if (ms == null || ms.Count == 0) continue;
                    foreach (var m in ms)
                    {
                        Console.WriteLine("==dump method:" + m);
                        if (m.HasBody)
                        {
                            foreach (Mono.Cecil.Cil.Instruction i in m.Body.Instructions)
                            {
                                string line = i.ToString();

                                //i.Offset
                                if (i.SequencePoint != null)
                                {
                                    line += "(" + i.SequencePoint.Document.Url + ")";
                                    line += " |(" + i.SequencePoint.StartLine + "," + i.SequencePoint.StartColumn + ")";

                                    //if (codes[i.SequencePoint.Document.Url] != null)
                                    //{
                                    //    int lines = i.SequencePoint.StartLine;
                                    //    if (lines - 1 < codes[i.SequencePoint.Document.Url].Length)
                                    //        line += "| " + codes[i.SequencePoint.Document.Url][i.SequencePoint.StartLine - 1];
                                    //}
                                }
                                Console.WriteLine(line);
                            }
                        }
                    }

                }

            }
            catch (Exception err)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("error:" + err.ToString());

            }
            finally
            {
                if (s != null)
                {
                    s.Close();
                }
                if (sp != null)
                {
                    sp.Close();
                }
            }

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("End Dump " + file);
        }
Example #5
0
        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;
            //执行他其实返回的是脚本的类型,但是可以让他自动转型成 程序认识的类型

        }
Example #6
0
        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;
            //执行他其实返回的是脚本的类型,但是可以让他自动转型成 程序认识的类型
        }