diff --git a/AirFighter/AirFighter/MapsCollection.cs b/AirFighter/AirFighter/MapsCollection.cs index 48d1f5e..2335690 100644 --- a/AirFighter/AirFighter/MapsCollection.cs +++ b/AirFighter/AirFighter/MapsCollection.cs @@ -44,74 +44,59 @@ namespace AirFighter _mapStorages.Remove(name); } - 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 fs = new(filename)) { - WriteToFile($"MapsCollection{Environment.NewLine}", fs); + fs.Write($"MapsCollection{Environment.NewLine}"); foreach (var storage in _mapStorages) { - WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); + fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); } } return true; } - /// - /// Загрузка нформации по автомобилям на парковках из файла - /// - /// - /// + public bool LoadData(string filename) { if (!File.Exists(filename)) { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader fs = new(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string current = fs.ReadLine(); + if (!current.Contains("MapsCollection")) { - bufferTextFromFile += temp.GetString(b); + //если нет такой записи, то это не те данные + return false; + } + + _mapStorages.Clear(); + + while ((current = fs.ReadLine()) != null) + { + var elem = current.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MyMap": + map = new MyMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetCarsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } } - 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]) - { - case "SimpleMap": - map = new SimpleMap(); - break; - case "MyMap": - map = new MyMap(); - break; - } - _mapStorages.Add(elem[0], new MapWithSetCarsGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); - } + return true; }