Ejemplo n.º 1
0
        private void FileExtractor_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            var dtafolder  = txtFolder.Text + "\\" + (organizeFilesByType.Checked ? "dta_files\\" : "");
            var dtacount   = 0;
            var midicount  = 0;
            var moggcount  = 0;
            var milocount  = 0;
            var pngcount   = 0;
            var thumbcount = 0;
            var hasdta     = false;
            var rename     = "";
            var counter    = 0;

            foreach (var file in inputFiles.Where(File.Exists).TakeWhile(file => !FileExtractor.CancellationPending))
            {
                try
                {
                    if (VariousFunctions.ReadFileType(file) != XboxFileType.STFS)
                    {
                        continue;
                    }
                    if (!Parser.ExtractDTA(file))
                    {
                        Log("Error extracting songs.dta from file: '" + Path.GetFileName(file) + "' ... skipping");
                        continue;
                    }
                    if (!Parser.ReadDTA(Parser.DTA) || !Parser.Songs.Any())
                    {
                        Log("Error reading that songs.dta file ... skipping");
                        continue;
                    }

                    try
                    {
                        xPackage = new STFSPackage(file);
                        if (!xPackage.ParseSuccess)
                        {
                            Log("Error opening file '" + Path.GetFileName(file) + "' ... skipping");
                            xPackage.CloseIO();
                        }
                        else
                        {
                            hasdta = true;

                            Log("CON file '" + Path.GetFileNameWithoutExtension(file) + "' contains " + Parser.Songs.Count + " " + (Parser.Songs.Count == 1 ? "song" : "songs"));
                            for (var i = 0; i < Parser.Songs.Count; i++)
                            {
                                if (FileExtractor.CancellationPending)
                                {
                                    xPackage.CloseIO();
                                    break;
                                }
                                var song = Parser.Songs[i];
                                counter++;
                                Log("Extracting files for song #" + (counter) + ": '" + song.Artist + " - " + song.Name + "'");
                                var songid = song.InternalName;

                                var name   = Tools.CleanString(song.Name, true);
                                var artist = Tools.CleanString(song.Artist, true);
                                songid = Tools.CleanString(songid, true);

                                rename = arrangeName(name, artist, songid).Replace("!", "").Replace("'", "");
                                rename = Tools.CleanString(rename, false);

                                if (chkMIDI.Checked && !FileExtractor.CancellationPending)
                                {
                                    if (ExtractFile("mid", false, songid, rename))
                                    {
                                        midicount++;
                                    }
                                }
                                if (chkMOGG.Checked && !FileExtractor.CancellationPending)
                                {
                                    if (ExtractFile("mogg", false, songid, rename))
                                    {
                                        moggcount++;
                                    }
                                }
                                if (chkPNG.Checked && !FileExtractor.CancellationPending)
                                {
                                    if (ExtractFile("png_xbox", true, songid, rename, true))
                                    {
                                        pngcount++;
                                    }
                                }
                                if (!chkMILO.Checked)
                                {
                                    continue;
                                }
                                if (ExtractFile("milo_xbox", true, songid, rename))
                                {
                                    milocount++;
                                }
                            }

                            if (FileExtractor.CancellationPending)
                            {
                                xPackage.CloseIO();
                                break;
                            }
                            var xUpgrade = xPackage.GetFile("songs_upgrades/upgrades.dta");
                            if (xUpgrade != null)
                            {
                                var temp_upg = Path.GetTempPath() + "upg.dta";
                                Tools.DeleteFile(temp_upg);

                                if (xUpgrade.ExtractToFile(temp_upg))
                                {
                                    var upg_midi = "";
                                    var sr       = new StreamReader(temp_upg);
                                    while (sr.Peek() >= 0)
                                    {
                                        var line = sr.ReadLine();
                                        if (string.IsNullOrWhiteSpace(line))
                                        {
                                            continue;
                                        }

                                        if (line.Contains("midi_file"))
                                        {
                                            upg_midi = line.Replace("midi_file", "").Replace("songs_upgrades", "")
                                                       .Replace("(", "").Replace(")", "").Replace("\"", "")
                                                       .Replace("/", "").Replace("'", "").Trim();
                                        }

                                        if (string.IsNullOrWhiteSpace(upg_midi))
                                        {
                                            continue;
                                        }
                                        if (chkMIDI.Checked)
                                        {
                                            var xmidi = xPackage.GetFile("songs_upgrades/" + upg_midi);
                                            if (xmidi != null)
                                            {
                                                var outputfolder = txtFolder.Text + "\\" + (organizeFilesByType.Checked ? "midi_files\\" : "");
                                                if (!Directory.Exists(outputfolder))
                                                {
                                                    Directory.CreateDirectory(outputfolder);
                                                }
                                                var out_midi = outputfolder + upg_midi;
                                                Log("Extracting MIDI file " + Path.GetFileName(out_midi));
                                                Tools.DeleteFile(out_midi);
                                                if (xmidi.ExtractToFile(out_midi))
                                                {
                                                    Log("Extracted " + Path.GetFileName(out_midi) + " successfully");
                                                    midicount++;
                                                }
                                                else
                                                {
                                                    Log("There was an error extracting upgrade MIDI file " + upg_midi);
                                                }
                                            }
                                            else
                                            {
                                                Log("Could not find upgrade MIDI file " + upg_midi + " in that file");
                                            }
                                        }
                                        upg_midi = "";
                                    }
                                    sr.Dispose();

                                    if (chkDTA.Checked)
                                    {
                                        if (!Directory.Exists(dtafolder))
                                        {
                                            Directory.CreateDirectory(dtafolder);
                                        }
                                        var upgdta = (string.IsNullOrWhiteSpace(xPackage.Header.Title_Display) ? Path.GetFileName(file) : xPackage.Header.Title_Display).Replace("!", "")
                                                     .Replace("'", "").Replace(" ", replaceSpacesWithUnderscores.Checked ? "_" : (removeSpacesFromFileName.Checked ? "" : " "));
                                        var upg_out = dtafolder + "\\" + Tools.CleanString(upgdta, false) + "_upgrade.dta";
                                        Tools.DeleteFile(upg_out);
                                        if (Tools.MoveFile(temp_upg, upg_out))
                                        {
                                            Log("Extracted " + Path.GetFileName(upg_out) + " successfully");
                                            dtacount++;
                                        }
                                        else
                                        {
                                            Log("There was an error extracting the upgrades.dta for " + Path.GetFileName(file));
                                        }
                                    }
                                }
                                else
                                {
                                    Log("There was an error extracting the upgrades.dta for " + Path.GetFileName(file));
                                }
                            }
                            else if (!hasdta)
                            {
                                Log("Could not find songs.dta or upgrades.dta inside '" + Path.GetFileName(file) + "'");
                            }
                        }

                        var packname = (string.IsNullOrWhiteSpace(xPackage.Header.Title_Display) ? Path.GetFileName(file) : xPackage.Header.Title_Display).Replace("!", "")
                                       .Replace("'", "").Replace(" ", replaceSpacesWithUnderscores.Checked ? "_" : (removeSpacesFromFileName.Checked ? "" : " "));
                        packname = Parser.Songs.Count == 1 && !string.IsNullOrWhiteSpace(rename) ? rename : Tools.CleanString(packname, false);

                        if (chkDTA.Checked && hasdta)
                        {
                            try
                            {
                                var newDTA = dtafolder + packname + (appendsongsToFiles.Checked ? "_songs" : "") + ".dta";
                                Log("Extracting DTA file " + Path.GetFileName(newDTA));
                                if (!Directory.Exists(dtafolder))
                                {
                                    Directory.CreateDirectory(dtafolder);
                                }
                                if (Parser.WriteDTAToFile(newDTA))
                                {
                                    Log(Path.GetFileName(newDTA) + " extracted successfully");
                                    dtacount++;
                                }
                                else
                                {
                                    Log("Looks like extracting the DTA file for " + Path.GetFileName(file) + " failed. Sorry.");
                                }
                            }
                            catch (Exception ex)
                            {
                                Log("Error extracting DTA file for " + Path.GetFileName(file));
                                Log("The error says: " + ex.Message);
                            }
                        }

                        if (chkThumbs.Checked && !FileExtractor.CancellationPending)
                        {
                            var thumbfolder = txtFolder.Text + "\\" + (organizeFilesByType.Checked ? "thumbnails\\" : "");
                            var package     = thumbfolder + packname + " Package.png";
                            var content     = thumbfolder + packname + " Content.png";
                            if (!Directory.Exists(thumbfolder))
                            {
                                Directory.CreateDirectory(thumbfolder);
                            }
                            try
                            {
                                Tools.DeleteFile(content);
                                var img = xPackage.Header.ContentImage;
                                img.Save(content, ImageFormat.Png);
                                img.Dispose();
                                thumbcount++;

                                Tools.DeleteFile(package);
                                img = xPackage.Header.PackageImage;
                                img.Save(package, ImageFormat.Png);
                                img.Dispose();
                                thumbcount++;

                                Log("Extracted thumbnails successfully");
                            }
                            catch (Exception ex)
                            {
                                Log("There was an error extracting the thumbnails");
                                Log("The error says: " + ex.Message);
                            }
                        }
                        xPackage.CloseIO();
                    }
                    catch (Exception ex)
                    {
                        Log("Error processing file '" + Path.GetFileName(file) + "'");
                        Log("The error says: " + ex.Message);
                        xPackage.CloseIO();
                    }
                }
                catch (Exception ex)
                {
                    Log("There was a problem accessing that file");
                    Log("The error says: " + ex.Message);
                    xPackage.CloseIO();
                }
            }

            if (dtacount + midicount + moggcount + milocount + pngcount + thumbcount == 0)
            {
                Log("Nothing was extracted ... please see the log for any failure reports");
            }
            else
            {
                if (dtacount > 0)
                {
                    Log("Extracted " + dtacount + " DTA " + (dtacount > 1 ? "files" : "file"));
                }
                if (pngcount > 0)
                {
                    Log("Extracted " + pngcount + " PNG_XBOX " + (pngcount > 1 ? "files" : "file"));
                    btnConverter.Invoke(new MethodInvoker(() => btnConverter.Visible = true));
                }
                if (midicount > 0)
                {
                    Log("Extracted " + midicount + " MIDI " + (midicount > 1 ? "files" : "file"));
                }
                if (moggcount > 0)
                {
                    Log("Extracted " + moggcount + " MOGG " + (moggcount > 1 ? "files" : "file"));
                }
                if (milocount > 0)
                {
                    Log("Extracted " + milocount + " MILO_XBOX " + (milocount > 1 ? "files" : "file"));
                }
                if (thumbcount > 0)
                {
                    Log("Extracted " + thumbcount + " " + (thumbcount > 1 ? "thumbnails" : "thumbnail"));
                }
            }
            xPackage.CloseIO();
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Tools.CurrentFolder = Path.GetDirectoryName(con);
            Log("Reading CON file " + Path.GetFileName(con));

            var dta    = Path.GetTempPath() + "temp_dta.txt";
            var backup = con + "_backup";

            Tools.DeleteFile(backup);

            if (!Parser.ExtractDTA(con))//grab byte[] content of DTA
            {
                Log("Error extracting DTA file");
                ErrorOut();
                return;
            }
            if (!Parser.WriteDTAToFile(dta)) //write it out to file
            {
                Log("Error extracting DTA file");
                ErrorOut();
                return;
            }

            var song = new STFSPackage(con);

            if (backUpCONFile.Checked)
            {
                Log("Found songs.dta file, making a backup of your file before starting");
                Log("THIS STEP MAY TAKE A WHILE. DON'T CLOSE ME DOWN!");

                song.CloseIO();
                File.Copy(con, backup);
                song = new STFSPackage(con);
            }

            var xDTA = song.GetFile("/songs/songs.dta");

            Log("Opening DTA file...");

            var process = Process.Start(dta);

            Log("DTA file is being edited by the user ... waiting...");

            do
            {
                //wait while user has DTA file opened
            } while (!process.HasExited);

            process.Dispose();

            Log("DTA file closed by user, continuing...");
            Log("Replacing old DTA file with modified DTA file");
            Log("THIS STEP MAY TAKE A WHILE. DON'T CLOSE ME DOWN!");

            if (!xDTA.Replace(dta))
            {
                Log("Error replacing DTA file with modified one");
                Tools.DeleteFile(backup);
                ErrorOut();
                return;
            }

            Log("Replaced DTA file successfully");
            song.Header.MakeAnonymous();
            song.Header.ThisType = PackageType.SavedGame;

            var success = false;

            try
            {
                Log("Saving changes to pack ... sit tight");
                Log("THIS STEP MAY TAKE A WHILE. DON'T CLOSE ME DOWN!");

                signature = new RSAParams(Application.StartupPath + "\\bin\\KV.bin");
                song.RebuildPackage(signature);
                song.FlushPackage(signature);
                song.CloseIO();
                success = true;
            }
            catch (Exception ex)
            {
                Log("There was an error: " + ex.Message);
                song.CloseIO();
            }

            if (success)
            {
                Log("Trying to unlock CON file");
                if (Tools.UnlockCON(con))
                {
                    Log("Unlocked CON file successfully");
                }
                else
                {
                    Log("Error unlocking CON file");
                    success = false;
                }
            }

            if (success)
            {
                if (Tools.SignCON(con))
                {
                    Log("CON file signed successfully");
                }
                else
                {
                    Log("Error signing CON file");
                    success = false;
                }
            }

            Tools.DeleteFile(dta);

            Log(success ? "Process completed successfully!" : "Something went wrong along the way, sorry!");
            if (success)
            {
                return;
            }
            if (!backUpCONFile.Checked)
            {
                return;
            }
            Log("Restoring backup file");
            Tools.DeleteFile(con);
            Log(Tools.MoveFile(backup, con) ? "Backup file restored successfully, nothing was lost" : "Sorry, there was an error restoring the backup file");
        }