CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

MegafileManager.cs (3123B)


      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 
      9 // The Command & Conquer Map Editor and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 using System;
     16 using System.Collections;
     17 using System.Collections.Generic;
     18 using System.IO;
     19 
     20 namespace MobiusEditor.Utility
     21 {
     22     public class MegafileManager : IEnumerable<string>, IEnumerable, IDisposable
     23     {
     24         private readonly string looseFilePath;
     25 
     26         private readonly List<Megafile> megafiles = new List<Megafile>();
     27 
     28         private readonly HashSet<string> filenames = new HashSet<string>();
     29 
     30         public MegafileManager(string looseFilePath)
     31         {
     32             this.looseFilePath = looseFilePath;
     33         }
     34 
     35         public bool Load(string megafilePath)
     36         {
     37             if (!File.Exists(megafilePath))
     38             {
     39                 return false;
     40             }
     41 
     42             var megafile = new Megafile(megafilePath);
     43             filenames.UnionWith(megafile);
     44             megafiles.Add(megafile);
     45             return true;
     46         }
     47 
     48         public bool Exists(string path)
     49         {
     50             return File.Exists(Path.Combine(looseFilePath, path)) || filenames.Contains(path.ToUpper());
     51         }
     52 
     53         public Stream Open(string path)
     54         {
     55             string loosePath = Path.Combine(looseFilePath, path);
     56             if (File.Exists(loosePath))
     57             {
     58                 return File.Open(loosePath, FileMode.Open, FileAccess.Read);
     59             }
     60 
     61             foreach (var megafile in megafiles)
     62             {
     63                 var stream = megafile.Open(path.ToUpper());
     64                 if (stream != null)
     65                 {
     66                     return stream;
     67                 }
     68             }
     69 
     70             return null;
     71         }
     72 
     73         public IEnumerator<string> GetEnumerator()
     74         {
     75             return filenames.GetEnumerator();
     76         }
     77 
     78         IEnumerator IEnumerable.GetEnumerator()
     79         {
     80             return GetEnumerator();
     81         }
     82 
     83         #region IDisposable Support
     84         private bool disposedValue = false;
     85 
     86         protected virtual void Dispose(bool disposing)
     87         {
     88             if (!disposedValue)
     89             {
     90                 if (disposing)
     91                 {
     92                     megafiles.ForEach(m => m.Dispose());
     93                 }
     94                 disposedValue = true;
     95             }
     96         }
     97 
     98         public void Dispose()
     99         {
    100             Dispose(true);
    101         }
    102         #endregion
    103     }
    104 }