This commit is contained in:
Калышев Ян 2022-11-06 19:21:45 +04:00
parent 4664385343
commit 53a65a328e
4 changed files with 38 additions and 33 deletions

View File

@ -269,12 +269,14 @@
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// openFileDialog
//

View File

@ -242,6 +242,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
if (_mapsCollection.LoadData(openFileDialog.FileName))
{
ReloadMaps();
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else

View File

@ -101,11 +101,12 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
File.Delete(filename);
}
using (FileStream fs = new(filename, FileMode.Create))
using (StreamWriter sw = new StreamWriter(fs))
{
WriteToFile($"MapsCollection{Environment.NewLine}", fs);
sw.WriteLine($"MapsCollection{Environment.NewLine}");
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)}{Environment.NewLine}");
}
}
return true;
@ -121,41 +122,40 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
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)
string str;
int count = 0;
while ((str = sr.ReadLine()) != null)
{
bufferTextFromFile += temp.GetString(b);
if (count == 0)
{
if (!str.Contains("MapsCollection"))
{
return false;
} else
{
count++;
continue;
}
}
_mapStorages.Clear();
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "MySecondMap":
map = new MySecondMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetBoatsGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
return true;
}
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)
{
var elem = strs[i].Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "MySecondMap":
map = new MySecondMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetBoatsGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
return true;
}
}
}

2
ok.txt Normal file
View File

@ -0,0 +1,2 @@
MapsCollection
ok|SimpleMap|100:100:Blue;100:100:Yellow:Blue:False:True:True;