Доработка
This commit is contained in:
parent
7bb0be419d
commit
c050a5b51f
@ -190,7 +190,20 @@ namespace AirPlaneWithRadar
|
|||||||
|
|
||||||
private void loadToolStrip_Click(object sender, EventArgs e)
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ namespace AirPlaneWithRadar
|
|||||||
|
|
||||||
private readonly char separatorData = ';';
|
private readonly char separatorData = ';';
|
||||||
|
|
||||||
|
public int Count { get { return _mapStorages.Count; } }
|
||||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||||
{
|
{
|
||||||
_mapStorages = new Dictionary<string, MapWithSetPlainGeneric<IDrawingObject, AbstractMap>>();
|
_mapStorages = new Dictionary<string, MapWithSetPlainGeneric<IDrawingObject, AbstractMap>>();
|
||||||
@ -53,68 +54,63 @@ namespace AirPlaneWithRadar
|
|||||||
return _mapStorages[ind];
|
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)
|
public bool SaveData(string filename)
|
||||||
{
|
{
|
||||||
if (File.Exists(filename))
|
if (File.Exists(filename))
|
||||||
{
|
{
|
||||||
File.Delete(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)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public bool LoadData(string filename)
|
public bool LoadData(string filename)
|
||||||
{
|
{
|
||||||
if (!File.Exists(filename))
|
if (!File.Exists(filename))
|
||||||
{
|
{
|
||||||
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];
|
if (!sr.ReadLine().Equals("MapsCollection"))
|
||||||
UTF8Encoding temp = new(true);
|
|
||||||
while (fs.Read(b, 0, b.Length) > 0)
|
|
||||||
{
|
{
|
||||||
bufferTextFromFile += temp.GetString(b);
|
return false;
|
||||||
}
|
}
|
||||||
}
|
_mapStorages.Clear();
|
||||||
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
string line = sr.ReadLine();
|
||||||
if (!strs[0].Contains("MapsCollection"))
|
while (line != 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 = line.Split(separatorDict);
|
||||||
map = new SimpleMap();
|
AbstractMap map = null;
|
||||||
break;
|
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();
|
||||||
}
|
}
|
||||||
_mapStorages.Add(elem[0], new MapWithSetPlainGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
|
||||||
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user