2024-06-16 19:46:37 +04:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2024-06-15 13:28:42 +04:00
|
|
|
|
using ProjectCruiser.DrawningSamples;
|
2024-06-15 23:48:53 +04:00
|
|
|
|
using ProjectCruiser.Exceptions;
|
2024-06-15 13:28:42 +04:00
|
|
|
|
|
|
|
|
|
namespace ProjectCruiser.CollectionGenericObj;
|
2024-06-15 09:05:36 +04:00
|
|
|
|
|
|
|
|
|
public class StorageCollection<T>
|
2024-06-15 18:06:35 +04:00
|
|
|
|
where T : DrawningBase // class
|
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)
|
|
|
|
|
{
|
2024-06-16 19:46:37 +04:00
|
|
|
|
throw new NullReferenceException("> Not enough information to save");
|
2024-06-15 09:05:36 +04:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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);
|
2024-06-16 19:46:37 +04:00
|
|
|
|
else throw new NullReferenceException("> No such key in the list");
|
2024-06-15 09:05:36 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 18:06:35 +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>
|
2024-06-15 23:48:53 +04:00
|
|
|
|
public void SaveData(string filename)
|
2024-06-15 13:28:42 +04:00
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
if (_storages.Count == 0)
|
2024-06-16 19:46:37 +04:00
|
|
|
|
throw new NullReferenceException("> No existing collections to save");
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
if (File.Exists(filename)) { File.Delete(filename); }
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new();
|
|
|
|
|
|
|
|
|
|
sb.Append(_collectionKey); // const
|
|
|
|
|
|
2024-06-15 18:06:35 +04:00
|
|
|
|
foreach (KeyValuePair<string, ICollectionGenObj<T>> pair in _storages)
|
2024-06-15 13:28:42 +04:00
|
|
|
|
{
|
|
|
|
|
sb.Append(Environment.NewLine); // не сохраняем пустые коллекции
|
2024-06-15 18:06:35 +04:00
|
|
|
|
if (pair.Value.Count == 0) { continue; }
|
2024-06-15 13:28:42 +04:00
|
|
|
|
|
2024-06-15 18:06:35 +04:00
|
|
|
|
sb.Append(pair.Key);
|
2024-06-15 13:28:42 +04:00
|
|
|
|
sb.Append(_separatorForKeyValue);
|
2024-06-15 18:06:35 +04:00
|
|
|
|
sb.Append(pair.Value.GetCollectionType);
|
2024-06-15 13:28:42 +04:00
|
|
|
|
sb.Append(_separatorForKeyValue);
|
2024-06-15 18:06:35 +04:00
|
|
|
|
sb.Append(pair.Value.MaxCount);
|
2024-06-15 13:28:42 +04:00
|
|
|
|
sb.Append(_separatorForKeyValue);
|
|
|
|
|
|
2024-06-15 18:06:35 +04:00
|
|
|
|
foreach (T? item in pair.Value.GetItems())
|
2024-06-15 13:28:42 +04:00
|
|
|
|
{
|
|
|
|
|
string data = item?.GetDataForSave() ?? string.Empty;
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-06-15 18:06:35 +04:00
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Создание коллекции по типу
|
|
|
|
|
private static ICollectionGenObj<T>? CreateCollection(CollectionType collectionType)
|
|
|
|
|
{
|
|
|
|
|
return collectionType switch
|
|
|
|
|
{
|
|
|
|
|
CollectionType.Array => new ArrayGenObj<T>(),
|
|
|
|
|
CollectionType.List => new ListGenObj<T>(),
|
|
|
|
|
_ => null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Загрузка информации по кораблям в хранилище из файла
|
2024-06-15 23:48:53 +04:00
|
|
|
|
public void LoadData(string filename)
|
2024-06-15 13:28:42 +04:00
|
|
|
|
{
|
2024-06-16 19:46:37 +04:00
|
|
|
|
if (!File.Exists(filename)) throw new FileNotFoundException("> No such file");
|
2024-06-15 13:28:42 +04:00
|
|
|
|
|
|
|
|
|
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)
|
2024-06-15 23:48:53 +04:00
|
|
|
|
throw new NullReferenceException("> No data to decode");
|
2024-06-15 13:28:42 +04:00
|
|
|
|
if (!strs[0].Equals(_collectionKey))
|
2024-06-15 23:48:53 +04:00
|
|
|
|
throw new InvalidDataException("> Incorrect data");
|
2024-06-15 13:28:42 +04:00
|
|
|
|
|
2024-06-15 18:06:35 +04:00
|
|
|
|
string[] companies = new string[strs.Length - 1];
|
|
|
|
|
for (int k = 1; k < strs.Length; k++)
|
|
|
|
|
{
|
|
|
|
|
companies[k - 1] = strs[k];
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
_storages.Clear();
|
2024-06-15 18:06:35 +04:00
|
|
|
|
foreach (string data in companies)
|
2024-06-15 13:28:42 +04:00
|
|
|
|
{
|
2024-06-15 18:06:35 +04:00
|
|
|
|
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
2024-06-15 13:28:42 +04:00
|
|
|
|
if (record.Length != 4) // >
|
|
|
|
|
// key | collType | maxcount | all next inf > 4
|
2024-06-16 19:46:37 +04:00
|
|
|
|
{ continue; }
|
|
|
|
|
|
2024-06-15 18:06:35 +04:00
|
|
|
|
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
2024-06-15 13:28:42 +04:00
|
|
|
|
ICollectionGenObj<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
if (collection == null)
|
2024-06-16 19:46:37 +04:00
|
|
|
|
throw new NullReferenceException("[!] Failed to create collection");
|
2024-06-15 13:28:42 +04:00
|
|
|
|
|
|
|
|
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
|
|
|
|
string[] set = record[3].Split(_separatorItems,
|
|
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
2024-06-15 18:06:35 +04:00
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
foreach (string elem in set)
|
|
|
|
|
{
|
2024-06-15 18:06:35 +04:00
|
|
|
|
if (elem?.CreateDrawningCar() is T ship)
|
2024-06-15 13:28:42 +04:00
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-16 19:46:37 +04:00
|
|
|
|
collection.Insert(ship);
|
|
|
|
|
|
|
|
|
|
// throw new IndexOutOfRangeException IF IT WAS Insert(item, pos)
|
|
|
|
|
// NullReferenceException >
|
|
|
|
|
// CollectionOverflowException >
|
2024-06-15 23:48:53 +04:00
|
|
|
|
}
|
2024-06-16 19:46:37 +04:00
|
|
|
|
catch (Exception e)
|
2024-06-15 13:28:42 +04:00
|
|
|
|
{
|
2024-06-16 19:46:37 +04:00
|
|
|
|
throw new Exception(e.Message);
|
2024-06-15 13:28:42 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_storages.Add(record[0], collection);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-15 09:05:36 +04:00
|
|
|
|
}
|
|
|
|
|
|