You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace IPA.Patcher
  8. {
  9. public class BackupManager
  10. {
  11. public static BackupUnit FindLatestBackup(PatchContext context)
  12. {
  13. new DirectoryInfo(context.BackupPath).Create();
  14. return new DirectoryInfo(context.BackupPath)
  15. .GetDirectories()
  16. .OrderByDescending(p => p.Name)
  17. .Select(p => BackupUnit.FromDirectory(p, context))
  18. .FirstOrDefault();
  19. }
  20. public static bool HasBackup(PatchContext context)
  21. {
  22. return FindLatestBackup(context) != null;
  23. }
  24. public static bool Restore(PatchContext context)
  25. {
  26. var backup = FindLatestBackup(context);
  27. if(backup != null)
  28. {
  29. backup.Restore();
  30. backup.Delete();
  31. DeleteEmptyDirs(context.ProjectRoot);
  32. return true;
  33. }
  34. return false;
  35. }
  36. public static void DeleteEmptyDirs(string dir)
  37. {
  38. if (string.IsNullOrEmpty(dir))
  39. throw new ArgumentException(
  40. "Starting directory is a null reference or an empty string",
  41. "dir");
  42. try
  43. {
  44. foreach (var d in Directory.EnumerateDirectories(dir))
  45. {
  46. DeleteEmptyDirs(d);
  47. }
  48. var entries = Directory.EnumerateFileSystemEntries(dir);
  49. if (!entries.Any())
  50. {
  51. try
  52. {
  53. Directory.Delete(dir);
  54. }
  55. catch (UnauthorizedAccessException) { }
  56. catch (DirectoryNotFoundException) { }
  57. }
  58. }
  59. catch (UnauthorizedAccessException) { }
  60. }
  61. }
  62. }