/// <summary> /// Load the key map override file if it exists. /// </summary> /// <exception cref="ArgumentException">If the key map override file is in invalid format</exception> protected void LoadOverrides() { // Only load overrides if the file actually exists if (!File.Exists(OverrideFilePath)) { return; } try { // Parse schema file as JSON object using (var fileReader = File.OpenText(OverrideFilePath)) using (var jsonReader = new JsonTextReader(fileReader)) { var array = JToken.ReadFrom(jsonReader) as JObject; if (array == null) { throw new ArgumentException("Failed to load top level object from key binding override file"); } // Parse each key binding foreach (var entry in array) { // All entries need to be objects if (entry.Value.Type != JTokenType.Object) { Logger.PostMessageTagged(SeverityLevel.Fatal, "InputMapper", String.Format("Expected JSON object in key binding override file, got \"{0}\" instead", entry.Value.Type.ToString())); throw new ArgumentException("Expected JSON object in schema definition"); } // Check that a schema entry actually exists for given identifier if (!Schema.ContainsKey(entry.Key)) { Logger.PostMessageTagged(SeverityLevel.Fatal, "InputMapper", String.Format("Unknown action identifier \"{0}\" in override file for key map \"{1}\"", entry.Key, Identifier)); throw new ArgumentException("Unknown action identifier in override file"); } var binding = JsonLoader.Deserialize <KeyBinding>(entry.Value as JObject); OverrideBindings.Add(entry.Key, binding); } } } catch (Exception e) { Logger.PostMessageTagged(SeverityLevel.Fatal, "InputMapper", String.Format("Failed to load input mapping schema file: {0}", e.Message)); throw; } }
/// <summary> /// Check if the keys associated with given input action are currently pressed. /// </summary> /// <param name="input">Input action to use</param> /// <returns>Flag indicating if the keys associated with given input action are pressed</returns> /// <exception cref="ArgumentException">If the given input action is not known to this mapping</exception> public bool HasInput(string input) { if (OverrideBindings.ContainsKey(input)) { return(HasInput(OverrideBindings[input])); } else if (Schema.ContainsKey(input)) { return(HasInput(Schema[input].DefaultBinding)); } else { throw new ArgumentException("Unknown input action \"{0}\"", input); } }
/// <summary> /// Update a key binding /// </summary> /// <param name="input">Input action identifier to set key binding for</param> /// <param name="kb">Key binding information</param> /// <exception cref="ArgumentException">If the given input action is not known to this mapping</exception> public void SetBinding(string input, KeyBinding kb) { if (!Schema.ContainsKey(input)) { throw new ArgumentException("Unknown input action \"{0}\"", input); } if (OverrideBindings.ContainsKey(input)) { OverrideBindings[input] = kb; } else { OverrideBindings.Add(input, kb); } }
/// <summary> /// Save all changes made to the bindings to the override key map file. This method /// automatically detects which bindings are redundant and ignores them. /// </summary> public void SaveChanges() { // Extract parent directory path from override file path var parentDirectory = Path.GetDirectoryName(OverrideFilePath); // Create directory if not already exists Directory.CreateDirectory(parentDirectory); // Determine all binding entries that are actually different from the // defaults stored in the schema var bindings = OverrideBindings.Where(kvp => kvp.Value != Schema[kvp.Key].DefaultBinding); // Build override JSON document in memory var parent = new JObject(); // Serialize them all foreach (var kvp in bindings) { parent.Add(kvp.Key, JsonWriter.Serialize(kvp.Value)); } // Create (or override) file and save JSON document File.WriteAllText(OverrideFilePath, parent.ToString()); }