using System.Collections.Generic; using Vellum; namespace Clave.Vellum { /// /// Saves a ChordArbiter as its mounts in the order they were mounted, each with its chords and whether it is /// enabled. /// /// /// The loaded arbiter's mounts come back in the same order, so the first mount of /// is the first one saved. Chords are new objects, so a chord that sat on two mounts is two chords after loading. /// public sealed class ChordArbiterConverter : SaveConverter { public const string MountsKey = "mounts"; public override SaveNode Write(ChordArbiter value, SaveSession session) { SaveNode mounts = SaveNode.NewList(); foreach (ChordMount mount in value.Mounts()) { var chords = new List(value.CountOn(mount)); foreach (Chord chord in value.ChordsOn(mount)) { chords.Add(new SavedChord(chord, value.PriorityOf(chord, mount), value.IsEnabled(chord, mount))); } SaveNode entry = SavedChords.Write(chords, session); entry.Entries[ChordSetConverter.EnabledKey] = session.ToNode(value.IsEnabled(mount)); mounts.Items.Add(entry); } SaveNode returnMe = SaveNode.NewTable(); returnMe.Entries[MountsKey] = mounts; return returnMe; } public override ChordArbiter Read(SaveNode node, SaveSession session) { IReadOnlyList saved = node.RequireEntry(MountsKey).RequireItems(); new SaveReader(node, session).Skip(MountsKey); var returnMe = new ChordArbiter(); for (int i = 0; i < saved.Count; ++i) { ChordMount mount = returnMe.Mount(); foreach (SavedChord chord in SavedChords.Read(saved[i], session)) { returnMe.Add(chord.Chord, mount, chord.Priority); if (!chord.Enabled) { returnMe.SetEnabled(chord.Chord, mount, false); } } var reader = new SaveReader(saved[i], session); bool enabled; if (reader.TryRead(ChordSetConverter.EnabledKey, out enabled) && !enabled) { returnMe.SetEnabled(mount, false); } } return returnMe; } public override bool TryListChildren(SaveNode node, ChildList children) { SaveNode mounts; IReadOnlyList entries; if (!node.TryGetEntry(MountsKey, out mounts) || !mounts.TryGetItems(out entries)) { return false; } foreach (SaveNode entry in entries) { if (!SavedChords.TryListChildren(entry, children)) { return false; } } return true; } } }