diff --git a/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs b/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs index 9fc86d9..bccdbd7 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs @@ -106,41 +106,39 @@ public class StorageCollection File.Delete(filename); } - StringBuilder sb = new(); - - sb.Append(_collectionKey); - foreach (KeyValuePair> value in _storages) + using (StreamWriter writer = new StreamWriter(filename)) { - sb.Append(Environment.NewLine); - // не сохраняем пустые коллекции - if (value.Value.Count == 0) + writer.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) { - continue; - } - - sb.Append(value.Key); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.GetCollectionType); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.MaxCount); - sb.Append(_separatorForKeyValue); - - foreach (T? item in value.Value.GetItems()) - { - string data = item?.GetDataForSave() ?? string.Empty; - if (string.IsNullOrEmpty(data)) + StringBuilder sb = new(); + sb.Append(Environment.NewLine); + // не сохраняем пустые коллекции + if (value.Value.Count == 0) { continue; } - - sb.Append(data); - sb.Append(_separatorItems); + sb.Append(value.Key); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.GetCollectionType); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.MaxCount); + sb.Append(_separatorForKeyValue); + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) + { + continue; + } + sb.Append(data); + sb.Append(_separatorItems); + } + writer.Write(sb); } + } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); - fs.Write(info, 0, info.Length); return true; } @@ -155,64 +153,53 @@ public class StorageCollection { return false; } - - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader fs = File.OpenText(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) - { - bufferTextFromFile += temp.GetString(b); - } - } - - string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - return false; - } - - if (!strs[0].Equals(_collectionKey)) - { - //если нет такой записи, то это не те данные - return false; - } - - _storages.Clear(); - foreach (string data in strs) - { - string[] record = data.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) + string str = fs.ReadLine(); + if (str == null || str.Length == 0) { return false; } - - collection.MaxCount = Convert.ToInt32(record[2]); - - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + if (!str.StartsWith(_collectionKey)) { - if (elem?.CreateDrawningMilitaryAircraft() is T militaryAircraft) + 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) { - if (!collection.Insert(militaryAircraft)) + 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) { - return false; + if (collection.Insert(militaryAircraft) == -1) + { + return false; + } } } + _storages.Add(record[0], collection); } - - _storages.Add(record[0], collection); + return true; } - - return true; } /// diff --git a/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs b/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs index 8a16875..760c7b2 100644 --- a/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs +++ b/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs @@ -259,6 +259,7 @@ public partial class FormMilitaryAircraftCollection : Form if (_storageCollection.LoadData(openFileDialog.FileName)) { MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + RerfreshListBoxItems(); } else { diff --git a/AirFighter/AirFighter/FormMilitaryAircraftConfig.cs b/AirFighter/AirFighter/FormMilitaryAircraftConfig.cs index 9653392..4e319a6 100644 --- a/AirFighter/AirFighter/FormMilitaryAircraftConfig.cs +++ b/AirFighter/AirFighter/FormMilitaryAircraftConfig.cs @@ -41,7 +41,14 @@ public partial class FormMilitaryAircraftConfig : Form /// public void AddEvent(Action militaryAircraftDelegate) { - MilitaryAircraftDelegate += militaryAircraftDelegate; + if (MilitaryAircraftDelegate != null) + { + MilitaryAircraftDelegate = militaryAircraftDelegate; + } + else + { + MilitaryAircraftDelegate += militaryAircraftDelegate; + } } ///