コード例 #1
0
ファイル: Application.cs プロジェクト: Kokoro87/mangarack.cs
 /// <summary>
 /// Run in single processing mode for the location.
 /// </summary>
 /// <param name="location">The location.</param>
 /// <param name="options">The collection of options.</param>
 public static void Single(string location, Options options) {
     var provider = Providers.FirstOrDefault(x => x.Open(location) != null);
     if (provider == null) return;
     using (var series = provider.Open(location)) {
         using (series.Populate()) {
             var seriesTitle = series.Title.InvalidatePath();
             var persistencePath = Path.Combine(seriesTitle, ".mangarack-persist");
             var persistence = new List<List<string>>();
             if (File.Exists(persistencePath)) {
                 const int persistenceVersion = 2;
                 foreach (var pieces in File.ReadAllLines(persistencePath).Select(line => new List<string>(line.Split('\0')))) {
                     while (pieces.Count < persistenceVersion) pieces.Add(string.Empty);
                     persistence.Add(pieces);
                 }
             }
             foreach (var chapter in series.Children) {
                 var line = persistence.FirstOrDefault(x => string.Equals(x[1], chapter.UniqueIdentifier));
                 if (line == null) continue;
                 var currentFilePath = Path.Combine(seriesTitle, line[0]);
                 var nextFileName = chapter.ToFileName(seriesTitle, options);
                 if (!string.Equals(line[0], nextFileName) && File.Exists(currentFilePath)) {
                     File.Move(currentFilePath, Path.Combine(seriesTitle, nextFileName));
                     line[0] = nextFileName;
                     Persist(persistencePath, persistence);
                     Console.WriteLine("Switched {0}", nextFileName);
                 }
             }
             foreach (var chapter in series.Children.Filter(options)) {
                 var hasFailed = false;
                 var fileName = chapter.ToFileName(seriesTitle, options);
                 var filePath = Path.Combine(seriesTitle, fileName);
                 var persistenceFile = persistence.FirstOrDefault(x => string.Equals(x[0], fileName));
                 if (options.EnablePersistentSynchronization && persistenceFile != null) {
                     continue;
                 }
                 if (persistenceFile != null) {
                     persistenceFile[1] = chapter.UniqueIdentifier ?? string.Empty;
                 } else {
                     persistence.Add(new List<string> {fileName, chapter.UniqueIdentifier ?? string.Empty});
                 }
                 do {
                     if (options.DisableDuplicationPrevention || !File.Exists(filePath)) {
                         using (chapter.Populate()) {
                             using (var publisher = new Publisher(filePath, options, provider)) {
                                 using (var synchronizer = new Synchronize(publisher, series, chapter)) {
                                     synchronizer.Populate();
                                     hasFailed = false;
                                 }
                             }
                         }
                     } else {
                         if (options.EnableOverwriteMetaInformation) {
                             var comicInfo = new ComicInfo();
                             using (var zipFile = new ZipFile(filePath)) {
                                 var zipEntry = zipFile.GetEntry("ComicInfo.xml");
                                 if (zipEntry != null) {
                                     var previousComicInfo = ComicInfo.Load(zipFile.GetInputStream(zipEntry));
                                     comicInfo.Transcribe(series, chapter, previousComicInfo.Pages);
                                     if (comicInfo.Genre.Any(x => !previousComicInfo.Genre.Contains(x)) ||
                                         previousComicInfo.Genre.Any(x => !comicInfo.Genre.Contains(x)) ||
                                         comicInfo.Manga != previousComicInfo.Manga ||
                                         comicInfo.Number != previousComicInfo.Number ||
                                         comicInfo.PageCount != previousComicInfo.PageCount ||
                                         comicInfo.Penciller.Any(x => !previousComicInfo.Penciller.Contains(x)) ||
                                         previousComicInfo.Penciller.Any(x => !comicInfo.Penciller.Contains(x)) ||
                                         comicInfo.Series != previousComicInfo.Series ||
                                         comicInfo.Summary != previousComicInfo.Summary ||
                                         comicInfo.Title != previousComicInfo.Title ||
                                         comicInfo.Volume != previousComicInfo.Volume ||
                                         comicInfo.Writer.Any(x => !previousComicInfo.Writer.Contains(x)) ||
                                         previousComicInfo.Writer.Any(x => !comicInfo.Writer.Contains(x))) {
                                         using (var memoryStream = new MemoryStream()) {
                                             comicInfo.Save(memoryStream);
                                             memoryStream.Position = 0;
                                             zipFile.BeginUpdate();
                                             zipFile.Add(new DataSource(memoryStream), "ComicInfo.xml");
                                             zipFile.CommitUpdate();
                                             Console.WriteLine("Modified {0}", fileName);
                                         }
                                     }
                                 }
                             }
                         }
                         if (!options.DisableRepairAndErrorTracking && File.Exists(string.Format("{0}.txt", filePath))) {
                             using (chapter.Populate()) {
                                 ComicInfo comicInfo;
                                 var hasBrokenPages = false;
                                 using (var zipFile = new ZipFile(filePath)) {
                                     var zipEntry = zipFile.GetEntry("ComicInfo.xml");
                                     if (zipEntry == null) {
                                         return;
                                     }
                                     comicInfo = ComicInfo.Load(zipFile.GetInputStream(zipEntry));
                                 }
                                 using (var publisher = new Publisher(filePath, options, provider, true)) {
                                     using (var repair = new Repair(publisher, chapter, comicInfo, File.ReadAllLines(string.Format("{0}.txt", filePath)))) {
                                         repair.Populate();
                                         hasBrokenPages = publisher.HasBrokenPages;
                                         hasFailed = publisher.HasFailed = repair.HasFailed;
                                     }
                                 }
                                 if (!hasBrokenPages && File.Exists(string.Format("{0}.txt", filePath))) {
                                     File.Delete(string.Format("{0}.txt", filePath));
                                 }
                             }
                         }
                     }
                 } while (hasFailed);
                 Persist(persistencePath, persistence);
             }
         }
     }
 }