summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs62
-rw-r--r--src/Noddybox.Emulation.Keyboard.Schema/KeyboardKey.cs61
2 files changed, 53 insertions, 70 deletions
diff --git a/src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs b/src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs
index 5ca3f47..10a1c51 100644
--- a/src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs
+++ b/src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs
@@ -16,7 +16,7 @@
// Copyright (c) 2012 Ian Cowburn
//
using System;
-using System.Xml;
+using System.IO;
using System.Collections.Generic;
namespace Noddybox.Emulation.Keyboard.Schema
@@ -26,6 +26,12 @@ namespace Noddybox.Emulation.Keyboard.Schema
/// </summary>
public class KeyboardDefinition
{
+ #region Version
+
+ private const string magic = "KEYBDEF1.0";
+
+ #endregion
+
#region Properties
/// <summary>
@@ -35,57 +41,45 @@ namespace Noddybox.Emulation.Keyboard.Schema
#endregion
- #region Xml functions
+ #region Load/Save
/// <summary>
- /// Create the XML for this keyboard definition.
+ /// Save this keybaord definition to the stream.
/// </summary>
- /// <returns>The created XML document.</returns>
- public XmlDocument CreateXML()
+ /// <param name="stream">The stream to write to.</param>
+ public void Save(BinaryWriter stream)
{
- XmlDocument doc = new XmlDocument();
-
- doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null));
- doc.AppendChild(doc.CreateElement("Keyboard"));
-
+ stream.Write(magic);
+ stream.Write(Definitions.Count);
+
foreach (KeyboardKey k in Definitions)
{
- XmlElement e = doc.CreateElement("Key");
- k.SetElement(e);
- doc.DocumentElement.AppendChild(e);
+ k.Save(stream);
}
-
- return doc;
}
/// <summary>
- /// Create an object from XML.
+ /// Load a keyboard definition from the passed stream.
/// </summary>
- /// <param name="doc">The XML document to load from.</param>
- /// <returns>The created object.</returns>
- public static KeyboardDefinition CreateFromXML(XmlDocument doc)
+ /// <param name="stream">The stream to read from.</param>
+ /// <returns>The KeyboardDefinition object.</returns>
+ public static KeyboardDefinition Load(BinaryReader stream)
{
- KeyboardDefinition definition = new KeyboardDefinition();
-
- if (doc.DocumentElement.Name != "Keyboard")
+ if (stream.ReadString() != magic)
{
- throw new ArgumentException("Root element not a keyboard");
+ throw new ArgumentException("Stream has wrong magic");
}
- foreach (XmlNode n in doc.DocumentElement.ChildNodes)
+ KeyboardDefinition keyboard = new KeyboardDefinition();
+
+ int count = stream.ReadInt32();
+
+ for(int f = 0; f < count ; f++)
{
- if (n is XmlElement)
- {
- XmlElement e = (XmlElement)n;
-
- if (e.Name == "Key")
- {
- definition.Definitions.Add(KeyboardKey.CreateFromElement(e));
- }
- }
+ keyboard.Definitions.Add(KeyboardKey.Load(stream));
}
- return definition;
+ return keyboard;
}
#endregion
diff --git a/src/Noddybox.Emulation.Keyboard.Schema/KeyboardKey.cs b/src/Noddybox.Emulation.Keyboard.Schema/KeyboardKey.cs
index 6dac9b1..501fd2f 100644
--- a/src/Noddybox.Emulation.Keyboard.Schema/KeyboardKey.cs
+++ b/src/Noddybox.Emulation.Keyboard.Schema/KeyboardKey.cs
@@ -16,7 +16,7 @@
// Copyright (c) 2012 Ian Cowburn
//
using System;
-using System.Xml;
+using System.IO;
namespace Noddybox.Emulation.Keyboard.Schema
{
@@ -61,50 +61,39 @@ namespace Noddybox.Emulation.Keyboard.Schema
#endregion
- #region Xml members
+ #region Load/Save
/// <summary>
- /// Set up an XmlElement with the contents of this object.
+ /// Save this object to a stream.
/// </summary>
- /// <param name="doc">The document the element is to be attached to.</param>
- public void SetElement(XmlElement element)
+ /// <param name="stream">The stream to write to.</param>
+ public void Save(BinaryWriter stream)
{
- element.SetAttribute("KeySymbol", KeySymbol);
- element.SetAttribute("X", X.ToString());
- element.SetAttribute("Y", Y.ToString());
- element.SetAttribute("Width", Width.ToString());
- element.SetAttribute("Height", Height.ToString());
- element.SetAttribute("Sticky", Sticky.ToString());
+ stream.Write(KeySymbol);
+ stream.Write(X);
+ stream.Write(Y);
+ stream.Write(Width);
+ stream.Write(Height);
+ stream.Write(Sticky);
}
/// <summary>
- /// Create an instance of this object from an XmlElement.
+ /// Create an instance of this object from a stream.
/// </summary>
- /// <param name="doc">The document the element is to be attached to.</param>
- /// <returns>The XML element.</returns>
- public static KeyboardKey CreateFromElement(XmlElement element)
+ /// <param name="stream">The stream to read from.</param>
+ /// <returns>The KeyboardKey object.</returns>
+ public static KeyboardKey Load(BinaryReader stream)
{
- XmlAttribute symbol = element.Attributes["KeySymbol"];
- XmlAttribute x = element.Attributes["X"];
- XmlAttribute y = element.Attributes["Y"];
- XmlAttribute width = element.Attributes["Width"];
- XmlAttribute height = element.Attributes["Height"];
- XmlAttribute sticky = element.Attributes["Sticky"];
-
- if (symbol == null || x == null || y == null || width == null || height == null || sticky == null)
- {
- throw new ArgumentException("Element does not contain expected attributes");
- }
-
- return new KeyboardKey()
- {
- KeySymbol = symbol.Value,
- X = Convert.ToInt32(x.Value),
- Y = Convert.ToInt32(y.Value),
- Width = Convert.ToInt32(width.Value),
- Height = Convert.ToInt32(height.Value),
- Sticky = Convert.ToBoolean(sticky.Value)
- };
+ KeyboardKey key = new KeyboardKey();
+
+ key.KeySymbol = stream.ReadString();
+ key.X = stream.ReadInt32();
+ key.Y = stream.ReadInt32();
+ key.Width = stream.ReadInt32();
+ key.Height = stream.ReadInt32();
+ key.Sticky = stream.ReadBoolean();
+
+ return key;
}
#endregion