Добавлена сериализация (почти)

This commit is contained in:
ShabOl 2023-11-19 18:34:46 +04:00
parent 344fbdbca8
commit 44c20bbcfe
3 changed files with 194 additions and 0 deletions

View File

@ -15,6 +15,8 @@ namespace AirBomber.Generics
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
public IEnumerable<T?> Entities => _collection.GetEntities();
public EntitiesGenericCollection(int PictureWidth, int PictureHeight) public EntitiesGenericCollection(int PictureWidth, int PictureHeight)
{ {
int width = PictureWidth / _placeSizeWidth; int width = PictureWidth / _placeSizeWidth;

View File

@ -1,5 +1,6 @@
using AirBomber.MovementStrategy; using AirBomber.MovementStrategy;
using AirBomber.Rendering; using AirBomber.Rendering;
using System.Text;
namespace AirBomber.Generics namespace AirBomber.Generics
{ {
@ -12,6 +13,10 @@ namespace AirBomber.Generics
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
private readonly char _keyValueDelimiter = '|';
private readonly char _recordsDelimiter = ';';
private readonly char _entityDelimiter = ':';
public EntitiesGenericStorage(int PictureWidth, int PictureHeight) public EntitiesGenericStorage(int PictureWidth, int PictureHeight)
{ {
_entityStorages = new Dictionary<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>>(); _entityStorages = new Dictionary<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>>();
@ -49,5 +54,136 @@ namespace AirBomber.Generics
return _entityStorages[Index]; return _entityStorages[Index];
} }
} }
public bool SaveData(string FileName)
{
//if (File.Exists(FileName))
// File.Delete(FileName);
if (_entityStorages.Count == 0)
return false;
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}");
}
}
//StringBuilder Data = new();
//foreach (KeyValuePair<string, EntitiesGenericCollection<RendererBase, ObjectEntityRenderer>> Record in _entityStorages)
//{
// StringBuilder Records = new StringBuilder();
//
// foreach (RendererBase? Element in Record.Value.Entities)
// Records.Append($"{Element?.SerializeRenderer(_entityDelimiter)}{_recordsDelimiter}");
//
// Data.AppendLine($"{Record.Key}{_keyValueDelimiter}{Records}");
//}
//if (Data.Length == 0)
// return false;
//using FileStream fs = new(FileName, FileMode.Create);
//byte[] info = new
//UTF8Encoding(true).GetBytes($"BomberStorage{Environment.NewLine}{Data}");
//fs.Write(info, 0, info.Length);
return true;
}
public bool LoadData(string FileName)
{
if (!File.Exists(FileName))
return false;
using (StreamReader reader = new StreamReader(FileName))
{
if (reader.ReadLine() != "BomberStorage")
return false;
_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)
return false;
}
}
_entityStorages.Add(Record[0], Collection);
}
}
//string Buffer = string.Empty;
//using (FileStream fs = new(FileName, FileMode.Open))
//{
// byte[] b = new byte[fs.Length];
// UTF8Encoding temp = new(true);
//
// while (fs.Read(b, 0, b.Length) > 0)
// Buffer += temp.GetString(b);
//}
//
//var strs = Buffer.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
//
//if (strs == null || strs.Length == 0)
// return false;
//
//if (!strs[0].StartsWith("BomberStorage"))
// //если нет такой записи, то это не те данные
// return false;
//
//_entityStorages.Clear();
//
//foreach (string Data in strs)
//{
// string[] Record = Data.Split(_keyValueDelimiter, StringSplitOptions.RemoveEmptyEntries);
//
// if (Record.Length != 2)
// continue;
//
// EntitiesGenericCollection<RendererBase, ObjectEntityRenderer> Collection = new(_pictureWidth, _pictureHeight);
// string[] Set = Record[1].Split(_recordsDelimiter, StringSplitOptions.RemoveEmptyEntries);
//
// foreach (string Element in Set)
// {
// RendererBase? Renderer = Element?.DeserializeRenderer(_entityDelimiter, _pictureWidth, _pictureHeight);
//
// if (Renderer != null)
// {
// if ((Collection + Renderer) != -1)
// return false;
// }
// }
//
// _entityStorages.Add(Record[0], Collection);
//}
return true;
}
} }
} }

View File

@ -0,0 +1,56 @@
using AirBomber.Entities;
namespace AirBomber.Rendering
{
public static class ExtensionBomberRenderer
{
public static BomberRendererBase? DeserializeRenderer(this string Info, char Delimiter, int Width, int Height)
{
string[] Properties = Info.Split(Delimiter);
if (Properties.Length == 3)
{
return new BomberRendererBase(
Convert.ToInt32(Properties[0]),
Convert.ToInt32(Properties[1]),
Color.FromName(Properties[2]),
Width,
Height
);
}
if (Properties.Length == 6)
{
return new BomberRenderer(
Convert.ToInt32(Properties[0]),
Convert.ToInt32(Properties[1]),
Color.FromName(Properties[2]),
Color.FromName(Properties[3]),
Convert.ToBoolean(Properties[4]),
Convert.ToBoolean(Properties[5]),
Width,
Height
);
}
return null;
}
public static string SerializeRenderer(this BomberRendererBase Renderer, char Delimiter)
{
var Bomber = Renderer.EntityBomber;
if (Bomber is null)
return string.Empty;
string ResultString = string.Empty;
ResultString += $"{Bomber.Speed}{Delimiter}{Bomber.Weight}{Delimiter}{Bomber.BodyColor.Name}";
if (Bomber is BomberEntity AdvancedBomber)
ResultString += $"{Delimiter}{AdvancedBomber.AdditionalColor.Name}" +
$"{Delimiter}{AdvancedBomber.Bombs}{Delimiter}{AdvancedBomber.FuelTanks}";
return ResultString;
}
}
}