PIbd-22_Fedorenko_G.Y._Hydr.../Hydroplane/PlanesGenericStorage.cs

130 lines
4.5 KiB
C#
Raw Permalink Normal View History

2023-10-25 10:59:54 +04:00
using Hydroplane.DrawningObjects;
using Hydroplane.MovementStrategy;
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 = ':';
public bool SaveData(string filename)
{
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)
{
return false;
}
using (StreamWriter writer = new StreamWriter(filename))
{
writer.Write($"PlaneStorage{Environment.NewLine}{data}");
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
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-11-22 09:46:13 +04:00
return false;
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-11-22 09:46:13 +04:00
return false;
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)
{
return false;
}
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))
{
return false;
}
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
return true;
2023-11-18 18:35:07 +04:00
}
2023-11-22 09:46:13 +04:00
}
2023-10-25 10:59:54 +04:00
}
}