From 923224c02e68da26174250bd59da65585dff717c Mon Sep 17 00:00:00 2001 From: Ctepa Date: Mon, 22 Apr 2024 13:00:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=206=20=D0=BB=D0=B0=D0=B1=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 80 ++++++++++--------- .../Drawnings/ExtentionDrawningGun.cs | 2 +- .../AntiAircraftGun/Entities/EntityGun.cs | 4 +- 3 files changed, 47 insertions(+), 39 deletions(-) diff --git a/AntiAircraftGun/AntiAircraftGun/CollectionGenericObjects/StorageCollection.cs b/AntiAircraftGun/AntiAircraftGun/CollectionGenericObjects/StorageCollection.cs index 702f47d..1097ec3 100644 --- a/AntiAircraftGun/AntiAircraftGun/CollectionGenericObjects/StorageCollection.cs +++ b/AntiAircraftGun/AntiAircraftGun/CollectionGenericObjects/StorageCollection.cs @@ -127,46 +127,52 @@ where T : DrawningGun /// public bool LoadData(string filename) { - if(!File.Exists(filename)) return false; - - 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); - } - - 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) return false; - - collection.MaxCount = Convert.ToInt32(record[2]); - - string[] set = record[3].Split(_separatorItems,StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) - { - if(elem?.CreateDrawningCar() is T gun) - { - if(!collection.Insert(gun)) return false; - } - } - _storages.Add(record[0], collection); } - return true; + using (StreamReader reader = File.OpenText(filename)) + { + string str = reader.ReadLine(); + if (str == null || str.Length == 0) + { + 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) + { + 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?.CreateDrawningCun() is T gun) + { + if (!collection.Insert(gun)) + { + return false; + } + } + } + _storages.Add(record[0], collection); + } + return true; + } } diff --git a/AntiAircraftGun/AntiAircraftGun/Drawnings/ExtentionDrawningGun.cs b/AntiAircraftGun/AntiAircraftGun/Drawnings/ExtentionDrawningGun.cs index 55d86f2..d7f685f 100644 --- a/AntiAircraftGun/AntiAircraftGun/Drawnings/ExtentionDrawningGun.cs +++ b/AntiAircraftGun/AntiAircraftGun/Drawnings/ExtentionDrawningGun.cs @@ -15,7 +15,7 @@ public static class ExtentionDrawningGun /// /// Строка с данными для создания объекта /// Объект - public static DrawningGun? CreateDrawningCar(this string info) + public static DrawningGun? CreateDrawningCun(this string info) { string[] strs = info.Split(_separatorForObject); EntityGun? gun = EntityAntiAircraftGun.CreateEntityAntiaircraftGun(strs); diff --git a/AntiAircraftGun/AntiAircraftGun/Entities/EntityGun.cs b/AntiAircraftGun/AntiAircraftGun/Entities/EntityGun.cs index 5c38a32..1fe6c8a 100644 --- a/AntiAircraftGun/AntiAircraftGun/Entities/EntityGun.cs +++ b/AntiAircraftGun/AntiAircraftGun/Entities/EntityGun.cs @@ -1,4 +1,6 @@ -namespace AntiAircraftGun.Entities; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace AntiAircraftGun.Entities; /// /// Класс-сущности "Орудие" ///