Ejemplo n.º 1
0
        public static object DeserializeFrom(string file, int strophe, Config config)
        {
            XmlDocument xmlDoc = new XmlDocument();

            try {
                xmlDoc.Load(file);
            } catch {
                return(new Song(config));
            }
            Song s       = null;
            bool newSong = false;

            XmlNode root = xmlDoc.DocumentElement;
            XmlNode versionNode;

            if (root.Name.Equals("NewSong"))
            {
                newSong     = true;
                versionNode = xmlDoc.SelectSingleNode(@"/NewSong/Version");
            }
            else
            {
                versionNode = xmlDoc.SelectSingleNode(@"/DreamSong/Version");
            }
            //XmlNodeList nodes = xmlDoc.GetElementsByTagName("Version");
            Console.WriteLine("Loading: " + file);
            if (versionNode != null)
            {
                float version = 0;
                try {
                    version = float.Parse(versionNode.InnerText);
                    if (version < 5)
                    {
                        version = version * 100;              // a Localization bug might exist - this would fix it
                    }
                } catch {
                    version = 0;
                }
                if (version < 50)
                {
                    OldSong oldS = new OldSong(file);
                    if (oldS.strophe_count > 0)
                    {
                        s = new Song(config, oldS);
                    }
                    else
                    {
                        s = new Song(config);
                    }
                }
                else if (version <= 71 || newSong)
                {
                    /* Prior to version 0.72 old songs were saved with NewSong XML root
                     * instead of DreamSong and contained an node called BGImagePath. The
                     * BGImagePath has now been generalized to ThemePath.
                     */
                    if (newSong)
                    {
                        DreamTools.RenameXmlNode(root, root.NamespaceURI, "DreamSong");
                        XmlNode bgImageNode = xmlDoc.SelectSingleNode(@"/DreamSong/BGImagePath");
                        if (bgImageNode != null)
                        {
                            DreamTools.RenameXmlNode(bgImageNode, root.NamespaceURI, "ThemePath");
                        }
                        s = (Song)Song.DeserializeFrom(typeof(Song), new StringReader(xmlDoc.OuterXml));

                        if (s != null)
                        {
                            // There are some songs from 0.7x days with <NewSong> and version == 1.00.
                            // Let's save them back automatically with the correct version.
                            // Version 0.71 allowed the user to save files with keys such as "D# / Eb".
                            s.KeyRangeLow  = NormalizeKey(s.KeyRangeLow);
                            s.KeyRangeHigh = NormalizeKey(s.KeyRangeHigh);
                            Song.SerializeTo(s, file);
                        }
                    }
                    else
                    {
                        s = (Song)Song.DeserializeFrom(typeof(Song), file);
                    }
                }
                else                     // version >= 0.72

                {
                    s = (Song)Song.DeserializeFrom(typeof(Song), file);
                }
                // Songs which don't have the AutoSequencerOption
                if (version <= 82)
                {
                    s.AutoSequencer = false;     // For a new Song the Autosequencer should be true - only for older versions it has to be disabled
                }
            }

            if (s != null)
            {
                if (s.Theme == null)
                {
                    // OldSong did not have a custom theme. Give it the default theme.
                    // If we don't clone the theme, background changes will be saved as part of the default theme.
                    s.Theme = (Theme)config.theme.Song.Clone();
                    // Hide the theme path so it doesn't look like the song has a custom theme.
                    s.Theme.ThemeFile = null;
                }

                s.config       = config;
                s.CurrentLyric = strophe;
                s.FileName     = file;
                return(s);
            }
            else
            {
                return(new Song(config));
            }
        }