Beispiel #1
0
        public static byte[] Convert(System.IO.Stream dllstream, ILogger logger = null)
        {
            var module = new ILModule();

            module.LoadModule(dllstream, null);
            if (logger == null)
            {
                logger = new DefLogger();
            }
            var converter = new ModuleConverter(logger);
            //有异常的话在 convert 函数中会直接throw 出来
            var antmodule = converter.Convert(module);

            return(antmodule.Build());
        }
Beispiel #2
0
        private static async Task parseCSharp(IOwinContext context, FormData formdata)
        {
            try
            {
                var file = formdata.mapFiles["file"];
                var code = System.Text.Encoding.UTF8.GetString(file);

                //编译
                List <string> codes = new List <string>();
                codes.Add(code);
                CompilerResults r = null;

                try
                {
                    r = gencode.GenCode(codes, true);
                    if (r.Errors.Count > 0)
                    {
                        MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                        json["tag"] = new MyJson.JsonNode_ValueNumber(-3);
                        json["msg"] = new MyJson.JsonNode_ValueString("compile fail.");
                        MyJson.JsonNode_Array errs = new MyJson.JsonNode_Array();
                        json["errors"] = errs;
                        for (var i = 0; i < r.Errors.Count; i++)
                        {
                            MyJson.JsonNode_Object errtag = new MyJson.JsonNode_Object();
                            errs.Add(errtag);
                            errtag.SetDictValue("msg", r.Errors[i].ErrorText);
                            errtag.SetDictValue("line", r.Errors[i].Line);
                            errtag.SetDictValue("col", r.Errors[i].Column);
                            errtag.SetDictValue("tag", r.Errors[i].IsWarning ? "警告" : "错误");
                        }
                        await context.Response.WriteAsync(json.ToString());

                        return;
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on comp.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
                //conv
                try
                {
                    var st = System.IO.File.OpenRead(r.PathToAssembly);
                    using (st)
                    {
                        var logjson = new Log2Json();

                        var module = new ILModule();
                        module.LoadModule(st, null);

                        var conv  = new Neo.Compiler.MSIL.ModuleConverter(logjson);
                        var neomd = conv.Convert(module);
                        //var mm = neomd.mapMethods[neomd.mainMethod];


                        var bs = neomd.Build();
                        if (bs != null)
                        {
                            {
                                MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                                json["tag"] = new MyJson.JsonNode_ValueNumber(0);
                                StringBuilder sb     = new StringBuilder();
                                StringBuilder sb2    = new StringBuilder();
                                var           hash   = System.Security.Cryptography.SHA256.Create();
                                var           hashbs = hash.ComputeHash(bs);
                                foreach (var b in bs)
                                {
                                    sb.Append(b.ToString("X02"));
                                }
                                foreach (var b in hashbs)
                                {
                                    sb2.Append(b.ToString("X02"));
                                }
                                json["hex"]  = new MyJson.JsonNode_ValueString(sb.ToString());
                                json["hash"] = new MyJson.JsonNode_ValueString(sb2.ToString());

                                var funcsigns = new MyJson.JsonNode_Object();
                                json["funcsigns"] = funcsigns;
                                foreach (var function in neomd.mapMethods)
                                {
                                    var mm       = function.Value;
                                    var ps       = mm.name.Split(new char[] { ' ', '(' }, StringSplitOptions.RemoveEmptyEntries);
                                    var funcsign = new MyJson.JsonNode_Object();
                                    funcsigns[ps[1]] = funcsign;
                                    funcsign.SetDictValue("name", ps[1]);
                                    var rtype = ConvCSharpType(mm.returntype);
                                    funcsign.SetDictValue("returntype", rtype);
                                    MyJson.JsonNode_Array funcparams = new MyJson.JsonNode_Array();
                                    funcsign["params"] = funcparams;
                                    if (mm.paramtypes != null)
                                    {
                                        foreach (var v in mm.paramtypes)
                                        {
                                            var ptype = ConvCSharpType(v.type);
                                            var item  = new MyJson.JsonNode_Object();
                                            funcparams.Add(item);
                                            item.SetDictValue("name", v.name);
                                            item.SetDictValue("type", ptype);
                                        }
                                    }
                                }
                                await context.Response.WriteAsync(json.ToString());

                                return;
                            }
                        }
                        else
                        {
                            {
                                MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                                json["tag"]  = new MyJson.JsonNode_ValueNumber(-4);
                                json["msg"]  = new MyJson.JsonNode_ValueString("compile fail.");
                                json["info"] = logjson.array;
                                await context.Response.WriteAsync(json.ToString());

                                return;
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on conv.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
            catch
            {
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("parse fail.");
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
        }