using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO; namespace SCJMapper_V2 { /// /// Maintains the complete ActionMaps - something like: /// /// /// /// /// /// /// ... /// /// /// class ActionMapsCls : List { public String version { get; set; } // own additions for JS mapping - should not harm.. public String js1 { get; set; } public String js2 { get; set; } public String js3 { get; set; } public String js4 { get; set; } public String js5 { get; set; } public String js6 { get; set; } public String js7 { get; set; } public String js8 { get; set; } /// /// ctor /// public ActionMapsCls( ) { version = "0"; js1 = ""; js2 = ""; js3 = ""; js4 = ""; js5 = ""; js6 = ""; js7 = ""; js8 = ""; } /// /// Merge the given Map with this Map /// new ones are ignored - we don't learn from XML input for the time beeing /// /// private void Merge( ActionMapCls newAcm ) { // do we find an actionmap like the new one in our list ? ActionMapCls ACM = this.Find( delegate( ActionMapCls acm ) { return acm.name == newAcm.name; } ); if ( ACM == null ) { ; // this.Add( newAcm ); // no, add new } else { ACM.Merge( newAcm ); // yes, merge it } } /// /// Dump the ActionMaps as partial XML nicely formatted /// /// the action as XML fragment public String toXML( ) { String r = String.Format( "\n"); foreach ( ActionMapCls amc in this ) { r += String.Format( "{0}\n", amc.toXML( ) ); } r += String.Format( "\n" ); return r; } /// /// Read an ActionMaps from XML - do some sanity check /// /// the XML action fragment /// True if an action was decoded public Boolean fromXML( String xml ) { XmlReaderSettings settings = new XmlReaderSettings( ); settings.ConformanceLevel = ConformanceLevel.Fragment; settings.IgnoreWhitespace = true; settings.IgnoreComments = true; XmlReader reader = XmlReader.Create( new StringReader( xml ), settings ); reader.Read( ); if ( reader.Name == "ActionMaps" ) { if ( reader.HasAttributes ) { version = reader["version"]; // get the joystick mapping if there is one js1 = reader["js1"]; js2 = reader["js2"]; js3 = reader["js3"]; js4 = reader["js4"]; js5 = reader["js5"]; js6 = reader["js6"]; js7 = reader["js7"]; js8 = reader["js8"]; } else { return false; } } reader.Read( ); // move to next element String x = reader.ReadOuterXml( ); while ( !String.IsNullOrEmpty( x ) ) { ActionMapCls acm = new ActionMapCls( ); if ( acm.fromXML( x ) ) { this.Merge( acm ); // merge list } x = reader.ReadOuterXml( ); } return true; } } }