diff --git a/FileWithSave.txt b/FileWithSave.txt new file mode 100644 index 0000000..0c324cd --- /dev/null +++ b/FileWithSave.txt @@ -0,0 +1,5 @@ +MapsCollection +were|SimpleMap|100:100:Lime; +ert4rg|SimpleMap|100:100:Red;100:100:Fuchsia:Yellow:True:True:True; +32e32|SecondMap|100:100:Lime:Fuchsia:True:True:False;100:100:Cyan:Cyan:False:False:True;100:100:Red:Yellow:True:True:True; +fdgwdf|SecondMap| diff --git a/Warship/Warship/FormMapWithSetWarships.cs b/Warship/Warship/FormMapWithSetWarships.cs index e3eb974..4a5a575 100644 --- a/Warship/Warship/FormMapWithSetWarships.cs +++ b/Warship/Warship/FormMapWithSetWarships.cs @@ -200,8 +200,8 @@ namespace Warship { if (_mapsCollection.LoadData(OpenFileDialog.FileName)) { - ReloadMaps(); MessageBox.Show("Загрузка прошла успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); } else { diff --git a/Warship/Warship/MapsCollection.cs b/Warship/Warship/MapsCollection.cs index 80740c5..b129965 100644 --- a/Warship/Warship/MapsCollection.cs +++ b/Warship/Warship/MapsCollection.cs @@ -25,24 +25,18 @@ namespace Warship _pictureHeight = pictureHeight; } - 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 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; @@ -54,37 +48,30 @@ namespace Warship { 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("MapCollection")) - { - 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 "SecondMap": - map = new SecondMap(); - break; + var elem = str.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "SecondMap": + map = new SecondMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetWarshipsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } - _mapStorages.Add(elem[0], new MapWithSetWarshipsGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } return true; } @@ -98,7 +85,7 @@ namespace Warship } else { - _mapStorages.Add(name, new MapWithSetWarshipsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetWarshipsGeneric(_pictureWidth, _pictureHeight, map)); } } @@ -107,7 +94,7 @@ namespace Warship _mapStorages.Remove(name); } - public MapWithSetWarshipsGeneric this[string ind] + public MapWithSetWarshipsGeneric this[string ind] { get {