private string ExtractMIDI(string con, bool isUpgrade) { var midi = ""; var xPackage = new STFSPackage(con); if (!xPackage.ParseSuccess) { MessageBox.Show("There was an error parsing CON file, can't extract MIDI", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); Log("Can't work with this CON file, try again"); return(""); } try { var xent = xPackage.GetFolder("songs"); if (xent == null && !isUpgrade) { xent = xPackage.GetFolder("songs_upgrades"); MessageBox.Show(xent != null ? "This looks like a pro upgrade, only song files are valid here" : "I can't find a 'songs' folder in that CON file, make sure sure it's a Rock Band song file", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); xPackage.CloseIO(); Log("Can't work with this CON file, try again"); return(""); } //we can't work with packs or pro upgrades, so check and skip xent = xPackage.GetFolder("songs_upgrades"); if (xent != null && !isUpgrade) { xent = xPackage.GetFolder("songs"); MessageBox.Show(xent != null ? "It looks like this is a pack, only individual song files are valid here" : "This looks like a pro upgrade, only song files are valid here", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); xPackage.CloseIO(); Log("Can't work with this CON file, try again"); return(""); } var folder = isUpgrade ? "songs_upgrades/" : "songs/"; var dtaFile = isUpgrade ? "upgrades.dta" : "songs.dta"; var dta = temp_folder + dtaFile; if (Parser.ExtractDTA(xPackage, false, isUpgrade)) { if (Parser.ReadDTA(Parser.DTA) && Parser.Songs.Count > 1) { MessageBox.Show("It looks like this is a pack, only individual song files are valid here", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); xPackage.CloseIO(); Log("Can't work with this CON file, try again"); return(""); } } var xFile = xPackage.GetFile(folder + dtaFile); if (xFile == null) { MessageBox.Show("Can't find " + dtaFile + " inside this CON file\nI can't work without it", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); xPackage.CloseIO(); Log("Can't work with this CON file, try again"); return(""); } var fileName = Path.GetFileName(con); if (fileName != null) { if (!Parser.WriteDTAToFile(dta)) { MessageBox.Show("Something went wrong in extracting the " + dtaFile + " file\nI can't work without it", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); xPackage.CloseIO(); Log("Can't work with this CON file, try again"); return(""); } } var artists = 0; var songname = ""; var sr = new StreamReader(dta, Parser.GetDTAEncoding(Parser.DTA)); // read one line at a time until the end while (sr.Peek() >= 0) { var line = sr.ReadLine(); if (string.IsNullOrWhiteSpace(line.Trim())) { continue; } if (line.ToLowerInvariant().Contains("artist") && !line.ToLowerInvariant().Contains(";") && !isUpgrade) { artists++; } else if (line.ToLowerInvariant().Contains("songs/") && !line.Contains("midi_file") && !isUpgrade) { songname = Parser.GetInternalName(line); song_int_name = songname; } else if (line.Contains("song_id")) { if (isUpgrade) { upgradeID = Parser.GetSongID(line); } else { songID = Parser.GetSongID(line); CheckIDMatch(); } } else if (line.Contains("midi_file") && isUpgrade) { var midipath = line.Replace("(", ""); midipath = midipath.Replace(")", ""); midipath = midipath.Replace("midi_file", ""); midipath = midipath.Replace("songs_upgrades", ""); midipath = midipath.Replace("\"", ""); midipath = midipath.Replace("/", ""); songname = midipath.Trim(); upgrade_int_name = songname.Replace(".mid", ""); } } sr.Dispose(); if (artists > 1) //if single song, packs will have values > 1 { MessageBox.Show("It looks like this is a pack, only individual song files are valid here", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); xPackage.CloseIO(); Log("Can't work with this CON file, try again"); return(""); } xFile = xPackage.GetFile(folder + songname + (isUpgrade ? "" : "/" + songname + ".mid")); if (xFile != null) { midi = temp_folder + songname + (isUpgrade? "" : ".mid"); Tools.DeleteFile(midi); if (!xFile.ExtractToFile(midi)) { MessageBox.Show("Can't find a MIDI file inside this CON file\nI can't work without it", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); xPackage.CloseIO(); Log("Can't work with this CON file, try again"); return(""); } } xPackage.CloseIO(); if (isUpgrade) { newupgdta = dta; ReadUpgDTA(newupgdta); } else { orig_dta = dta; } } catch (Exception ex) { MessageBox.Show("There was an error:\n" + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); xPackage.CloseIO(); } return(midi); }
private void RBAPatchSongsDta(bool isRBS) { var fsongsraw = tempfolder + "songs.dta.raw"; var fsongsdta = tempfolder + "songs.dta"; try { // Create an instance of StreamReader to read from a file. using (var srSongsRaw = new StreamReader(fsongsraw, Encoding.Default)) { var swSongsDta = new StreamWriter(fsongsdta, false, Encoding.Default); using (swSongsDta) { // copy the first line var line = srSongsRaw.ReadLine(); swSongsDta.WriteLine(line); // patch 'song' in the second line to be '<basename>' line = srSongsRaw.ReadLine(); if (line != null && line.Contains("'song'")) { line = " '" + basesongname + "'"; } swSongsDta.WriteLine(line); // Read and display lines from the file until the end of // the file is reached. while ((line = srSongsRaw.ReadLine()) != null) { if (line.Contains("'name'")) { swSongsDta.WriteLine(line); line = srSongsRaw.ReadLine(); if (line != null && !line.Contains("songs/")) { SongTitle = Parser.GetSongName(line); } // - "songs/song/song" => "songs/<basename>/<basename" if (line != null && line.Contains("songs/")) { if (isRBS) { basesongname = Parser.GetInternalName(line); } else { line = " \"songs/" + basesongname + "/" + basesongname + "\""; } } } else if (line.Contains("songs/") && line.Contains(".mid")) { line = " \"songs/" + basesongname + "/" + basesongname + ".mid"; } else if (line.Contains("'artist'")) { swSongsDta.WriteLine(line); line = srSongsRaw.ReadLine(); SongArtist = Parser.GetArtistName(line); } // - ('song_id' 0) => ('song_id' <basename>) else if (line.Contains("('song_id' 0)")) { line = " ('song_id' " + basesongname + ")"; } else if (line.Contains("('real_bass' 0)") || line.Contains("('real_guitar' 0)")) { // Swallow this line - don't put it in the final songs.dta // because it prevents you from making a separate upgrades.dta //only if there is no pro bass in the song! line = ""; } if (line != "") { swSongsDta.WriteLine(line); } if (srSongsRaw.EndOfStream) { break; } } } swSongsDta.Dispose(); srSongsRaw.Dispose(); } } catch (Exception ex) { Log("There was an error:"); Log(ex.Message); } }