Example #1
0
        /// <summary>
        /// Reads in an engine.xml file and turns it into a CaptionList, SpeakerSet and engine
        /// Settings.
        /// </summary>
        /// <param name="path">The Path to the engine.xml file.</param>
        /// <returns>A 3-Tuple containing a CaptionList, a SpeakerSet, and a Settings
        /// object in that specific order.</returns>
        public static Tuple <List <Caption>, Dictionary <string, Speaker>, SettingsXml> ParseEngineXml(string path)
        {
            var captionList = new List <Caption>();
            var speakerSet  = new Dictionary <string, Speaker>();
            var settings    = new SettingsXml();

            XmlReaderSettings readerSettings = new XmlReaderSettings
            {
                IgnoreWhitespace = true,
                IgnoreComments   = true
            };

            using (XmlReader r = XmlReader.Create(path, readerSettings))
            {
                while (r.Read())
                {
                    //Look for start elements only.
                    if (!r.IsStartElement())
                    {
                        continue;
                    }

                    // Get element name and switch on it.
                    switch (r.Name)
                    {
                    case XmlElements.Enact: break;

                    case XmlElements.Settings:

                        r.Read();
                        r.AssertNode(XmlElements.Meta);
                        settings.Base    = r.GetNonNullAttribute(XmlAttributes.Base);
                        settings.Spacing = r.GetNonNullAttribute(XmlAttributes.WordSpacing);
                        settings.SeparateEmotionWords = r.GetNonNullAttribute(XmlAttributes.SeparateEmotionWords);

                        r.Read();
                        r.AssertNode(XmlElements.Playback);
                        settings.Playback.AutoPlay     = r.GetBoolAttribute(XmlAttributes.AutoPlay);
                        settings.Playback.AutoRewind   = r.GetBoolAttribute(XmlAttributes.AutoRewind);
                        settings.Playback.Seek         = r.GetNonNullAttribute(XmlAttributes.Seek);
                        settings.Playback.AutoSize     = r.GetBoolAttribute(XmlAttributes.AutoSize);
                        settings.Playback.Scale        = r.GetIntAttribute(XmlAttributes.Scale);
                        settings.Playback.Volume       = r.GetIntAttribute(XmlAttributes.Volume);
                        settings.Playback.ShowCaptions = r.GetBoolAttribute(XmlAttributes.ShowCaptions);

                        r.Read();
                        r.AssertNode(XmlElements.Skin);
                        settings.Skin.Source           = r.GetNonNullAttribute(XmlAttributes.Source);
                        settings.Skin.AutoHide         = r.GetBoolAttribute(XmlAttributes.AutoHide);
                        settings.Skin.FadeTime         = r.GetIntAttribute(XmlAttributes.FadeTime);
                        settings.Skin.BackGroundAlpha  = r.GetIntAttribute(XmlAttributes.BackgroundAlpha);
                        settings.Skin.BackgroundColour = r.GetNonNullAttribute(XmlAttributes.BackgroundColour);

                        r.Read();
                        r.AssertNode(XmlElements.Video);
                        settings.VideoSource = r.GetNonNullAttribute(XmlAttributes.Source);

                        r.Read();
                        r.AssertNode(XmlElements.Emotions);
                        r.Read();
                        r.AssertNode(XmlElements.Happy);
                        settings.Happy.Fps         = r.GetNonNullAttribute(XmlAttributes.FPS);
                        settings.Happy.Duration    = r.GetNonNullAttribute(XmlAttributes.Duration);
                        settings.Happy.AlphaBegin  = r.GetNonNullAttribute(XmlAttributes.AlphaBegin);
                        settings.Happy.AlphaFinish = r.GetNonNullAttribute(XmlAttributes.AlphaFinish);
                        settings.Happy.ScaleBegin  = r.GetNonNullAttribute(XmlAttributes.ScaleBegin);
                        settings.Happy.ScaleFinish = r.GetNonNullAttribute(XmlAttributes.ScaleFinish);
                        settings.Happy.YFinish     = r.GetNonNullAttribute(XmlAttributes.YFinish);

                        r.Read();
                        r.AssertNode(XmlElements.Sad);
                        settings.Sad.Fps         = r.GetNonNullAttribute(XmlAttributes.FPS);
                        settings.Sad.Duration    = r.GetNonNullAttribute(XmlAttributes.Duration);
                        settings.Sad.AlphaBegin  = r.GetNonNullAttribute(XmlAttributes.AlphaBegin);
                        settings.Sad.AlphaFinish = r.GetNonNullAttribute(XmlAttributes.AlphaFinish);
                        settings.Sad.ScaleBegin  = r.GetNonNullAttribute(XmlAttributes.ScaleBegin);
                        settings.Sad.ScaleFinish = r.GetNonNullAttribute(XmlAttributes.ScaleFinish);
                        settings.Sad.YFinish     = r.GetNonNullAttribute(XmlAttributes.YFinish);

                        r.Read();
                        r.AssertNode(XmlElements.Fear);
                        settings.Fear.Fps         = r.GetNonNullAttribute(XmlAttributes.FPS);
                        settings.Fear.Duration    = r.GetNonNullAttribute(XmlAttributes.Duration);
                        settings.Fear.ScaleBegin  = r.GetNonNullAttribute(XmlAttributes.ScaleBegin);
                        settings.Fear.ScaleFinish = r.GetNonNullAttribute(XmlAttributes.ScaleFinish);
                        settings.Fear.VibrateX    = r.GetNonNullAttribute(XmlAttributes.VibrateX);
                        settings.Fear.VibrateY    = r.GetNonNullAttribute(XmlAttributes.VibrateY);

                        r.Read();
                        r.AssertNode(XmlElements.Anger);
                        settings.Anger.Fps         = r.GetNonNullAttribute(XmlAttributes.FPS);
                        settings.Anger.Duration    = r.GetNonNullAttribute(XmlAttributes.Duration);
                        settings.Anger.ScaleBegin  = r.GetNonNullAttribute(XmlAttributes.ScaleBegin);
                        settings.Anger.ScaleFinish = r.GetNonNullAttribute(XmlAttributes.ScaleFinish);
                        settings.Anger.VibrateX    = r.GetNonNullAttribute(XmlAttributes.VibrateX);
                        settings.Anger.VibrateY    = r.GetNonNullAttribute(XmlAttributes.VibrateY);
                        break;

                    case XmlElements.Speakers: break;     //Do Nothing

                    case XmlElements.Speaker:
                        r.AssertNode(XmlElements.Speaker);
                        string  name = r.GetNonNullAttribute(XmlAttributes.Name);
                        Speaker s    = new Speaker(name);

                        r.Read();
                        r.AssertNode(XmlElements.Background);
                        //Create background colour based on xml paramaters
                        bool   visible    = r.GetBoolAttribute(XmlAttributes.Visible);
                        double alphaD     = r.GetDoubleAttribute(XmlAttributes.Alpha);
                        int    colourCode = r.GetHexAttribute(XmlAttributes.Colour);
                        int    alpha      = (int)((visible) ? alphaD * 0xFF : 0x00);

                        // Bitshift alpha to the most significant byte of the integer
                        s.Font.BackgroundColour = Color.FromArgb(alpha << 24 | colourCode);

                        r.Read();
                        r.AssertNode(XmlElements.Font);
                        s.Font.Family           = r.GetNonNullAttribute(XmlAttributes.Name);
                        s.Font.Size             = r.GetIntAttribute(XmlAttributes.Size);
                        s.Font.ForegroundColour = Color.FromArgb(SpeakerFont.ForegroundAlphaValue << 24
                                                                 | r.GetHexAttribute(XmlAttributes.Colour));
                        s.Font.Bold = r.GetIntAttribute(XmlAttributes.Bold);
                        r.ReadStartElement(XmlElements.Font);

                        //Add to speakerSet
                        speakerSet[s.Name] = s;
                        break;

                    case XmlElements.Captions: break;     //Do Nothing

                    case XmlElements.Caption:
                        r.AssertNode(XmlElements.Caption);
                        Caption c = new Caption
                        {
                            Begin     = r.GetNonNullAttribute(XmlAttributes.Begin),
                            End       = r.GetNonNullAttribute(XmlAttributes.End),
                            Speaker   = speakerSet[r.GetNonNullAttribute(XmlAttributes.Speaker)],
                            Location  = (ScreenLocation)r.GetIntAttribute(XmlAttributes.Location),
                            Alignment = (Alignment)r.GetIntAttribute(XmlAttributes.Align)
                        };

                        List <CaptionWord> wordList = new List <CaptionWord>();

                        while (r.Read())
                        {
                            //If the Node is an end element, then the reader has parsed
                            //through all of this caption's words.
                            if (r.NodeType == XmlNodeType.EndElement && r.Name.Equals(XmlElements.Caption))
                            {
                                break;
                            }
                            if (r.NodeType == XmlNodeType.Element && r.Name.Equals(XmlElements.Word))
                            {
                                r.AssertNode(XmlElements.Word);     //Doublecheck, it's the only way to be sure.

                                Emotion   e = (Emotion)r.GetIntAttribute(XmlAttributes.Emotion);
                                Intensity i = (Intensity)r.GetIntAttribute(XmlAttributes.Intensity);

                                //Get word from node and add it to the list
                                CaptionWord word = new CaptionWord(e, i, r.ReadString(), 0);
                                wordList.Add(word);
                            }
                        }
                        c.Words.SetWordList(wordList);
                        captionList.Add(c);
                        break;

                    default: throw new ArgumentException("Value '" + r.Name + "' is not a valid node", r.Name);
                    }
                }//Enact
            }

            return(Tuple.Create(captionList, speakerSet, settings));
        }