using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WarmlyLocomotive.DrawningObjects; using WarmlyLocomotive.MovementStrategy; using System.Xml.Linq; namespace WarmlyLocomotive.Generics { internal class WarmlyLocomotiveGenericStorage { readonly Dictionary> _warmlylocomotiveStorages; public List Keys => _warmlylocomotiveStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; private static readonly char _separatorForKeyValue = '|'; private readonly char _separatorRecords = ';'; private static readonly char _separatorForObject = ':'; public WarmlyLocomotiveGenericStorage(int pictureWidth, int pictureHeight) { _warmlylocomotiveStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string name) { _warmlylocomotiveStorages.Add(new WarmlyLocomotivesCollectionInfo(name, string.Empty), new WarmlyLocomotiveGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name) { if (!_warmlylocomotiveStorages.ContainsKey(new WarmlyLocomotivesCollectionInfo(name, string.Empty))) return; _warmlylocomotiveStorages.Remove(new WarmlyLocomotivesCollectionInfo(name, string.Empty)); } public WarmlyLocomotiveGenericCollection? this[string ind] { get { WarmlyLocomotivesCollectionInfo index = new WarmlyLocomotivesCollectionInfo(ind, string.Empty); if (_warmlylocomotiveStorages.ContainsKey(index)) return _warmlylocomotiveStorages[index]; return null; } } public bool SaveData(string filename) { if (File.Exists(filename)) { File.Delete(filename); } StringBuilder data = new(); foreach (KeyValuePair> record in _warmlylocomotiveStorages) { StringBuilder records = new(); foreach (DrawningWarmlyLocomotive? elem in record.Value.GetWarmlyLocomotive) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { throw new IOException("Невалидная операция, нет данных для сохранения"); } string toWrite = $"WarmlyLocomotiveStorage{Environment.NewLine}{data}"; var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); using (StreamWriter sw = new(filename)) { foreach (var str in strs) { sw.WriteLine(str); } } return true; } public bool LoadData(string filename) { if (!File.Exists(filename)) { throw new IOException("Файл не найден"); } using (StreamReader sr = new(filename)) { string str = sr.ReadLine(); var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); if (strs == null || strs.Length == 0) { throw new IOException("Нет данных для загрузки"); } if (!strs[0].StartsWith("WarmlyLocomotiveStorage")) { throw new IOException("Неверный формат данных"); } _warmlylocomotiveStorages.Clear(); do { string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (record.Length != 2) { str = sr.ReadLine(); continue; } WarmlyLocomotiveGenericCollection collection = new(_pictureWidth, _pictureHeight); string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { DrawningWarmlyLocomotive? ship = elem?.CreateDrawningWarmlyLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); if (ship != null) { if (!(collection + ship)) { return false; } } } _warmlylocomotiveStorages.Add(new WarmlyLocomotivesCollectionInfo(record[0], string.Empty), collection); str = sr.ReadLine(); } while (str != null); } return true; } } }