From 2507c118935498119e1afa526fe76c62135d29e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=B3=20=D0=9A=D1=83=D0=B4=D1=80=D0=B8?= =?UTF-8?q?=D0=BD=D1=81=D0=BA=D0=B8=D0=B9?= Date: Mon, 6 May 2024 20:34:10 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=3D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 72 +++++++++---------- .../ProjectContainerShip/FormShipConfig.cs | 4 +- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/StorageCollection.cs b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/StorageCollection.cs index f8f4ba6..627b581 100644 --- a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/StorageCollection.cs @@ -145,58 +145,56 @@ where T : DrawningShip /// Загрузка информации по кораблям в хранилище из файла /// /// Путь и имя файла - public void LoadData(string filename) + public bool LoadData(string filename) { - if (!File.Exists(filename)) throw new Exception("Файл не существует"); - - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + if (!File.Exists(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) bufferTextFromFile += temp.GetString(b); + return false; } - - string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - - if (strs == null || strs.Length == 0) throw new Exception("В файле нет данных"); - - if (!strs[0].Equals(_collectionKey)) throw new Exception("В файле неверные данные"); - - _storages.Clear(); - foreach (string data in strs) + using (StreamReader reader = File.OpenText(filename)) { - 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) throw new Exception("Не удалось создать коллекцию"); - - collection.MaxCount = Convert.ToInt32(record[2]); - - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + string str = reader.ReadLine(); + if (str == null || str.Length == 0) { - if (elem?.CreateDrawningShip() is T ship) + return false; + } + if (!str.StartsWith(_collectionKey)) + { + return false; + } + _storages.Clear(); + string strs = ""; + while ((strs = reader.ReadLine()) != null) + { + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) { - try + 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?.CreateDrawningShip() is T ship) { if (collection.Insert(ship) == -1) { - throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]); + return false; } } - catch (CollectionOverflowException ex) - { - throw new Exception("Коллекция переполнена", ex); - } } + _storages.Add(record[0], collection); } - _storages.Add(record[0], collection); + return true; } } + /// /// Создание коллекции по типу /// diff --git a/ProjectContainerShip/ProjectContainerShip/FormShipConfig.cs b/ProjectContainerShip/ProjectContainerShip/FormShipConfig.cs index 340e51a..f11a4dc 100644 --- a/ProjectContainerShip/ProjectContainerShip/FormShipConfig.cs +++ b/ProjectContainerShip/ProjectContainerShip/FormShipConfig.cs @@ -26,7 +26,7 @@ public partial class FormShipConfig : Form /// /// Событие для передачи объекта /// - private event ShipDelegate? ShipDelegate; + private event Action? ShipDelegate; /// /// Конструктор @@ -51,7 +51,7 @@ public partial class FormShipConfig : Form /// Привязка внешнего метода к событию /// /// - public void AddEvent(ShipDelegate shipDelegate) + public void AddEvent(Action shipDelegate) { ShipDelegate += shipDelegate; }