Archive.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.IO;
  2. #pragma warning disable CS0649
  3. namespace OpenVIII
  4. {
  5. public static partial class Memory
  6. {
  7. /// <summary>
  8. /// Archive class handles the filename formatting and extensions for archive files.
  9. /// </summary>
  10. public class Archive
  11. {
  12. public Archive Parent;
  13. public string _Root { get; set; }
  14. public string _Filename { get; private set; }
  15. public Archive(Archive parent, string path)
  16. {
  17. Parent = parent;
  18. _Root = "";
  19. if (Path.HasExtension(path))
  20. {
  21. string ext = Path.GetExtension(path);
  22. if (ext == B_FileArchive || ext == B_FileIndex || ext == B_FileList)
  23. {
  24. int index = path.LastIndexOf('.');
  25. path = index == -1 ? path : path.Substring(0, index);
  26. }
  27. }
  28. _Filename = path;
  29. }
  30. public Archive(string path) : this(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path))
  31. { }
  32. public static implicit operator string(Archive val) => Extended.GetUnixFullPath($"{Path.Combine(val._Root, val._Filename)}");
  33. public static implicit operator Archive(string path) => new Archive(path);
  34. public Archive(string root, string filename)
  35. {
  36. _Root = root;
  37. _Filename = filename;
  38. }
  39. /// <summary>
  40. /// File Archive Extension
  41. /// </summary>
  42. public const string B_FileList = ".fl";
  43. /// <summary>
  44. /// File Index Extension
  45. /// </summary>
  46. public const string B_FileIndex = ".fi";
  47. /// <summary>
  48. /// File Archive Extension
  49. /// </summary>
  50. public const string B_FileArchive = ".fs";
  51. public const string B_ZZZ = ".zzz";
  52. /// <summary>
  53. /// File Index
  54. /// </summary>
  55. public string FI => Test(Extended.GetUnixFullPath($"{Path.Combine(_Root, _Filename)}{B_FileIndex}"));
  56. /// <summary>
  57. /// File List
  58. /// </summary>
  59. public string FL => Test(Extended.GetUnixFullPath($"{Path.Combine(_Root, _Filename)}{B_FileList}"));
  60. /// <summary>
  61. /// File Archive
  62. /// </summary>
  63. public string FS => Test(Extended.GetUnixFullPath($"{Path.Combine(_Root, _Filename)}{B_FileArchive}"));
  64. /// <summary>
  65. /// ZZZ File
  66. /// </summary>
  67. public string ZZZ => Test(Extended.GetUnixFullPath($"{Path.Combine(_Root, _Filename)}{B_ZZZ}"));
  68. public bool FileExistsInFolder { get; private set; }
  69. /// <summary>
  70. /// Test if input file path exists
  71. /// </summary>
  72. /// <param name="input">file path</param>
  73. /// <returns></returns>
  74. private string Test(string input)
  75. {
  76. //using this for archives in archives would always throw exceptions
  77. if (!File.Exists(input)) FileExistsInFolder = false; //throw new FileNotFoundException($"There is no {input} file!\nExiting...");
  78. else FileExistsInFolder = true;
  79. return input;
  80. }
  81. public override string ToString() => this;
  82. }
  83. }
  84. }