From 0059e8d9a20ad10ee0fe370423bf21b8b589c1e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D1=83=D1=80=D0=B7=D0=B0=20=D0=9B=D0=B5=D0=B9=D0=BD?= Date: Wed, 28 Sep 2022 16:34:17 +0300 Subject: [PATCH] Add fallback "Unknown Controller" --- Controllers.cs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ JSONSchema.cs | 6 ++++ 2 files changed, 96 insertions(+) diff --git a/Controllers.cs b/Controllers.cs index 3021be2..46672b0 100644 --- a/Controllers.cs +++ b/Controllers.cs @@ -268,6 +268,96 @@ public string DumpToString(bool format) } } + internal class UnknownController : Controller + { + public override List keybinds { get; set; } + public override List axes { get; set; } + public UnknownController() + { + keybinds = new List(); + axes = new List(); + } + + public override void AddBind(int actionId) + { + GamepadKeybind newbind = new GamepadKeybind + { + elementIdentifierId = 0, + actionId = actionId + }; + keybinds.Add(newbind); + } + + public override void EditBind(Keybind bind, int key) + { + bind.elementIdentifierId = key; + } + + public override void LoadFromString(string xmlstring) + { + XNamespace ns = "http://guavaman.com/rewired"; + XElement xml = XElement.Parse(xmlstring); + sourceMapId = (int)(from el in xml.Descendants() where el.Name == ns + "sourceMapId" select el).First(); + categoryId = (int)(from el in xml.Descendants() where el.Name == ns + "categoryId" select el).First(); + layoutId = (int)(from el in xml.Descendants() where el.Name == ns + "layoutId" select el).First(); + name = (string)(from el in xml.Descendants() where el.Name == ns + "name" select el).First(); + hardwareGuid = (string)(from el in xml.Descendants() where el.Name == ns + "hardwareGuid" select el).First(); + enabled = (bool)(from el in xml.Descendants() where el.Name == ns + "enabled" select el).First(); + IEnumerable keybindings = + from el in xml.Descendants() + where el.Name == ns + "ActionElementMap" + select el; + foreach (XElement bindingNode in keybindings) + { + if (bindingNode.Element(ns + "elementType").Value == "1") + { + GamepadKeybind bind = new GamepadKeybind(bindingNode); + keybinds.Add(bind); + } + else if (bindingNode.Element(ns + "elementType").Value == "0") + { + GamepadAxis bind = new GamepadAxis(bindingNode); + axes.Add(bind); + } + } + } + + public override XDocument BuildObject() + { + XNamespace ns = "http://guavaman.com/rewired"; + XElement buttonMaps = new XElement(ns + "buttonMaps"); + XElement axisMaps = new XElement(ns + "axisMaps"); + + foreach (GamepadKeybind bind in keybinds) + { + buttonMaps.Add(bind.DumpToXml()); + } + + foreach (GamepadAxis bind in axes) + { + axisMaps.Add(bind.DumpToXml()); + } + + XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance"); + XDocument xml = new XDocument(new XDeclaration("1.0", "utf-16", null), + new XElement(ns + "JoystickMap", + new XAttribute("dataVersion", 2), + new XAttribute(XNamespace.Xmlns + "xsi", xsi), + new XAttribute(xsi + "schemaLocation", "http://guavaman.com/rewired http://guavaman.com/schemas/rewired/1.1/JoystickMap.xsd"), + new XElement(ns + "sourceMapId", sourceMapId), + new XElement(ns + "categoryId", categoryId), + new XElement(ns + "layoutId", layoutId), + new XElement(ns + "name", name), + new XElement(ns + "hardwareGuid", hardwareGuid), + new XElement(ns + "enabled", enabled), + buttonMaps, + axisMaps + ) + ); + + return xml; + } + } internal class MouseController : Controller { public override List keybinds { get; set; } diff --git a/JSONSchema.cs b/JSONSchema.cs index 7449ffa..2cb1f6d 100644 --- a/JSONSchema.cs +++ b/JSONSchema.cs @@ -118,6 +118,12 @@ internal void loadJSON(StreamingContext context) dsc.LoadFromString(xml_string); controllers.Add(dsc); } + else + { + UnknownController c = new UnknownController(); + c.LoadFromString(xml_string); + controllers.Add(c); + } } } } catch