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; } } }