private Games CreateGamesFromMachines(List <Machine> machines) { var 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); } } } var parents = (from m in machines where m.cloneof == null && !hiddenGames.ContainsKey(m.name) select m); var clones = (from m in machines where m.cloneof != null && !hiddenGames.ContainsKey(m.name) select m); foreach (var machine in parents) { var game = new Game(machine); games.TryAdd(game.Name, game); } //Go through all the games and add clones to the parents. foreach (var machine in clones) { var game = new Game(machine); if (games.ContainsKey(game.ParentSet)) { games[game.ParentSet].Children.Add(game.Description, game); } } return(games); }
/// <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 } }