Esempio n. 1
0
        public static List <LogInfo> TXTReplaceOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>(8);

            CodeInfo_TXTReplaceOp infoOp = cmd.Info.Cast <CodeInfo_TXTReplaceOp>();

            CodeInfo_TXTReplace firstInfo = infoOp.Infos[0];
            string fileName = StringEscaper.Preprocess(s, firstInfo.FileName);

            if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            if (!File.Exists(fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"File [{fileName}] does not exist"));
            }

            List <(CodeCommand, string, string)> prepReplace = new List <(CodeCommand, string, string)>();

            // foreach (CodeInfo_TXTReplace info in infoOp.Infos)
            foreach (CodeCommand subCmd in infoOp.Cmds)
            {
                CodeInfo_TXTReplace info = subCmd.Info.Cast <CodeInfo_TXTReplace>();

                string oldStr = StringEscaper.Preprocess(s, info.OldStr);
                string newStr = StringEscaper.Preprocess(s, info.NewStr);

                prepReplace.Add((subCmd, oldStr, newStr));
            }

            Encoding encoding = EncodingHelper.DetectBom(fileName);

            string tempPath = FileHelper.GetTempFile();
            string txtStr;

            using (StreamReader r = new StreamReader(fileName, encoding, false))
            {
                txtStr = r.ReadToEnd();
            }

            foreach ((CodeCommand subCmd, string oldStr, string newStr) in prepReplace)
            {
                txtStr = StringHelper.ReplaceEx(txtStr, oldStr, newStr, StringComparison.OrdinalIgnoreCase);
                logs.Add(new LogInfo(LogState.Success, $"Replaced [{oldStr}] with [{newStr}]", subCmd));
            }

            using (StreamWriter w = new StreamWriter(tempPath, false, encoding))
            {
                w.Write(txtStr);
            }
            logs.Add(new LogInfo(LogState.Success, $"Replaced [{prepReplace.Count}] strings from [{fileName}]"));

            FileHelper.FileReplaceEx(tempPath, fileName);

            return(logs);
        }
Esempio n. 2
0
        public static List <LogInfo> TXTReplace(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_TXTReplace info = cmd.Info.Cast <CodeInfo_TXTReplace>();

            string fileName = StringEscaper.Preprocess(s, info.FileName);
            string oldStr   = StringEscaper.Preprocess(s, info.OldStr);
            string newStr   = StringEscaper.Preprocess(s, info.NewStr);

            if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            if (!File.Exists(fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"File [{fileName}] does not exist"));
            }

            // Detect encoding of text. If text does not exists, create blank file (ANSI)
            Encoding encoding;

            if (File.Exists(fileName))
            {
                encoding = EncodingHelper.SmartDetectEncoding(fileName, () =>
                {
                    return(EncodingHelper.IsActiveCodePageCompatible(info.OldStr) &&
                           EncodingHelper.IsActiveCodePageCompatible(info.NewStr));
                });
            }
            else
            {
                encoding = EncodingHelper.DefaultAnsi;
            }

            string tempPath = FileHelper.GetTempFile();
            string txtStr;

            using (StreamReader r = new StreamReader(fileName, encoding, false))
            {
                txtStr = r.ReadToEnd();
            }

            using (StreamWriter w = new StreamWriter(tempPath, false, encoding))
            {
                txtStr = StringHelper.ReplaceEx(txtStr, oldStr, newStr, StringComparison.OrdinalIgnoreCase);
                w.Write(txtStr);
            }
            FileHelper.FileReplaceEx(tempPath, fileName);

            logs.Add(new LogInfo(LogState.Success, $"Replaced [{oldStr}] with [{newStr}]", cmd));

            return(logs);
        }
Esempio n. 3
0
        public static List <LogInfo> TXTReplace(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_TXTReplace info = cmd.Info.Cast <CodeInfo_TXTReplace>();

            string fileName = StringEscaper.Preprocess(s, info.FileName);
            string oldStr   = StringEscaper.Preprocess(s, info.OldStr);
            string newStr   = StringEscaper.Preprocess(s, info.NewStr);

            if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            if (File.Exists(fileName) == false)
            {
                logs.Add(new LogInfo(LogState.Error, $"File [{fileName}] does not exist"));
                return(logs);
            }

            Encoding encoding = EncodingHelper.DetectBom(fileName);

            string tempPath = Path.GetTempFileName();
            string txtStr;

            using (StreamReader r = new StreamReader(fileName, encoding))
            {
                txtStr = r.ReadToEnd();
            }

            using (StreamWriter w = new StreamWriter(tempPath, false, encoding))
            {
                txtStr = StringHelper.ReplaceEx(txtStr, oldStr, newStr, StringComparison.OrdinalIgnoreCase);
                w.Write(txtStr);
            }
            FileHelper.FileReplaceEx(tempPath, fileName);

            logs.Add(new LogInfo(LogState.Success, $"Replaced [{oldStr}] with [{newStr}]", cmd));

            return(logs);
        }
Esempio n. 4
0
        public static List <LogInfo> TXTReplace(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_TXTReplace));
            CodeInfo_TXTReplace info = cmd.Info as CodeInfo_TXTReplace;

            string fileName = StringEscaper.Preprocess(s, info.FileName);
            string oldStr   = StringEscaper.Preprocess(s, info.OldStr);
            string newStr   = StringEscaper.Preprocess(s, info.NewStr);

            if (StringEscaper.PathSecurityCheck(fileName, out string errorMsg) == false)
            {
                logs.Add(new LogInfo(LogState.Error, errorMsg));
                return(logs);
            }

            if (File.Exists(fileName) == false)
            {
                logs.Add(new LogInfo(LogState.Error, $"File [{fileName}] not exists"));
                return(logs);
            }

            Encoding encoding = FileHelper.DetectTextEncoding(fileName);

            string tempPath = Path.GetTempFileName();

            using (StreamReader reader = new StreamReader(fileName, encoding))
                using (StreamWriter writer = new StreamWriter(tempPath, false, encoding))
                {
                    string str = reader.ReadToEnd();
                    str = StringHelper.ReplaceEx(str, oldStr, newStr, StringComparison.OrdinalIgnoreCase);
                    writer.Write(str);
                }
            FileHelper.FileReplaceEx(tempPath, fileName);

            logs.Add(new LogInfo(LogState.Success, $"Replaced [{oldStr}] with [{newStr}]", cmd));

            return(logs);
        }
Esempio n. 5
0
        private static List <CodeCommand> InternalOptimize(List <CodeCommand> cmds)
        {
            List <CodeCommand> optimized = new List <CodeCommand>(cmds.Count);

            Dictionary <CodeType, List <CodeCommand> > opDict = OptimizedCodeTypes.ToDictionary(x => x, x => new List <CodeCommand>(cmds.Count / 2));

            CodeType s = CodeType.None;

            foreach (CodeCommand cmd in cmds)
            {
                bool loopAgain;
                do
                {
                    loopAgain = false;
                    switch (s)
                    {
                        #region Default
                    case CodeType.None:
                        switch (cmd.Type)
                        {
                        case CodeType.TXTAddLine:
                        case CodeType.TXTReplace:
                        case CodeType.TXTDelLine:
                        case CodeType.IniRead:
                        case CodeType.IniWrite:
                        case CodeType.IniDelete:
                        case CodeType.IniReadSection:
                        case CodeType.IniAddSection:
                        case CodeType.IniDeleteSection:
                        case CodeType.IniWriteTextLine:
                        case CodeType.Visible:
                        case CodeType.ReadInterface:
                        case CodeType.WriteInterface:
                        case CodeType.WimExtract:
                            s = cmd.Type;
                            opDict[cmd.Type].Add(cmd);
                            break;

                        case CodeType.WimPathAdd:
                        case CodeType.WimPathDelete:
                        case CodeType.WimPathRename:
                            s = CodeType.WimPathAdd;         // Use WimPathAdd as representative
                            opDict[CodeType.WimPathAdd].Add(cmd);
                            break;

                        default:
                            optimized.Add(cmd);
                            break;
                        }
                        break;

                        #endregion
                        #region TXTAddLine
                    case CodeType.TXTAddLine:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_TXTAddLine), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.TXTAddLine:
                        {
                            CodeInfo_TXTAddLine firstInfo = opDict[s][0].Info.Cast <CodeInfo_TXTAddLine>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region TXTReplace
                    case CodeType.TXTReplace:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_TXTReplace));
                        switch (cmd.Type)
                        {
                        case CodeType.TXTReplace:
                        {
                            CodeInfo_TXTReplace firstInfo = opDict[s][0].Info.Cast <CodeInfo_TXTReplace>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region TXTDelLine
                    case CodeType.TXTDelLine:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_TXTDelLine), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.TXTDelLine:
                        {
                            CodeInfo_TXTDelLine firstInfo = opDict[s][0].Info.Cast <CodeInfo_TXTDelLine>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region IniRead
                    case CodeType.IniRead:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_IniRead), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.IniRead:
                        {
                            CodeInfo_IniRead firstInfo = opDict[s][0].Info.Cast <CodeInfo_IniRead>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region IniWrite
                    case CodeType.IniWrite:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_IniWrite), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.IniWrite:
                        {
                            CodeInfo_IniWrite firstInfo = opDict[s][0].Info.Cast <CodeInfo_IniWrite>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region IinDelete
                    case CodeType.IniDelete:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_IniDelete), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.IniDelete:
                        {
                            CodeInfo_IniDelete firstInfo = opDict[s][0].Info.Cast <CodeInfo_IniDelete>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region IniReadSection
                    case CodeType.IniReadSection:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_IniReadSection));
                        switch (cmd.Type)
                        {
                        case CodeType.IniReadSection:
                        {
                            CodeInfo_IniReadSection firstInfo = opDict[s][0].Info.Cast <CodeInfo_IniReadSection>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region IniAddSection
                    case CodeType.IniAddSection:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_IniAddSection), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.IniAddSection:
                        {
                            CodeInfo_IniAddSection firstInfo = opDict[s][0].Info.Cast <CodeInfo_IniAddSection>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region IniDeleteSection
                    case CodeType.IniDeleteSection:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_IniDeleteSection), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.IniDeleteSection:
                        {
                            CodeInfo_IniDeleteSection firstInfo = opDict[s][0].Info.Cast <CodeInfo_IniDeleteSection>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region IniWriteTextLine
                    case CodeType.IniWriteTextLine:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_IniWriteTextLine), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.IniWriteTextLine:
                        {
                            CodeInfo_IniWriteTextLine firstInfo = opDict[s][0].Info.Cast <CodeInfo_IniWriteTextLine>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region Visible
                    case CodeType.Visible:
                        switch (cmd.Type)
                        {
                        case CodeType.Visible:
                            opDict[s].Add(cmd);
                            break;

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region ReadInterface
                    case CodeType.ReadInterface:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_ReadInterface), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.ReadInterface:
                        {
                            CodeInfo_ReadInterface firstInfo = opDict[s][0].Info.Cast <CodeInfo_ReadInterface>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region WriteInterface
                    case CodeType.WriteInterface:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_WriteInterface), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.WriteInterface:
                        {
                            CodeInfo_WriteInterface firstInfo = opDict[s][0].Info.Cast <CodeInfo_WriteInterface>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region WimExtract
                    case CodeType.WimExtract:
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_WimExtract), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.WimExtract:
                        {
                            CodeInfo_WimExtract firstInfo = opDict[s][0].Info.Cast <CodeInfo_WimExtract>();
                            if (firstInfo.OptimizeCompare(cmd.Info))
                            {
                                opDict[s].Add(cmd);
                            }
                            else
                            {
                                goto default;
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region WimPath Series
                    case CodeType.WimPathAdd:     // Use WimPathAdd as a representative of WimPath{Add, Delete, Rename}
                        Debug.Assert(opDict[s][0].Info.GetType() == typeof(CodeInfo_WimPathAdd) ||
                                     opDict[s][0].Info.GetType() == typeof(CodeInfo_WimPathDelete) ||
                                     opDict[s][0].Info.GetType() == typeof(CodeInfo_WimPathRename), "Invalid CodeInfo");
                        switch (cmd.Type)
                        {
                        case CodeType.WimPathAdd:
                        case CodeType.WimPathDelete:
                        case CodeType.WimPathRename:
                        {
                            CodeCommand firstCmd = opDict[s][0];
                            if (firstCmd.Type == CodeType.WimPathAdd)
                            {
                                CodeInfo_WimPathAdd firstInfo = opDict[s][0].Info.Cast <CodeInfo_WimPathAdd>();
                                if (firstInfo.OptimizeCompare(cmd.Info))
                                {
                                    opDict[s].Add(cmd);
                                }
                                else
                                {
                                    goto default;
                                }
                            }
                            else if (firstCmd.Type == CodeType.WimPathDelete)
                            {
                                CodeInfo_WimPathDelete firstInfo = opDict[s][0].Info.Cast <CodeInfo_WimPathDelete>();
                                if (firstInfo.OptimizeCompare(cmd.Info))
                                {
                                    opDict[s].Add(cmd);
                                }
                                else
                                {
                                    goto default;
                                }
                            }
                            else if (firstCmd.Type == CodeType.WimPathRename)
                            {
                                CodeInfo_WimPathRename firstInfo = opDict[s][0].Info.Cast <CodeInfo_WimPathRename>();
                                if (firstInfo.OptimizeCompare(cmd.Info))
                                {
                                    opDict[s].Add(cmd);
                                }
                                else
                                {
                                    goto default;
                                }
                            }
                            break;
                        }

                        case CodeType.Comment:         // Remove comments
                            break;

                        default:         // Optimize them
                            FinalizeSequence(s, opDict[s]);
                            s         = CodeType.None;
                            loopAgain = true;
                            break;
                        }
                        break;

                        #endregion
                        #region Error
                    default:
                        throw new InternalException("Internal Logic Error at CodeOptimizer.InternalOptimize()");
                        #endregion
                    }
                }while (loopAgain);
            }

            #region Finish
            foreach (var kv in opDict)
            {
                FinalizeSequence(kv.Key, kv.Value);
            }
            #endregion

            #region FinalizeSequence
            void FinalizeSequence(CodeType state, List <CodeCommand> cmdSeq)
            {
                CodeCommand opCmd;

                if (cmdSeq.Count == 1)
                {
                    opCmd = cmdSeq[0];
                }
                else if (1 < cmdSeq.Count)
                {
                    opCmd = PackCommand(state, new List <CodeCommand>(cmdSeq));
                }
                else // if (cmds.Count <= 0)
                {
                    return;
                }

                Debug.Assert(opCmd != null, "Internal Logic Error in CodeOptimizer.Optimize");
                optimized.Add(opCmd);

                cmdSeq.Clear();
            }

            #endregion

            return(optimized);
        }