6 лаба итог

This commit is contained in:
AnnZhimol 2022-11-12 21:35:23 +04:00
parent 5d0339bfdc
commit a17a1a9f2b
3 changed files with 30 additions and 38 deletions

5
FileWithSave.txt Normal file
View File

@ -0,0 +1,5 @@
MapsCollection
were|SimpleMap|100:100:Lime;
ert4rg|SimpleMap|100:100:Red;100:100:Fuchsia:Yellow:True:True:True;
32e32|SecondMap|100:100:Lime:Fuchsia:True:True:False;100:100:Cyan:Cyan:False:False:True;100:100:Red:Yellow:True:True:True;
fdgwdf|SecondMap|

View File

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

View File

@ -25,24 +25,18 @@ namespace Warship
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
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))
{ {
WriteToFile($"MapsCollection{Environment.NewLine}", fs); sw.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages) foreach (var storage in _mapStorages)
{ {
WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
} }
} }
return true; return true;
@ -54,37 +48,30 @@ namespace Warship
{ {
return false; return false;
} }
string bufferTextFromFile = ""; using (StreamReader sr = new(filename))
using (FileStream fs = new(filename, FileMode.Open))
{ {
byte[] b = new byte[fs.Length]; string str = "";
UTF8Encoding temp = new(true); if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
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); while ((str = sr.ReadLine()) != null)
if (!strs[0].Contains("MapCollection"))
{
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 = str.Split(separatorDict);
map = new SimpleMap(); AbstractMap map = null;
break; switch (elem[1])
case "SecondMap": {
map = new SecondMap(); case "SimpleMap":
break; map = new SimpleMap();
break;
case "SecondMap":
map = new SecondMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
} }
_mapStorages.Add(elem[0], new MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
} }
return true; return true;
} }
@ -98,7 +85,7 @@ namespace Warship
} }
else else
{ {
_mapStorages.Add(name, new MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>(_pictureWidth, _pictureHeight, map)); _mapStorages.Add(name, new MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
} }
} }
@ -107,7 +94,7 @@ namespace Warship
_mapStorages.Remove(name); _mapStorages.Remove(name);
} }
public MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> this[string ind] public MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap> this[string ind]
{ {
get get
{ {