Изменения в MapsCollection
This commit is contained in:
parent
4b59366b42
commit
ae6e1074b8
@ -10,7 +10,7 @@ namespace Locomotive
|
||||
internal class MapsCollection
|
||||
{
|
||||
/// Словарь (хранилище) с картами
|
||||
readonly Dictionary<string, MapWithSetLocomotivesGeneric<DrawningObject,
|
||||
readonly Dictionary<string, MapWithSetLocomotivesGeneric<IDrawningObject,
|
||||
AbstractMap>> _mapStorages;
|
||||
/// Возвращение списка названий карт
|
||||
public List<string> Keys => _mapStorages.Keys.ToList();
|
||||
@ -18,11 +18,14 @@ namespace Locomotive
|
||||
private readonly int _pictureWidth;
|
||||
/// Высота окна отрисовки
|
||||
private readonly int _pictureHeight;
|
||||
// Сепараторы
|
||||
private readonly char separatorDict = '|';
|
||||
private readonly char separatorData = ';';
|
||||
/// Конструктор
|
||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_mapStorages = new Dictionary<string,
|
||||
MapWithSetLocomotivesGeneric<DrawningObject, AbstractMap>>();
|
||||
MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
@ -30,7 +33,7 @@ namespace Locomotive
|
||||
public void AddMap(string name, AbstractMap map)
|
||||
{
|
||||
// Логика для добавления
|
||||
if (!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetLocomotivesGeneric<DrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
if (!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
}
|
||||
/// Удаление карты
|
||||
public void DelMap(string name)
|
||||
@ -39,7 +42,7 @@ namespace Locomotive
|
||||
if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name);
|
||||
}
|
||||
/// Доступ к парковке
|
||||
public MapWithSetLocomotivesGeneric<DrawningObject, AbstractMap> this[string ind]
|
||||
public MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
@ -48,5 +51,75 @@ namespace Locomotive
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// Метод записи информации в файл
|
||||
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 (FileStream fs = new(filename, FileMode.Create))
|
||||
{
|
||||
WriteToFile($"MapsCollection{Environment.NewLine}", fs);
|
||||
foreach (var storage in _mapStorages)
|
||||
{
|
||||
|
||||
WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs);
|
||||
}
|
||||
}
|
||||
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[0].Contains("MapsCollection"))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
return false;
|
||||
}
|
||||
//очищаем записи
|
||||
_mapStorages.Clear();
|
||||
for (int i = 1; i < strs.Length; ++i)
|
||||
{
|
||||
var elem = strs[i].Split(separatorDict);
|
||||
AbstractMap map = null;
|
||||
switch (elem[1])
|
||||
{
|
||||
case "Simple Map":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "Spike Map":
|
||||
map = new SpikeMap();
|
||||
break;
|
||||
case "Rail Map":
|
||||
map = new RailroadMap();
|
||||
break;
|
||||
}
|
||||
_mapStorages.Add(elem[0], new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user