156 lines
6.0 KiB
C#
156 lines
6.0 KiB
C#
using ProjectLainer.DrawningObjects;
|
|
using ProjectLainer.Entities;
|
|
using ProjectLainer.MovementStrategy;
|
|
using System.Text;
|
|
|
|
namespace ProjectLainer.Generics
|
|
{
|
|
internal class LainersGenericStorage
|
|
{
|
|
|
|
readonly Dictionary<LainersCollectionInfo, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>> _lainerStorages;
|
|
|
|
public List<LainersCollectionInfo> Keys => _lainerStorages.Keys.ToList();
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
private readonly int _pictureHeight;
|
|
public LainersGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_lainerStorages = new Dictionary<LainersCollectionInfo, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
|
|
public void AddSet(string name)
|
|
{
|
|
//проверка
|
|
var addItem = new LainersCollectionInfo(name, null);
|
|
foreach(LainersCollectionInfo elem in Keys)
|
|
{
|
|
if (addItem.Equals(elem))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
_lainerStorages.Add(new LainersCollectionInfo(name, null), new LainersGenericCollection<DrawingEntity, DrawningObjectLainer>(_pictureWidth, _pictureHeight));
|
|
}
|
|
|
|
public void DelSet(string name)
|
|
{
|
|
var deleteItem = new LainersCollectionInfo(name, null);
|
|
foreach (LainersCollectionInfo elem in Keys)
|
|
{
|
|
if (deleteItem.Equals(elem))
|
|
{
|
|
_lainerStorages.Remove(elem);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
public LainersGenericCollection<DrawingEntity, DrawningObjectLainer>? this[string ind]
|
|
{
|
|
get
|
|
{
|
|
var getItem = new LainersCollectionInfo(ind, null);
|
|
foreach(LainersCollectionInfo elem in Keys)
|
|
{
|
|
if (getItem.Equals(elem))
|
|
{
|
|
return _lainerStorages[elem];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
private readonly char _separatorRecords = ';';
|
|
private static readonly char _separatorForObject = ':';
|
|
public void SaveData(string filename)
|
|
{
|
|
if (File.Exists(filename))
|
|
{
|
|
File.Delete(filename);
|
|
}
|
|
StringBuilder data = new();
|
|
foreach (KeyValuePair<LainersCollectionInfo, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>> record in _lainerStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawingEntity? elem in record.Value.GetLainers)
|
|
{
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
}
|
|
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
|
|
}
|
|
if (data.Length == 0)
|
|
{
|
|
throw new ArgumentException("Невалиданя операция, нет данных для сохранения");
|
|
}
|
|
using FileStream fs = new(filename, FileMode.Create);
|
|
byte[] info = new
|
|
UTF8Encoding(true).GetBytes($"LainerStorage{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 NullReferenceException("Нет данных для загрузки");
|
|
}
|
|
if (!strs[0].StartsWith("CarStorage"))
|
|
{
|
|
//если нет такой записи, то это не те данные
|
|
throw new ArgumentException("Неверный формат данных");
|
|
}
|
|
_lainerStorages.Clear();
|
|
foreach (string data in strs)
|
|
{
|
|
string[] record = data.Split(_separatorForKeyValue,
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 2)
|
|
{
|
|
continue;
|
|
}
|
|
LainersGenericCollection<DrawingEntity, DrawningObjectLainer>
|
|
collection = new(_pictureWidth, _pictureHeight);
|
|
string[] set = record[1].Split(_separatorRecords,
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
DrawingEntity? lainer =
|
|
elem?.CreateDrawningLainer(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (lainer != null)
|
|
{
|
|
if (!(collection + lainer))
|
|
{
|
|
throw new OverflowException("Ошибка добавления в коллекцию");
|
|
}
|
|
}
|
|
}
|
|
_lainerStorages.Add(new LainersCollectionInfo(record[0],
|
|
string.Empty), collection);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|