2024-06-16 21:39:45 +04:00
|
|
|
|
using System.CodeDom;
|
2024-06-16 19:46:37 +04:00
|
|
|
|
using System.Text;
|
2024-06-15 13:28:42 +04:00
|
|
|
|
using ProjectCruiser.DrawningSamples;
|
|
|
|
|
|
|
|
|
|
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-16 21:39:45 +04:00
|
|
|
|
// Словарь (хранилище) с коллекциями < CollectionInfo, type (class) >
|
|
|
|
|
readonly Dictionary<CollectionInfo, ICollectionGenObj<T>> _storages;
|
2024-06-15 09:05:36 +04:00
|
|
|
|
|
2024-06-16 21:39:45 +04:00
|
|
|
|
// Возвращение списка коллекций
|
|
|
|
|
public List<CollectionInfo> Keys => _storages.Keys.ToList();
|
2024-06-15 09:05:36 +04:00
|
|
|
|
|
|
|
|
|
public StorageCollection()
|
|
|
|
|
{
|
2024-06-16 21:39:45 +04:00
|
|
|
|
_storages = new Dictionary<CollectionInfo, ICollectionGenObj<T>>();
|
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 AddCollection(string name, CollectionType collType)
|
|
|
|
|
{
|
2024-06-16 21:39:45 +04:00
|
|
|
|
// descroption [ ? ] >>>
|
|
|
|
|
CollectionInfo coll = new CollectionInfo(name, collType, string.Empty);
|
|
|
|
|
|
|
|
|
|
if (name == null || _storages.ContainsKey(coll)
|
2024-06-15 09:05:36 +04:00
|
|
|
|
|| 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-16 21:39:45 +04:00
|
|
|
|
_storages.Add(coll, CreateCollection(collType));
|
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)
|
|
|
|
|
{
|
2024-06-16 21:39:45 +04:00
|
|
|
|
// descroption [ ? ] >>>
|
|
|
|
|
CollectionInfo coll = new CollectionInfo(name,
|
|
|
|
|
CollectionType.None, string.Empty);
|
|
|
|
|
|
|
|
|
|
if (_storages.ContainsKey(coll)) _storages.Remove(coll);
|
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-16 21:39:45 +04:00
|
|
|
|
// Доступ к коллекции , индексатор [!!!]
|
2024-06-15 09:05:36 +04:00
|
|
|
|
public ICollectionGenObj<T>? this[string name]
|
|
|
|
|
{
|
2024-06-16 21:39:45 +04:00
|
|
|
|
get => _storages.ContainsKey(new CollectionInfo(name, CollectionType.None, string.Empty))
|
|
|
|
|
? _storages[new CollectionInfo(name, CollectionType.None, string.Empty)] : null;
|
2024-06-15 09:05:36 +04:00
|
|
|
|
}
|
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-16 21:39:45 +04:00
|
|
|
|
foreach (KeyValuePair<CollectionInfo, 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-16 21:39:45 +04:00
|
|
|
|
// <...>
|
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-16 21:39:45 +04:00
|
|
|
|
if (record.Length != 3) // >
|
|
|
|
|
// [key + collType] | maxcount | all next inf > 4
|
2024-06-16 19:46:37 +04:00
|
|
|
|
{ continue; }
|
|
|
|
|
|
2024-06-16 21:39:45 +04:00
|
|
|
|
CollectionInfo? collInfo = CollectionInfo.GetCollectionInfo(record[0]) ??
|
|
|
|
|
throw new Exception("[!] Failed to decode information : " + record[0]);
|
|
|
|
|
|
|
|
|
|
ICollectionGenObj<T>? collection = StorageCollection<T>.CreateCollection(
|
|
|
|
|
collInfo.CollectionType) ??
|
|
|
|
|
throw new Exception("[!] Failed to create a collection");
|
2024-06-15 13:28:42 +04:00
|
|
|
|
|
2024-06-16 21:39:45 +04:00
|
|
|
|
collection.MaxCount = Convert.ToInt32(record[1]);
|
|
|
|
|
string[] set = record[2].Split(_separatorItems,
|
2024-06-15 13:28:42 +04:00
|
|
|
|
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);
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-16 21:39:45 +04:00
|
|
|
|
_storages.Add(collInfo, collection);
|
2024-06-15 13:28:42 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-15 09:05:36 +04:00
|
|
|
|
}
|
|
|
|
|
|