Exemple #1
0
        private bool ImportSoundPack(AppDatabase db)
        {
            // Define folder path
            string folderName = folderNameBox.Text.MakeFileNameSafe();
            string folderPath = Path.Combine(Program.RootPath, "sounds", Package.PackageTypeFolderName, folderName);

            // Delete old data
            if (!NewPackage)
            {
                // Delete the old sounds
                switch (Package.SoundType)
                {
                case SoundType.Engine:
                    var ep = (EngineSoundPackage)Package;
                    foreach (var sound in ep.EngineSounds)
                    {
                        db.EngineSounds.Remove(sound);
                    }
                    break;

                case SoundType.Truck:
                    var tp = (TruckSoundPackage)Package;
                    foreach (var sound in tp.TruckSounds)
                    {
                        db.TruckSounds.Remove(sound);
                    }
                    break;

                default:
                    throw new Exception("Invalid sound type");
                }
            }

            // Delete existing data if it is there.
            if (Directory.Exists(folderPath))
            {
                string       query = String.Empty;
                SoundPackage existing;

                // Fetch existing sound from database
                switch (Package.SoundType)
                {
                // Add or update the existing package
                case SoundType.Engine:
                    query    = "SELECT * FROM `EngineSoundPackage` WHERE `FolderName` = @P0";
                    existing = db.Query <EngineSoundPackage>(query, folderNameBox.Text).FirstOrDefault();
                    break;

                case SoundType.Truck:
                    query    = "SELECT * FROM `TruckSoundPackage` WHERE `FolderName` = @P0";
                    existing = db.Query <TruckSoundPackage>(query, folderNameBox.Text).FirstOrDefault();
                    break;

                default:
                    throw new Exception("Invalid sound type");
                }

                // If the folder name is already in use, and (is new package OR the existing package ID does not match the current)
                if (existing != null && (NewPackage || Package.Id != existing.Id))
                {
                    var result = MessageBox.Show(
                        $"The sound folder chosen belongs to the sound package \"{existing?.Name}\"! "
                        + "Do you want to replace the existing sound package with this one?",
                        "Verification", MessageBoxButtons.YesNo, MessageBoxIcon.Warning
                        );

                    // Quit if the user see's the error in his ways
                    if (result != DialogResult.Yes)
                    {
                        return(false);
                    }

                    // Delete the old sound
                    switch (Package.SoundType)
                    {
                    // Add or update the existing package
                    case SoundType.Engine:
                        db.EngineSoundPackages.Remove((EngineSoundPackage)existing);
                        break;

                    case SoundType.Truck:
                        db.TruckSoundPackages.Remove((TruckSoundPackage)existing);
                        break;

                    default:
                        throw new Exception("Invalid sound type");
                    }
                }
            }

            // Wrap in a transaction
            using (var trans = db.BeginTransaction())
            {
                try
                {
                    // Add or update the existing package
                    switch (Package.SoundType)
                    {
                    // Add or update the existing package
                    case SoundType.Engine:
                        db.EngineSoundPackages.AddOrUpdate((EngineSoundPackage)Package);
                        break;

                    case SoundType.Truck:
                        db.TruckSoundPackages.AddOrUpdate((TruckSoundPackage)Package);
                        break;

                    default:
                        throw new Exception("Invalid sound type");
                    }

                    // Re-open the Sound package ZipFile
                    using (var reader = new SoundPackageReader(PackageFilePath, Package.SoundType))
                    {
                        // Save sounds in the database
                        reader.InstallPackage(db, Package, folderPath, true);
                    }

                    // Commit and return
                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                    throw;
                }
            }

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Import Button Click Event
        /// </summary>
        private void importButton_Click(object sender, EventArgs e)
        {
            // Request the user supply the sound package path
            OpenFileDialog dialog = new OpenFileDialog();

            switch (Package.SoundType)
            {
            case SoundType.Engine:
                dialog.Title  = "Engine Sound Package Import";
                dialog.Filter = "Engine Sound Pack|*.espack";
                break;

            case SoundType.Truck:
                dialog.Title  = "Truck Sound Package Import";
                dialog.Filter = "Truck Sound Pack|*.tspack";
                break;

            default:
                throw new Exception("Invalid sound type");
            }

            // If the user selects a file
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                // Try and mount the sound package
                using (var reader = new SoundPackageReader(dialog.FileName, Package.SoundType))
                {
                    // Load the manifest, interior and exterior files
                    string            name     = string.Empty;
                    SoundPackManifest manifest = null;
                    Interior = null; // Reset
                    Exterior = null; // Reset

                    // Try and parse the internal sii files
                    try
                    {
                        manifest = reader.GetManifest(out name);
                        Interior = reader.GetSoundFile(SoundLocation.Interior);
                        Exterior = reader.GetSoundFile(SoundLocation.Exterior);
                    }
                    catch (Exception ex)
                    {
                        // Be fancy and generate a good error message
                        StringBuilder builder = new StringBuilder();
                        if (manifest == null)
                        {
                            builder.AppendLine("Failed to parse the package manifest file!");
                        }
                        else
                        {
                            builder.AppendLineIf(Interior == null, "Failed to parse the interior.sii file!", "Failed to parse the exterior.sii file!");
                        }
                        builder.AppendLine();
                        builder.Append("Error: ").AppendLine(ex.Message);

                        if (ex is Sii.SiiSyntaxException)
                        {
                            var siiEx = ex as Sii.SiiSyntaxException;
                            builder.AppendLine("Line: " + siiEx.Span.Start.Line);
                            builder.AppendLine("Column: " + siiEx.Span.Start.Column);
                        }

                        // Alert the user
                        MessageBox.Show(builder.ToString().TrimEnd(), "Sii Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    // Ensure manifest
                    if (manifest == null)
                    {
                        MessageBox.Show("Sound package is missing its manifest.sii file.",
                                        "Manifest Error", MessageBoxButtons.OK, MessageBoxIcon.Warning
                                        );
                        return;
                    }
                    else if (Interior == null || Exterior == null)
                    {
                        MessageBox.Show("Sound package is missing its interior.sii or exterior.sii file.",
                                        "Sound Package Error", MessageBoxButtons.OK, MessageBoxIcon.Warning
                                        );
                        return;
                    }

                    // Set form values
                    labelAuthor.Text  = manifest.Author;
                    labelVersion.Text = manifest.Version;
                    if (NewPackage)
                    {
                        int index = name.IndexOf('.');
                        unitNameBox.Text    = name.Substring(0, (index == -1) ? name.Length : index);
                        packageNameBox.Text = manifest.Name;
                        intFilenameBox.Text = manifest.InteriorName;
                        extFilenameBox.Text = manifest.ExteriorName;
                    }

                    // Set internal
                    Imported        = true;
                    PackageFilePath = dialog.FileName;

                    // Enable controls
                    packageNameBox.Enabled = true;
                    unitNameBox.Enabled    = true;
                    folderNameBox.Enabled  = true;
                    confirmButton.Enabled  = true;
                    if (Package.SoundType == SoundType.Engine)
                    {
                        intFilenameBox.Enabled = true;
                        extFilenameBox.Enabled = true;
                    }

                    if (String.IsNullOrWhiteSpace(manifest.YoutubeVideoId))
                    {
                        youtubeLinkLabel.Enabled = false;
                        youtubeLinkLabel.Visible = false;
                        youtubeIcon.Visible      = false;
                    }
                    else
                    {
                        Package.YoutubeVideoId   = manifest.YoutubeVideoId;
                        youtubeLinkLabel.Enabled = true;
                        youtubeLinkLabel.Visible = true;
                        youtubeIcon.Visible      = true;
                    }
                }
            }
        }