using System;
using System.Collections.Generic;
using Vellum;
namespace Clave.Glass
{
///
/// The bindings a player changed, with no knowledge of the sources they belong to.
///
[Saveable]
public partial class SavedKeyBindings
{
public const string BindingsKey = "keyBindings";
// Source id, to the id of each action that differs from its defaults, to its chords. An empty list is an
// action the player cleared.
public Dictionary>> Sources =
new Dictionary>>();
// The chords of Sources that take the kind their action has when loaded. They are named, not counted, so a
// chord that fails to load doesn't shift the rest. Anything not here keeps the kind it was saved with.
public Dictionary>> FollowsKind =
new Dictionary>>();
///
/// Returns whether , saved for an action, takes its action's kind when loaded.
///
public bool Follows(string sourceId, string actionId, Chord chord)
{
if (FollowsKind == null
|| !FollowsKind.TryGetValue(sourceId, out Dictionary> actions)
|| actions == null
|| !actions.TryGetValue(actionId, out List following)
|| following == null)
{
return false;
}
foreach (Chord candidate in following)
{
if (candidate != null && candidate.Formula.Equals(chord.Formula))
{
return true;
}
}
return false;
}
private bool SameChords(string sourceId, string actionId, List chords, SavedKeyBindings other,
List others)
{
if (chords.Count != others.Count)
{
return false;
}
for (int i = 0; i < chords.Count; ++i)
{
if (!chords[i].Formula.Equals(others[i].Formula)
|| Follows(sourceId, actionId, chords[i]) != other.Follows(sourceId, actionId, others[i]))
{
return false;
}
}
return true;
}
private bool SameActions(string sourceId, Dictionary> actions, SavedKeyBindings other,
Dictionary> others)
{
if (actions.Count != others.Count)
{
return false;
}
foreach (KeyValuePair> action in actions)
{
if (!others.TryGetValue(action.Key, out List chords)
|| !SameChords(sourceId, action.Key, action.Value, other, chords))
{
return false;
}
}
return true;
}
///
/// Returns whether holds the same chords for the same actions, in the same order, each
/// following its action's kind or not alike. Chords match by the keys they read, since two loads of one file
/// make distinct chords.
///
public bool Matches(SavedKeyBindings other)
{
if (other == null || Sources.Count != other.Sources.Count)
{
return false;
}
foreach (KeyValuePair>> source in Sources)
{
if (!other.Sources.TryGetValue(source.Key, out Dictionary> actions)
|| !SameActions(source.Key, source.Value, other, actions))
{
return false;
}
}
return true;
}
private static SaveSession NewSession()
{
return SaveConverters.For(typeof(SavedKeyBindings).Assembly).CreateSession();
}
///
/// Returns the text of a save file holding these bindings.
///
public string ToText()
{
SaveNode save = SaveNode.NewTable();
save.Entries[BindingsKey] = NewSession().ToNode(this);
return LuaSaveFormat.Format(save);
}
///
/// Reads bindings from the text of a save file, and returns whether it held any. A chord that doesn't load is
/// left out, so the player loses only that one.
///
public static bool TryFromText(string text, out SavedKeyBindings saved)
{
saved = null;
if (text == null)
{
return false;
}
try
{
SaveNode save = LuaSaveFormat.Parse(text);
if (!save.TryGetEntry(BindingsKey, out SaveNode node))
{
return false;
}
saved = NewSession().FromNode(node);
return saved != null;
}
catch (Exception)
{
// Text that parses can still hold values the bindings don't accept.
saved = null;
return false;
}
}
}
}