2022-11-29 01:53:02 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-12-13 01:49:12 +04:00
|
|
|
|
using System.IO;
|
2022-11-29 01:53:02 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace WinFormsApp1
|
|
|
|
|
{
|
|
|
|
|
internal class MapsCollection
|
|
|
|
|
{
|
2022-12-13 01:49:12 +04:00
|
|
|
|
readonly Dictionary<string, MapWithSetTraktorGeneric<IDrawningObject, AbstractMap>> _mapStorages;
|
2022-11-29 01:53:02 +04:00
|
|
|
|
|
|
|
|
|
public List<string> Keys => _mapStorages.Keys.ToList();
|
|
|
|
|
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
|
2022-12-13 01:49:12 +04:00
|
|
|
|
private readonly char separatorDict = '|';
|
|
|
|
|
|
|
|
|
|
private readonly char separatorData = ';';
|
|
|
|
|
|
2022-11-29 01:53:02 +04:00
|
|
|
|
public MapsCollection(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
2022-12-13 01:49:12 +04:00
|
|
|
|
_mapStorages = new Dictionary<string, MapWithSetTraktorGeneric<IDrawningObject, AbstractMap>>();
|
2022-11-29 01:53:02 +04:00
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddMap(string name, AbstractMap map)
|
|
|
|
|
{
|
|
|
|
|
if (_mapStorages.ContainsKey(name)) return; //уникальное имя
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-12-13 01:49:12 +04:00
|
|
|
|
_mapStorages.Add(name, new MapWithSetTraktorGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
2022-11-29 01:53:02 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DelMap(string name)
|
|
|
|
|
{
|
|
|
|
|
if (_mapStorages.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
_mapStorages.Remove(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 01:49:12 +04:00
|
|
|
|
public MapWithSetTraktorGeneric<IDrawningObject, AbstractMap> this[string ind]
|
2022-11-29 01:53:02 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2022-12-13 01:49:12 +04:00
|
|
|
|
_mapStorages.TryGetValue(ind, out var result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private static void WriteToFile(string text, FileStream stream)
|
|
|
|
|
{
|
|
|
|
|
byte[] info = new UTF8Encoding(true).GetBytes(text);
|
|
|
|
|
stream.Write(info, 0, info.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SaveData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
using (StreamWriter fs = new(filename))
|
|
|
|
|
{
|
|
|
|
|
fs.Write($"MapsCollection{Environment.NewLine}");
|
|
|
|
|
foreach (var storage in _mapStorages)
|
|
|
|
|
{
|
|
|
|
|
fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
|
|
|
|
|
}
|
2022-11-29 01:53:02 +04:00
|
|
|
|
}
|
2022-12-13 01:49:12 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-11-29 01:53:02 +04:00
|
|
|
|
|
2022-12-13 01:49:12 +04:00
|
|
|
|
public bool LoadData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
using (StreamReader sr = new(filename))
|
|
|
|
|
{
|
|
|
|
|
string str = "";
|
|
|
|
|
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
|
|
|
|
|
{
|
|
|
|
|
//если нет такой записи, то это не те данные
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
//очищаем записи
|
|
|
|
|
_mapStorages.Clear();
|
|
|
|
|
while ((str = sr.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
var elem = str.Split(separatorDict);
|
|
|
|
|
AbstractMap map = null;
|
|
|
|
|
switch (elem[1])
|
|
|
|
|
{
|
|
|
|
|
case "Простая карта":
|
|
|
|
|
map = new SimpleMap();
|
|
|
|
|
break;
|
|
|
|
|
case "Поле":
|
|
|
|
|
map = new FieldMap();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
_mapStorages.Add(elem[0], new MapWithSetTraktorGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
|
|
|
|
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2022-11-29 01:53:02 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|