2023-12-13 18:22:58 +04:00
|
|
|
|
using ProjectBulldozer.Drawing;
|
|
|
|
|
using ProjectBulldozer.Drawning;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2023-11-30 02:42:08 +04:00
|
|
|
|
namespace ProjectBulldozer.Generics
|
|
|
|
|
{
|
|
|
|
|
internal class TractorGenericStorage
|
|
|
|
|
{
|
2023-12-13 18:22:58 +04:00
|
|
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
|
|
|
private readonly char _separatorRecords = ';';
|
|
|
|
|
private static readonly char _separatorForObject = ':';
|
2023-12-29 06:20:55 +04:00
|
|
|
|
readonly Dictionary<TractorsCollectionInfo, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>> _TractorsStorage;
|
|
|
|
|
public List<TractorsCollectionInfo> Keys => _TractorsStorage.Keys.ToList();
|
2023-11-30 03:15:19 +04:00
|
|
|
|
private readonly int _pictureWidth;
|
2023-11-30 02:42:08 +04:00
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
public TractorGenericStorage(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
2023-12-29 06:20:55 +04:00
|
|
|
|
_TractorsStorage = new Dictionary<TractorsCollectionInfo, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>>();
|
2023-11-30 02:42:08 +04:00
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
public void AddSet(string name)
|
|
|
|
|
{
|
2023-12-29 06:20:55 +04:00
|
|
|
|
if (!_TractorsStorage.ContainsKey(new TractorsCollectionInfo(name, "")))
|
2023-11-30 02:42:08 +04:00
|
|
|
|
{
|
2023-12-29 06:20:55 +04:00
|
|
|
|
_TractorsStorage.Add(new TractorsCollectionInfo(name, ""), new TractorGenericCollection<DrawingTractor, DrawingObjectTractor>(_pictureWidth, _pictureHeight));
|
2023-11-30 02:42:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void DelSet(string name)
|
|
|
|
|
{
|
2023-12-29 06:20:55 +04:00
|
|
|
|
if (_TractorsStorage.ContainsKey(new TractorsCollectionInfo(name, "")))
|
2023-11-30 02:42:08 +04:00
|
|
|
|
{
|
2023-12-29 06:20:55 +04:00
|
|
|
|
_TractorsStorage.Remove(new TractorsCollectionInfo(name, ""));
|
2023-11-30 02:42:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-13 18:22:58 +04:00
|
|
|
|
public TractorGenericCollection<DrawingTractor, DrawingObjectTractor>? this[string ind]
|
2023-11-30 02:42:08 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-12-29 06:20:55 +04:00
|
|
|
|
var info = new TractorsCollectionInfo(ind, "");
|
|
|
|
|
if (_TractorsStorage.ContainsKey(info)) return _TractorsStorage[info];
|
2023-11-30 03:15:19 +04:00
|
|
|
|
return null;
|
2023-11-30 02:42:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-13 18:22:58 +04:00
|
|
|
|
public bool SaveData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
StringBuilder data = new();
|
2023-12-29 06:20:55 +04:00
|
|
|
|
foreach (KeyValuePair<TractorsCollectionInfo, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>> record in _TractorsStorage)
|
2023-12-13 18:22:58 +04:00
|
|
|
|
{
|
|
|
|
|
StringBuilder records = new();
|
|
|
|
|
foreach (DrawingTractor? elem in record.Value.GetTractors)
|
|
|
|
|
{
|
|
|
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
|
|
|
}
|
|
|
|
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
|
|
|
}
|
|
|
|
|
if (data.Length == 0)
|
|
|
|
|
{
|
2023-12-29 03:16:00 +04:00
|
|
|
|
throw new InvalidOperationException("Невалиданя операция, нет данных для сохранения");
|
2023-12-13 18:22:58 +04:00
|
|
|
|
}
|
|
|
|
|
using StreamWriter sw = new(filename);
|
|
|
|
|
sw.Write($"TractorsStorage{Environment.NewLine}{data}");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public bool LoadData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2023-12-29 03:16:00 +04:00
|
|
|
|
throw new FileNotFoundException("Файл не найден");
|
2023-12-13 18:22:58 +04:00
|
|
|
|
}
|
|
|
|
|
using (StreamReader sr = File.OpenText(filename))
|
|
|
|
|
{
|
|
|
|
|
string str = sr.ReadLine();
|
|
|
|
|
if (str == null || str.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!str.StartsWith("TractorsStorage"))
|
|
|
|
|
{
|
2023-12-29 03:16:00 +04:00
|
|
|
|
throw new FormatException("Неверный формат данных");
|
2023-12-13 18:22:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_TractorsStorage.Clear();
|
|
|
|
|
string strs = "";
|
|
|
|
|
|
|
|
|
|
while ((strs = sr.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
if (strs == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (record.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
TractorGenericCollection<DrawingTractor, DrawingObjectTractor> collection =
|
|
|
|
|
new(_pictureWidth, _pictureHeight);
|
|
|
|
|
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (string elem in set)
|
|
|
|
|
{
|
|
|
|
|
DrawingTractor? tractor = elem?.CreateDrawingTractor(_separatorForObject,
|
|
|
|
|
_pictureWidth, _pictureHeight);
|
|
|
|
|
if (tractor != null)
|
|
|
|
|
{
|
2023-12-29 06:20:55 +04:00
|
|
|
|
if ((collection + tractor)==-1)
|
2023-12-13 18:22:58 +04:00
|
|
|
|
{
|
2023-12-29 03:16:00 +04:00
|
|
|
|
throw new ApplicationException("Ошибка добавления в коллекцию");
|
2023-12-13 18:22:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-29 06:20:55 +04:00
|
|
|
|
_TractorsStorage.Add(new TractorsCollectionInfo(record[0], string.Empty), collection);
|
2023-12-13 18:22:58 +04:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-30 02:42:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|