From 849deb0066a40732e8264dc4fec5d37ad63882f7 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Sat, 18 Nov 2023 13:49:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20=D1=8D?= =?UTF-8?q?=D1=82=D0=B0=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SPAUGenericCollection.cs | 4 + .../SPAUGenericStorage.cs | 107 +++++++++++++++++- 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs index 0a4a07f..749640c 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs @@ -51,6 +51,10 @@ namespace SelfPropelledArtilleryUnit.Generics _collection = new SetGeneric(width * height); } /// + /// Получение объектов коллекции + /// + public IEnumerable GetCars => _collection.GetSPAUs(); + /// /// Перегрузка оператора сложения /// /// diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs index fb2db48..24fc8d0 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs @@ -79,6 +79,111 @@ namespace SelfPropelledArtilleryUnit.Generics } } } - } + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename){ + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _SPAUStorages) + { + StringBuilder records = new(); + foreach (DrawningSPAU? elem in record.Value.GetCars) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new + UTF8Encoding(true).GetBytes($"SpAUStorage{Environment.NewLine}{data}"); + fs.Write(info, 0, info.Length); + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + 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 == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("CarStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + _SPAUStorages.Clear(); + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + SPAUGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningSPAU? sPAU = + elem?.CreateDrawningCar(_separatorForObject, _pictureWidth, _pictureHeight); + if (sPAU != null) + { + if ((collection + sPAU) == -1) + { + return false; + } + } + } + _SPAUStorages.Add(record[0], collection); + } + return true; + } + + } }