Ejemplo n.º 1
0
        /// <summary>
        /// Reads the IV/Play Data file, technically should work with a compressed mame data file as well.
        /// </summary>
        public static void ReadDat()
        {
            //Get the mame commands this seems like the best place to do it
            SettingsManager.MameCommands = new MameCommands(Settings.Default.MAME_EXE);

            String gameElementName = "game";

            Games = new Games();
            var hiddenGames = new System.Collections.Hashtable();

            if (File.Exists("Hidden.ini"))
            {
                foreach (var item in File.ReadAllLines("Hidden.ini"))
                {
                    if (!hiddenGames.ContainsKey(item))
                    {
                        hiddenGames.Add(item, true);
                    }
                }
            }

            XmlReaderSettings xs = new XmlReaderSettings();

            xs.DtdProcessing    = DtdProcessing.Ignore;
            xs.ConformanceLevel = ConformanceLevel.Fragment;
            if (File.Exists("IV-Play.dat"))
            {
                using (FileStream infileStream = new FileStream(@"IV-Play.dat", FileMode.Open))
                {
                    using (GZipStream gZipStream = new GZipStream(infileStream, CompressionMode.Decompress))
                    {
                        using (StreamReader streamReader = new StreamReader(gZipStream, Encoding.ASCII))
                        {
                            using (XmlReader xmlReader = XmlReader.Create(streamReader, xs))
                            {
                                //Get the MAME version info
                                xmlReader.ReadToFollowing("mame");
                                Games.MameVersion = xmlReader["build"];

                                float floatMameVersion = float.Parse(Games.MameVersion.Substring(0, 5), System.Globalization.CultureInfo.GetCultureInfo("en-US"));

                                if (floatMameVersion >= 0.162)
                                {
                                    gameElementName = "machine";
                                }

                                //Read Game elements
                                while (xmlReader.ReadToFollowing(gameElementName))
                                {
                                    //No bios sets should be in our XML, but check anyways
                                    if (!string.IsNullOrEmpty(xmlReader["isbios"]) &&
                                        xmlReader["isbios"].Equals("yes"))
                                    {
                                        continue;
                                    }
                                    string sIsMechanical = "";
                                    string sourcefile    = "";
                                    string name          = "";
                                    string parent        = "";
                                    string description   = "";
                                    string year          = "";
                                    string manufacturer  = "";
                                    bool   status        = true;
                                    string driver        =
                                        "Status={0}, Emulation={1}, Color={2}, Sound={3}, Graphic={4}, Savestate={5}";
                                    string        colors  = "";
                                    string        input   = "";
                                    string        display = "";
                                    StringBuilder sbRoms  = new StringBuilder();
                                    StringBuilder sbAudio = new StringBuilder();
                                    StringBuilder sbCpu   = new StringBuilder();

                                    name          = xmlReader["name"];
                                    sourcefile    = xmlReader["sourcefile"];
                                    parent        = xmlReader["cloneof"];
                                    sIsMechanical = xmlReader["ismechanical"];


                                    while (xmlReader.Read())
                                    {
                                        //If we reached a game element here it means we must have reached the
                                        //end of the current game, so we break out of this loop to find the next game.
                                        if (xmlReader.Name.Equals(gameElementName))
                                        {
                                            break;
                                        }

                                        switch (xmlReader.Name)
                                        {
                                        case "description":
                                            description = xmlReader.ReadElementString();
                                            description = description.TrimStart('\'');
                                            break;

                                        case "year":
                                            year = xmlReader.ReadString();
                                            break;

                                        case "manufacturer":
                                            manufacturer = xmlReader.ReadString();
                                            break;

                                        case "rom":
                                        case "disk":
                                            sbRoms.AppendLine(xmlReader["region"] + "\t" + xmlReader["name"]);
                                            break;

                                        case "chip":
                                            if (xmlReader["type"] == "cpu")
                                            {
                                                string clock = !string.IsNullOrEmpty(xmlReader["clock"])
                                                                       ? " " +
                                                               (Convert.ToSingle(xmlReader["clock"]) / 1000000).
                                                               ToString().TrimEnd('0').TrimEnd('.') +
                                                               " MHz"
                                                                       : "";
                                                sbCpu.AppendLine(xmlReader["name"] + clock);
                                            }

                                            if (xmlReader["type"] == "audio")
                                            {
                                                string clock = !string.IsNullOrEmpty(xmlReader["clock"])
                                                                       ? " " +
                                                               (Convert.ToSingle(xmlReader["clock"]) / 1000).
                                                               ToString().TrimEnd('0').TrimEnd('.') +
                                                               " kHz"
                                                                       : "";
                                                sbAudio.AppendLine(xmlReader["name"] + clock);
                                            }
                                            break;

                                        case "driver":
                                            driver = string.Format(driver, xmlReader["status"],
                                                                   xmlReader["emulation"], xmlReader["color"],
                                                                   xmlReader["sound"], xmlReader["graphic"],
                                                                   xmlReader["savestate"]);

                                            //Colors is a separate element
                                            colors = xmlReader["palettesize"];

                                            //Cocktail and protection are not mandatory, so checking them.
                                            if (!string.IsNullOrEmpty(xmlReader["cocktail"]))
                                            {
                                                driver += string.Format(", Cocktail={0}", xmlReader["cocktail"]);
                                            }
                                            if (!string.IsNullOrEmpty(xmlReader["protection"]))
                                            {
                                                driver += string.Format(", Protection={0}",
                                                                        xmlReader["protection"]);
                                            }
                                            status = xmlReader["emulation"].Equals("good");
                                            break;

                                        case "display":

                                            string refresh = xmlReader["refresh"].TrimEnd('0').TrimEnd('.') + " Hz";

                                            char rotation = xmlReader["rotate"] == "0" ||
                                                            xmlReader["rotate"] == "180"
                                                                    ? 'H'
                                                                    : 'V';
                                            display = xmlReader["width"] + "x" + xmlReader["height"] + " (" +
                                                      rotation + ") " + refresh;
                                            break;

                                        case "input":
                                            if (xmlReader.IsStartElement())
                                            {
                                                input  = xmlReader["players"] + " Player(s) ";
                                                input += xmlReader["buttons"] + " Button(s)";
                                            }
                                            break;

                                        case "control":
                                            input += " " + xmlReader["type"];
                                            break;
                                        }
                                    }
                                    Game game = new Game
                                    {
                                        CloneOf      = string.IsNullOrEmpty(parent) ? name : parent,
                                        CPU          = sbCpu.ToString(),
                                        Description  = description,
                                        SourceFile   = sourcefile,
                                        Name         = name,
                                        Manufacturer = manufacturer,
                                        ParentSet    = parent,
                                        Screen       = display.Trim(),
                                        Sound        = sbAudio.ToString(),
                                        Working      = status,
                                        Year         = year,
                                        IconPath     = Settings.Default.icons_directory + name + ".ico",
                                        Driver       = driver.Trim(),
                                        Input        = input.Trim(),
                                        Display      = display.Trim(),
                                        Colors       = colors,
                                        Roms         = sbRoms.ToString(),
                                        IsMechanical = isMechanical(sIsMechanical)
                                    };
                                    if (!hiddenGames.ContainsKey(game.Name))
                                    {
                                        Games.Add(game.Name, game);
                                    }
                                } //while readto game
                            }     //using xmlreader
                        }         //streamreader
                    }             //gzip
                }                 //filestream

                Games.TotalGames = Games.Count;

                //Go through all the games and add clones to the parents.
                //We can't do it while reading the XML because the clones can come before a parent.
                foreach (var game in Games)
                {
                    if (!game.Value.IsParent && Games.ContainsKey(game.Value.ParentSet))
                    {
                        Games[game.Value.ParentSet].Children.Add(game.Value.Description, game.Value);
                    }
                }

                //Create a new, and final list of games of just the parents, who now have clones in them.
                ParsedGames = new Games();
                foreach (var game in Games)
                {
                    if (game.Value.IsParent) //parent set, goes in
                    {
                        ParsedGames.Add(game.Value.Name, game.Value);
                    }
                }

                //Store this information for the titlebar later
                ParsedGames.TotalGames  = Games.TotalGames;
                ParsedGames.MameVersion = Games.MameVersion;

                //games = null; //No need for this anymore, will be collected by the GC
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads our favorites from favorites.ini
        /// </summary>
        /// <returns>How many favorites have been added already to our internal list.</returns>
        private int LoadFavorites()
        {
            _gamesFavorites = new Games();
            _countFavorites = 0;
            if (File.Exists(Settings.Default.favorites_ini))
            {
                string[] favs = File.ReadAllLines(Settings.Default.favorites_ini);
                foreach (string s in favs) //Some of the lines are not favorites, just ignores them.
                {
                    try
                    {
                        Game game = XmlParser.Games.FindGame(s);
                        if (game != null)
                        {
                            Game g = game.Copy();
                            g.Name       = "fav_" + g.Name;
                            g.IsFavorite = true;
                            _gamesFavorites.Add(g.Name, g);
                        }
                    }
                    catch (Exception)
                    {
                        //Not a game, do nothing.
                    }
                }

                //Sorts out the list by description alphabetically and filters it according to what the user set.
                var sortedDict = (from entry in _gamesFavorites
                                  where
                                  entry.Value.Name.Contains(_filter, StringComparison.InvariantCultureIgnoreCase) ||
                                  entry.Value.Manufacturer.Contains(_filter,
                                                                    StringComparison.InvariantCultureIgnoreCase) ||
                                  entry.Value.Year.Contains(_filter,
                                                            StringComparison.InvariantCultureIgnoreCase) ||
                                  entry.Value.SourceFile.Contains(_filter,
                                                                  StringComparison.InvariantCultureIgnoreCase) ||
                                  entry.Value.Description.Contains(_filter,
                                                                   StringComparison.InvariantCultureIgnoreCase)
                                  orderby entry.Value.Description.ToLower() ascending
                                  select entry);


                //Add the filtered favorites to our game list.
                Game prevGame = null;
                int  i        = 0;
                foreach (var fGame in sortedDict)
                {
                    fGame.Value.Index = i++;
                    Games.Add(fGame.Key, fGame.Value);
                    _countFavorites++;
                    if (prevGame != null)
                    {
                        prevGame.NextGame = fGame.Value;
                    }
                    fGame.Value.PreviousGame = prevGame;
                    prevGame = fGame.Value;
                }
                return(i);
            }
            return(0);
        }