diff --git a/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs b/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs index bccdbd7..a3c69d7 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs @@ -94,6 +94,7 @@ public class StorageCollection /// /// Путь и имя файла /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) { if (_storages.Count == 0) @@ -112,12 +113,14 @@ public class StorageCollection foreach (KeyValuePair> value in _storages) { StringBuilder sb = new(); + sb.Append(Environment.NewLine); // не сохраняем пустые коллекции if (value.Value.Count == 0) { continue; } + sb.Append(value.Key); sb.Append(_separatorForKeyValue); sb.Append(value.Value.GetCollectionType); @@ -147,6 +150,8 @@ public class StorageCollection /// /// Путь и имя файла /// true - загрузка прошла успешно, false - ошибка при загрузке данных + + // public bool LoadData(string filename) { if (!File.Exists(filename)) @@ -154,38 +159,44 @@ public class StorageCollection return false; } using (StreamReader fs = File.OpenText(filename)) - { + { string str = fs.ReadLine(); + if (str == null || str.Length == 0) { return false; } + if (!str.StartsWith(_collectionKey)) { return false; } + _storages.Clear(); + string strs = ""; + while ((strs = fs.ReadLine()) != null) { - //по идее этого произойти не должно - //if (strs == null) - //{ - // return false; - //} string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) { continue; } + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + if (collection == null) { return false; } + collection.MaxCount = Convert.ToInt32(record[2]); + string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) { if (elem?.CreateDrawningMilitaryAircraft() is T militaryAircraft) @@ -207,6 +218,7 @@ public class StorageCollection /// /// /// + /// private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType) { return collectionType switch