Skip to content

Commit

Permalink
Add fallback "Unknown Controller"
Browse files Browse the repository at this point in the history
  • Loading branch information
Myp3a committed Sep 28, 2022
1 parent 34eb584 commit 0059e8d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
90 changes: 90 additions & 0 deletions Controllers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,96 @@ public string DumpToString(bool format)
}
}

internal class UnknownController : Controller
{
public override List<Keybind> keybinds { get; set; }
public override List<Keybind> axes { get; set; }
public UnknownController()
{
keybinds = new List<Keybind>();
axes = new List<Keybind>();
}

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<XElement> 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<Keybind> keybinds { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions JSONSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0059e8d

Please sign in to comment.