PIbd-22_Shabunov_O.A._AirBo.../AirBomber/Generics/EntitiesGenericStorage.cs
2023-11-25 23:59:45 +04:00

119 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AirBomber.MovementStrategy;
using AirBomber.Rendering;
using System.Text;
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;
private readonly char _keyValueDelimiter = '|';
private readonly char _recordsDelimiter = ';';
private readonly char _entityDelimiter = ':';
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];
}
}
public void SaveData(string FileName)
{
if (_entityStorages.Count == 0)
throw new Exception("Невалидная операция: нет данных для сохранения");
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}");
}
}
}
public void LoadData(string FileName)
{
if (!File.Exists(FileName))
throw new Exception("Файл не найден");
using (StreamReader reader = new StreamReader(FileName))
{
if (reader.ReadLine() != "BomberStorage")
throw new Exception("Неверный формат данных");
_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)
{
if ((Collection + Renderer) == -1)
throw new Exception("Ошибка добавления в коллекцию");
}
}
_entityStorages.Add(Record[0], Collection);
}
}
}
}
}