160 lines
6.4 KiB
C#
160 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
using ProjectGasolineTanker.Drawings;
|
|
using ProjectGasolineTanker.Generics;
|
|
using ProjectGasolineTanker.MovementStratg;
|
|
|
|
namespace ProjectGasolineTanker.Generic
|
|
{
|
|
internal class TruckGenericStorage
|
|
{
|
|
//Словарь (хранилище)
|
|
readonly Dictionary<TruckCollectionInfo, TruckGenericCollection<DrawingTruck, DrawingObjectTruck>> _TruckStorages;
|
|
//Возвращение списка названий наборов
|
|
public List<TruckCollectionInfo> Keys => _TruckStorages.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 TruckGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_TruckStorages = new Dictionary<TruckCollectionInfo, TruckGenericCollection<DrawingTruck, DrawingObjectTruck>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
|
|
// Добавление набора
|
|
public void AddSet(string name)
|
|
{
|
|
TruckCollectionInfo set = new TruckCollectionInfo(name, string.Empty);
|
|
if (_TruckStorages.ContainsKey(set))
|
|
return;
|
|
_TruckStorages.Add(set, new TruckGenericCollection<DrawingTruck, DrawingObjectTruck>(_pictureWidth, _pictureHeight));
|
|
}
|
|
|
|
// Удаление набора
|
|
public void DelSet(string name)
|
|
{
|
|
TruckCollectionInfo set = new TruckCollectionInfo(name, string.Empty);
|
|
// TODO Прописать логику для удаления
|
|
if (!_TruckStorages.ContainsKey(set))
|
|
{
|
|
return;
|
|
}
|
|
_TruckStorages.Remove(set);
|
|
}
|
|
|
|
// Доступ к набору
|
|
public TruckGenericCollection<DrawingTruck, DrawingObjectTruck>? this[string ind]
|
|
{
|
|
get
|
|
{
|
|
TruckCollectionInfo set = new TruckCollectionInfo(ind, string.Empty);
|
|
// TODO Продумать логику получения набора
|
|
if (!_TruckStorages.ContainsKey(set))
|
|
{
|
|
return null;
|
|
}
|
|
return _TruckStorages[set];
|
|
}
|
|
}
|
|
|
|
public void SaveData(string filename)
|
|
{
|
|
if (File.Exists(filename))
|
|
{
|
|
File.Delete(filename);
|
|
}
|
|
StringBuilder data = new();
|
|
foreach (KeyValuePair<TruckCollectionInfo,
|
|
TruckGenericCollection<DrawingTruck, DrawingObjectTruck>> record in _TruckStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawingTruck? elem in record.Value.GetTruck)
|
|
{
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
}
|
|
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
|
|
}
|
|
if (data.Length == 0)
|
|
{
|
|
throw new Exception("Невалиданя операция, нет данных для сохранения");
|
|
}
|
|
using FileStream fs = new(filename, FileMode.Create);
|
|
byte[] info = new
|
|
UTF8Encoding(true).GetBytes($"CarStorage{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("TruckStorage"))
|
|
{
|
|
//если нет такой записи, то это не те данные
|
|
throw new Exception("Неверный формат данных");
|
|
}
|
|
_TruckStorages.Clear();
|
|
foreach (string data in strs)
|
|
{
|
|
string[] record = data.Split(_separatorForKeyValue,
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 2)
|
|
{
|
|
continue;
|
|
}
|
|
TruckGenericCollection<DrawingTruck, DrawingObjectTruck>
|
|
collection = new(_pictureWidth, _pictureHeight);
|
|
string[] set = record[1].Split(_separatorRecords,
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
DrawingTruck? Truck =
|
|
elem?.CreateDrawingTruck(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (Truck != null)
|
|
{
|
|
if ((collection + Truck)==-1)
|
|
{
|
|
throw new Exception("Ошибка добавления в коллекцию");
|
|
}
|
|
}
|
|
}
|
|
_TruckStorages.Add(new TruckCollectionInfo(record[0],
|
|
string.Empty), collection);
|
|
}
|
|
}
|
|
}
|
|
}
|