2024-06-15 13:28:42 +04:00
|
|
|
|
using System.Text;
|
|
|
|
|
using ProjectCruiser.DrawningSamples;
|
|
|
|
|
|
|
|
|
|
namespace ProjectCruiser.CollectionGenericObj;
|
2024-06-15 09:05:36 +04:00
|
|
|
|
|
|
|
|
|
public class StorageCollection<T>
|
2024-06-15 13:28:42 +04:00
|
|
|
|
where T : DrawningBase
|
2024-06-15 09:05:36 +04:00
|
|
|
|
{
|
2024-06-15 13:28:42 +04:00
|
|
|
|
// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
private readonly string _separatorForKeyValue = "|";
|
|
|
|
|
// Разделитель для записей коллекции данных в файл
|
|
|
|
|
private readonly string _separatorItems = ";";
|
|
|
|
|
|
|
|
|
|
// Ключевое слово, с которого должен начинаться файл
|
|
|
|
|
private readonly string _collectionKey = "CollectionsStorage";
|
|
|
|
|
|
2024-06-15 09:05:36 +04:00
|
|
|
|
// Словарь (хранилище) с коллекциями < name, type (class) >
|
|
|
|
|
readonly Dictionary<string, ICollectionGenObj<T>> _storages;
|
|
|
|
|
|
|
|
|
|
// Возвращение списка названий коллекций
|
|
|
|
|
public List<string> Keys => _storages.Keys.ToList();
|
|
|
|
|
|
|
|
|
|
public StorageCollection()
|
|
|
|
|
{
|
|
|
|
|
_storages = new Dictionary<string, ICollectionGenObj<T>>();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
// Добавление коллекции в хранилище
|
2024-06-15 09:05:36 +04:00
|
|
|
|
public void AddCollection(string name, CollectionType collType)
|
|
|
|
|
{
|
|
|
|
|
if (name == null || _storages.ContainsKey(name)
|
|
|
|
|
|| collType == CollectionType.None)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
ICollectionGenObj<T> collection = CreateCollection(collType);
|
|
|
|
|
_storages.Add(name, collection);
|
|
|
|
|
|
|
|
|
|
/*
|
2024-06-15 09:05:36 +04:00
|
|
|
|
switch (collType)
|
|
|
|
|
{
|
|
|
|
|
case CollectionType.List: _storages.Add(name, new ListGenObj<T>()); break;
|
|
|
|
|
case CollectionType.Array: _storages.Add(name, new ArrayGenObj<T>()); break;
|
|
|
|
|
}
|
2024-06-15 13:28:42 +04:00
|
|
|
|
*/
|
2024-06-15 09:05:36 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
// Удаление коллекции ( по ключу-строке - её имени )
|
2024-06-15 09:05:36 +04:00
|
|
|
|
public void DelCollection(string name)
|
|
|
|
|
{
|
|
|
|
|
if (_storages.ContainsKey(name)) _storages.Remove(name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
// Доступ к коллекции ( по ключу-строке - её имени )
|
2024-06-15 09:05:36 +04:00
|
|
|
|
public ICollectionGenObj<T>? this[string name]
|
|
|
|
|
{
|
|
|
|
|
get => _storages.ContainsKey(name) ? _storages[name] : null;
|
|
|
|
|
}
|
2024-06-15 13:28:42 +04:00
|
|
|
|
|
|
|
|
|
/// Сохранение информации по автомобилям в хранилище в файл
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - сохранение прошло успешно,
|
|
|
|
|
/// false - ошибка при сохранении данных</returns>
|
|
|
|
|
public bool SaveData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (_storages.Count == 0) { return false; }
|
|
|
|
|
if (File.Exists(filename)) { File.Delete(filename); }
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new();
|
|
|
|
|
|
|
|
|
|
sb.Append(_collectionKey); // const
|
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<string, ICollectionGenObj<T>> value in _storages)
|
|
|
|
|
{
|
|
|
|
|
sb.Append(Environment.NewLine); // не сохраняем пустые коллекции
|
|
|
|
|
if (value.Value.Count == 0) { continue; }
|
|
|
|
|
|
|
|
|
|
sb.Append(value.Key);
|
|
|
|
|
sb.Append(_separatorForKeyValue);
|
|
|
|
|
sb.Append(value.Value.GetCollectionType);
|
|
|
|
|
sb.Append(_separatorForKeyValue);
|
|
|
|
|
sb.Append(value.Value.MaxCount);
|
|
|
|
|
sb.Append(_separatorForKeyValue);
|
|
|
|
|
|
|
|
|
|
foreach (T? item in value.Value.GetItems())
|
|
|
|
|
{
|
|
|
|
|
string data = item?.GetDataForSave() ?? string.Empty;
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
sb.Append(data);
|
|
|
|
|
sb.Append(_separatorItems);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using FileStream fs = new(filename, FileMode.Create);
|
|
|
|
|
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
|
|
|
|
|
fs.Write(info, 0, info.Length);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Создание коллекции по типу
|
|
|
|
|
private static ICollectionGenObj<T>? CreateCollection(CollectionType collectionType)
|
|
|
|
|
{
|
|
|
|
|
return collectionType switch
|
|
|
|
|
{
|
|
|
|
|
CollectionType.Array => new ArrayGenObj<T>(),
|
|
|
|
|
CollectionType.List => new ListGenObj<T>(),
|
|
|
|
|
_ => null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Загрузка информации по кораблям в хранилище из файла
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] strs = bufferTextFromFile.Split(
|
|
|
|
|
new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
if (strs == null || strs.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!strs[0].Equals(_collectionKey))
|
|
|
|
|
{
|
|
|
|
|
//если нет такой записи, то это не те данные
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_storages.Clear();
|
|
|
|
|
foreach (string data in strs)
|
|
|
|
|
{
|
|
|
|
|
string[] record = data.Split(_separatorForKeyValue,
|
|
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (record.Length != 4) // >
|
|
|
|
|
// key | collType | maxcount | all next inf > 4
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
CollectionType collectionType =
|
|
|
|
|
(CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
|
|
|
|
ICollectionGenObj<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
|
|
|
|
string[] set = record[3].Split(_separatorItems,
|
|
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (string elem in set)
|
|
|
|
|
{
|
|
|
|
|
if (elem?.CreateDrawningCar() is T car)
|
|
|
|
|
{
|
|
|
|
|
if (!(collection.Insert(car) == -1))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_storages.Add(record[0], collection);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-06-15 09:05:36 +04:00
|
|
|
|
}
|
|
|
|
|
|