2023-11-19 17:56:27 +04:00
|
|
|
|
using AirBomber.MovementStrategy;
|
|
|
|
|
using AirBomber.Rendering;
|
2023-11-19 18:34:46 +04:00
|
|
|
|
using System.Text;
|
2023-11-19 17:56:27 +04:00
|
|
|
|
|
|
|
|
|
namespace AirBomber.Generics
|
|
|
|
|
{
|
|
|
|
|
internal class EntitiesGenericStorage
|
|
|
|
|
{
|
|
|
|
|
readonly Dictionary<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>> _entityStorages;
|
|
|
|
|
|
|
|
|
|
public List<string> Keys => _entityStorages.Keys.ToList();
|
|
|
|
|
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
|
2023-11-19 18:34:46 +04:00
|
|
|
|
private readonly char _keyValueDelimiter = '|';
|
|
|
|
|
private readonly char _recordsDelimiter = ';';
|
|
|
|
|
private readonly char _entityDelimiter = ':';
|
|
|
|
|
|
2023-11-19 17:56:27 +04:00
|
|
|
|
public EntitiesGenericStorage(int PictureWidth, int PictureHeight)
|
|
|
|
|
{
|
|
|
|
|
_entityStorages = new Dictionary<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>>();
|
|
|
|
|
|
|
|
|
|
_pictureWidth = PictureWidth;
|
|
|
|
|
_pictureHeight = PictureHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddSet(string Name)
|
|
|
|
|
{
|
|
|
|
|
var NewCollection =
|
|
|
|
|
new EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>(_pictureWidth, _pictureHeight);
|
|
|
|
|
|
|
|
|
|
if (Keys.Contains(Name))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Набор с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_entityStorages.Add(Name, NewCollection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveSet(string Name)
|
|
|
|
|
{
|
|
|
|
|
_entityStorages.Remove(Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>? this[string Index]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!_entityStorages.ContainsKey(Index))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return _entityStorages[Index];
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-19 18:34:46 +04:00
|
|
|
|
|
|
|
|
|
public bool SaveData(string FileName)
|
|
|
|
|
{
|
|
|
|
|
if (_entityStorages.Count == 0)
|
2023-11-25 23:46:44 +04:00
|
|
|
|
throw new Exception("Невалиданя операция, нет данных для сохранения");
|
2023-11-19 18:34:46 +04:00
|
|
|
|
|
|
|
|
|
using (StreamWriter writer = new StreamWriter(FileName, false))
|
|
|
|
|
{
|
|
|
|
|
writer.WriteLine("BomberStorage");
|
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>> Record in _entityStorages)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder Records = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
foreach (BomberRendererBase? Element in Record.Value.Entities)
|
|
|
|
|
Records.Append($"{Element?.SerializeRenderer(_entityDelimiter)}{_recordsDelimiter}");
|
|
|
|
|
|
|
|
|
|
writer.WriteLine($"{Record.Key}{_keyValueDelimiter}{Records}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool LoadData(string FileName)
|
|
|
|
|
{
|
2023-11-25 23:27:05 +04:00
|
|
|
|
if (!File.Exists(FileName))
|
2023-11-25 23:46:44 +04:00
|
|
|
|
throw new Exception("Файл не найден");
|
2023-11-25 23:27:05 +04:00
|
|
|
|
|
2023-11-19 18:34:46 +04:00
|
|
|
|
using (StreamReader reader = new StreamReader(FileName))
|
|
|
|
|
{
|
|
|
|
|
if (reader.ReadLine() != "BomberStorage")
|
2023-11-25 23:46:44 +04:00
|
|
|
|
throw new Exception("Неверный формат данных");
|
2023-11-19 18:34:46 +04:00
|
|
|
|
|
|
|
|
|
_entityStorages.Clear();
|
|
|
|
|
|
|
|
|
|
string? Data;
|
|
|
|
|
while ((Data = reader.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
string[] Record = Data.Split(_keyValueDelimiter, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
if (Record.Length != 2)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer> Collection = new(_pictureWidth, _pictureHeight);
|
|
|
|
|
string[] Set = Record[1].Split(_recordsDelimiter, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
foreach (string Element in Set)
|
|
|
|
|
{
|
|
|
|
|
BomberRendererBase? Renderer = Element?.DeserializeRenderer(_entityDelimiter, _pictureWidth, _pictureHeight);
|
|
|
|
|
|
|
|
|
|
if (Renderer != null)
|
|
|
|
|
{
|
2023-11-25 23:09:40 +04:00
|
|
|
|
if ((Collection + Renderer) == -1)
|
2023-11-25 23:46:44 +04:00
|
|
|
|
throw new Exception("Ошибка добавления в коллекцию");
|
2023-11-19 18:34:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_entityStorages.Add(Record[0], Collection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-11-19 17:56:27 +04:00
|
|
|
|
}
|
|
|
|
|
}
|