Archive.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. // ReSharper disable InconsistentNaming
  7. namespace OpenVIII
  8. {
  9. public static partial class Memory
  10. {
  11. #region Classes
  12. /// <summary>
  13. /// Archive class handles the filename formatting and extensions for archive files.
  14. /// </summary>
  15. public class Archive : IReadOnlyList<string>
  16. {
  17. #region Fields
  18. /// <summary>
  19. /// File Archive Extension
  20. /// </summary>
  21. public const string B_FileArchive = ".fs";
  22. /// <summary>
  23. /// File Index Extension
  24. /// </summary>
  25. public const string B_FileIndex = ".fi";
  26. /// <summary>
  27. /// File Archive Extension
  28. /// </summary>
  29. public const string B_FileList = ".fl";
  30. public const string B_ZZZ = ".zzz";
  31. private readonly string[] _ext = { B_FileList, B_FileIndex, B_FileArchive, B_ZZZ };
  32. private string _filename;
  33. #endregion Fields
  34. #region Constructors
  35. public Archive(string path, params Archive[] parent) : this(path, false) => Parent = parent.ToList();
  36. public Archive(string path, bool keepExtension, params Archive[] parent) : this(path, keepExtension) => Parent = parent.ToList();
  37. public Archive(string path) : this(path, false)
  38. { }
  39. public Archive(string path, bool keepExtension)
  40. {
  41. Parent = null;
  42. SetFilename(path, keepExtension);
  43. }
  44. #endregion Constructors
  45. #region Properties
  46. public int Count => ((IReadOnlyList<string>)_ext).Count;
  47. /// <summary>
  48. /// File Index
  49. /// </summary>
  50. public string FI => $"{NoExtension}{B_FileIndex}";
  51. /// <summary>
  52. /// File List
  53. /// </summary>
  54. public string FL => $"{NoExtension}{B_FileList}";
  55. /// <summary>
  56. /// File Archive
  57. /// </summary>
  58. public string FS => $"{NoExtension}{B_FileArchive}";
  59. public bool IsDir { get; set; }
  60. public bool IsFile { get; set; }
  61. public bool IsFileArchive => _filename.EndsWith(B_FileArchive, StringComparison.OrdinalIgnoreCase);
  62. public bool IsFileIndex => _filename.EndsWith(B_FileIndex, StringComparison.OrdinalIgnoreCase);
  63. public bool IsFileList => _filename.EndsWith(B_FileList, StringComparison.OrdinalIgnoreCase);
  64. public bool IsZZZ => _filename.EndsWith(B_ZZZ, StringComparison.OrdinalIgnoreCase);
  65. public List<Archive> Parent { get; set; }
  66. /// <summary>
  67. /// ZZZ File
  68. /// </summary>
  69. public string ZZZ => $"{NoExtension}{B_ZZZ}";
  70. private string NoExtension => !string.IsNullOrWhiteSpace(_filename) ? Path.Combine(Path.GetDirectoryName(_filename) ?? throw new InvalidOperationException(), Path.GetFileNameWithoutExtension(_filename)) : "";
  71. #endregion Properties
  72. #region Indexers
  73. public string this[int index] => ((IReadOnlyList<string>)_ext)[index];
  74. #endregion Indexers
  75. #region Methods
  76. public static implicit operator Archive(string path) => new Archive(path);
  77. public static implicit operator string(Archive val) => val._filename;
  78. public IEnumerator<string> GetEnumerator() => ((IReadOnlyList<string>)_ext).GetEnumerator();
  79. IEnumerator IEnumerable.GetEnumerator() => ((IReadOnlyList<string>)_ext).GetEnumerator();
  80. /// <summary>
  81. /// SetFileName can be used to update path.
  82. /// </summary>
  83. /// <param name="path">Path to search parent for.</param>
  84. /// <param name="keepExtension">Extensions for FI FL FS are all different. Where ZZZ would be just one extension</param>
  85. public void SetFilename(string path, bool keepExtension = false)
  86. {
  87. if (Directory.Exists(path))
  88. {
  89. IsDir = true;
  90. _filename = path;
  91. }
  92. else if (File.Exists(path))
  93. {
  94. IsFile = true;
  95. _filename = path;
  96. }
  97. else if (!keepExtension)
  98. _filename = Path.GetFileNameWithoutExtension(path);
  99. else
  100. _filename = Path.GetFileName(path);
  101. }
  102. public override string ToString() => this;
  103. #endregion Methods
  104. }
  105. #endregion Classes
  106. }
  107. }