127 lines
4.6 KiB
C#
127 lines
4.6 KiB
C#
using ProjectLainer.DrawningObjects;
|
|
using ProjectLainer.MovementStrategy;
|
|
namespace ProjectLainer.Generics
|
|
{
|
|
internal class LainersGenericStorage
|
|
{
|
|
|
|
readonly Dictionary<string, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>> _lainerStorages;
|
|
|
|
public List<string> Keys => _lainerStorages.Keys.ToList();
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
private readonly int _pictureHeight;
|
|
public LainersGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_lainerStorages = new Dictionary<string, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
|
|
public void AddSet(string name)
|
|
{
|
|
_lainerStorages.Add(name, new LainersGenericCollection<DrawingEntity, DrawningObjectLainer>(_pictureWidth, _pictureHeight));
|
|
}
|
|
|
|
public void DelSet(string name)
|
|
{
|
|
if (!_lainerStorages.ContainsKey(name))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_lainerStorages.Remove(name);
|
|
}
|
|
|
|
public LainersGenericCollection<DrawingEntity, DrawningObjectLainer>? this[string ind]
|
|
{
|
|
get
|
|
{
|
|
if (_lainerStorages.ContainsKey(ind))
|
|
{
|
|
return _lainerStorages[ind];
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
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, LainersGenericCollection<DrawingLainer, DrawningObjectLainer>> record in _lainerStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawingLainer? elem in record.Value.GetLainers)
|
|
{
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
}
|
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
}
|
|
if (data.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
using (StreamWriter sw = new(filename))
|
|
{
|
|
sw.WriteLine($"LainerStorage{Environment.NewLine}{data}");
|
|
}
|
|
return true;
|
|
}
|
|
public bool LoadData(string filename)
|
|
{
|
|
if (!File.Exists(filename))
|
|
{
|
|
return false;
|
|
}
|
|
using (StreamReader sr = new(filename))
|
|
{
|
|
string str = sr.ReadLine();
|
|
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
if (strs == null || strs.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (!strs[0].StartsWith("LainerStorage"))
|
|
{
|
|
return false;
|
|
}
|
|
_lainerStorages.Clear();
|
|
do
|
|
{
|
|
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 1)
|
|
{
|
|
str = sr.ReadLine();
|
|
continue;
|
|
}
|
|
LainersGenericCollection<DrawingLainer, DrawningObjectLainer> collection = new(_pictureWidth, _pictureHeight);
|
|
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
DrawingLainer? lainer = elem?.CreateDrawningLainer(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (lainer != null)
|
|
{
|
|
if (!(collection + lainer))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
_lainerStorages.Add(record[0], collection);
|
|
str = sr.ReadLine();
|
|
} while (str != null);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|