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.

69 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. return new DirectoryInfo(context.BackupPath)
  14. .GetDirectories()
  15. .OrderByDescending(p => p.Name)
  16. .Select(p => BackupUnit.FromDirectory(p, context))
  17. .FirstOrDefault();
  18. }
  19. public static bool HasBackup(PatchContext context)
  20. {
  21. return FindLatestBackup(context) != null;
  22. }
  23. public static bool Restore(PatchContext context)
  24. {
  25. var backup = FindLatestBackup(context);
  26. if(backup != null)
  27. {
  28. backup.Restore();
  29. backup.Delete();
  30. DeleteEmptyDirs(context.ProjectRoot);
  31. return true;
  32. }
  33. return false;
  34. }
  35. public static void DeleteEmptyDirs(string dir)
  36. {
  37. if (string.IsNullOrEmpty(dir))
  38. throw new ArgumentException(
  39. "Starting directory is a null reference or an empty string",
  40. "dir");
  41. try
  42. {
  43. foreach (var d in Directory.EnumerateDirectories(dir))
  44. {
  45. DeleteEmptyDirs(d);
  46. }
  47. var entries = Directory.EnumerateFileSystemEntries(dir);
  48. if (!entries.Any())
  49. {
  50. try
  51. {
  52. Directory.Delete(dir);
  53. }
  54. catch (UnauthorizedAccessException) { }
  55. catch (DirectoryNotFoundException) { }
  56. }
  57. }
  58. catch (UnauthorizedAccessException) { }
  59. }
  60. }
  61. }