A fork of Eusth's IPA
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

131 строка
3.8KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace IPA.Patcher
  5. {
  6. /// <summary>
  7. /// A unit for backup. WIP.
  8. /// </summary>
  9. public class BackupUnit
  10. {
  11. public string Name { get; private set; }
  12. private DirectoryInfo _BackupPath;
  13. private PatchContext _Context;
  14. private List<string> _Files = new List<string>();
  15. public BackupUnit(PatchContext context) : this(context, DateTime.Now.ToString("yyyy-MM-dd_h-mm-ss"))
  16. {
  17. }
  18. private BackupUnit(PatchContext context, string name)
  19. {
  20. Name = name;
  21. _Context = context;
  22. _BackupPath = new DirectoryInfo(Path.Combine(_Context.BackupPath, Name));
  23. }
  24. public static BackupUnit FromDirectory(DirectoryInfo directory, PatchContext context)
  25. {
  26. var unit = new BackupUnit(context, directory.Name);
  27. // Parse directory
  28. foreach (var file in directory.GetFiles("*", SearchOption.AllDirectories))
  29. {
  30. var relativePath = file.FullName.Substring(directory.FullName.Length + 1);
  31. unit._Files.Add(relativePath);
  32. }
  33. return unit;
  34. }
  35. public void Add(string file)
  36. {
  37. Add(new FileInfo(file));
  38. }
  39. internal void Delete()
  40. {
  41. _BackupPath.Delete(true);
  42. }
  43. /// <summary>
  44. /// Adds a file to the list of changed files and backups it.
  45. /// </summary>
  46. /// <param name="path"></param>
  47. public void Add(FileInfo file)
  48. {
  49. if (!file.FullName.StartsWith(_Context.ProjectRoot))
  50. {
  51. Console.Error.WriteLine("Invalid file path for backup! {0}", file);
  52. return;
  53. }
  54. var relativePath = file.FullName.Substring(_Context.ProjectRoot.Length + 1);
  55. var backupPath = new FileInfo(Path.Combine(_BackupPath.FullName, relativePath));
  56. if (_Files.Contains(relativePath))
  57. {
  58. Console.WriteLine("Skipping backup of {0}", relativePath);
  59. return;
  60. }
  61. // Copy over
  62. backupPath.Directory.Create();
  63. if (file.Exists)
  64. {
  65. file.CopyTo(backupPath.FullName);
  66. }
  67. else
  68. {
  69. // Make empty file
  70. backupPath.Create().Close();
  71. }
  72. // Add to list
  73. _Files.Add(relativePath);
  74. }
  75. /// <summary>
  76. /// Reverts the changes made in this unit.
  77. /// </summary>
  78. public void Restore()
  79. {
  80. foreach (var relativePath in _Files)
  81. {
  82. Console.WriteLine("Restoring {0}", relativePath);
  83. // Original version
  84. var backupFile = new FileInfo(Path.Combine(_BackupPath.FullName, relativePath));
  85. var target = new FileInfo(Path.Combine(_Context.ProjectRoot, relativePath));
  86. if (backupFile.Exists)
  87. {
  88. if (backupFile.Length > 0)
  89. {
  90. Console.WriteLine(" {0} => {1}", backupFile.FullName, target.FullName);
  91. target.Directory.Create();
  92. backupFile.CopyTo(target.FullName, true);
  93. }
  94. else
  95. {
  96. Console.WriteLine(" x {0}", target.FullName);
  97. if (target.Exists)
  98. {
  99. target.Delete();
  100. }
  101. }
  102. }
  103. else
  104. {
  105. Console.Error.WriteLine("Backup not found!");
  106. }
  107. }
  108. }
  109. }
  110. }