Exemplo n.º 1
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;
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static void RepairEProjectFile(string source, string target, ProjectFileReader.OnInputPassword inputPassword = null, int engine = 0)
        {
            var file = new EProjectFile();

            file.Load(File.OpenRead(source), inputPassword);
            var libNameMap  = new IdToNameMap(file.Code.Libraries);
            var classIdMap  = file.Code.Classes.ToDictionary(x => x.Id);
            var methodIdMap = file.Code.Methods.ToDictionary(x => x.Id);

            file.ESystemInfo.FileType = 1;
            foreach (var classInfo in file.Code.Classes)
            {
                if (!ValidEplName(classInfo.Name))
                {
                    classInfo.Name = ParseDebugComment(classInfo.Comment);
                    if (classInfo.Name == null)
                    {
                        if (classInfo.Comment == "_-@M<>")
                        {
                            classInfo.Comment = "";
                        }
                        classInfo.Name = (classInfo.BaseClass == 0 ? "_程序集" : "_类") + (classInfo.Id & EplSystemId.Mask_Num).ToString("X");
                    }
                    else
                    {
                        classInfo.Comment = "";
                    }
                }
                FixVariablesName(classInfo.Variables, classInfo.BaseClass == 0 ? "_程序集变量" : "_成员");
            }
            FixVariablesName(file.Code.GlobalVariables, "_全局");
            foreach (var method in file.Code.Methods)
            {
                if (!ValidEplName(method.Name))
                {
                    method.Name = ParseDebugComment(method.Comment);
                    if (method.Name == null)
                    {
                        if (method.Comment == "_-@S<>")
                        {
                            method.Comment = "";
                        }
                        method.Name = $"_子程序{(method.Id & EplSystemId.Mask_Num).ToString("X")}";
                    }
                    else
                    {
                        method.Comment = "";
                    }
                }
                FixVariablesName(method.Parameters, "_参数", true);
                FixVariablesName(method.Variables, "_局部", true);

                StatementBlock block = null;
                try
                {
                    var codeData = method.CodeData;
#pragma warning disable CS0612 // 类型或成员已过时
                    block = CodeDataParser.ParseStatementBlock(method.CodeData.ExpressionData, file.Encoding, out codeData.LineOffest, out codeData.BlockOffest);
#pragma warning restore CS0612 // 类型或成员已过时
                    if (engine == 1)
                    {
                        codeData = block.ToCodeData(file.Encoding);
                    }
                    method.CodeData = codeData;
                }
                catch (Exception exception)
                {
                    method.Comment = $"[**修复失败:{exception.ToString().Replace("\r\n","<NewLine>")}**]{method.Comment}";
                    continue;
                }
            }
            foreach (var structInfo in file.Code.Structs)
            {
                if (!ValidEplName(structInfo.Name))
                {
                    structInfo.Name = $"_结构{(structInfo.Id & EplSystemId.Mask_Num).ToString("X")}";
                }
                FixVariablesName(structInfo.Member, "_成员", false);
            }
            foreach (var dll in file.Code.DllDeclares)
            {
                if (!ValidEplName(dll.Name))
                {
                    dll.Name = dll.EntryPoint;
                    if (dll.Name.StartsWith("@"))
                    {
                        dll.Name = dll.Name.Substring(1);
                    }
                    dll.Name = "_" + dll.Name;
                    if (!ValidEplName("_" + dll.Name))
                    {
                        dll.Name = "";
                    }
                    dll.Name = $"_DLL命令{(dll.Id & EplSystemId.Mask_Num).ToString("X")}{dll.Name}";
                }
                FixVariablesName(dll.Parameters, "_参数", true);
            }
            foreach (var constant in file.Resource.Constants)
            {
                if (!ValidEplName(constant.Name))
                {
                    constant.Name = constant.Value == null ? "" : $"_常量{(constant.Id & EplSystemId.Mask_Num).ToString("X")}";
                }
            }
            foreach (var formInfo in file.Resource.Forms)
            {
                if (!ValidEplName(formInfo.Name))
                {
                    formInfo.Name = $"_窗口{(formInfo.Id & EplSystemId.Mask_Num).ToString("X")}";
                }
                foreach (var elem in formInfo.Elements)
                {
                    if (elem is FormMenuInfo menu)
                    {
                        MethodInfo eventMethod = null;
                        if (menu.ClickEvent != 0)
                        {
                            methodIdMap.TryGetValue(menu.ClickEvent, out eventMethod);
                        }
                        if (string.IsNullOrEmpty(menu.Name))
                        {
                            if (ValidEplName("_" + menu.Text))
                            {
                                menu.Name = $"_菜单{(menu.Id & EplSystemId.Mask_Num).ToString("X")}_{menu.Text}";
                            }
                            else
                            {
                                menu.Name = $"_菜单{(menu.Id & EplSystemId.Mask_Num).ToString("X")}";
                            }
                            if (eventMethod != null && eventMethod.Name != null && eventMethod.Name.StartsWith("_") && eventMethod.Name.EndsWith("_被选择"))//尝试从事件子程序名还原名称
                            {
                                menu.Name = eventMethod.Name.Substring(1, eventMethod.Name.Length - 5);
                            }
                        }
                        if (eventMethod != null)
                        {
                            eventMethod.Name = $"_{menu.Name}_被选择";
                        }
                    }
                    else if (elem is FormControlInfo control)
                    {
                        var elemName = control.Name;

                        if (!ValidEplName(elemName))
                        {
                            if (control.Events.Length > 0)//尝试从子程序名恢复窗口名
                            {
                                var eventItem = control.Events[0];
                                if (methodIdMap.TryGetValue(eventItem.Value, out var eventMethod))
                                {
                                    var eventName = libNameMap.GetLibTypeName(control.DataType, eventItem.Key);
                                    if (eventMethod.Name.StartsWith("_") && eventMethod.Name.EndsWith($"_{eventName}"))
                                    {
                                        formInfo.Name = eventMethod.Name.Substring(1, eventMethod.Name.Length - 1 - eventName.Length - 1);
                                    }
                                }
                            }
                            elemName = formInfo.Name;
                        }
                        foreach (var eventItem in control.Events)
                        {
                            if (methodIdMap.TryGetValue(eventItem.Value, out var eventMethod))
                            {
                                var eventName = libNameMap.GetLibTypeName(control.DataType, eventItem.Key);
                                eventMethod.Name = $"_{elemName}_{eventName}";
                            }
                        }
                    }
                }
                if (classIdMap.TryGetValue(formInfo.Class, out var formClass))
                {
                    var prefix = $"[“{formInfo.Name}”的窗口程序集]";
                    if (!formClass.Comment.StartsWith(prefix))
                    {
                        formClass.Comment = $"{prefix}{formClass.Comment}";
                    }
                }
            }
            {
                var newInitMethod = new List <int>(file.InitEcSectionInfo.InitMethod.Length);
                var newEcName     = new List <string>(file.InitEcSectionInfo.InitMethod.Length);
                for (int i = 0; i < file.InitEcSectionInfo.InitMethod.Length; i++)
                {
                    if (!methodIdMap.TryGetValue(file.InitEcSectionInfo.InitMethod[i], out var initMethod))
                    {
                        continue;
                    }
                    initMethod.Name = $"初始模块_{i + 1}";
                    if (ValidEplName("_" + file.InitEcSectionInfo.EcName[i]))
                    {
                        initMethod.Name += "_" + file.InitEcSectionInfo.EcName[i];
                    }

                    var prefix = $"[禁止删除][注意:本子程序将自动在启动时被调用,且早于 _启动子程序 被调用][为内联的模块“{file.InitEcSectionInfo.EcName[i]}”做初始化工作]";
                    if (!initMethod.Comment.StartsWith(prefix))
                    {
                        initMethod.Comment = $"{prefix}{initMethod.Comment}";
                    }

                    newInitMethod.Add(file.InitEcSectionInfo.InitMethod[i]);
                    newEcName.Add(i < file.InitEcSectionInfo.EcName.Length ? file.InitEcSectionInfo.EcName[i] : "");
                }
                file.InitEcSectionInfo.InitMethod = newInitMethod.ToArray();
                file.InitEcSectionInfo.EcName     = newEcName.ToArray();
            }
            {
                if (methodIdMap.TryGetValue(file.Code.MainMethod, out var mainMethod))
                {
                    mainMethod.Name = "_启动子程序";
                    if (file.InitEcSectionInfo.InitMethod.Length > 0)
                    {
                        var prefix = "[注意:本子程序将在 初始模块_X 后调用]";
                        if (!mainMethod.Comment.StartsWith(prefix))
                        {
                            mainMethod.Comment = $"{prefix}{mainMethod.Comment}";
                        }
                    }
                }
            }
            file.Save(File.Create(target));
        }
Exemplo n.º 3
0
        private static void FixEProjectFile(string source, string target, ProjectFileReader.OnInputPassword inputPassword = null)
        {
            ESystemInfo         systemInfo          = null;
            CodeSectionInfo     codeSectionInfo     = null;
            ResourceSectionInfo resourceSectionInfo = null;
            InitEcSectionInfo   initEcSectionInfo   = null;
            var sections = new List <SectionInfo>();

            using (var projectFileReader = new ProjectFileReader(File.OpenRead(source), inputPassword))
            {
                while (!projectFileReader.IsFinish)
                {
                    var section = projectFileReader.ReadSection();
                    switch (section.Name)
                    {
                    case ESystemInfo.SectionName:
                        systemInfo = ESystemInfo.Parse(section.Data);
                        break;

                    case CodeSectionInfo.SectionName:
                        codeSectionInfo = CodeSectionInfo.Parse(section.Data, projectFileReader.CryptEc);
                        break;

                    case ResourceSectionInfo.SectionName:
                        resourceSectionInfo = ResourceSectionInfo.Parse(section.Data);
                        break;

                    case InitEcSectionInfo.SectionName:
                        initEcSectionInfo = InitEcSectionInfo.Parse(section.Data);
                        break;

                    default:
                        break;
                    }
                    sections.Add(section);
                }
            }
            systemInfo.FileType = 1;
            foreach (var classInfo in codeSectionInfo.Classes)
            {
                if (!ValidEplName(classInfo.Name))
                {
                    classInfo.Name = ParseDebugComment(classInfo.Comment);
                    if (classInfo.Name == null)
                    {
                        if (classInfo.Comment == "_-@M<>")
                        {
                            classInfo.Comment = "";
                        }
                        classInfo.Name = (classInfo.BaseClass == 0 ? "_程序集" : "_类") + (classInfo.Id & IdMask).ToString("X");
                    }
                    else
                    {
                        classInfo.Comment = "";
                    }
                }
                FixVariablesName(classInfo.Variables, classInfo.BaseClass == 0 ? "_程序集变量" : "_成员");
            }
            FixVariablesName(codeSectionInfo.GlobalVariables, "_全局");
            foreach (var method in codeSectionInfo.Methods)
            {
                if (!ValidEplName(method.Name))
                {
                    method.Name = ParseDebugComment(method.Comment);
                    if (method.Name == null)
                    {
                        if (method.Comment == "_-@S<>")
                        {
                            method.Comment = "";
                        }
                        method.Name = $"_子程序{(method.Id & IdMask).ToString("X")}";
                    }
                    else
                    {
                        method.Comment = "";
                    }
                }
                FixVariablesName(method.Parameters, "_参数", true);
                FixVariablesName(method.Variables, "_局部", true);

                var reader           = new BinaryReader(new MemoryStream(method.CodeData[5], false));
                var lineOffestStream = new MemoryStream();
                CodeDataParser.StatementBlock block = null;
                try
                {
                    block = CodeDataParser.ParseStatementBlock(reader, new BinaryWriter(lineOffestStream));
                }
                catch (Exception)
                {
                    method.Comment = $"[修复失败]{method.Comment}";
                    continue;
                }

                method.CodeData[0] = lineOffestStream.ToArray();
                method.CodeData[1] = CodeDataParser.GenerateBlockOffest(block);
            }
            foreach (var structInfo in codeSectionInfo.Structs)
            {
                if (!ValidEplName(structInfo.Name))
                {
                    structInfo.Name = $"_结构{(structInfo.Id & IdMask).ToString("X")}";
                }
                FixVariablesName(structInfo.Member, "_成员", false);
            }
            foreach (var dll in codeSectionInfo.DllDeclares)
            {
                if (!ValidEplName(dll.Name))
                {
                    dll.Name = dll.NameInLibrary;
                    if (dll.Name.StartsWith("@"))
                    {
                        dll.Name = dll.Name.Substring(1);
                    }
                    dll.Name = "_" + dll.Name;
                    if (!ValidEplName("_" + dll.Name))
                    {
                        dll.Name = "";
                    }
                    dll.Name = $"_DLL命令{(dll.Id & IdMask).ToString("X")}{dll.Name}";
                }
                FixVariablesName(dll.Parameters, "_参数", true);
            }
            foreach (var constant in resourceSectionInfo.Constants)
            {
                if (!ValidEplName(constant.Name))
                {
                    constant.Name = $"_常量{(constant.Id & IdMask).ToString("X")}";
                }
            }
            foreach (var formInfo in resourceSectionInfo.Forms)
            {
                if (!ValidEplName(formInfo.Name))
                {
                    formInfo.Name = $"_窗口{(formInfo.Id & IdMask).ToString("X")}";
                }
                foreach (var elem in formInfo.Elements)
                {
                    if (elem is FormMenuInfo)
                    {
                        var        menu        = (FormMenuInfo)elem;
                        MethodInfo eventMethod = null;
                        if (menu.ClickEvent != 0)
                        {
                            eventMethod = Array.Find(codeSectionInfo.Methods, x => x.Id == menu.ClickEvent);
                        }
                        if (string.IsNullOrEmpty(menu.Name))
                        {
                            if (ValidEplName("_" + menu.Text))
                            {
                                menu.Name = $"_菜单{(menu.Id & IdMask).ToString("X")}_{menu.Text}";
                            }
                            else
                            {
                                menu.Name = $"_菜单{(menu.Id & IdMask).ToString("X")}";
                            }
                            if (eventMethod != null && eventMethod.Name != null &&
                                eventMethod.Name.StartsWith("_") && eventMethod.Name.EndsWith("_被选择"))//尝试从事件子程序名还原名称
                            {
                                menu.Name = eventMethod.Name.Substring(1, eventMethod.Name.Length - 5);
                            }
                        }
                        if (eventMethod != null)
                        {
                            eventMethod.Name = $"_{menu.Name}_被选择";
                        }
                    }
                    else if (elem is FormControlInfo)
                    {
                        var control  = (FormControlInfo)elem;
                        var elemName = control.Name;

                        if (!ValidEplName(elemName))
                        {
                            if (control.Events.Length > 0)//尝试从子程序名恢复窗口名
                            {
                                var        eventItem   = control.Events[0];
                                MethodInfo eventMethod = Array.Find(codeSectionInfo.Methods, x => x.Id == eventItem.Value);//TODO:使用哈希表提高效率
                                if (eventMethod != null)
                                {
                                    var eventName = GetEventName(codeSectionInfo.Libraries, control.DataType, eventItem.Key);
                                    if (eventName != null && eventMethod.Name.StartsWith("_") && eventMethod.Name.EndsWith($"_{eventName}"))
                                    {
                                        formInfo.Name = eventMethod.Name.Substring(1, eventMethod.Name.Length - 1 - eventName.Length - 1);
                                    }
                                }
                            }
                            elemName = formInfo.Name;
                        }
                        foreach (var eventItem in control.Events)
                        {
                            MethodInfo eventMethod = Array.Find(codeSectionInfo.Methods, x => x.Id == eventItem.Value);//TODO:使用哈希表提高效率
                            if (eventMethod != null)
                            {
                                var eventName = GetEventName(codeSectionInfo.Libraries, control.DataType, eventItem.Key);
                                if (eventName != null)
                                {
                                    eventMethod.Name = $"_{elemName}_{eventName}";
                                }
                                else if (!eventMethod.Name.StartsWith($"_{elemName}_"))
                                {
                                    eventName        = $"事件{eventItem.Key.ToString("X8")}";
                                    eventMethod.Name = $"_{elemName}_{eventName}";
                                }
                            }
                        }
                    }
                }
                var formClass = Array.Find(codeSectionInfo.Classes, x => x.Id == formInfo.Class);//TODO:使用哈希表提高效率
                if (formClass != null)
                {
                    var prefix = $"[“{formInfo.Name}”的窗口程序集]";
                    if (!formClass.Comment.StartsWith(prefix))
                    {
                        formClass.Comment = $"{prefix}{formClass.Comment}";
                    }
                }
            }
            {
                //TODO:使用哈希表提高效率
                var mainMethod = Array.Find(codeSectionInfo.Methods, x => x.Id == codeSectionInfo.MainMethod);
                if (mainMethod != null)
                {
                    mainMethod.Name = "_启动子程序";
                    if (initEcSectionInfo.InitMethod.Length > 0)
                    {
                        var prefix = "[注意:本子程序将在 初始模块_X 后调用]";
                        if (!mainMethod.Comment.StartsWith(prefix))
                        {
                            mainMethod.Comment = $"{prefix}{mainMethod.Comment}";
                        }
                    }
                }
                for (int i = 0; i < initEcSectionInfo.InitMethod.Length; i++)
                {
                    var initMethod = Array.Find(codeSectionInfo.Methods, x => x.Id == initEcSectionInfo.InitMethod[i]);
                    initMethod.Name = $"初始模块_{i + 1}";
                    if (ValidEplName("_" + initEcSectionInfo.EcName[i]))
                    {
                        initMethod.Name += "_" + initEcSectionInfo.EcName[i];
                    }

                    var prefix = $"[禁止删除][注意:本子程序将自动在启动时被调用,且早于 _启动子程序 被调用][为内联的模块“{initEcSectionInfo.EcName[i]}”做初始化工作]";
                    if (!initMethod.Comment.StartsWith(prefix))
                    {
                        initMethod.Comment = $"{prefix}{initMethod.Comment}";
                    }
                }
            }

            using (var projectFileWriter = new ProjectFileWriter(File.OpenWrite(target)))
            {
                foreach (var section in sections)
                {
                    switch (section.Name)
                    {
                    case ESystemInfo.SectionName:
                        section.Data = systemInfo.ToBytes();
                        break;

                    case CodeSectionInfo.SectionName:
                        section.Data = codeSectionInfo.ToBytes();
                        break;

                    case ResourceSectionInfo.SectionName:
                        section.Data = resourceSectionInfo.ToBytes();
                        break;

                    case InitEcSectionInfo.SectionName:
                        section.Data = initEcSectionInfo.ToBytes();
                        break;

                    default:
                        break;
                    }
                    projectFileWriter.WriteSection(section);
                }
            }
        }