From 1a4b0f8bb80f936aed6ca8de857c14b6ba0a355e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Fri, 4 Nov 2022 16:49:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B2=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B8=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B0=20=D0=B8=D0=B7?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=20=D0=B2=20MapsCollection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SelfPropelledArtilleryUnit/MapsCollection.cs | 65 ++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/SelfPropelledArtilleryUnit/MapsCollection.cs b/SelfPropelledArtilleryUnit/MapsCollection.cs index cdd47ac..7a6cd83 100644 --- a/SelfPropelledArtilleryUnit/MapsCollection.cs +++ b/SelfPropelledArtilleryUnit/MapsCollection.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Tracing; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -14,6 +15,9 @@ namespace Artilleries private readonly int _pictureWidth; private readonly int _pictureHeight; + private readonly char separatorDict = '|'; + private readonly char separatorData = ';'; + public MapsCollection(int pictureWidth, int pictureHeight) { _mapsStorage = new Dictionary>(); @@ -44,5 +48,66 @@ namespace Artilleries return _mapsStorage.ContainsKey(index) ? _mapsStorage[index] : null; } } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + + using (FileStream fs = new FileStream(filename, FileMode.Create)) + using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) + { + sw.WriteLine("MapsCollection"); + foreach (var storage in _mapsStorage) + { + sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}"); + } + } + + return true; + } + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + + using (FileStream fs = new FileStream(filename, FileMode.Open)) + using (StreamReader sr = new StreamReader(fs, Encoding.UTF8)) + { + string current_line = sr.ReadLine(); + + if (current_line == null || !current_line.Contains("MapsCollection")) + { + return false; + } + + _mapsStorage.Clear(); + while ((current_line = sr.ReadLine()) != null) + { + var elements = current_line.Split(separatorDict); + AbstractMap map = null; + + switch (elements[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "ForestMap": + map = new ForestMap(); + break; + } + + _mapsStorage.Add(elements[0], new MapWithSetArtilleriesGeneric(_pictureWidth, _pictureHeight, map)); + _mapsStorage[elements[0]].LoadData(elements[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + + return true; + } + } } }