diff --git a/ProjectLocomotive/ProjectLocomotive/MapsCollection.cs b/ProjectLocomotive/ProjectLocomotive/MapsCollection.cs index 92b798c..24a2457 100644 --- a/ProjectLocomotive/ProjectLocomotive/MapsCollection.cs +++ b/ProjectLocomotive/ProjectLocomotive/MapsCollection.cs @@ -10,7 +10,7 @@ namespace Locomotive internal class MapsCollection { /// Словарь (хранилище) с картами - readonly Dictionary> _mapStorages; /// Возвращение списка названий карт public List Keys => _mapStorages.Keys.ToList(); @@ -18,11 +18,14 @@ namespace Locomotive private readonly int _pictureWidth; /// Высота окна отрисовки private readonly int _pictureHeight; + // Сепараторы + private readonly char separatorDict = '|'; + private readonly char separatorData = ';'; /// Конструктор public MapsCollection(int pictureWidth, int pictureHeight) { _mapStorages = new Dictionary>(); + MapWithSetLocomotivesGeneric>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -30,7 +33,7 @@ namespace Locomotive public void AddMap(string name, AbstractMap map) { // Логика для добавления - if (!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + if (!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); } /// Удаление карты public void DelMap(string name) @@ -39,7 +42,7 @@ namespace Locomotive if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); } /// Доступ к парковке - public MapWithSetLocomotivesGeneric this[string ind] + public MapWithSetLocomotivesGeneric this[string ind] { get { @@ -48,5 +51,75 @@ namespace Locomotive return null; } } + /// Метод записи информации в файл + private static void WriteToFile(string text, FileStream stream) + { + byte[] info = new UTF8Encoding(true).GetBytes(text); + stream.Write(info, 0, info.Length); + } + /// Сохранение информации по локомотивам в хранилище в файл + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (FileStream fs = new(filename, FileMode.Create)) + { + WriteToFile($"MapsCollection{Environment.NewLine}", fs); + foreach (var storage in _mapStorages) + { + + WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); + } + } + return true; + } + /// Загрузка информации по локомотивам на парковках из файла + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + if (!strs[0].Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + //очищаем записи + _mapStorages.Clear(); + for (int i = 1; i < strs.Length; ++i) + { + var elem = strs[i].Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "Simple Map": + map = new SimpleMap(); + break; + case "Spike Map": + map = new SpikeMap(); + break; + case "Rail Map": + map = new RailroadMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } } } \ No newline at end of file