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 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)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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 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>
|
|
|
|
|
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
|
|
|
|
|
|
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;
|
2024-06-15 18:06:35 +04:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
string n = item.GetType().Name;
|
|
|
|
|
string data = null;
|
|
|
|
|
|
|
|
|
|
if (n != null && n == "DrawningCruiser")
|
|
|
|
|
{
|
|
|
|
|
data = ExtentionDrShip.GetDataForSave(item);
|
|
|
|
|
}
|
|
|
|
|
else if (n != null && n == "DrawningBase")
|
|
|
|
|
{
|
|
|
|
|
data = ExtentionDrShip.GetDataForSave(item);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 18:06:35 +04:00
|
|
|
|
if (collection.Insert(ship) == -1)
|
2024-06-15 13:28:42 +04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_storages.Add(record[0], collection);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-06-15 09:05:36 +04:00
|
|
|
|
}
|
|
|
|
|
|