public static GetXMLField ( string input, string fieldName, string>.Dictionary |
||
input | string | |
fieldName | string | |
propertiesAndValues | string>.Dictionary | |
fieldData | string | |
leftoverInput | string | |
return | bool |
// Creates an event from an XML-formatted description. xmlDescription is modified as part of the constructor. The // first VREvent described <VREvent>...</VREvent> is popped off the string and xmlDescription is set to whatever // remains in the string. public VREvent(ref string xmlDescription) { Dictionary <string, string> props = new Dictionary <string, string>(); string xmlDataIndex = string.Empty; string xmlRemaining = string.Empty; bool success = XMLUtils.GetXMLField(xmlDescription, "VREvent", ref props, ref xmlDataIndex, ref xmlRemaining); if (!success) { Debug.Log("Error decoding VREvent"); return; } _name = props["name"]; _dataIndex = new VRDataIndex(ref xmlDataIndex); xmlDescription = xmlRemaining; }
void WaitForAndReceiveInputEvents(ref List <VREvent> inputEvents) { // 1. receive 1-byte message header WaitForAndReceiveMessageHeader(INPUT_EVENTS_MSG); // 2. receive int that tells us the size of the data portion of the message in bytes Int32 dataSize = ReadInt32(); // 3. receive dataSize bytes, then decode these as InputEvents byte[] buf2 = new byte[dataSize + 1]; int status = ReceiveAll(ref buf2, dataSize); if (status == -1) { Console.WriteLine("WaitForAndReceiveInputEvents error reading data"); return; } buf2[dataSize] = 0; // buf2 is the XML string that contains all the events. string xmlEventList = System.Text.Encoding.UTF8.GetString(buf2); //Debug.Log(xmlEventList); Dictionary <string, string> props = new Dictionary <string, string>(); string xmlEvents = string.Empty; string leftover = string.Empty; bool success = XMLUtils.GetXMLField(xmlEventList, "VREventList", ref props, ref xmlEvents, ref leftover); if (!success) { Debug.Log("Error decoding VREventList"); return; } int nEvents = Convert.ToInt32(props["num"]); for (int i = 0; i < nEvents; i++) { // Create the next VREvent from the XML description VREvent e = new VREvent(ref xmlEvents); inputEvents.Add(e); } }
// Creates a VREvent from an XML-formatted description. xmlDescription is modified as part of the constructor. // Following the current C++ implementation of MinVR, events are serialized in an XML defined by the C++ // VRDataIndex class. This constructor pops the first <MyEventName>...</MyEventName> field off of xmlDescription // to create the event and xmlDescription is set to whatever remains in the string. public VREvent(ref string xmlDescription) { // TODO 1: It might be faster to use C#'s XMLReader object to parse the XML. The XMLUtils class // was never intended to be used during the rendering loop, just for config files. // TODO 2: It might be even faster to convert the entire serialization scheme to use binary! _name = XMLUtils.GetNextXMLFieldName(xmlDescription); Dictionary <string, string> props = new Dictionary <string, string>(); string xmlData = string.Empty; string xmlRemaining = string.Empty; bool success = XMLUtils.GetXMLField(xmlDescription, _name, ref props, ref xmlData, ref xmlRemaining); if (!success) { Debug.Log("Error decoding VRDataIndex"); return; } string nextField = XMLUtils.GetNextXMLFieldName(xmlData); while (nextField != string.Empty) { string datumValue = string.Empty; string xmlDataRemaining = string.Empty; success = XMLUtils.GetXMLField(xmlData, nextField, ref props, ref datumValue, ref xmlDataRemaining); if (!success) { Debug.Log("Error decoding VRDatum named " + nextField); return; } char[] separatingChars = { ',' }; if (props["type"] == "int") { //Debug.Log ("Got int: " + nextField + "=" + datumValue); AddData(nextField, Convert.ToInt32(datumValue)); } else if (props["type"] == "float") { //Debug.Log ("Got float: " + nextField + "=" + datumValue); AddData(nextField, Convert.ToSingle(datumValue)); } else if (props["type"] == "string") { //Debug.Log ("Got string: " + nextField + "=" + datumValue); AddData(nextField, datumValue); } else if (props["type"] == "intarray") { //Debug.Log ("Got intarray: " + nextField + "=" + datumValue); string[] elements = datumValue.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries); int[] intarray = new int[elements.Length]; for (int i = 0; i < elements.Length; i++) { intarray[i] = Convert.ToInt32(elements[i]); } AddData(nextField, intarray); } else if (props["type"] == "floatarray") { //Debug.Log ("Got floatarray: " + nextField + "=" + datumValue); string[] elements = datumValue.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries); float[] floatarray = new float[elements.Length]; for (int i = 0; i < elements.Length; i++) { floatarray[i] = Convert.ToSingle(elements[i]); } AddData(nextField, floatarray); } else if (props["type"] == "stringarray") { //Debug.Log ("Got stringarray: " + nextField + "=" + datumValue); string[] elements = datumValue.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries); string[] strarray = new string[elements.Length]; for (int i = 0; i < elements.Length; i++) { strarray[i] = elements[i]; } AddData(nextField, strarray); } else { Debug.Log("Unknown VRDatum type: " + props["type"]); } xmlData = xmlDataRemaining; nextField = XMLUtils.GetNextXMLFieldName(xmlData); } xmlDescription = xmlRemaining; }
// Creates a VRDataIndex from an XML-formatted description. xmlDescription is modified as part of the constructor. The // first VRDataIndex described <VRDataIndex>...</VRDataIndex> is popped off the string and xmlDescription is set to whatever // remains in the string. public VRDataIndex(ref string xmlDescription) { _hash = new Hashtable(); _name = XMLUtils.GetNextXMLFieldName(xmlDescription); Dictionary <string, string> props = new Dictionary <string, string>(); string xmlData = string.Empty; string xmlRemaining = string.Empty; bool success = XMLUtils.GetXMLField(xmlDescription, _name, ref props, ref xmlData, ref xmlRemaining); if (!success) { Debug.Log("Error decoding VRDataIndex"); return; } string nextField = XMLUtils.GetNextXMLFieldName(xmlData); while (nextField != string.Empty) { string datumValue = string.Empty; string xmlDataRemaining = string.Empty; success = XMLUtils.GetXMLField(xmlData, nextField, ref props, ref datumValue, ref xmlDataRemaining); if (!success) { Debug.Log("Error decoding VRDatum named " + nextField); return; } char[] separatingChars = { ',' }; if (props["type"] == "int") { //Debug.Log ("Got int: " + nextField + "=" + datumValue); _hash.Add(nextField, Convert.ToInt32(datumValue)); } else if (props["type"] == "float") { //Debug.Log ("Got float: " + nextField + "=" + datumValue); _hash.Add(nextField, Convert.ToSingle(datumValue)); } else if (props["type"] == "double") { //Debug.Log ("Got double: " + nextField + "=" + datumValue); _hash.Add(nextField, Convert.ToDouble(datumValue)); } else if (props["type"] == "string") { //Debug.Log ("Got string: " + nextField + "=" + datumValue); _hash.Add(nextField, datumValue); } else if (props["type"] == "intarray") { //Debug.Log ("Got intarray: " + nextField + "=" + datumValue); string[] elements = datumValue.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries); int[] intarray = new int[elements.Length]; for (int i = 0; i < elements.Length; i++) { intarray[i] = Convert.ToInt32(elements[i]); } _hash.Add(nextField, intarray); } else if (props["type"] == "floatarray") { //Debug.Log ("Got floatarray: " + nextField + "=" + datumValue); string[] elements = datumValue.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries); float[] floatarray = new float[elements.Length]; for (int i = 0; i < elements.Length; i++) { floatarray[i] = Convert.ToSingle(elements[i]); } _hash.Add(nextField, floatarray); } else if (props["type"] == "doublearray") { //Debug.Log ("Got doublearray: " + nextField + "=" + datumValue); string[] elements = datumValue.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries); double[] doublearray = new double[elements.Length]; for (int i = 0; i < elements.Length; i++) { doublearray[i] = Convert.ToDouble(elements[i]); } _hash.Add(nextField, doublearray); } else if (props["type"] == "stringarray") { //Debug.Log ("Got stringarray: " + nextField + "=" + datumValue); string[] elements = datumValue.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries); string[] strarray = new string[elements.Length]; for (int i = 0; i < elements.Length; i++) { strarray[i] = elements[i]; } _hash.Add(nextField, strarray); } else { Debug.Log("Unknown VRDatum type: " + props["type"]); } xmlData = xmlDataRemaining; nextField = XMLUtils.GetNextXMLFieldName(xmlData); } xmlDescription = xmlRemaining; }