/// <summary>Reads the JSON representation of the object.</summary> /// <param name="reader">The JSON reader.</param> /// <param name="objectType">The object type.</param> /// <param name="existingValue">The object being read.</param> /// <param name="serializer">The calling serializer.</param> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { string path = reader.Path; switch (reader.TokenType) { case JsonToken.Null: return(objectType == typeof(Keybind) ? (object)new Keybind() : new KeybindList()); case JsonToken.String: { string str = JToken.Load(reader).Value <string>(); if (objectType == typeof(Keybind)) { return(Keybind.TryParse(str, out Keybind parsed, out string[] errors) ? parsed : throw new SParseException($"Can't parse {nameof(Keybind)} from invalid value '{str}' (path: {path}).\n{string.Join("\n", errors)}")); } else { return(KeybindList.TryParse(str, out KeybindList parsed, out string[] errors) ? parsed : throw new SParseException($"Can't parse {nameof(KeybindList)} from invalid value '{str}' (path: {path}).\n{string.Join("\n", errors)}")); } } default: throw new SParseException($"Can't parse {objectType} from unexpected {reader.TokenType} node (path: {reader.Path})."); } }
public void TryParse_InvalidValues(string input, string expectedError) { // act bool success = KeybindList.TryParse(input, out KeybindList parsed, out string[] errors); // assert Assert.IsFalse(success, "Parsing unexpectedly succeeded."); Assert.IsNull(parsed, "The parsed result should be null."); Assert.IsNotNull(errors, message: "The errors should never be null."); Assert.AreEqual(expectedError, string.Join("; ", errors), "The errors don't match the expected ones."); }
public string TryParse_MultiValues(string input) { // act bool success = KeybindList.TryParse(input, out KeybindList parsed, out string[] errors); // assert Assert.IsTrue(success, "Parsing unexpectedly failed."); Assert.IsNotNull(parsed, "The parsed result should not be null."); Assert.IsNotNull(errors, message: "The errors should never be null."); Assert.IsEmpty(errors, message: "The input bindings incorrectly reported errors."); return(parsed.ToString()); }
public void TryParse_SimpleValue(SButton button) { // act bool success = KeybindList.TryParse($"{button}", out KeybindList parsed, out string[] errors); // assert Assert.IsTrue(success, "Parsing unexpectedly failed."); Assert.IsNotNull(parsed, "The parsed result should not be null."); Assert.AreEqual(parsed.ToString(), $"{button}"); Assert.IsNotNull(errors, message: "The errors should never be null."); Assert.IsEmpty(errors, message: "The input bindings incorrectly reported errors."); }
private void Input_ButtonsChanged(object sender, StardewModdingAPI.Events.ButtonsChangedEventArgs e) { if (!Config.EnableMod || animationDict == null) { return; } foreach (var kvp in animationDict) { if (kvp.Value.keyTrigger != null && KeybindList.TryParse(kvp.Value.keyTrigger, out KeybindList keybind, out string[] errors) && keybind.JustPressed()) { PlayAnimation(kvp.Key, kvp.Value); } } }
public SButtonState GetState(string input, string stateMap) { // act bool success = KeybindList.TryParse(input, out KeybindList parsed, out string[] errors); if (success && parsed?.Keybinds != null) { foreach (var keybind in parsed.Keybinds) #pragma warning disable 618 // method is marked obsolete because it should only be used in unit tests { keybind.GetButtonState = key => this.GetStateFromMap(key, stateMap); } #pragma warning restore 618 } // assert Assert.IsTrue(success, "Parsing unexpected failed"); Assert.IsNotNull(parsed, "The parsed result should not be null."); Assert.IsNotNull(errors, message: "The errors should never be null."); Assert.IsEmpty(errors, message: "The input bindings incorrectly reported errors."); return(parsed.GetState()); }