133 lines
4.5 KiB
C#
133 lines
4.5 KiB
C#
using Lab.DrawningObjects;
|
|
using Lab.MovementStrategy;
|
|
using Lab.Generics;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Lab.Generics
|
|
{
|
|
internal class CarsGenericStorage
|
|
{
|
|
readonly Dictionary<string, CarsGenericCollection<DrawTanker, DrawingObjectTanker>> _carStorages;
|
|
public List<string> Keys => _carStorages.Keys.ToList();
|
|
private readonly int _pictureWidth;
|
|
private readonly int _pictureHeight;
|
|
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
private readonly char _separatorRecords = ';';
|
|
private static readonly char _separatorForObject = ':';
|
|
|
|
public CarsGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_carStorages = new Dictionary<string, CarsGenericCollection<DrawTanker, DrawingObjectTanker>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
public void AddSet(string name)
|
|
{
|
|
if (_carStorages.ContainsKey(name)) return;
|
|
_carStorages[name] = new CarsGenericCollection<DrawTanker, DrawingObjectTanker>(_pictureWidth, _pictureHeight);
|
|
}
|
|
|
|
public void DelSet(string name)
|
|
{
|
|
if (!_carStorages.ContainsKey(name)) return;
|
|
_carStorages.Remove(name);
|
|
}
|
|
|
|
public CarsGenericCollection<DrawTanker, DrawingObjectTanker>?
|
|
this[string ind]
|
|
{
|
|
get
|
|
{
|
|
if (_carStorages.ContainsKey(ind)) return _carStorages[ind];
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public bool SaveData(string filename)
|
|
{
|
|
if (File.Exists(filename))
|
|
{
|
|
File.Delete(filename);
|
|
}
|
|
StringBuilder data = new();
|
|
foreach (KeyValuePair<string, CarsGenericCollection<DrawTanker, DrawingObjectTanker>> record in _carStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawTanker? elem in record.Value.GetCars)
|
|
{
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
}
|
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
}
|
|
if (data.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
using FileStream fs = new(filename, FileMode.Create);
|
|
byte[] info = new
|
|
UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}");
|
|
fs.Write(info, 0, info.Length);
|
|
return true;
|
|
}
|
|
|
|
public bool LoadData(string filename)
|
|
{
|
|
if (!File.Exists(filename))
|
|
{
|
|
return false;
|
|
}
|
|
string bufferTextFromFile = "";
|
|
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)
|
|
{
|
|
bufferTextFromFile += temp.GetString(b);
|
|
}
|
|
}
|
|
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
if (strs == null || strs.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (!strs[0].StartsWith("CarStorage"))
|
|
{
|
|
return false;
|
|
}
|
|
_carStorages.Clear();
|
|
foreach (string data in strs)
|
|
{
|
|
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 2)
|
|
{
|
|
continue;
|
|
}
|
|
CarsGenericCollection<DrawTanker, DrawingObjectTanker> collection = new(_pictureWidth, _pictureHeight);
|
|
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
DrawTanker? car =
|
|
elem?.CreateDrawTanker(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (car != null)
|
|
{
|
|
if (!(collection + car))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
_carStorages.Add(record[0], collection);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|
|
}
|