159 lines
6.1 KiB
C#
159 lines
6.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using DoubleDeckerbus.Generics;
|
||
using DoubleDeckerbus.Drawing;
|
||
using DoubleDeckerbus.Generics;
|
||
using DoubleDeckerbus.Move_Strategy;
|
||
|
||
namespace DoubleDeckerbus.Generic
|
||
{
|
||
internal class BusesGenericStorage
|
||
{
|
||
//Словарь (хранилище)
|
||
readonly Dictionary<BusCollectionInfo, BusGenericCollection<DrawingBus, DrawingObjectBus>> _busStorages;
|
||
//Возвращение списка названий наборов
|
||
public List<BusCollectionInfo> Keys => _busStorages.Keys.ToList();
|
||
//Ширина окна отрисовки
|
||
private readonly int _pictureWidth;
|
||
//Высота окна отрисовки
|
||
private readonly int _pictureHeight;
|
||
|
||
// Разделитель для записи ключа и значения элемента словаря
|
||
private static readonly char _separatorForKeyValue = '|';
|
||
// Разделитель для записей коллекции данных в файл
|
||
private readonly char _separatorRecords = ';';
|
||
// Разделитель для записи информации по объекту в файл
|
||
private static readonly char _separatorForObject = ':';
|
||
|
||
public BusesGenericStorage(int pictureWidth, int pictureHeight)
|
||
{
|
||
_busStorages = new Dictionary<BusCollectionInfo, BusGenericCollection<DrawingBus, DrawingObjectBus>>();
|
||
_pictureWidth = pictureWidth;
|
||
_pictureHeight = pictureHeight;
|
||
}
|
||
|
||
// Добавление набора
|
||
public void AddSet(string name)
|
||
{
|
||
BusCollectionInfo set = new BusCollectionInfo(name, string.Empty);
|
||
|
||
if (_busStorages.ContainsKey(set))
|
||
return;
|
||
|
||
_busStorages.Add(set, new BusGenericCollection<DrawingBus, DrawingObjectBus>(_pictureWidth, _pictureHeight));
|
||
}
|
||
|
||
// Удаление набора
|
||
public void DelSet(string name)
|
||
{
|
||
BusCollectionInfo set = new BusCollectionInfo(name, string.Empty);
|
||
|
||
// проверка, что нет набора с таким именем
|
||
if (!_busStorages.ContainsKey(set))
|
||
return;
|
||
|
||
_busStorages.Remove(set);
|
||
|
||
}
|
||
|
||
// Доступ к набору
|
||
public BusGenericCollection<DrawingBus, DrawingObjectBus>? this[string ind]
|
||
{
|
||
get
|
||
{
|
||
BusCollectionInfo set = new BusCollectionInfo(ind, string.Empty);
|
||
|
||
if (!_busStorages.ContainsKey(set))
|
||
{
|
||
return null;
|
||
}
|
||
return _busStorages[set];
|
||
}
|
||
}
|
||
public void SaveData(string filename)
|
||
{
|
||
if (File.Exists(filename))
|
||
{
|
||
File.Delete(filename);
|
||
}
|
||
StringBuilder data = new();
|
||
foreach (KeyValuePair<BusCollectionInfo,
|
||
BusGenericCollection<DrawingBus, DrawingObjectBus>> record in _busStorages)
|
||
{
|
||
StringBuilder records = new();
|
||
foreach (DrawingBus? elem in record.Value.GetBus)
|
||
{
|
||
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
||
}
|
||
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
||
}
|
||
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;
|
||
}
|
||
public void LoadData(string filename)
|
||
{
|
||
if (!File.Exists(filename))
|
||
{
|
||
throw new Exception("Файл не найден");
|
||
}
|
||
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 == 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)
|
||
{
|
||
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)
|
||
{
|
||
if ((collection + Bus) == -1)
|
||
{
|
||
throw new Exception("Ошибка добавления в коллекцию");
|
||
}
|
||
}
|
||
}
|
||
_busStorages.Add(new BusCollectionInfo(record[0], string.Empty), collection);
|
||
}
|
||
}
|
||
}
|
||
}
|