using Hydroplane.DrawningObjects; using Hydroplane.MovementStrategy; using Hydroplane.Exceptions; using System; using System.Collections.Generic; using System.DirectoryServices.ActiveDirectory; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Hydroplane.Generics { internal class PlanesGenericStorage { readonly Dictionary> _planeStorages; public List Keys => _planeStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; public PlanesGenericStorage(int pictureWidth, int pictureHeight) { _planeStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string name) { if (_planeStorages.ContainsKey(name)) return; _planeStorages[name] = new PlanesGenericCollection(_pictureWidth, _pictureHeight); } public void DelSet(string name) { if (!_planeStorages.ContainsKey(name)) return; _planeStorages.Remove(name); } public PlanesGenericCollection? this[string ind] { get { if (_planeStorages.ContainsKey(ind)) return _planeStorages[ind]; return null; } } private static readonly char _separatorForKeyValue = '|'; private readonly char _separatorRecords = ';'; private static readonly char _separatorForObject = ':'; public void SaveData(string filename) { if (File.Exists(filename)) { File.Delete(filename); } StringBuilder data = new(); foreach (KeyValuePair> record in _planeStorages) { StringBuilder records = new(); foreach (DrawningPlane? elem in record.Value.GetPlanes) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { throw new ArgumentException("Invalid operation, there isn't any data"); } using (StreamWriter writer = new StreamWriter(filename)) { writer.Write($"PlaneStorage{Environment.NewLine}{data}"); } } public void LoadData(string filename) { if (!File.Exists(filename)) { throw new FileNotFoundException("File not found"); } using (StreamReader fs = File.OpenText(filename)) { string str = fs.ReadLine(); if (str == null || str.Length == 0) { throw new ArgumentException("There isn't any data for load"); } if (!str.StartsWith("PlaneStorage")) { throw new InvalidDataException("Invalid format of data"); } _planeStorages.Clear(); string strs = ""; while ((strs = fs.ReadLine()) != null) { if (strs == null) { return; } string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (record.Length != 2) { continue; } PlanesGenericCollection collection = new(_pictureWidth, _pictureHeight); string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { DrawningPlane? plane = elem?.CreateDrawningPlane(_separatorForObject, _pictureWidth, _pictureHeight); if (plane != null) { if (!(collection + plane)) { try { _ = collection + plane; } catch (PlaneNotFoundException e) { throw e; } catch (StorageOverflowException e) { throw e; } } } } _planeStorages.Add(record[0], collection); } } } } }