diff --git a/ProjectPlane/ProjectPlane/ExtentionPlane.cs b/ProjectPlane/ProjectPlane/ExtentionPlane.cs
new file mode 100644
index 0000000..22720af
--- /dev/null
+++ b/ProjectPlane/ProjectPlane/ExtentionPlane.cs
@@ -0,0 +1,58 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectPlane
+{
+ internal static class ExtentionPlane
+ {
+
+ ///
+ /// Разделитель для записи информации по объекту в файл
+ ///
+ private static readonly char _separatorForObject = ':';
+ ///
+ /// Создание объекта из строки
+ ///
+ ///
+ ///
+ public static DrawingPlane CreateDrawingplane(this string info)
+ {
+ string[] strs = info.Split(_separatorForObject);
+ if (strs.Length == 3)
+ {
+ return new DrawingPlane(Convert.ToInt32(strs[0]),
+ Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
+ }
+ if (strs.Length == 6)
+ {
+ return new DrawingWarPlane(Convert.ToInt32(strs[0]),
+ Convert.ToInt32(strs[1]), Color.FromName(strs[2]),
+ Color.FromName(strs[3]), Convert.ToBoolean(strs[4]),
+ Convert.ToBoolean(strs[5]));
+ }
+ return null;
+ }
+ ///
+ /// Получение данных для сохранения в файл
+ ///
+ ///
+ ///
+ public static string GetDataForSave(this DrawingPlane drawingplane)
+ {
+ var plane = drawingplane.Plane;
+ var str =
+ $"{plane.Speed}{_separatorForObject}{plane.Weight}{_separatorForObject}{plane.BodyColor.Name}";
+ if (plane is not EntityWarPlane warPlane)
+ {
+ return str;
+ }
+ return
+ $"{str}{_separatorForObject}{warPlane.DopColor.Name}{_separatorForObject}{warPlane.extraCell}" +
+ $"{_separatorForObject}{warPlane.SuperTurbine}{_separatorForObject}";
+ }
+
+ }
+}
diff --git a/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs b/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs
index 1ded118..d03b30f 100644
--- a/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs
+++ b/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs
@@ -108,6 +108,31 @@ namespace ProjectPlane
return new(_pictureWidth, _pictureHeight);
}
///
+ /// Получение данных в виде строки
+ ///
+ ///
+ ///
+ public string GetData(char separatorType, char separatorData)
+ {
+ string data = $"{_map.GetType().Name}{separatorType}";
+ foreach (var car in _setPlanes.GetPlanes())
+ {
+ data += $"{car.GetInfo()}{separatorData}";
+ }
+ return data;
+ }
+ ///
+ /// Загрузка списка из массива строк
+ ///
+ ///
+ public void LoadData(string[] records)
+ {
+ foreach (var rec in records)
+ {
+ _setPlanes.Insert(DrawingObject.Create(rec) as T);
+ }
+ }
+ ///
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
///
private void Shaking()
diff --git a/ProjectPlane/ProjectPlane/MapsCollection.cs b/ProjectPlane/ProjectPlane/MapsCollection.cs
index bab6984..491c9f5 100644
--- a/ProjectPlane/ProjectPlane/MapsCollection.cs
+++ b/ProjectPlane/ProjectPlane/MapsCollection.cs
@@ -1,4 +1,5 @@
-namespace ProjectPlane
+using System.Text;
+namespace ProjectPlane
{
///
/// Класс для хранения коллекции карт
@@ -8,7 +9,7 @@
///
/// Словарь (хранилище) с картами
///
- readonly Dictionary> _mapStorages;
+ readonly Dictionary> _mapStorages;
///
/// Возвращение списка названий карт
///
@@ -26,9 +27,18 @@
///
///
///
+ /// ///
+ /// Разделитель для записи информации по элементу словаря в файл
+ ///
+ private readonly char separatorDict = '|';
+ ///
+ /// Разделитель для записей коллекции данных в файл
+ ///
+ private readonly char separatorData = ';';
+
public MapsCollection(int pictureWidth, int pictureHeight)
{
- _mapStorages = new Dictionary>();
+ _mapStorages = new Dictionary>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@@ -40,7 +50,7 @@
public void AddMap(string name, AbstractMap map)
{
if (!_mapStorages.ContainsKey(name))
- _mapStorages.Add(name, new MapWithSetPlanesGeneric(_pictureWidth, _pictureHeight, map));
+ _mapStorages.Add(name, new MapWithSetPlanesGeneric(_pictureWidth, _pictureHeight, map));
}
///
/// Удаление карты
@@ -56,7 +66,7 @@
///
///
///
- public MapWithSetPlanesGeneric this[string ind]
+ public MapWithSetPlanesGeneric this[string ind]
{
get
{
@@ -64,5 +74,79 @@
return null;
}
}
+ ///
+ /// Метод записи информации в файл
+ ///
+ /// Строка, которую следует записать
+ /// Поток для записи
+ 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))
+ {
+ WriteToFile($"MapsCollection{Environment.NewLine}", fs);
+ foreach (var storage in _mapStorages)
+ {
+ WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs);
+ }
+ }
+ return true;
+ }
+
+ ///
+ /// Загрузка нформации по автомобилям на парковках из файла
+ ///
+ ///
+ ///
+ public bool LoadData(string filename)
+ {
+ if (!File.Exists(filename))
+ {
+ return false;
+ }
+ string bufferTextFromFile = "";
+ using (FileStream fs = new(filename, FileMode.Open))
+ {
+ byte[] b = new byte[fs.Length];
+ UTF8Encoding temp = new(true);
+ while (fs.Read(b, 0, b.Length) > 0)
+ {
+ 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)
+ {
+ var elem = strs[i].Split(separatorDict);
+ AbstractMap map = null;
+ switch (elem[1])
+ {
+ case "SimpleMap":
+ map = new SimpleMap();
+ break;
+ case "SkyMap":
+ map = new SkyMap();
+ break;
+ }
+ _mapStorages.Add(elem[0], new MapWithSetPlanesGeneric(_pictureWidth, _pictureHeight, map));
+ _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
+ }
+ return true;
+ }
}
}
\ No newline at end of file