Exemplo n.º 1
0
        public NeoModule Convert(ILModule _in, ConvOption option = null)
        {
            this.inModule = _in;
            //logger.Log("beginConvert.");
            this.outModule        = new NeoModule(this.logger);
            this.outModule.option = option == null ? ConvOption.Default : option;
            foreach (var t in _in.mapType)
            {
                logger.Log("Key: " + t.Key);
                if (t.Key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    NeoMethod nm = new NeoMethod();

                    if (m.Key.Contains(".cctor"))
                    {
                        logger.Log("Neo Method: " + m.Value);
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.IsConstructor)
                    {
                        continue;
                    }
                    nm._namespace  = m.Value.method.DeclaringType.FullName;
                    nm.name        = m.Value.method.FullName;
                    nm.displayName = m.Value.method.Name;

                    Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = m.Value.method.CustomAttributes;
                    foreach (var attr in ca)
                    {
                        if (attr.AttributeType.Name == "DisplayNameAttribute")
                        {
                            nm.displayName = (string)attr.ConstructorArguments[0].Value;
                        }
                    }
                    nm.inSmartContract            = m.Value.method.DeclaringType.BaseType.Name == "SmartContract";
                    nm.isPublic                   = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent();
                        ae._namespace  = e.Value.field.DeclaringType.FullName;
                        ae.name        = ae._namespace + "::" + e.Key;
                        ae.displayName = e.Value.displayName;
                        ae.returntype  = e.Value.returntype;
                        ae.paramtypes  = e.Value.paramtypes;
                        outModule.mapEvents[ae.name] = ae;
                    }
                }
            }

            var keys = new List <string>(_in.mapType.Keys);

            foreach (var key in keys)
            {
                var value = _in.mapType[key];
                if (key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (key.Contains("_API_"))
                {
                    continue;                       //api的,不要
                }
                if (key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Key.Contains(".cctor"))
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    var nm = this.methodLink[m.Value];


                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        try
                        {
                            var type = m.Value.method.ReturnType.Resolve();
                            foreach (var i in type.Interfaces)
                            {
                                if (i.InterfaceType.Name == "IApiInterface")
                                {
                                    nm.returntype = "IInteropInterface";
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        byte[] outcall; string name; Lux.VM.OpCode[] opcodes; string[] opdata;
                        if (IsAppCall(m.Value.method, out outcall))
                        {
                            continue;
                        }
                        if (IsNonCall(m.Value.method))
                        {
                            continue;
                        }
                        if (IsMixAttribute(m.Value.method, out opcodes, out opdata))
                        {
                            continue;
                        }

                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                if (key.Contains("::Main("))
                {
                    NeoMethod m = outModule.mapMethods[key];
                    if (m.inSmartContract)
                    {
                        foreach (var l in this.methodLink)
                        {
                            if (l.Value == m)
                            {
                                if (mainmethod != "")
                                {
                                    throw new Exception("Have too mush EntryPoint,Check it.");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "")
            {
                throw new Exception("Can't find EntryPoint,Check it.");
            }
            else
            {
                //单一默认入口
                logger.Log("Find entrypoint:" + mainmethod);
            }

            outModule.mainMethod = mainmethod;
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数
            //然后给每个method 分配一个func addr
            //还需要对所有的call 做一次地址转换

            //this.outModule.Build();
            return(outModule);
        }
Exemplo n.º 2
0
        public NeoModule Convert(ILModule _in, ConvOption option = null)
        {
            this.inModule  = _in;
            this.outModule = new NeoModule(this.logger)
            {
                option = option ?? ConvOption.Default
            };
            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;                        //skip system type
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                          // skip api
                }
                if (t.Key.Contains(".My."))
                {
                    continue;                         //vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                      // skip the code generated by event
                    }
                    if (m.Value.method.Is_ctor())
                    {
                        continue;
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        //if cctor contains sth can not be as a const value.
                        //  then need 1.record these cctor's code.
                        //            2.insert them to main function
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }

                    NeoMethod nm = new NeoMethod(m.Value);
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }

                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent(e.Value);
                        outModule.mapEvents[ae.name] = ae;
                    }
                    else if (e.Value.field.IsStatic)
                    {
                        var _fieldindex = outModule.mapFields.Count;
                        var field       = new NeoField(e.Key, e.Value.type, _fieldindex);
                        outModule.mapFields[e.Value.field.FullName] = field;
                    }
                }
            }

            var keys = new List <string>(_in.mapType.Keys);

            foreach (var key in keys)
            {
                var value = _in.mapType[key];
                if (key.Contains("<"))
                {
                    continue;                    // skip system typee
                }
                if (key.Contains("_API_"))
                {
                    continue;                        // skip api
                }
                if (key.Contains(".My."))
                {
                    continue;                       //vb system
                }
                foreach (var m in value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                      // skip the code generated by event
                    }
                    var nm = this.methodLink[m.Value];

                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        try
                        {
                            var type = m.Value.method.ReturnType.Resolve();
                            foreach (var i in type.Interfaces)
                            {
                                if (i.InterfaceType.Name == "IApiInterface")
                                {
                                    nm.returntype = "IInteropInterface";
                                }
                            }
                        }
                        catch
                        {
                        }

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        if (IsAppCall(m.Value.method, out byte[] outcall))
Exemplo n.º 3
0
        public NeoModule Convert(ILModule _in, ConvOption option = null)
        {
            this.inModule  = _in;
            this.outModule = new NeoModule(this.logger)
            {
                option = option ?? ConvOption.Default
            };
            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;                        //skip system type
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                          // skip api
                }
                if (t.Key.Contains(".My."))
                {
                    continue;                         //vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                      // skip the code generated by event
                    }
                    if (m.Value.method.Is_ctor())
                    {
                        continue;
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        //if cctor contains sth can not be as a const value.
                        //  then need 1.record these cctor's code.
                        //            2.insert them to main function
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }

                    NeoMethod nm = new NeoMethod(m.Value);
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }

                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent(e.Value);
                        outModule.mapEvents[ae.name] = ae;
                    }
                    else if (e.Value.field.IsStatic)
                    {
                        var _fieldindex = outModule.mapFields.Count;
                        var field       = new NeoField(e.Key, e.Value.type, _fieldindex);
                        outModule.mapFields[e.Value.field.FullName] = field;
                    }
                }
            }

            var keys = new List <string>(_in.mapType.Keys);

            foreach (var key in keys)
            {
                var value = _in.mapType[key];
                if (key.Contains("<"))
                {
                    continue;                    // skip system typee
                }
                if (key.Contains("_API_"))
                {
                    continue;                        // skip api
                }
                if (key.Contains(".My."))
                {
                    continue;                       //vb system
                }
                foreach (var m in value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                      // skip the code generated by event
                    }
                    var nm = this.methodLink[m.Value];

                    //try
                    {
                        nm.returntype = m.Value.returntype;

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        if (IsContractCall(m.Value.method, out _))
                        {
                            continue;
                        }
                        if (IsNonCall(m.Value.method))
                        {
                            continue;
                        }
                        if (IsMixAttribute(m.Value.method, out _, out _))
                        {
                            continue;
                        }

                        if (m.Key.Contains("::Main("))
                        {
                            NeoMethod _m = outModule.mapMethods[m.Key];
                        }
                        this.ConvertMethod(m.Value, nm);
                    }
                }
            }

            if (this.outModule.mapFields.Count > MAX_STATIC_FIELDS_COUNT)
            {
                throw new Exception("too much static fields");
            }
            if (this.outModule.mapFields.Count > 0)
            {
                InsertInitializeMethod();
                logger.Log("Insert _initialize().");
            }

            var attr = outModule.mapMethods.Values.Where(u => u.inSmartContract).Select(u => u.type?.attributes.ToArray()).FirstOrDefault();

            if (attr?.Length > 0)
            {
                outModule.attributes.AddRange(attr);
            }

            this.LinkCode();

            // this.findFirstFunc();// Need to find the first method
            // Assign func addr for each method
            // Then convert the call address

            return(outModule);
        }
Exemplo n.º 4
0
        public AntsModule Convert(ILModule _in)
        {
            //logger.Log("beginConvert.");
            this.outModule = new AntsModule(this.logger);
            foreach (var t in _in.mapType)
            {
                if (t.Key[0] == '<')
                {
                    continue;                 //系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    AntsMethod nm = new AntsMethod();
                    if (m.Key == ".cctor")
                    {
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.IsConstructor)
                    {
                        continue;
                    }
                    nm._namespace                 = m.Value.method.DeclaringType.FullName;
                    nm.name                       = m.Value.method.FullName;
                    nm.isPublic                   = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
            }
            foreach (var t in _in.mapType)
            {
                if (t.Key[0] == '<')
                {
                    continue;                 //系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Key == ".cctor")
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    var nm = this.methodLink[m.Value];

                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new AntsParam(src.name, src.type));
                        }

                        byte[] outcall; string name;
                        if (IsAppCall(m.Value.method, out outcall))
                        {
                            continue;
                        }
                        if (IsNonCall(m.Value.method))
                        {
                            continue;
                        }
                        if (IsOpCall(m.Value.method, out name))
                        {
                            continue;
                        }
                        if (IsSysCall(m.Value.method, out name))
                        {
                            continue;
                        }


                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                if (key.Contains("::Main("))
                {
                    var m = outModule.mapMethods[key];
                    foreach (var l in this.methodLink)
                    {
                        if (l.Value == m)
                        {
                            var srcm = l.Key.method;
                            if (srcm.DeclaringType.BaseType.Name == "SmartContract")
                            {
                                logger.Log("找到函数入口点:" + key);
                                if (mainmethod != "")
                                {
                                    throw new Exception("拥有多个函数入口点,请检查");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "")
            {
                throw new Exception("找不到入口函数,请检查");
            }
            outModule.mainMethod = mainmethod;
            //得找到第一个函数
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数
            //然后给每个method 分配一个func addr
            //还需要对所有的call 做一次地址转换

            //this.outModule.Build();
            return(outModule);
        }
Exemplo n.º 5
0
        public AntsModule Convert(ILModule _in)
        {
            //logger.Log("beginConvert.");
            this.outModule = new AntsModule(this.logger);
            foreach (var t in _in.mapType)
            {
                if (t.Key[0] == '<')
                {
                    continue;                 //系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    AntsMethod nm = new AntsMethod();
                    if (m.Key == ".cctor")
                    {
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.IsConstructor)
                    {
                        continue;
                    }
                    nm._namespace  = m.Value.method.DeclaringType.FullName;
                    nm.name        = m.Value.method.FullName;
                    nm.displayName = m.Value.method.Name;

                    Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = m.Value.method.CustomAttributes;
                    foreach (var attr in ca)
                    {
                        if (attr.AttributeType.Name == "DisplayNameAttribute")
                        {
                            nm.displayName = (string)attr.ConstructorArguments[0].Value;
                        }
                    }

                    nm.isPublic = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        AntsEvent ae = new AntsEvent();
                        ae._namespace  = e.Value.field.DeclaringType.FullName;
                        ae.name        = ae._namespace + "::" + e.Key;
                        ae.displayName = e.Value.displayName;
                        ae.returntype  = e.Value.returntype;
                        ae.paramtypes  = e.Value.paramtypes;
                        outModule.mapEvents[ae.name] = ae;
                    }
                }
            }

            Dictionary <byte, string> spmains = new Dictionary <byte, string>();

            foreach (var t in _in.mapType)
            {
                if (t.Key[0] == '<')
                {
                    continue;                 //系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Key == ".cctor")
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    var  nm = this.methodLink[m.Value];
                    byte entryid;
                    if (IsEntryCall(m.Value.method, out entryid))
                    {
                        spmains[entryid] = nm.name;
                        logger.Log("找到函数入口点:[" + entryid + "]" + nm.name);
                    }

                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new AntsParam(src.name, src.type));
                        }

                        byte[] outcall; string name;
                        if (IsAppCall(m.Value.method, out outcall))
                        {
                            continue;
                        }
                        if (IsNonCall(m.Value.method))
                        {
                            continue;
                        }
                        if (IsOpCall(m.Value.method, out name))
                        {
                            continue;
                        }
                        if (IsSysCall(m.Value.method, out name))
                        {
                            continue;
                        }

                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                if (key.Contains("::Main("))
                {
                    AntsMethod m = outModule.mapMethods[key];

                    foreach (var l in this.methodLink)
                    {
                        if (l.Value == m)
                        {
                            var srcm = l.Key.method;
                            if (srcm.DeclaringType.BaseType.Name == "SmartContract")
                            {
                                if (mainmethod != "")
                                {
                                    throw new Exception("拥有多个函数入口点,请检查");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "" && spmains.Count == 0)
            {
                throw new Exception("找不到入口函数,请检查");
            }
            else if (mainmethod != "" && spmains.Count > 0)
            {
                throw new Exception("同时拥有指定入口函数和默认入口函数,请检查");
            }
            else if (mainmethod != "")
            {
                //单一默认入口
                logger.Log("找到函数入口点:" + mainmethod);
            }
            else if (spmains.Count > 0) //拥有条件入口的情况
            {
                mainmethod = this.CreateJmpMain(spmains);
            }

            outModule.mainMethod = mainmethod;
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数
            //然后给每个method 分配一个func addr
            //还需要对所有的call 做一次地址转换

            //this.outModule.Build();
            return(outModule);
        }
Exemplo n.º 6
0
        private bool TryInsertMethod(NeoModule outModule, Mono.Cecil.MethodDefinition method)
        {
            var oldaddr     = this.addr;
            var oldaddrconv = new Dictionary <int, int>();

            foreach (int k in addrconv.Keys)
            {
                oldaddrconv[k] = addrconv[k];
            }
            var    typename = method.DeclaringType.FullName;
            ILType type;

            if (inModule.mapType.TryGetValue(typename, out type) == false)
            {
                type = new ILType(null, method.DeclaringType);
                inModule.mapType[typename] = type;
            }

            var _method = type.methods[method.FullName];

            try
            {
                NeoMethod nm = new NeoMethod();
                if (method.FullName.Contains(".cctor"))
                {
                    CctorSubVM.Parse(_method, this.outModule);
                    //continue;
                    return(false);
                }
                if (method.IsConstructor)
                {
                    return(false);
                    //continue;
                }
                nm._namespace  = method.DeclaringType.FullName;
                nm.name        = method.FullName;
                nm.displayName = method.Name;
                Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = method.CustomAttributes;
                foreach (var attr in ca)
                {
                    if (attr.AttributeType.Name == "DisplayNameAttribute")
                    {
                        nm.displayName = (string)attr.ConstructorArguments[0].Value;
                    }
                }
                nm.inSmartContract            = method.DeclaringType.BaseType.Name == "SmartContract";
                nm.isPublic                   = method.IsPublic;
                this.methodLink[_method]      = nm;
                outModule.mapMethods[nm.name] = nm;

                ConvertMethod(_method, nm);
                return(true);
            }
            catch
            {
                return(false);
            }
            finally
            {
                this.addr = oldaddr;
                this.addrconv.Clear();
                foreach (int k in oldaddrconv.Keys)
                {
                    addrconv[k] = oldaddrconv[k];
                }
            }
        }
Exemplo n.º 7
0
        public NeoModule Convert(ILModule _in)
        {
            //logger.Log("beginConvert.");
            this.outModule = new NeoModule(this.logger);
            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;//系统的,不要 system, no
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要 api, do not
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要 Auto - generated code, do not
                    }
                    NeoMethod nm = new NeoMethod();
                    if (m.Key.Contains(".cctor"))
                    {
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.IsConstructor)
                    {
                        continue;
                    }
                    nm._namespace  = m.Value.method.DeclaringType.FullName;
                    nm.name        = m.Value.method.FullName;
                    nm.displayName = m.Value.method.Name;

                    Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = m.Value.method.CustomAttributes;
                    foreach (var attr in ca)
                    {
                        if (attr.AttributeType.Name == "DisplayNameAttribute")
                        {
                            nm.displayName = (string)attr.ConstructorArguments[0].Value;
                        }
                    }
                    nm.inSmartContract            = m.Value.method.DeclaringType.BaseType.Name == "SmartContract";
                    nm.isPublic                   = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent();
                        ae._namespace  = e.Value.field.DeclaringType.FullName;
                        ae.name        = ae._namespace + "::" + e.Key;
                        ae.displayName = e.Value.displayName;
                        ae.returntype  = e.Value.returntype;
                        ae.paramtypes  = e.Value.paramtypes;
                        outModule.mapEvents[ae.name] = ae;
                    }
                }
            }


            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;//系统的,不要 Systematic, no
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要 api, do not
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Key.Contains(".cctor"))
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要 Auto-generated code, do not
                    }
                    var nm = this.methodLink[m.Value];


                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        try
                        {
                            var type = m.Value.method.ReturnType.Resolve();
                            foreach (var i in type.Interfaces)
                            {
                                if (i.Name == "IApiInterface")
                                {
                                    nm.returntype = "IInteropInterface";
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        byte[] outcall; string name;
                        if (IsAppCall(m.Value.method, out outcall))
                        {
                            continue;
                        }
                        if (IsNonCall(m.Value.method))
                        {
                            continue;
                        }
                        if (IsOpCall(m.Value.method, out name))
                        {
                            continue;
                        }
                        if (IsSysCall(m.Value.method, out name))
                        {
                            continue;
                        }

                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起 Conversion finished, be a link, all together
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                if (key.Contains("::Main("))
                {
                    NeoMethod m = outModule.mapMethods[key];
                    if (m.inSmartContract)
                    {
                        foreach (var l in this.methodLink)
                        {
                            if (l.Value == m)
                            {
                                if (mainmethod != "")
                                {
                                    throw new Exception("Have too mush EntryPoint,Check it.");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "")
            {
                throw new Exception("Can't find EntryPoint,Check it.");
            }
            else
            {
                //单一默认入口 Single default entry
                logger.Log("Find entrypoint:" + mainmethod);
            }

            outModule.mainMethod = mainmethod;
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数 Have to find the first function
            //然后给每个method 分配一个func addr Then assign a func addr to each method
            //还需要对所有的call 做一次地址转换 Also need to do an address translation for all calls

            //this.outModule.Build();
            return(outModule);
        }
Exemplo n.º 8
0
        public NeoModule Convert(ILModule _in, ConvOption option = null)
        {
            this.inModule = _in;
            //logger.Log("beginConvert.");
            this.outModule = new NeoModule(this.logger)
            {
                option = option ?? ConvOption.Default
            };
            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    NeoMethod nm = new NeoMethod();
                    if (m.Key.Contains(".cctor"))
                    {
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.IsConstructor)
                    {
                        continue;
                    }
                    nm._namespace  = m.Value.method.DeclaringType.FullName;
                    nm.name        = m.Value.method.FullName;
                    nm.displayName = m.Value.method.Name;

                    Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = m.Value.method.CustomAttributes;
                    foreach (var attr in ca)
                    {
                        if (attr.AttributeType.Name == "DisplayNameAttribute")
                        {
                            nm.displayName = (string)attr.ConstructorArguments[0].Value;
                        }
                    }
                    nm.inSmartContract            = m.Value.method.DeclaringType.BaseType.Name == "SmartContract";
                    nm.isPublic                   = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }

                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent
                        {
                            _namespace  = e.Value.field.DeclaringType.FullName,
                            name        = e.Value.field.DeclaringType.FullName + "::" + e.Key,
                            displayName = e.Value.displayName,
                            returntype  = e.Value.returntype,
                            paramtypes  = e.Value.paramtypes
                        };
                        outModule.mapEvents[ae.name] = ae;
                    }
                    else if (e.Value.field.IsStatic)
                    {
                        var _fieldindex = outModule.mapFields.Count;
                        var field       = new NeoField(e.Key, e.Value.type, _fieldindex);
                        outModule.mapFields[e.Value.field.FullName] = field;
                    }
                }
            }

            var keys = new List <string>(_in.mapType.Keys);

            foreach (var key in keys)
            {
                var value = _in.mapType[key];
                if (key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (key.Contains("_API_"))
                {
                    continue;                       //api的,不要
                }
                if (key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Key.Contains(".cctor"))
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    var nm = this.methodLink[m.Value];

                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        try
                        {
                            var type = m.Value.method.ReturnType.Resolve();
                            foreach (var i in type.Interfaces)
                            {
                                if (i.InterfaceType.Name == "IApiInterface")
                                {
                                    nm.returntype = "IInteropInterface";
                                }
                            }
                        }
                        catch
                        {
                        }

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        if (IsAppCall(m.Value.method, out byte[] outcall))