From e2305e4cdbde8bbfbeecca59a614f75f1d4830e9 Mon Sep 17 00:00:00 2001 From: Semka Date: Mon, 5 Dec 2022 13:09:34 +0400 Subject: [PATCH] FixSaveLoad --- .../GasolineTanker/MapsCollection.cs | 60 ++++++++----------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/GasolineTanker/GasolineTanker/MapsCollection.cs b/GasolineTanker/GasolineTanker/MapsCollection.cs index 883257b..2056157 100644 --- a/GasolineTanker/GasolineTanker/MapsCollection.cs +++ b/GasolineTanker/GasolineTanker/MapsCollection.cs @@ -60,12 +60,12 @@ namespace GasolineTanker { File.Delete(filename); } - using (FileStream fs = new(filename, FileMode.Create)) + using (StreamWriter sw = new(filename)) { - WriteToFile($"MapsCollection{Environment.NewLine}", fs); + sw.Write($"MapsCollection{Environment.NewLine}"); foreach (var storage in _mapStorages) { - WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); + sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); } } return true; @@ -82,42 +82,34 @@ namespace GasolineTanker { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string str = ""; + if ((str = sr.ReadLine()) == null || !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]) + _mapStorages.Clear(); + while ((str = sr.ReadLine()) != null) { - case "SimpleMap": - map = new SimpleMap(); - break; - case "MineField": - map = new MineField(); - break; - case "FormLawn": - map = new FormLawn(); - break; + var tempElem = str.Split(separatorDict); + AbstractMap map = null; + switch (tempElem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MineField": + map = new MineField(); + break; + case "FormLawn": + map = new FormLawn(); + break; + + } + _mapStorages.Add(tempElem[0], new MapWithSetTankersGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[tempElem[0]].LoadData(tempElem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } - _mapStorages.Add(elem[0], new MapWithSetTankersGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } return true; }