159 lines
6.1 KiB
C#
Raw Permalink Normal View History

2023-11-25 13:55:27 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-12-29 22:19:11 +03:00
using DoubleDeckerbus.Generics;
2023-11-25 13:55:27 +03:00
using DoubleDeckerbus.Drawing;
2023-12-29 22:19:11 +03:00
using DoubleDeckerbus.Generics;
2023-11-25 13:55:27 +03:00
using DoubleDeckerbus.Move_Strategy;
namespace DoubleDeckerbus.Generic
{
internal class BusesGenericStorage
{
2023-12-29 22:19:11 +03:00
//Словарь (хранилище)
readonly Dictionary<BusCollectionInfo, BusGenericCollection<DrawingBus, DrawingObjectBus>> _busStorages;
//Возвращение списка названий наборов
public List<BusCollectionInfo> Keys => _busStorages.Keys.ToList();
//Ширина окна отрисовки
2023-11-25 13:55:27 +03:00
private readonly int _pictureWidth;
2023-12-29 22:19:11 +03:00
//Высота окна отрисовки
2023-11-25 13:55:27 +03:00
private readonly int _pictureHeight;
2023-12-29 22:19:11 +03:00
// Разделитель для записи ключа и значения элемента словаря
2023-12-15 19:07:22 +03:00
private static readonly char _separatorForKeyValue = '|';
2023-12-29 22:19:11 +03:00
// Разделитель для записей коллекции данных в файл
2023-12-15 19:07:22 +03:00
private readonly char _separatorRecords = ';';
2023-12-29 22:19:11 +03:00
// Разделитель для записи информации по объекту в файл
2023-12-15 19:07:22 +03:00
private static readonly char _separatorForObject = ':';
2023-11-25 13:55:27 +03:00
public BusesGenericStorage(int pictureWidth, int pictureHeight)
{
2023-12-29 22:19:11 +03:00
_busStorages = new Dictionary<BusCollectionInfo, BusGenericCollection<DrawingBus, DrawingObjectBus>>();
2023-11-25 13:55:27 +03:00
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
2023-12-29 22:19:11 +03:00
// Добавление набора
2023-11-25 13:55:27 +03:00
public void AddSet(string name)
{
2023-12-29 22:19:11 +03:00
BusCollectionInfo set = new BusCollectionInfo(name, string.Empty);
if (_busStorages.ContainsKey(set))
return;
_busStorages.Add(set, new BusGenericCollection<DrawingBus, DrawingObjectBus>(_pictureWidth, _pictureHeight));
2023-11-25 13:55:27 +03:00
}
2023-12-29 22:19:11 +03:00
// Удаление набора
2023-11-25 13:55:27 +03:00
public void DelSet(string name)
{
2023-12-29 22:19:11 +03:00
BusCollectionInfo set = new BusCollectionInfo(name, string.Empty);
// проверка, что нет набора с таким именем
if (!_busStorages.ContainsKey(set))
return;
_busStorages.Remove(set);
2023-12-15 19:07:22 +03:00
2023-11-25 13:55:27 +03:00
}
2023-12-29 22:19:11 +03:00
// Доступ к набору
2023-11-25 13:55:27 +03:00
public BusGenericCollection<DrawingBus, DrawingObjectBus>? this[string ind]
{
get
{
2023-12-29 22:19:11 +03:00
BusCollectionInfo set = new BusCollectionInfo(ind, string.Empty);
if (!_busStorages.ContainsKey(set))
2023-11-25 13:55:27 +03:00
{
2023-12-29 22:19:11 +03:00
return null;
2023-11-25 13:55:27 +03:00
}
2023-12-29 22:19:11 +03:00
return _busStorages[set];
2023-11-25 13:55:27 +03:00
}
}
2023-12-29 21:22:51 +03:00
public void SaveData(string filename)
2023-12-15 19:07:22 +03:00
{
if (File.Exists(filename))
{
File.Delete(filename);
}
2023-12-29 21:22:51 +03:00
StringBuilder data = new();
2023-12-29 22:19:11 +03:00
foreach (KeyValuePair<BusCollectionInfo,
2023-12-29 21:22:51 +03:00
BusGenericCollection<DrawingBus, DrawingObjectBus>> record in _busStorages)
2023-12-15 19:07:22 +03:00
{
2023-12-29 21:22:51 +03:00
StringBuilder records = new();
foreach (DrawingBus? elem in record.Value.GetBus)
2023-12-15 19:07:22 +03:00
{
2023-12-29 21:22:51 +03:00
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
2023-12-15 19:07:22 +03:00
}
2023-12-29 21:22:51 +03:00
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
2023-12-15 19:07:22 +03:00
}
2023-12-29 21:22:51 +03:00
if (data.Length == 0)
{
throw new Exception("Невалиданя операция, нет данных для сохранения");
}
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new
UTF8Encoding(true).GetBytes($"BusStorage{Environment.NewLine}{data}");
fs.Write(info, 0, info.Length);
return;
2023-12-15 19:07:22 +03:00
}
2023-12-29 21:22:51 +03:00
public void LoadData(string filename)
2023-12-15 19:07:22 +03:00
{
if (!File.Exists(filename))
{
2023-12-29 21:22:51 +03:00
throw new Exception("Файл не найден");
2023-12-15 19:07:22 +03:00
}
2023-12-29 21:22:51 +03:00
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
2023-12-15 19:07:22 +03:00
{
2023-12-29 21:22:51 +03:00
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0)
2023-12-15 19:07:22 +03:00
{
2023-12-29 21:22:51 +03:00
bufferTextFromFile += temp.GetString(b);
2023-12-15 19:07:22 +03:00
}
2023-12-29 21:22:51 +03:00
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
throw new Exception("Нет данных для загрузки");
}
if (!strs[0].StartsWith("BusStorage"))
{
throw new Exception("Неверный формат данных");
}
_busStorages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
2023-12-15 19:07:22 +03:00
{
2023-12-29 21:22:51 +03:00
continue;
}
BusGenericCollection<DrawingBus, DrawingObjectBus>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingBus? Bus =
elem?.CreateDrawingBus(_separatorForObject, _pictureWidth, _pictureHeight);
if (Bus != null)
2023-12-15 19:07:22 +03:00
{
2023-12-29 21:22:51 +03:00
if ((collection + Bus) == -1)
2023-12-15 19:07:22 +03:00
{
2023-12-29 21:22:51 +03:00
throw new Exception("Ошибка добавления в коллекцию");
2023-12-15 19:07:22 +03:00
}
}
}
2023-12-29 22:19:11 +03:00
_busStorages.Add(new BusCollectionInfo(record[0], string.Empty), collection);
2023-12-15 19:07:22 +03:00
}
}
2023-11-25 13:55:27 +03:00
}
}