From dcd7bf33e208ee56b4058ec40f28627781fe775f Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Sun, 6 Nov 2022 20:43:35 +0400 Subject: [PATCH] =?UTF-8?q?StreamWriter,=20StreamReader=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B8=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA?= =?UTF-8?q?=D0=B8,=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=BD=D0=B0?= =?UTF-8?q?=D0=B6=D0=B0=D1=82=D0=B8=D1=8F=20=D0=BD=D0=B0=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D1=83=20=D0=B2=20=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExtentionAntiAircraftGun.cs | 2 +- .../FormMapWithSetAntiAircraftGuns.cs | 13 +++- .../AntiAircraftGun/MapsCollection.cs | 69 +++++++------------ 3 files changed, 37 insertions(+), 47 deletions(-) diff --git a/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs index bd42abe..9e21588 100644 --- a/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs +++ b/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AntiAircraftGun { /// - /// Расширение для класса DrawningCar + /// Расширение для класса DrawingAntiAircraftGun /// internal static class ExtentionAntiAircraftGun { diff --git a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs index 93e4bba..f8af888 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs @@ -243,7 +243,18 @@ namespace AntiAircraftGun /// private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { - // TODO продумать логику + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } } diff --git a/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs b/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs index 2ef493f..5c520f3 100644 --- a/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs +++ b/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs @@ -74,16 +74,6 @@ namespace AntiAircraftGun } } /// - /// Метод записи информации в файл - /// - /// Строка, которую следует записать - /// Поток для записи - private static void WriteToFile(string text, FileStream stream) - { - byte[] info = new UTF8Encoding(true).GetBytes(text); - stream.Write(info, 0, info.Length); - } - /// /// Сохранение информации по орудиям в хранилище в файл /// /// Путь и имя файла @@ -94,12 +84,12 @@ namespace AntiAircraftGun { File.Delete(filename); } - using (FileStream fs = new(filename, FileMode.Create)) + using (StreamWriter sw = new StreamWriter(filename)) { - WriteToFile($"MapsCollection{Environment.NewLine}", fs); + sw.WriteLine($"MapsCollection"); foreach (var storage in _mapStorages) { - WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); + sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}"); } } return true; @@ -115,44 +105,33 @@ namespace AntiAircraftGun { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new StreamReader(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string str; + _mapStorages.Clear(); + str = sr.ReadLine(); + if (!str.Contains("MapsCollection")) { - bufferTextFromFile += temp.GetString(b); + return false; } - } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (!strs[0].Contains("MapsCollection")) - { - //если нет такой записи, то это не те данные - return false; - } - //очищаем записи - _mapStorages.Clear(); - for (int i = 1; i < strs.Length; ++i) - { - var elem = strs[i].Split(separatorDict); - AbstractMap map = null; - switch (elem[1]) + while ((str = sr.ReadLine()) != null) { - case "SimpleMap": - map = new SimpleMap(); - break; + var elem = str.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MySecondMap": + map = new SimpleMap2(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetAntiAircraftGunsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } - switch (elem[2]) - { - case "SimpleMap2": - map = new SimpleMap2(); - break; - } - _mapStorages.Add(elem[0], new MapWithSetAntiAircraftGunsGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + return true; } - return true; } } }