143 lines
5.6 KiB
C#
Raw Permalink Normal View History

2023-12-19 18:48:02 +04:00
using ProjectLainer.DrawningObjects;
2023-12-22 19:20:28 +04:00
using ProjectLainer.Entities;
2023-12-19 18:48:02 +04:00
using ProjectLainer.MovementStrategy;
2023-12-21 21:20:07 +04:00
using System.Text;
2023-12-19 18:48:02 +04:00
namespace ProjectLainer.Generics
{
2023-12-22 19:34:17 +04:00
internal class LainersGenericStorage
2023-12-19 18:48:02 +04:00
{
2023-12-21 21:20:07 +04:00
2023-12-22 19:20:28 +04:00
readonly Dictionary<LainersCollectionInfo, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>> _lainerStorages;
2023-12-21 21:20:07 +04:00
2023-12-22 19:20:28 +04:00
public List<LainersCollectionInfo> Keys => _lainerStorages.Keys.ToList();
2023-12-21 21:20:07 +04:00
2023-12-19 18:48:02 +04:00
private readonly int _pictureWidth;
2023-12-21 21:20:07 +04:00
2023-12-19 18:48:02 +04:00
private readonly int _pictureHeight;
public LainersGenericStorage(int pictureWidth, int pictureHeight)
{
2023-12-22 19:20:28 +04:00
_lainerStorages = new Dictionary<LainersCollectionInfo, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>>();
2023-12-19 18:48:02 +04:00
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
2023-12-21 21:20:07 +04:00
2023-12-19 18:48:02 +04:00
public void AddSet(string name)
{
2023-12-22 19:20:28 +04:00
//проверка
var addItem = new LainersCollectionInfo(name, null);
2023-12-22 19:34:17 +04:00
foreach (LainersCollectionInfo elem in Keys)
2023-12-22 19:20:28 +04:00
{
if (addItem.Equals(elem))
{
return;
}
}
_lainerStorages.Add(new LainersCollectionInfo(name, null), new LainersGenericCollection<DrawingEntity, DrawningObjectLainer>(_pictureWidth, _pictureHeight));
2023-12-19 18:48:02 +04:00
}
2023-12-21 21:20:07 +04:00
2023-12-19 18:48:02 +04:00
public void DelSet(string name)
{
2023-12-22 19:20:28 +04:00
var deleteItem = new LainersCollectionInfo(name, null);
foreach (LainersCollectionInfo elem in Keys)
2023-12-19 18:48:02 +04:00
{
2023-12-22 19:20:28 +04:00
if (deleteItem.Equals(elem))
{
_lainerStorages.Remove(elem);
}
2023-12-19 18:48:02 +04:00
}
2023-12-22 19:20:28 +04:00
return;
2023-12-19 18:48:02 +04:00
}
2023-12-21 21:20:07 +04:00
2023-12-19 18:48:02 +04:00
public LainersGenericCollection<DrawingEntity, DrawningObjectLainer>? this[string ind]
{
get
{
2023-12-22 19:20:28 +04:00
var getItem = new LainersCollectionInfo(ind, null);
2023-12-22 19:34:17 +04:00
foreach (LainersCollectionInfo elem in Keys)
2023-12-19 18:48:02 +04:00
{
2023-12-22 19:20:28 +04:00
if (getItem.Equals(elem))
{
return _lainerStorages[elem];
}
2023-12-19 18:48:02 +04:00
}
return null;
}
}
2023-12-19 20:12:51 +04:00
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
2023-12-21 21:20:07 +04:00
public void SaveData(string filename)
2023-12-19 20:12:51 +04:00
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
2023-12-22 19:34:17 +04:00
foreach (KeyValuePair<LainersCollectionInfo, LainersGenericCollection<DrawingLainer, DrawningObjectLainer>> record in _lainerStorages)
2023-12-19 20:12:51 +04:00
{
StringBuilder records = new();
2023-12-22 19:34:17 +04:00
foreach (DrawingLainer? elem in record.Value.GetLainers)
2023-12-19 20:12:51 +04:00
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
2023-12-22 19:20:28 +04:00
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
2023-12-19 20:12:51 +04:00
}
if (data.Length == 0)
{
2023-12-21 21:20:07 +04:00
throw new ArgumentException("Невалиданя операция, нет данных для сохранения");
2023-12-19 20:12:51 +04:00
}
2023-12-22 19:34:17 +04:00
using (StreamWriter sw = new(filename))
{
sw.WriteLine($"LainerStorage{Environment.NewLine}{data}");
}
2023-12-19 20:12:51 +04:00
}
2023-12-21 21:20:07 +04:00
public void LoadData(string filename)
2023-12-19 20:12:51 +04:00
{
if (!File.Exists(filename))
{
2023-12-22 19:34:17 +04:00
throw new NullReferenceException("Файл не найден");
2023-12-19 20:12:51 +04:00
}
2023-12-22 19:34:17 +04:00
using (StreamReader sr = new(filename))
2023-12-19 20:12:51 +04:00
{
2023-12-22 19:34:17 +04:00
string str = sr.ReadLine();
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
2023-12-19 20:12:51 +04:00
{
2023-12-22 19:34:17 +04:00
throw new NullReferenceException("нет данных для загрузки");
2023-12-19 20:12:51 +04:00
}
2023-12-22 19:34:17 +04:00
if (!strs[0].StartsWith("LainerStorage"))
2023-12-19 20:12:51 +04:00
{
2023-12-22 19:34:17 +04:00
throw new ArgumentException("неверный фориат данных");
2023-12-19 20:12:51 +04:00
}
2023-12-22 19:34:17 +04:00
_lainerStorages.Clear();
do
2023-12-19 20:12:51 +04:00
{
2023-12-22 19:34:17 +04:00
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = sr.ReadLine();
continue;
}
LainersGenericCollection<DrawingLainer, DrawningObjectLainer> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
2023-12-19 20:12:51 +04:00
{
2023-12-22 19:34:17 +04:00
DrawingLainer? lainer = elem?.CreateDrawningLainer(_separatorForObject, _pictureWidth, _pictureHeight);
if (lainer != null)
2023-12-19 20:12:51 +04:00
{
2023-12-22 19:34:17 +04:00
if (!(collection + lainer))
{
throw new IOException("Ошибка добавления в коллекцию");
}
2023-12-19 20:12:51 +04:00
}
}
2023-12-22 19:34:17 +04:00
_lainerStorages.Add(new LainerCollectionInfo(record[0], null), collection);
str = sr.ReadLine();
} while (str != null);
2023-12-19 20:12:51 +04:00
}
}
2023-12-19 18:48:02 +04:00
}
}