Доработка

This commit is contained in:
Володя 2022-12-02 17:49:34 +03:00
parent 7bb0be419d
commit c050a5b51f
2 changed files with 45 additions and 36 deletions

View File

@ -190,7 +190,20 @@ namespace AirPlaneWithRadar
private void loadToolStrip_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(openFileDialog.FileName) && _mapsCollection.Count != 0)
{
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadMaps();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось загрузить", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

View File

@ -20,6 +20,7 @@ namespace AirPlaneWithRadar
private readonly char separatorData = ';';
public int Count { get { return _mapStorages.Count; } }
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetPlainGeneric<IDrawingObject, AbstractMap>>();
@ -53,68 +54,63 @@ namespace AirPlaneWithRadar
return _mapStorages[ind];
}
}
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, false))
{
WriteToFile($"MapsCollection{Environment.NewLine}", fs);
sw.WriteLine("MapsCollection");
foreach (var storage in _mapStorages)
{
WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs);
sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}");
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader sr = new StreamReader(filename))
{
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0)
if (!sr.ReadLine().Equals("MapsCollection"))
{
bufferTextFromFile += temp.GetString(b);
}
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (!strs[0].Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
//очищаем записи
_mapStorages.Clear();
for (int i = 1; i < strs.Length; ++i)
string line = sr.ReadLine();
while (line != null)
{
var elem = strs[i].Split(separatorDict);
var elem = line.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "UserMap_BigBox":
map = new UserMap_BigBox();
break;
case "UserMap_Colums":
map = new UserMap_Colums();
break;
}
_mapStorages.Add(elem[0], new MapWithSetPlainGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
line = sr.ReadLine();
}
}
return true;
}
}
}