From c050a5b51f912b49da6fd3ae5565890095b1b38d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Fri, 2 Dec 2022 17:49:34 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirPlaneWithRadar/FormMapWithSetPlains.cs | 15 ++++- .../AirPlaneWithRadar/MapsCollection.cs | 66 +++++++++---------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/FormMapWithSetPlains.cs b/AirPlaneWithRadar/AirPlaneWithRadar/FormMapWithSetPlains.cs index 3c68316..a2c6cc7 100644 --- a/AirPlaneWithRadar/AirPlaneWithRadar/FormMapWithSetPlains.cs +++ b/AirPlaneWithRadar/AirPlaneWithRadar/FormMapWithSetPlains.cs @@ -190,7 +190,20 @@ namespace AirPlaneWithRadar private void loadToolStrip_Click(object sender, EventArgs e) { - + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.LoadData(openFileDialog.FileName) && _mapsCollection.Count != 0) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); + + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось загрузить", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/MapsCollection.cs b/AirPlaneWithRadar/AirPlaneWithRadar/MapsCollection.cs index 42a7ca4..7c476e0 100644 --- a/AirPlaneWithRadar/AirPlaneWithRadar/MapsCollection.cs +++ b/AirPlaneWithRadar/AirPlaneWithRadar/MapsCollection.cs @@ -20,6 +20,7 @@ namespace AirPlaneWithRadar private readonly char separatorData = ';'; + public int Count { get { return _mapStorages.Count; } } public MapsCollection(int pictureWidth, int pictureHeight) { _mapStorages = new Dictionary>(); @@ -53,68 +54,63 @@ namespace AirPlaneWithRadar return _mapStorages[ind]; } } - private static void WriteToFile(string text, FileStream stream) - { - byte[] info = new UTF8Encoding(true).GetBytes(text); - stream.Write(info, 0, info.Length); - } - public bool SaveData(string filename) { if (File.Exists(filename)) { File.Delete(filename); } - using (FileStream fs = new(filename, FileMode.Create)) + using (StreamWriter sw = new(filename, false)) { - 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; } - + + public bool LoadData(string filename) { if (!File.Exists(filename)) { 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) + if (!sr.ReadLine().Equals("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]) + _mapStorages.Clear(); + string line = sr.ReadLine(); + while (line != null) { - case "SimpleMap": - map = new SimpleMap(); - break; + var elem = line.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "UserMap_BigBox": + map = new UserMap_BigBox(); + break; + case "UserMap_Colums": + map = new UserMap_Colums(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetPlainGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + line = sr.ReadLine(); } - _mapStorages.Add(elem[0], new MapWithSetPlainGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } return true; + } } }