2024-05-05 11:40:18 +04:00
|
|
|
|
using NLog.LayoutRenderers.Wrappers;
|
|
|
|
|
using AntiAircraftGun.CollectionGenereticObject;
|
2024-04-12 17:12:59 +04:00
|
|
|
|
using AntiAircraftGun.Drawnings;
|
2024-04-19 19:56:36 +04:00
|
|
|
|
using AntiAircraftGun.Exceptions;
|
2024-04-12 17:12:59 +04:00
|
|
|
|
using System.Text;
|
2024-05-14 19:49:41 +04:00
|
|
|
|
using System.Xml.Linq;
|
2024-03-23 14:01:50 +04:00
|
|
|
|
|
2024-05-05 11:40:18 +04:00
|
|
|
|
|
2024-03-23 14:01:50 +04:00
|
|
|
|
namespace AntiAircraftGun.CollectionGenericObjects;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Класс-хранилище коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public class StorageCollection<T>
|
2024-04-12 17:12:59 +04:00
|
|
|
|
where T : DrawningArmoredCar
|
2024-03-23 14:01:50 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Словарь (хранилище) с коллекциями
|
|
|
|
|
/// </summary>
|
2024-05-12 14:52:01 +04:00
|
|
|
|
readonly Dictionary<CollectionInfo, ICollectionGenericObjects<T>> _storages;
|
2024-03-23 14:01:50 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий коллекций
|
|
|
|
|
/// </summary>
|
2024-05-12 14:52:01 +04:00
|
|
|
|
public List<CollectionInfo> Keys => _storages.Keys.ToList();
|
2024-04-12 17:12:59 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ключевое слово, с которого должен начинаться файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _collectionKey = "CollectionsStorage";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorForKeyValue = "|";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorItems = ";";
|
2024-03-23 14:01:50 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public StorageCollection()
|
|
|
|
|
{
|
2024-05-12 14:52:01 +04:00
|
|
|
|
_storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
|
2024-03-23 14:01:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление коллекции в хранилище
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
/// <param name="collectionType">тип коллекции</param>
|
2024-05-14 19:49:41 +04:00
|
|
|
|
public void AddCollection(String name, CollectionType collectionType)
|
2024-03-23 14:01:50 +04:00
|
|
|
|
{
|
2024-05-14 19:49:41 +04:00
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty);
|
|
|
|
|
if (_storages.ContainsKey(collectionInfo)) return;
|
|
|
|
|
if (collectionType == CollectionType.None) return;
|
|
|
|
|
else if (collectionType == CollectionType.Massive)
|
2024-05-12 14:52:01 +04:00
|
|
|
|
_storages[collectionInfo] = new MassiveGenericObjects<T>();
|
2024-05-14 19:49:41 +04:00
|
|
|
|
else if (collectionType == CollectionType.List)
|
2024-05-12 14:52:01 +04:00
|
|
|
|
_storages[collectionInfo] = new ListGenericObjects<T>();
|
2024-03-23 14:01:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
2024-05-12 14:52:01 +04:00
|
|
|
|
public void DelCollection(CollectionInfo collectionInfo)
|
2024-03-23 14:01:50 +04:00
|
|
|
|
{
|
2024-05-12 14:52:01 +04:00
|
|
|
|
if (_storages.ContainsKey(collectionInfo))
|
|
|
|
|
_storages.Remove(collectionInfo);
|
2024-03-23 14:01:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Доступ к коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
/// <returns></returns>
|
2024-05-12 14:52:01 +04:00
|
|
|
|
public ICollectionGenericObjects<T>? this[CollectionInfo collectionInfo]
|
2024-03-23 14:01:50 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2024-05-12 14:52:01 +04:00
|
|
|
|
if (_storages.ContainsKey(collectionInfo))
|
|
|
|
|
return _storages[collectionInfo];
|
2024-03-23 14:01:50 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-12 17:12:59 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение информации по автомобилям в хранилище в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
2024-04-19 19:56:36 +04:00
|
|
|
|
public void SaveData(string filename)
|
2024-04-12 17:12:59 +04:00
|
|
|
|
{
|
|
|
|
|
if (_storages.Count == 0)
|
|
|
|
|
{
|
2024-04-19 19:56:36 +04:00
|
|
|
|
throw new Exception("В хранилище отсутствуют коллекции для сохранения");
|
2024-04-12 17:12:59 +04:00
|
|
|
|
|
2024-05-05 11:40:18 +04:00
|
|
|
|
}
|
2024-04-12 17:12:59 +04:00
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
2024-04-19 19:01:19 +04:00
|
|
|
|
using (StreamWriter writer = new(filename))
|
2024-04-12 17:12:59 +04:00
|
|
|
|
{
|
2024-04-19 19:01:19 +04:00
|
|
|
|
writer.Write(_collectionKey);
|
2024-05-12 14:52:01 +04:00
|
|
|
|
foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
|
2024-04-12 17:12:59 +04:00
|
|
|
|
{
|
2024-04-19 19:01:19 +04:00
|
|
|
|
writer.Write(Environment.NewLine);
|
2024-05-09 18:16:29 +04:00
|
|
|
|
|
2024-04-19 19:01:19 +04:00
|
|
|
|
if (value.Value.Count == 0)
|
2024-04-12 17:12:59 +04:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-04-19 19:01:19 +04:00
|
|
|
|
writer.Write(value.Key);
|
|
|
|
|
writer.Write(_separatorForKeyValue);
|
|
|
|
|
writer.Write(value.Value.GetCollectionType);
|
|
|
|
|
writer.Write(_separatorForKeyValue);
|
|
|
|
|
writer.Write(value.Value.MaxCount);
|
|
|
|
|
writer.Write(_separatorForKeyValue);
|
|
|
|
|
|
|
|
|
|
foreach (T? item in value.Value.GetItems())
|
|
|
|
|
{
|
|
|
|
|
string data = item?.GetDataForSave() ?? string.Empty;
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
writer.Write(data);
|
|
|
|
|
writer.Write(_separatorItems);
|
|
|
|
|
}
|
2024-04-12 17:12:59 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации по автомобилям в хранилище из файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
2024-04-19 19:56:36 +04:00
|
|
|
|
public void LoadData(string filename)
|
2024-04-12 17:12:59 +04:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2024-04-19 19:56:36 +04:00
|
|
|
|
throw new FileNotFoundException($"{filename} не существует");
|
2024-04-12 17:12:59 +04:00
|
|
|
|
}
|
2024-05-12 14:52:01 +04:00
|
|
|
|
using (StreamReader reader = File.OpenText(filename))
|
2024-04-12 17:12:59 +04:00
|
|
|
|
{
|
2024-04-19 19:01:19 +04:00
|
|
|
|
string line = reader.ReadLine();
|
|
|
|
|
if (line == null || line.Length == 0)
|
2024-04-12 17:12:59 +04:00
|
|
|
|
{
|
2024-05-05 11:40:18 +04:00
|
|
|
|
throw new IOException("Файл не подходит");
|
2024-04-12 17:12:59 +04:00
|
|
|
|
}
|
2024-04-19 19:01:19 +04:00
|
|
|
|
if (!line.Equals(_collectionKey))
|
2024-04-12 17:12:59 +04:00
|
|
|
|
{
|
2024-05-05 11:40:18 +04:00
|
|
|
|
|
|
|
|
|
throw new IOException("В файле неверные данные");
|
2024-04-12 17:12:59 +04:00
|
|
|
|
}
|
2024-04-19 19:01:19 +04:00
|
|
|
|
_storages.Clear();
|
2024-05-12 14:52:01 +04:00
|
|
|
|
string strs = "";
|
|
|
|
|
while ((strs = reader.ReadLine()) != null)
|
2024-04-12 17:12:59 +04:00
|
|
|
|
{
|
2024-05-12 14:52:01 +04:00
|
|
|
|
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (record.Length != 3)
|
2024-04-19 19:01:19 +04:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-05-12 14:52:01 +04:00
|
|
|
|
|
|
|
|
|
CollectionInfo? collectionInfo =
|
|
|
|
|
CollectionInfo.GetCollectionInfo(record[0]) ??
|
|
|
|
|
throw new Exception("Не удалось определить информацию коллекции:" + record[0]);
|
|
|
|
|
|
|
|
|
|
ICollectionGenericObjects<T>? collection =
|
|
|
|
|
StorageCollection<T>.CreateCollection(collectionInfo.CollectionType) ??
|
|
|
|
|
throw new Exception("Не удалось определить тип коллекции:" + record[1]);
|
|
|
|
|
collection.MaxCount = Convert.ToInt32(record[1]);
|
|
|
|
|
|
|
|
|
|
string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
2024-04-19 19:01:19 +04:00
|
|
|
|
foreach (string elem in set)
|
|
|
|
|
{
|
2024-05-12 14:52:01 +04:00
|
|
|
|
if (elem?.CreateDrawningArmoredCar() is T ship)
|
2024-04-19 19:01:19 +04:00
|
|
|
|
{
|
2024-04-19 19:56:36 +04:00
|
|
|
|
try
|
2024-04-19 19:01:19 +04:00
|
|
|
|
{
|
2024-05-12 14:52:01 +04:00
|
|
|
|
collection.Insert(ship);
|
2024-04-19 19:56:36 +04:00
|
|
|
|
}
|
2024-05-12 14:52:01 +04:00
|
|
|
|
catch (Exception ex)
|
2024-04-19 19:56:36 +04:00
|
|
|
|
{
|
2024-05-12 14:52:01 +04:00
|
|
|
|
throw new FileFormatException(filename, ex);
|
2024-04-19 19:01:19 +04:00
|
|
|
|
}
|
2024-04-12 17:12:59 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-12 14:52:01 +04:00
|
|
|
|
|
|
|
|
|
_storages.Add(collectionInfo, collection);
|
2024-04-12 17:12:59 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-05 11:40:18 +04:00
|
|
|
|
|
2024-04-12 17:12:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание коллекции по типу
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collectionType"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static ICollectionGenericObjects<T>? CreateCollection(CollectionType collectionType)
|
|
|
|
|
{
|
|
|
|
|
return collectionType switch
|
|
|
|
|
{
|
|
|
|
|
CollectionType.Massive => new MassiveGenericObjects<T>(),
|
|
|
|
|
CollectionType.List => new ListGenericObjects<T>(),
|
|
|
|
|
_ => null,
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-03-23 14:01:50 +04:00
|
|
|
|
}
|