From b516a248df2d898324b8c516aa56d39a403193a5 Mon Sep 17 00:00:00 2001 From: devil_1nc Date: Tue, 8 Nov 2022 17:27:10 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'ProjectPlane/ProjectPlane/MapsCollect?= =?UTF-8?q?ion.cs'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectPlane/ProjectPlane/MapsCollection.cs | 60 +++++++++------------ 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/ProjectPlane/ProjectPlane/MapsCollection.cs b/ProjectPlane/ProjectPlane/MapsCollection.cs index 491c9f5..24d9226 100644 --- a/ProjectPlane/ProjectPlane/MapsCollection.cs +++ b/ProjectPlane/ProjectPlane/MapsCollection.cs @@ -84,20 +84,13 @@ namespace ProjectPlane byte[] info = new UTF8Encoding(true).GetBytes(text); stream.Write(info, 0, info.Length); } - public bool SaveData(string filename) + + public bool SaveDict(string filename) { if (File.Exists(filename)) { File.Delete(filename); } - using (FileStream fs = new(filename, FileMode.Create)) - { - WriteToFile($"MapsCollection{Environment.NewLine}", fs); - foreach (var storage in _mapStorages) - { - WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); - } - } return true; } @@ -114,39 +107,34 @@ namespace ProjectPlane } string bufferTextFromFile = ""; using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new StreamReader(fs, Encoding.UTF8)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string curLine = sr.ReadLine(); + + if (!curLine.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 ((curLine = sr.ReadLine()) != null) { - case "SimpleMap": - map = new SimpleMap(); - break; - case "SkyMap": - map = new SkyMap(); - break; + var elem = curLine.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "SkyMap": + map = new SkyMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetPlanesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } - _mapStorages.Add(elem[0], new MapWithSetPlanesGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + return true; } - return true; } } } \ No newline at end of file