MegafileManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. namespace MobiusEditor.Utility
  19. {
  20. public class MegafileManager : IEnumerable<string>, IEnumerable, IDisposable
  21. {
  22. private readonly string looseFilePath;
  23. private readonly List<Megafile> megafiles = new List<Megafile>();
  24. private readonly HashSet<string> filenames = new HashSet<string>();
  25. public MegafileManager(string looseFilePath)
  26. {
  27. this.looseFilePath = looseFilePath;
  28. }
  29. public bool Load(string megafilePath)
  30. {
  31. if (!File.Exists(megafilePath))
  32. {
  33. return false;
  34. }
  35. var megafile = new Megafile(megafilePath);
  36. filenames.UnionWith(megafile);
  37. megafiles.Add(megafile);
  38. return true;
  39. }
  40. public bool Exists(string path)
  41. {
  42. return File.Exists(Path.Combine(looseFilePath, path)) || filenames.Contains(path.ToUpper());
  43. }
  44. public Stream Open(string path)
  45. {
  46. string loosePath = Path.Combine(looseFilePath, path);
  47. if (File.Exists(loosePath))
  48. {
  49. return File.Open(loosePath, FileMode.Open, FileAccess.Read);
  50. }
  51. foreach (var megafile in megafiles)
  52. {
  53. var stream = megafile.Open(path.ToUpper());
  54. if (stream != null)
  55. {
  56. return stream;
  57. }
  58. }
  59. return null;
  60. }
  61. public IEnumerator<string> GetEnumerator()
  62. {
  63. return filenames.GetEnumerator();
  64. }
  65. IEnumerator IEnumerable.GetEnumerator()
  66. {
  67. return GetEnumerator();
  68. }
  69. #region IDisposable Support
  70. private bool disposedValue = false;
  71. protected virtual void Dispose(bool disposing)
  72. {
  73. if (!disposedValue)
  74. {
  75. if (disposing)
  76. {
  77. megafiles.ForEach(m => m.Dispose());
  78. }
  79. disposedValue = true;
  80. }
  81. }
  82. public void Dispose()
  83. {
  84. Dispose(true);
  85. }
  86. #endregion
  87. }
  88. }