Пример #1
0
        private void ConvertSourceToTR5MainToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Wad2 dest = WadActions.ConvertWad2ToTR5Main(_tool, this, _tool.SourceWad);

            Wad2Writer.SaveToFile(dest, "F:\\temp.wad2");
            WadActions.LoadWad(_tool, this, true, "F:\\temp.wad2");
        }
Пример #2
0
        public static void SaveWad(WadToolClass tool, IWin32Window owner, Wad2 wadToSave, bool ask)
        {
            if (wadToSave == null)
            {
                tool.SendMessage("You have no wad opened. Nothing to save.", PopupType.Warning);
                return;
            }

            // Figure out the output path
            string outPath = wadToSave.FileName;

            if (!string.IsNullOrWhiteSpace(wadToSave.FileName))
            {
                try
                {
                    outPath = Path.ChangeExtension(outPath, "wad2");
                }
                catch
                {
                    // ignored
                }
            }


            // Ask about it
            if (ask || string.IsNullOrWhiteSpace(outPath))
            {
                using (var dialog = new SaveFileDialog())
                {
                    if (!string.IsNullOrWhiteSpace(outPath))
                    {
                        dialog.InitialDirectory = Path.GetDirectoryName(outPath);
                        dialog.FileName         = Path.GetFileName(outPath);
                    }
                    dialog.Filter       = Wad2Writer.FileFormats.GetFilter();
                    dialog.Title        = "Save Wad2";
                    dialog.AddExtension = true;
                    if (dialog.ShowDialog(owner) != DialogResult.OK)
                    {
                        return;
                    }
                    outPath = dialog.FileName;
                }
            }

            // Save the wad2
            try
            {
                // XML_SOUND_SYSTEM
                Wad2Writer.SaveToFile(wadToSave, outPath);

                // Immediately reload new wad, if it wasn't saved before (new or imported)
                if (wadToSave.FileName == null)
                {
                    LoadWad(tool, owner, true, outPath);
                }

                // Update last actual filename and call global event to update UI etc
                wadToSave.FileName = outPath;
                tool.ToggleUnsavedChanges(false);
            }
            catch (Exception exc)
            {
                logger.Warn(exc, "Unable to save to '" + outPath + "'");
                tool.SendMessage("Unable to save to '" + outPath + "'.   " + exc, PopupType.Error);
            }
        }
Пример #3
0
        public static bool ConvertWad2ToNewSoundFormat(string src, string dest)
        {
            /* PROCEDURE:
             * 1. Collect all sounds from Wad2
             * 2. Initialise the list of SoundInfoConversionRow, getting ID from TrCatalog
             * 3. Show the dialog to the user. Here he can load an additional catalog if he changed sounds via TRLE tools.
             *    He can also choose which sounds to export to Xml and if export also samples.
             * 4. Assign new IDs to sound infos
             * 5. Remap sounds in animcommands
             * 6. Optionally export samples if needed and bind them to sound infos
             * 7. Save Wad2 + Xml (if sounds are present)
             */

            try
            {
                // Load Wad2
                Wad2 wad = Wad2Loader.LoadFromFile(src, false);

                // Check if the Wad2 needs to be converted
                if (wad.SoundSystem != SoundSystem.Dynamic)
                {
                    return(true);
                }

                // Now collect all sound infos from obsolete lists and build a new list
                var soundInfos = wad.AllLoadedSoundInfos.Values.ToList();

                // Loop through each sound info and try to get the classic Id from TrCatalog.xml
                var conversionList = new List <SoundInfoConversionRow>();
                foreach (var soundInfo in soundInfos)
                {
                    var row = new SoundInfoConversionRow(soundInfo, soundInfo.Name);

                    // If user has changed name, result will be -1 and the user will need to manually set the new sound id
                    row.NewId = TrCatalog.TryGetSoundInfoIdByDescription(wad.GameVersion, soundInfo.Name);
                    if (row.NewId != -1)
                    {
                        row.NewName = TrCatalog.GetOriginalSoundName(wad.GameVersion, (uint)row.NewId);
                    }

                    conversionList.Add(row);
                }

                // Now we'll show a dialog with all conversion rows and the user will need to make some choices
                WadSounds sounds = null;
                using (var form = new Wad2SoundsConversionDialog(wad.GameVersion, conversionList))
                {
                    if (form.ShowDialog() == DialogResult.Cancel)
                    {
                        return(false);
                    }

                    // If the user has loaded a custom catalog, let's get a pointer to it
                    if (form.Sounds != null)
                    {
                        sounds = form.Sounds;
                    }
                }

                // Assign new Id and name
                foreach (var row in conversionList)
                {
                    row.SoundInfo.Id   = row.NewId;
                    row.SoundInfo.Name = row.NewName;
                }

                // Remap all sounds in animcommands
                foreach (var row in conversionList)
                {
                    if (row.NewId != -1)
                    {
                        foreach (var moveable in wad.Moveables)
                        {
                            foreach (var animation in moveable.Value.Animations)
                            {
                                foreach (var cmd in animation.AnimCommands)
                                {
                                    if (cmd.SoundInfoObsolete != null && cmd.SoundInfoObsolete == row.SoundInfo)
                                    {
                                        cmd.Parameter2        = (short)((cmd.Parameter2 & 0xc000) | row.NewId);
                                        cmd.SoundInfoObsolete = null;
                                    }
                                }
                            }
                        }
                    }
                }

                // Bind samples (only if additional catalog was loaded, TrCatalog has not samples names)
                if (sounds != null)
                {
                    foreach (var row in conversionList)
                    {
                        if (row.SaveToXml)
                        {
                            if (row.ExportSamples)
                            {
                                // We export samples only if user has marked both Export to Xml and Export samples checkboxes
                                var samples = new List <string>();
                                foreach (var sample in row.SoundInfo.Samples)
                                {
                                    if (sample.IsLoaded)
                                    {
                                        // If sample is valid, export the .WAV file to the same directory of Wad2
                                        string sampleName = row.NewName.ToLower() + "_" + row.SoundInfo.Samples.IndexOf(sample) + ".wav";
                                        samples.Add(sampleName);
                                        File.WriteAllBytes(Path.GetDirectoryName(dest) + "\\" + sampleName, sample.Data);
                                    }
                                }

                                // Assign new samples names
                                row.SoundInfo.Samples.Clear();
                                foreach (var sample in samples)
                                {
                                    row.SoundInfo.Samples.Add(new WadSample(sample));
                                }
                            }
                            else
                            {
                                // If samples count is the same then use samples from catalog
                                var refInfo = sounds.TryGetSoundInfo(row.NewId);

                                if (refInfo != null && row.SoundInfo.Samples.Count == refInfo.Samples.Count)
                                {
                                    row.SoundInfo.Samples.Clear();
                                    row.SoundInfo.Samples.AddRange(refInfo.Samples);
                                }
                                else
                                {
                                    row.SoundInfo.Samples.Clear();
                                }
                            }
                        }
                    }
                }

                // Create the new sounds archive
                foreach (var row in conversionList)
                {
                    if (row.SaveToXml)
                    {
                        wad.Sounds.SoundInfos.Add(row.SoundInfo);
                    }
                }

                // Sort sound infos
                wad.Sounds.SoundInfos.Sort((a, b) => a.Id.CompareTo(b.Id));

                // Make a backup copy
                if (src == dest)
                {
                    int    index          = 0;
                    string backupFilename = "";
                    while (true)
                    {
                        backupFilename = dest + "." + index + ".bak";
                        if (!File.Exists(backupFilename))
                        {
                            break;
                        }
                        index++;
                    }

                    File.Copy(src, backupFilename, true);
                }

                // Save Wad2 with Xml sounds
                wad.SoundSystem = SoundSystem.Xml;
                Wad2Writer.SaveToFile(wad, dest);

                // Finished!
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }