fix save and load

This commit is contained in:
Denis 2022-11-19 19:31:20 +04:00
parent 378cfe47f1
commit bdfd25c4cb
3 changed files with 31 additions and 43 deletions

View File

@ -155,7 +155,7 @@
//
// panelPurple
//
this.panelPurple.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
this.panelPurple.BackColor = System.Drawing.Color.Purple;
this.panelPurple.Location = new System.Drawing.Point(168, 59);
this.panelPurple.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.panelPurple.Name = "panelPurple";

View File

@ -246,11 +246,12 @@ namespace AirplaneWithRadar
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.SaveData(saveFileDialog.FileName))
try
{
_mapsCollection.SaveData(saveFileDialog.FileName);
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
catch (Exception ex)
{
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
@ -266,12 +267,13 @@ namespace AirplaneWithRadar
// TODO продумать логику
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(openFileDialog.FileName))
try
{
_mapsCollection.LoadData(openFileDialog.FileName);
ReloadMaps();
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
catch (Exception ex)
{
MessageBox.Show("Не получилось загрузить файл", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

View File

@ -54,7 +54,7 @@ namespace AirplaneWithRadar
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns></returns>
public bool SaveData(string filename)
public void SaveData(string filename)
{
if (File.Exists(filename))
{
@ -68,61 +68,47 @@ namespace AirplaneWithRadar
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
return true;
}
/// <summary>
/// Загрузка информации по локомотивам в депо из файла
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public bool LoadData(string filename)
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
throw new FileNotFoundException("Файл не найден");
}
using (StreamReader sr = new(filename))
{
bool isFirst = true;
string str;
string str = sr.ReadLine();
if (!str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
throw new FileFormatException("Формат данных в файле не правильный");
}
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
if (isFirst)
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
if (!str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
else
{
//очищаем записи
_mapStorages.Clear();
}
isFirst = false;
}
else
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "Простая карта":
map = new SimpleMap();
break;
case "Карта неба с облаками":
map = new SkyMap();
break;
case "Карта туманного неба с грозой":
map = new FoggySkyMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
case "SimpleMap":
map = new SimpleMap();
break;
case "SkyMap":
map = new SkyMap();
break;
case "FoggySkyMap":
map = new FoggySkyMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
return true;
}
}
}