Переписал загрузку на StreamReader

This commit is contained in:
Никита Потапов 2023-11-20 18:45:39 +04:00
parent ca9860ba86
commit b46a5f46b0

View File

@ -116,30 +116,18 @@ namespace ProjectStormtrooper
{ {
return false; return false;
} }
string bufferTextFromFile = ""; using (StreamReader sr = new StreamReader(filename))
using (FileStream fs = new(filename, FileMode.Open))
{ {
byte[] b = new byte[fs.Length]; string? currentLine = sr.ReadLine();
UTF8Encoding temp = new(true); if (currentLine == null || !currentLine.Contains("PlaneStorage"))
while (fs.Read(b, 0, b.Length) > 0)
{ {
bufferTextFromFile += temp.GetString(b);
}
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("PlaneStorage"))
{
// если нет такой записи, то это не те данные
return false; return false;
} }
_planeStorages.Clear(); _planeStorages.Clear();
foreach (string data in strs) currentLine = sr.ReadLine();
while (currentLine != null)
{ {
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); string[] record = currentLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2) if (record.Length != 2)
{ {
continue; continue;
@ -158,8 +146,11 @@ namespace ProjectStormtrooper
} }
} }
_planeStorages.Add(record[0], collection); _planeStorages.Add(record[0], collection);
currentLine = sr.ReadLine();
} }
return true; return true;
} }
}
} }
} }