Пример #1
0
        //翻译器-替换富友资源
        public static StringBuilder TranslatorReplaceResourceWfy(TranslatorConfig config)
        {
            StringBuilder result = new StringBuilder();

            if (config.WorkFolder.IsNullOrEmpty())
            {
                return(result);
            }

            using (MemoryStream ms = new MemoryStream(Resources.resource))
            {
                using (IArchive archive = ArchiveFactory.Open(ms))
                {
                    //创建临时目录
                    string tempDir = FilePathHelper.GetAbsolutePath(Guid.NewGuid().ToStringEx(), config.WorkFolder);
                    Directory.CreateDirectory(tempDir);
                    //解压到临时目录
                    foreach (var entry in archive.Entries)
                    {
                        if (!entry.IsDirectory)
                        {
                            entry.WriteToDirectory(tempDir, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
                        }
                    }
                    //复制wfy 到工作目录
                    new DirectoryInfo(FilePathHelper.GetAbsolutePath(".\\wfy", tempDir)).Copy(new DirectoryInfo(config.WorkFolder));
                    //删除临时目录
                    try { Directory.Delete(tempDir, true); }
                    catch { }
                }
            }
            //存在显式文件夹则复制
            DirectoryInfo dirInfoWfy = new DirectoryInfo(FilePathHelper.GetAbsolutePath(".\\resources\\wfy"));

            if (dirInfoWfy.Exists)
            {
                dirInfoWfy.Copy(new DirectoryInfo(config.WorkFolder));
            }
            //根据配置提交
            GitHelper.GitAdd(config);
            GitHelper.GitCommit(config, "【富友资源】替换富友资源");
            result.AppendLine("已替换富友资源");
            return(result);
        }
Пример #2
0
        //翻译器-替换
        public static StringBuilder TranslatorReplace(IWin32Window owner, TranslatorConfig config, Translator translator, string comment)
        {
            StringBuilder result = new StringBuilder(1024);

            if (!TranslatorAssert(config, result))
            {
                return(result);
            }

            IEnumerable <string> includefiles  = TranslatorVisitFiles(config, translator.IncludeFile);
            IEnumerable <string> exceludefiles = (translator.ExcludeFile == null || translator.ExcludeFile.Length == 0) ? new string[] { } : TranslatorVisitFiles(config, translator.ExcludeFile);

            int           count   = 0;
            List <string> affects = new List <string>();

            using (FrmBatch <string> dialog = new FrmBatch <string>(10, true))
            {
                dialog.Work += delegate(object sender, EventArgs <string> e)
                {
                    string fullName = e.Argument;

                    string           oldText       = File.ReadAllText(fullName, UTF8_NO_BOM);
                    string           newText       = oldText;
                    StringComparison comparison    = translator.CaseMatch ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; //大小写
                    bool             isWindowsText = oldText.IndexOf("\r\n") >= 0;                                                         //windows换行

                    if (isWindowsText)
                    {
                        if (translator.WordMatch)//全字
                        {
                            newText = newText.ReplaceWord(translator.Find.Replace("\r\n", "\n").Replace("\n", "\r\n"), translator.Replace.Replace("\r\n", "\n").Replace("\n", "\r\n"), translator.BanPrefix, translator.BanSuffix, comparison);
                        }
                        else
                        {
                            newText = newText.Replace(translator.Find.Replace("\r\n", "\n").Replace("\n", "\r\n"), translator.Replace.Replace("\r\n", "\n").Replace("\n", "\r\n"), translator.BanPrefix, translator.BanSuffix, comparison);
                        }
                    }
                    else
                    {
                        if (translator.WordMatch)//全字
                        {
                            newText = newText.ReplaceWord(translator.Find.Replace("\r\n", "\n"), translator.Replace.Replace("\r\n", "\n"), translator.BanPrefix, translator.BanSuffix, comparison);
                        }
                        else
                        {
                            newText = newText.Replace(translator.Find.Replace("\r\n", "\n"), translator.Replace.Replace("\r\n", "\n"), translator.BanPrefix, translator.BanSuffix, comparison);
                        }
                    }

                    if (newText != oldText)
                    {
                        File.WriteAllText(fullName, newText, UTF8_NO_BOM);
                        lock (affects)
                            affects.Add(fullName);
                        dialog.WriteLog("{0} 替换 {1}", translator.Find, fullName);
                    }
                };

                //全部完成,统计
                dialog.AllWorkComplete += delegate(object sender, EventArgs e)
                {
                    count = affects.Count;
                    affects.OrderBy(fullName => fullName).ForEach(fullName => result.AppendLine(fullName));
                    GitHelper.GitCommit(config, comment);
                };

                foreach (string fullname in includefiles.Except(exceludefiles))
                {
                    dialog.Enqueue(fullname);
                }

                dialog.ShowDialog(owner);
            }
            result.Insert(0, string.Format("{0} 替换以下文件,共 {1} 个\r\n", translator.Find, count));
            return(result);
        }