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.

106 lines
3.3 KiB

  1. using IPA.Logging;
  2. using IPA.Utilities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. namespace IPA.Injector.Backups
  10. {
  11. /// <summary>
  12. /// A unit for backup. WIP.
  13. /// </summary>
  14. public class BackupUnit
  15. {
  16. public string Name { get; private set; }
  17. private DirectoryInfo _BackupPath;
  18. private HashSet<string> _Files = new HashSet<string>();
  19. private FileInfo _ManifestFile;
  20. private static string _ManifestFileName = "$manifest$.txt";
  21. public BackupUnit(string dir) : this(dir, DateTime.Now.ToString("yyyy-MM-dd_h-mm-ss"))
  22. {
  23. }
  24. private BackupUnit(string dir, string name)
  25. {
  26. Name = name;
  27. _BackupPath = new DirectoryInfo(Path.Combine(dir, Name));
  28. _ManifestFile = new FileInfo(Path.Combine(_BackupPath.FullName, _ManifestFileName));
  29. }
  30. public static BackupUnit FromDirectory(DirectoryInfo directory, string dir)
  31. {
  32. var unit = new BackupUnit(dir, directory.Name);
  33. // Read Manifest
  34. if (unit._ManifestFile.Exists)
  35. {
  36. string manifest = File.ReadAllText(unit._ManifestFile.FullName);
  37. foreach (var line in manifest.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
  38. unit._Files.Add(line);
  39. }
  40. else
  41. {
  42. foreach (var file in directory.GetFiles("*", SearchOption.AllDirectories))
  43. {
  44. if (file.Name == _ManifestFileName) continue;
  45. var relativePath = file.FullName.Substring(directory.FullName.Length + 1);
  46. unit._Files.Add(relativePath);
  47. }
  48. }
  49. return unit;
  50. }
  51. public void Add(string file)
  52. {
  53. Add(new FileInfo(file));
  54. }
  55. internal void Delete()
  56. {
  57. _BackupPath.Delete(true);
  58. }
  59. /// <summary>
  60. /// Adds a file to the list of changed files and backups it.
  61. /// </summary>
  62. /// <param name="path"></param>
  63. public void Add(FileInfo file)
  64. {
  65. var relativePath = LoneFunctions.GetRelativePath(file.FullName, Environment.CurrentDirectory);
  66. var backupPath = new FileInfo(Path.Combine(_BackupPath.FullName, relativePath));
  67. // Copy over
  68. backupPath.Directory.Create();
  69. if (file.Exists)
  70. {
  71. if (File.Exists(backupPath.FullName))
  72. File.Delete(backupPath.FullName);
  73. file.CopyTo(backupPath.FullName);
  74. }
  75. else
  76. {
  77. // Make empty file
  78. backupPath.Create().Close();
  79. }
  80. if (!_Files.Contains(relativePath))
  81. {
  82. if (!File.Exists(_ManifestFile.FullName))
  83. _ManifestFile.Create().Close();
  84. var stream = _ManifestFile.AppendText();
  85. stream.WriteLine(relativePath);
  86. stream.Close();
  87. // Add to list
  88. _Files.Add(relativePath);
  89. }
  90. }
  91. }
  92. }