Изменил(а) на 'ProjectPlane/ProjectPlane/MapsCollection.cs'

This commit is contained in:
devil_1nc 2022-11-08 17:27:10 +04:00
parent 88dcfeef92
commit b516a248df

View File

@ -84,20 +84,13 @@ namespace ProjectPlane
byte[] info = new UTF8Encoding(true).GetBytes(text); byte[] info = new UTF8Encoding(true).GetBytes(text);
stream.Write(info, 0, info.Length); stream.Write(info, 0, info.Length);
} }
public bool SaveData(string filename)
public bool SaveDict(string filename)
{ {
if (File.Exists(filename)) if (File.Exists(filename))
{ {
File.Delete(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; return true;
} }
@ -114,39 +107,34 @@ namespace ProjectPlane
} }
string bufferTextFromFile = ""; string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open)) using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{ {
byte[] b = new byte[fs.Length]; string curLine = sr.ReadLine();
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0) if (!curLine.Contains("MapsCollection"))
{ {
bufferTextFromFile += temp.GetString(b); return false;
} }
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); _mapStorages.Clear();
if (!strs[0].Contains("MapsCollection")) while ((curLine = sr.ReadLine()) != null)
{
//если нет такой записи, то это не те данные
return false;
}
//очищаем записи
_mapStorages.Clear();
for (int i = 1; i < strs.Length; ++i)
{
var elem = strs[i].Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{ {
case "SimpleMap": var elem = curLine.Split(separatorDict);
map = new SimpleMap(); AbstractMap map = null;
break; switch (elem[1])
case "SkyMap": {
map = new SkyMap(); case "SimpleMap":
break; map = new SimpleMap();
break;
case "SkyMap":
map = new SkyMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetPlanesGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
} }
_mapStorages.Add(elem[0], new MapWithSetPlanesGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map)); return true;
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
} }
return true;
} }
} }
} }