2023-10-25 10:59:54 +04:00
|
|
|
|
using Hydroplane.DrawningObjects;
|
|
|
|
|
using Hydroplane.MovementStrategy;
|
2023-12-05 19:12:45 +04:00
|
|
|
|
using Hydroplane.Exceptions;
|
2023-10-25 10:59:54 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-11-22 09:46:13 +04:00
|
|
|
|
using System.DirectoryServices.ActiveDirectory;
|
2023-10-25 10:59:54 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Hydroplane.Generics
|
|
|
|
|
{
|
|
|
|
|
internal class PlanesGenericStorage
|
|
|
|
|
{
|
|
|
|
|
readonly Dictionary<string, PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>> _planeStorages;
|
|
|
|
|
public List<string> Keys => _planeStorages.Keys.ToList();
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
public PlanesGenericStorage(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
|
|
|
|
_planeStorages = new Dictionary<string, PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>>();
|
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
public void AddSet(string name)
|
|
|
|
|
{
|
|
|
|
|
if (_planeStorages.ContainsKey(name)) return;
|
|
|
|
|
_planeStorages[name] = new PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>(_pictureWidth, _pictureHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DelSet(string name)
|
|
|
|
|
{
|
|
|
|
|
if (!_planeStorages.ContainsKey(name)) return;
|
|
|
|
|
_planeStorages.Remove(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>?
|
|
|
|
|
this[string ind]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_planeStorages.ContainsKey(ind)) return _planeStorages[ind];
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-18 18:35:07 +04:00
|
|
|
|
|
|
|
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
|
|
|
private readonly char _separatorRecords = ';';
|
|
|
|
|
private static readonly char _separatorForObject = ':';
|
|
|
|
|
|
2023-12-05 19:12:45 +04:00
|
|
|
|
public void SaveData(string filename)
|
2023-11-18 18:35:07 +04:00
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
StringBuilder data = new();
|
|
|
|
|
foreach (KeyValuePair<string, PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>> record in _planeStorages)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder records = new();
|
|
|
|
|
foreach (DrawningPlane? elem in record.Value.GetPlanes)
|
|
|
|
|
{
|
|
|
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
|
|
|
}
|
|
|
|
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
|
|
|
}
|
|
|
|
|
if (data.Length == 0)
|
|
|
|
|
{
|
2023-12-05 19:12:45 +04:00
|
|
|
|
throw new ArgumentException("Invalid operation, there isn't any data");
|
2023-11-18 18:35:07 +04:00
|
|
|
|
}
|
|
|
|
|
using (StreamWriter writer = new StreamWriter(filename))
|
|
|
|
|
{
|
|
|
|
|
writer.Write($"PlaneStorage{Environment.NewLine}{data}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 19:12:45 +04:00
|
|
|
|
public void LoadData(string filename)
|
2023-11-18 18:35:07 +04:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2023-12-05 19:12:45 +04:00
|
|
|
|
throw new FileNotFoundException("File not found");
|
2023-11-18 18:35:07 +04:00
|
|
|
|
}
|
2023-11-22 09:46:13 +04:00
|
|
|
|
|
|
|
|
|
using (StreamReader fs = File.OpenText(filename))
|
2023-11-18 18:35:07 +04:00
|
|
|
|
{
|
2023-11-22 09:46:13 +04:00
|
|
|
|
string str = fs.ReadLine();
|
|
|
|
|
if (str == null || str.Length == 0)
|
2023-11-18 18:35:07 +04:00
|
|
|
|
{
|
2023-12-05 19:12:45 +04:00
|
|
|
|
throw new ArgumentException("There isn't any data for load");
|
2023-11-18 18:35:07 +04:00
|
|
|
|
}
|
2023-11-22 09:46:13 +04:00
|
|
|
|
if (!str.StartsWith("PlaneStorage"))
|
2023-11-18 18:35:07 +04:00
|
|
|
|
{
|
2023-12-05 19:12:45 +04:00
|
|
|
|
throw new InvalidDataException("Invalid format of data");
|
2023-11-18 18:35:07 +04:00
|
|
|
|
}
|
2023-11-22 09:46:13 +04:00
|
|
|
|
|
|
|
|
|
_planeStorages.Clear();
|
|
|
|
|
string strs = "";
|
|
|
|
|
|
|
|
|
|
while ((strs = fs.ReadLine()) != null)
|
2023-11-18 18:35:07 +04:00
|
|
|
|
{
|
2023-11-22 09:46:13 +04:00
|
|
|
|
if (strs == null)
|
|
|
|
|
{
|
2023-12-05 19:12:45 +04:00
|
|
|
|
return;
|
2023-11-22 09:46:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (record.Length != 2)
|
2023-11-18 18:35:07 +04:00
|
|
|
|
{
|
2023-11-22 09:46:13 +04:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
PlanesGenericCollection<DrawningPlane, DrawningObjectPlane> collection = new(_pictureWidth, _pictureHeight);
|
|
|
|
|
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (string elem in set)
|
|
|
|
|
{
|
|
|
|
|
DrawningPlane? plane = elem?.CreateDrawningPlane(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
|
|
|
if (plane != null)
|
2023-11-18 18:35:07 +04:00
|
|
|
|
{
|
2023-11-22 09:46:13 +04:00
|
|
|
|
if (!(collection + plane))
|
|
|
|
|
{
|
2023-12-05 19:12:45 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_ = collection + plane;
|
|
|
|
|
}
|
|
|
|
|
catch (PlaneNotFoundException e)
|
|
|
|
|
{
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
catch (StorageOverflowException e)
|
|
|
|
|
{
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2023-11-22 09:46:13 +04:00
|
|
|
|
}
|
2023-11-18 18:35:07 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-22 09:46:13 +04:00
|
|
|
|
_planeStorages.Add(record[0], collection);
|
2023-11-18 18:35:07 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-22 09:46:13 +04:00
|
|
|
|
}
|
2023-10-25 10:59:54 +04:00
|
|
|
|
}
|
|
|
|
|
}
|