Example #1
0
        public static CodeSectionInfo Parse(byte[] data, Encoding encoding, bool cryptEc = false)
        {
            var codeSectionInfo = new CodeSectionInfo();

            int[]   minRequiredCmds;
            short[] minRequiredDataTypes;
            short[] minRequiredConstants;
            using (var reader = new BinaryReader(new MemoryStream(data, false), encoding))
            {
                codeSectionInfo.allocatedIdNum = reader.ReadInt32();
                reader.ReadInt32(); // 确认于易语言V5.71
                minRequiredCmds = reader.ReadInt32sWithByteSizePrefix();
                if (cryptEc)
                {
                    reader.ReadInt32();
                    reader.ReadInt32();
                    minRequiredDataTypes       = reader.ReadInt16sWithByteSizePrefix();
                    codeSectionInfo.Flag       = reader.ReadInt32();
                    codeSectionInfo.MainMethod = reader.ReadInt32();
                    codeSectionInfo.Libraries  = LibraryRefInfo.ReadLibraries(reader, encoding);
                    minRequiredConstants       = reader.ReadInt16sWithByteSizePrefix();
                }
                else
                {
                    minRequiredDataTypes       = reader.ReadInt16sWithByteSizePrefix();
                    minRequiredConstants       = reader.ReadInt16sWithByteSizePrefix();
                    codeSectionInfo.Libraries  = LibraryRefInfo.ReadLibraries(reader, encoding);
                    codeSectionInfo.Flag       = reader.ReadInt32();
                    codeSectionInfo.MainMethod = reader.ReadInt32();
                }
                LibraryRefInfo.ApplyCompatibilityInfo(codeSectionInfo.Libraries, minRequiredCmds, minRequiredDataTypes, minRequiredConstants);
                if ((codeSectionInfo.Flag & 1) != 0)
                {
                    codeSectionInfo.UnknownBeforeIconData = reader.ReadBytes(16); // Unknown
                }
                codeSectionInfo.IconData = reader.ReadBytesWithLengthPrefix();
                codeSectionInfo.DebugCommandParameters = reader.ReadStringWithLengthPrefix(encoding);
                if (cryptEc)
                {
                    reader.ReadBytes(12);
                    codeSectionInfo.Methods         = MethodInfo.ReadMethods(reader, encoding);
                    codeSectionInfo.DllDeclares     = DllDeclareInfo.ReadDllDeclares(reader, encoding);
                    codeSectionInfo.GlobalVariables = AbstractVariableInfo.ReadVariables(reader, encoding, x => new GlobalVariableInfo(x));
                    codeSectionInfo.Classes         = ClassInfo.ReadClasses(reader, encoding);
                    codeSectionInfo.Structs         = StructInfo.ReadStructs(reader, encoding);
                }
                else
                {
                    codeSectionInfo.Classes         = ClassInfo.ReadClasses(reader, encoding);
                    codeSectionInfo.Methods         = MethodInfo.ReadMethods(reader, encoding);
                    codeSectionInfo.GlobalVariables = AbstractVariableInfo.ReadVariables(reader, encoding, x => new GlobalVariableInfo(x));
                    codeSectionInfo.Structs         = StructInfo.ReadStructs(reader, encoding);
                    codeSectionInfo.DllDeclares     = DllDeclareInfo.ReadDllDeclares(reader, encoding);
                }
            }
            return(codeSectionInfo);
        }
Example #2
0
        public static CodeSectionInfo Parse(byte[] data, bool cryptEc = false)
        {
            var codeSectionInfo = new CodeSectionInfo();

            using (var reader = new BinaryReader(new MemoryStream(data, false)))
            {
                codeSectionInfo.AllocatedIdNum = reader.ReadInt32();
                reader.ReadInt32();                                                          //确认于易语言V5.71
                codeSectionInfo.UnknownBeforeLibrary_1 = reader.ReadBytesWithLengthPrefix(); //Unknown
                if (cryptEc)
                {
                    reader.ReadInt32();
                    reader.ReadInt32();
                    codeSectionInfo.UnknownBeforeLibrary_2 = reader.ReadBytesWithLengthPrefix();//Unknown
                    codeSectionInfo.Flag                   = reader.ReadInt32();
                    codeSectionInfo.MainMethod             = reader.ReadInt32();
                    codeSectionInfo.Libraries              = LibraryRefInfo.ReadLibraries(reader);
                    codeSectionInfo.UnknownBeforeLibrary_3 = reader.ReadBytesWithLengthPrefix();//Unknown
                }
                else
                {
                    codeSectionInfo.UnknownBeforeLibrary_2 = reader.ReadBytesWithLengthPrefix(); //Unknown
                    codeSectionInfo.UnknownBeforeLibrary_3 = reader.ReadBytesWithLengthPrefix(); //Unknown
                    codeSectionInfo.Libraries  = LibraryRefInfo.ReadLibraries(reader);
                    codeSectionInfo.Flag       = reader.ReadInt32();
                    codeSectionInfo.MainMethod = reader.ReadInt32();
                }
                ;
                if ((codeSectionInfo.Flag & 1) != 0)
                {
                    codeSectionInfo.UnknownBeforeIconData = reader.ReadBytes(16);//Unknown
                }
                codeSectionInfo.IconData = reader.ReadBytesWithLengthPrefix();
                codeSectionInfo.DebugCommandParameters = reader.ReadStringWithLengthPrefix();
                if (cryptEc)
                {
                    reader.ReadBytes(12);
                    codeSectionInfo.Methods         = MethodInfo.ReadMethods(reader);
                    codeSectionInfo.DllDeclares     = DllDeclareInfo.ReadDllDeclares(reader);
                    codeSectionInfo.GlobalVariables = AbstractVariableInfo.ReadVariables(reader, x => new GlobalVariableInfo(x));
                    codeSectionInfo.Classes         = ClassInfo.ReadClasses(reader);
                    codeSectionInfo.Structs         = StructInfo.ReadStructs(reader);
                }
                else
                {
                    codeSectionInfo.Classes         = ClassInfo.ReadClasses(reader);
                    codeSectionInfo.Methods         = MethodInfo.ReadMethods(reader);
                    codeSectionInfo.GlobalVariables = AbstractVariableInfo.ReadVariables(reader, x => new GlobalVariableInfo(x));
                    codeSectionInfo.Structs         = StructInfo.ReadStructs(reader);
                    codeSectionInfo.DllDeclares     = DllDeclareInfo.ReadDllDeclares(reader);
                }
            }
            return(codeSectionInfo);
        }
Example #3
0
        /// <summary>
        /// CodeSectionInfo改变时必须重新创建IDToNameMap以便更新数据(尽管部分数据可能自动更新)
        /// </summary>
        /// <param name="codeSection"></param>
        public IdToNameMap(CodeSectionInfo codeSection, ResourceSectionInfo resourceSection)
        {
            LibDefinedName = codeSection.Libraries.Select(x =>
            {
                try
                {
                    return(LibInfo.LibInfo.Load(x));
                }
                catch (Exception)
                {
                    return(null);
                }
            }).ToArray();
            UserDefinedName = new Dictionary <int, string>();
            foreach (var method in codeSection.Methods)
            {
                UserDefinedName.Add(method.Id, method.Name);
                Array.ForEach(method.Parameters, x => UserDefinedName.Add(x.Id, x.Name));
                Array.ForEach(method.Variables, x => UserDefinedName.Add(x.Id, x.Name));
            }
            foreach (var dll in codeSection.DllDeclares)
            {
                UserDefinedName.Add(dll.Id, dll.Name);
                Array.ForEach(dll.Parameters, x => UserDefinedName.Add(x.Id, x.Name));
            }
            foreach (var classInfo in codeSection.Classes)
            {
                UserDefinedName.Add(classInfo.Id, classInfo.Name);
                Array.ForEach(classInfo.Variables, x => UserDefinedName.Add(x.Id, x.Name));
            }
            foreach (var structInfo in codeSection.Structs)
            {
                UserDefinedName.Add(structInfo.Id, structInfo.Name);
                Array.ForEach(structInfo.Member, x => UserDefinedName.Add(x.Id, x.Name));
            }
            Array.ForEach(codeSection.GlobalVariables, x => UserDefinedName.Add(x.Id, x.Name));
            Array.ForEach(resourceSection.Constants, x => UserDefinedName.Add(x.Id, x.Name));
            foreach (var formInfo in resourceSection.Forms)
            {
                UserDefinedName.Add(formInfo.Id, formInfo.Name);
                Array.ForEach(formInfo.Elements, x => UserDefinedName.Add(x.Id, x.Name));
            }

            var needToRemove = new List <int>();

            foreach (var item in UserDefinedName)
            {
                if (string.IsNullOrEmpty(item.Value))
                {
                    needToRemove.Add(item.Key);
                }
            }
            needToRemove.ForEach(x => UserDefinedName.Remove(x));
        }
Example #4
0
        public void Load(Stream stream, ProjectFileReader.OnInputPassword inputPassword = null)
        {
            ESystemInfo       = null;
            ProjectConfigInfo = null;
            Resource          = null;
            Code              = null;
            EPackageInfo      = null;
            InitEcSectionInfo = null;
            LosableSection    = null;
            FolderSection     = null;
            EcSection         = EcSection2 = AuxiliarySection2 = AuxiliarySection3 = EditInfoSection2 = AuxiliarySection1 = null;
            OtherSections     = new List <SectionInfo>();
            using (var reader = new ProjectFileReader(stream, inputPassword))
            {
                var processor = new Dictionary <int, Action <SectionInfo> >
                {
                    { ESystemInfo.SectionKey, x => ESystemInfo = ESystemInfo.Parse(x.Data) },
                    { ProjectConfigInfo.SectionKey, x => ProjectConfigInfo = ProjectConfigInfo.Parse(x.Data, Encoding) },
                    { ResourceSectionInfo.SectionKey, x => Resource = ResourceSectionInfo.Parse(x.Data, Encoding) },
                    { CodeSectionInfo.SectionKey, x => Code = CodeSectionInfo.Parse(x.Data, Encoding, reader.CryptEc) },
                    { EPackageInfo.SectionKey, x => EPackageInfo = EPackageInfo.Parse(x.Data, Encoding) },
                    { InitEcSectionInfo.SectionKey, x => InitEcSectionInfo = InitEcSectionInfo.Parse(x.Data, Encoding) },
                    { 0x0C007319, x => EcSection = x.Data },
                    { 0x0F007319, x => EcSection2 = x.Data },
                    { 0x0B007319, x => AuxiliarySection2 = x.Data },
                    { LosableSectionInfo.SectionKey, x => LosableSection = LosableSectionInfo.Parse(x.Data, Encoding) },
                    { 0x10007319, x => AuxiliarySection3 = x.Data },
                    { 0x09007319, x => EditInfoSection2 = x.Data },
                    { 0x0A007319, x => AuxiliarySection1 = x.Data },
                    { FolderSectionInfo.SectionKey, x => FolderSection = FolderSectionInfo.Parse(x.Data, Encoding) }
                };

                while (!reader.IsFinish)
                {
                    var section = reader.ReadSection();
                    switch (section.Key)
                    {
                    case int key when processor.ContainsKey(key):
                        processor[key](section);

                        break;

                    default:
                        OtherSections.Add(section);
                        break;
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// <paramref name="codeSection"/>或<paramref name="resourceSection"/>或<paramref name="losableSection"/>改变时必须重新创建IDToNameMap以便更新数据(尽管部分数据可能自动更新)
        /// </summary>
        /// <param name="codeSection">程序段</param>
        /// <param name="resourceSection">资源段</param>
        /// <param name="losableSection">可丢失程序段</param>
        public IdToNameMap(CodeSectionInfo codeSection, ResourceSectionInfo resourceSection, LosableSectionInfo losableSection) : this(codeSection?.Libraries)
        {
            if (codeSection != null)
            {
                foreach (var method in codeSection.Methods)
                {
                    if (string.IsNullOrEmpty(method.Name))
                    {
                        var symbol = ParseDebugComment(method.Comment);
                        if (symbol != null)
                        {
                            UserDefinedName.Add(method.Id, symbol);
                        }
                    }
                    else
                    {
                        UserDefinedName.Add(method.Id, method.Name);
                    }
                    Array.ForEach(method.Parameters, x => UserDefinedName.Add(x.Id, x.Name));
                    Array.ForEach(method.Variables, x => UserDefinedName.Add(x.Id, x.Name));
                }
                foreach (var dll in codeSection.DllDeclares)
                {
                    UserDefinedName.Add(dll.Id, dll.Name);
                    Array.ForEach(dll.Parameters, x => UserDefinedName.Add(x.Id, x.Name));
                }
                foreach (var classInfo in codeSection.Classes)
                {
                    foreach (var item in classInfo.Method)
                    {
                        MethodIdToClassId[item] = classInfo.Id;
                    }
                    if (string.IsNullOrEmpty(classInfo.Name))
                    {
                        var symbol = ParseDebugComment(classInfo.Comment);
                        if (symbol != null)
                        {
                            UserDefinedName.Add(classInfo.Id, symbol);
                        }
                    }
                    else
                    {
                        UserDefinedName.Add(classInfo.Id, classInfo.Name);
                    }
                    Array.ForEach(classInfo.Variables, x => UserDefinedName.Add(x.Id, x.Name));
                }
                foreach (var structInfo in codeSection.Structs)
                {
                    UserDefinedName.Add(structInfo.Id, structInfo.Name);
                    Array.ForEach(structInfo.Member, x => UserDefinedName.Add(x.Id, x.Name));
                }
                Array.ForEach(codeSection.GlobalVariables, x => UserDefinedName.Add(x.Id, x.Name));
            }
            if (resourceSection != null)
            {
                Array.ForEach(resourceSection.Constants, x => UserDefinedName.Add(x.Id, x.Name));
                foreach (var formInfo in resourceSection.Forms)
                {
                    UserDefinedName.Add(formInfo.Id, formInfo.Name);
                    Array.ForEach(formInfo.Elements, x => UserDefinedName.Add(x.Id, x.Name));
                }
            }
            if (losableSection != null)
            {
                Array.ForEach(losableSection.RemovedDefinedItem, x => UserDefinedName.Add(x.Id, x.Name));
            }
            if (codeSection.MainMethod != 0)
            {
                UserDefinedName[codeSection.MainMethod] = "_启动子程序";
            }

            // 处理无名对象
            var needToRemove = new List <int>();

            foreach (var item in UserDefinedName)
            {
                if (string.IsNullOrEmpty(item.Value))
                {
                    needToRemove.Add(item.Key);
                }
            }
            needToRemove.ForEach(x => UserDefinedName.Remove(x));
        }
Example #6
0
 /// <summary>
 /// <paramref name="codeSection"/>或<paramref name="resourceSection"/>改变时必须重新创建IDToNameMap以便更新数据(尽管部分数据可能自动更新)
 /// </summary>
 /// <param name="codeSection">程序段</param>
 /// <param name="resourceSection">资源段</param>
 public IdToNameMap(CodeSectionInfo codeSection, ResourceSectionInfo resourceSection) : this(codeSection, resourceSection, null)
 {
 }
Example #7
0
        /// <summary>
        /// <paramref name="codeSection"/>或<paramref name="resourceSection"/>或<paramref name="losableSection"/>改变时必须重新创建IDToNameMap以便更新数据(尽管部分数据可能自动更新)
        /// </summary>
        /// <param name="codeSection">程序段</param>
        /// <param name="resourceSection">资源段</param>
        /// <param name="losableSection">可丢失程序段</param>
        public IdToNameMap(CodeSectionInfo codeSection, ResourceSectionInfo resourceSection, LosableSectionInfo losableSection)
        {
            libDefinedName = codeSection.Libraries.Select(x =>
            {
                try
                {
                    return(LibInfo.LibInfo.Load(x));
                }
                catch (Exception)
                {
                    return(null);
                }
            }).ToArray();
            userDefinedName = new Dictionary <int, string>();
            if (codeSection != null)
            {
                foreach (var method in codeSection.Methods)
                {
                    if (string.IsNullOrEmpty(method.Name))
                    {
                        var symbol = ParseDebugComment(method.Comment);
                        if (symbol != null)
                        {
                            userDefinedName.Add(method.Id, symbol);
                        }
                    }
                    else
                    {
                        userDefinedName.Add(method.Id, method.Name);
                    }
                    Array.ForEach(method.Parameters, x => userDefinedName.Add(x.Id, x.Name));
                    Array.ForEach(method.Variables, x => userDefinedName.Add(x.Id, x.Name));
                }
                foreach (var dll in codeSection.DllDeclares)
                {
                    userDefinedName.Add(dll.Id, dll.Name);
                    Array.ForEach(dll.Parameters, x => userDefinedName.Add(x.Id, x.Name));
                }
                foreach (var classInfo in codeSection.Classes)
                {
                    userDefinedName.Add(classInfo.Id, classInfo.Name);
                    Array.ForEach(classInfo.Variables, x => userDefinedName.Add(x.Id, x.Name));
                }
                foreach (var structInfo in codeSection.Structs)
                {
                    userDefinedName.Add(structInfo.Id, structInfo.Name);
                    Array.ForEach(structInfo.Member, x => userDefinedName.Add(x.Id, x.Name));
                }
                Array.ForEach(codeSection.GlobalVariables, x => userDefinedName.Add(x.Id, x.Name));
            }
            if (resourceSection != null)
            {
                Array.ForEach(resourceSection.Constants, x => userDefinedName.Add(x.Id, x.Name));
                foreach (var formInfo in resourceSection.Forms)
                {
                    userDefinedName.Add(formInfo.Id, formInfo.Name);
                    Array.ForEach(formInfo.Elements, x => userDefinedName.Add(x.Id, x.Name));
                }
            }
            if (losableSection != null)
            {
                Array.ForEach(losableSection.RemovedDefinedItem, x => userDefinedName.Add(x.Id, x.Name));
            }
            if (codeSection.MainMethod != 0)
            {
                userDefinedName[codeSection.MainMethod] = "_启动子程序";
            }

            // 处理无名对象
            var needToRemove = new List <int>();

            foreach (var item in userDefinedName)
            {
                if (string.IsNullOrEmpty(item.Value))
                {
                    needToRemove.Add(item.Key);
                }
            }
            needToRemove.ForEach(x => userDefinedName.Remove(x));
        }
Example #8
0
 public static void WriteJoinCode(IEnumerable <ClassInfo> items, string separator, CodeSectionInfo codeSection, IdToNameMap nameMap, StringBuilder builder, int indent, bool writeCode = true)
 {
     WriteJoinCode(items, separator, builder, x => x.ToTextCode(nameMap, builder, indent, codeSection, writeCode));
 }