diff --git a/Monorail/Monorail/MonorailGenericStorage.cs b/Monorail/Monorail/MonorailGenericStorage.cs index f5ab720..aa87182 100644 --- a/Monorail/Monorail/MonorailGenericStorage.cs +++ b/Monorail/Monorail/MonorailGenericStorage.cs @@ -24,7 +24,7 @@ namespace Monorail.Generics public MonorailGenericStorage(int pictureWidth, int pictureHeight) { - _monorailStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -37,11 +37,11 @@ namespace Monorail.Generics File.Delete(filename); } StringBuilder data = new(); - foreach(KeyValuePair> record in _monorailStorages) { StringBuilder records = new(); - foreach(DrawningMonorail? elem in record.Value.GetMonorails) + foreach (DrawningMonorail? elem in record.Value.GetMonorails) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } @@ -52,10 +52,17 @@ MonorailGenericCollection> record in _ { return false; } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"MonorailStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); + string toWrite = $"MonorailStorage{Environment.NewLine}{data}"; + var strs = toWrite.Split(new char[] { '\n', '\r' }, +StringSplitOptions.RemoveEmptyEntries); + + using (StreamWriter sw = new(filename)) + { + foreach (var str in strs) + { + sw.WriteLine(str); + } + } return true; } @@ -65,61 +72,59 @@ MonorailGenericCollection> record in _ { 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) - { - bufferTextFromFile += temp.GetString(b); - } - } - - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + string str = sr.ReadLine(); + var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - return false; - } - if (!strs[0].StartsWith("MonorailStorage")) - { - return false; - } - _monorailStorages.Clear(); - foreach (string data in strs) - { - string[] record = data.Split(_separatorForKeyValue, -StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) + if (strs == null || strs.Length == 0) { - continue; + return false; } - MonorailGenericCollection -collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, - StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + if (!strs[0].StartsWith("MonorailStorage")) { - DrawningMonorail? monorail = - elem?.CreateDrawningMonorail(_separatorForObject, _pictureWidth, _pictureHeight); - if (monorail != null) + return false; + } + _monorailStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) { - if (!(collection + monorail)) + str = sr.ReadLine(); + continue; + } + MonorailGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningMonorail? monorail = + elem?.CreateDrawningMonorail(_separatorForObject, _pictureWidth, _pictureHeight); + if (monorail != null) { - return false; + if (!(collection + monorail)) + { + return false; + } } } - } - _monorailStorages.Add(record[0], collection); + _monorailStorages.Add(record[0], collection); + + str = sr.ReadLine(); + } while (str != null); } + + + return true; } public void AddSet(string name) { - _monorailStorages.Add(name, new MonorailGenericCollection (_pictureWidth, _pictureHeight)); + _monorailStorages.Add(name, new MonorailGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name)