using Hydroplane.DrawningObjects; using Hydroplane.MovementStrategy; 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 bool 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) { return false; } using (StreamWriter writer = new StreamWriter(filename)) { writer.Write($"PlaneStorage{Environment.NewLine}{data}"); } return true; } public bool LoadData(string filename) { if (!File.Exists(filename)) { return false; } using (StreamReader fs = File.OpenText(filename)) { string str = fs.ReadLine(); if (str == null || str.Length == 0) { return false; } if (!str.StartsWith("PlaneStorage")) { return false; } _planeStorages.Clear(); string strs = ""; while ((strs = fs.ReadLine()) != null) { if (strs == null) { return false; } 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)) { return false; } } } _planeStorages.Add(record[0], collection); } return true; } } } }