示例#1
0
        // D5puPvS6GzfOsdaW6Kjwle63AUeLFVVc
        private void SetSn(IntPtr m_Handle, string codeStr)
        {
            int resLen = 256;

            byte[] res = new byte[resLen];
            int    v   = YouyiSdk.M_GetDevSn(m_Handle, ref resLen, ref res[0]);

            if (v == 0)
            {
                byte[] sec    = new UTF8Encoding().GetBytes(BaseConfig.AC_SECRET);
                byte[] newRes = new byte[resLen];
                Array.Copy(res, newRes, resLen);
                LogHelper.ShowLog("设备SN:{0}", new UTF8Encoding().GetString(Hex.Encode(newRes)));
                // 验证激活码与SN
                byte[] md   = new byte[32];
                byte[] code = Encoding.Default.GetBytes(codeStr);
                byte[] bt   = new byte[resLen + sec.Length + code.Length];
                newRes.CopyTo(bt, 0);
                sec.CopyTo(bt, newRes.Length);
                code.CopyTo(bt, bt.Length - code.Length);

                SM3Digest sm3 = new SM3Digest();
                sm3.BlockUpdate(bt, 0, bt.Length);
                sm3.DoFinal(md, 0);
                string s = new UTF8Encoding().GetString(Hex.Encode(md));
                LogHelper.ShowLog("摘要加密:{0} 长度:{1}", s.ToUpper(), s.Length);

                YouyiSdk.M_SetUserData(m_Handle, md.Length, ref md[0]);
            }
        }
        /// <inheritdoc/>
        public void Remove(int id)
        {
            int offset = id * RecordSize;
            FileCabinetRecord record = this.list[id];

            using (FileStream fileStream = new FileStream("cabinet-records.db", FileMode.Open))
            {
                fileStream.Seek(offset + 276, SeekOrigin.Begin);
                byte[] arrayRecord = new byte[RecordSize];

                byte[] arrayId = new byte[4];
                arrayId = new UTF8Encoding(true).GetBytes(record.Id.ToString(new CultureInfo("en-US")));
                arrayId.CopyTo(arrayRecord, 0);

                byte[] arraySex = new byte[2];
                arraySex = new UTF8Encoding(true).GetBytes(record.Sex.ToString(new CultureInfo("en-US")));
                arraySex.CopyTo(arrayRecord, 4);

                byte[] arrayFirstName = new byte[120];
                arrayFirstName = new UTF8Encoding(true).GetBytes(record.FirstName);
                arrayFirstName.CopyTo(arrayRecord, 6);

                byte[] arrayLastName = new byte[120];
                arrayLastName = new UTF8Encoding(true).GetBytes(record.LastName);
                arrayLastName.CopyTo(arrayRecord, 126);

                byte[] arrayAge = new byte[2];
                arrayAge = new UTF8Encoding(true).GetBytes(record.Age.ToString(new CultureInfo("en-US")));
                arrayAge.CopyTo(arrayRecord, 246);

                byte[] arraySalary = new byte[16];
                arraySalary = new UTF8Encoding(true).GetBytes(record.Salary.ToString(new CultureInfo("en-US")));
                arraySalary.CopyTo(arrayRecord, 248);

                byte[] arrayYear = new byte[4];
                arrayYear = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Year.ToString(new CultureInfo("en-US")));
                arrayYear.CopyTo(arrayRecord, 264);

                byte[] arrayMonth = new byte[4];
                arrayMonth = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Month.ToString(new CultureInfo("en-US")));
                arrayMonth.CopyTo(arrayRecord, 268);

                byte[] arrayDay = new byte[4];
                arrayDay = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Day.ToString(new CultureInfo("en-US")));
                arrayDay.CopyTo(arrayRecord, 272);

                byte[] arrayIsDeleted = new byte[2];
                arrayIsDeleted = new UTF8Encoding(true).GetBytes(1.ToString(new CultureInfo("en-US")));
                arrayIsDeleted.CopyTo(arrayRecord, 276);

                fileStream.Seek(offset, SeekOrigin.Begin);
                fileStream.Write(arrayRecord, 0, arrayRecord.Length);
            }
        }
示例#3
0
        private void ValiCode(object sender, EventArgs e)
        {
            string codeStr = _ActivationCode;
            int    resLen  = 256;

            byte[] res = new byte[resLen];
            int    v   = YouyiSdk.M_GetDevSn(_globalParam.m_Handle, ref resLen, ref res[0]);

            if (v == 0)
            {
                byte[] sec    = new UTF8Encoding().GetBytes(BaseConfig.AC_SECRET);
                byte[] newRes = new byte[resLen];
                Array.Copy(res, newRes, resLen);
                LogHelper.ShowLog("设备SN:{0}", new UTF8Encoding().GetString(Hex.Encode(newRes)));
                // 验证激活码与SN
                byte[] md   = new byte[32];
                byte[] code = Encoding.Default.GetBytes(codeStr);
                byte[] bt   = new byte[resLen + sec.Length + code.Length];
                newRes.CopyTo(bt, 0);
                sec.CopyTo(bt, newRes.Length);
                code.CopyTo(bt, bt.Length - code.Length);

                SM3Digest sm3 = new SM3Digest();
                sm3.BlockUpdate(bt, 0, bt.Length);
                sm3.DoFinal(md, 0);
                //string s = new UTF8Encoding().GetString(Hex.Encode(md));
                //LogHelper.ShowLog("摘要加密:{0} 长度:{1}", s.ToUpper(), s.Length);

                // 验证自定义数据
                int vali = YouyiSdk.M_VerifyUserData(_globalParam.m_Handle, md.Length, ref md[0]);
                LogHelper.ShowLog("验证结果:{0}", vali);
                Loading = false;
                if (vali != 0)
                {
                    // 失败信息
                    MessageBox.Show("卡密不正确,请联系客服处理!");
                    _window.DelegeteShutDown();
                }
                else
                {
                    // 写入激活码
                    INIHelper.Write("Info", "ActivationCode", codeStr, BaseConfig.CONFIG_PATH);
                    _window.DelegeteClose();
                }
            }
        }
示例#4
0
        // codeStr:D5puPvS6GzfOsdaW6Kjwle63AUeLFVVc
        private void ValiCode(IntPtr m_Handle, string codeStr)
        {
            int resLen = 256;

            byte[] res = new byte[resLen];
            int    v   = YouyiSdk.M_GetDevSn(m_Handle, ref resLen, ref res[0]);

            if (v == 0)
            {
                byte[] sec    = new UTF8Encoding().GetBytes(BaseConfig.AC_SECRET);
                byte[] newRes = new byte[resLen];
                Array.Copy(res, newRes, resLen);
                LogHelper.ShowLog("设备SN:{0}", new UTF8Encoding().GetString(Hex.Encode(newRes)));
                // 验证激活码与SN
                byte[] md   = new byte[32];
                byte[] code = Encoding.Default.GetBytes(codeStr);
                byte[] bt   = new byte[resLen + sec.Length + code.Length];
                newRes.CopyTo(bt, 0);
                sec.CopyTo(bt, newRes.Length);
                code.CopyTo(bt, bt.Length - code.Length);

                SM3Digest sm3 = new SM3Digest();
                sm3.BlockUpdate(bt, 0, bt.Length);
                sm3.DoFinal(md, 0);

                // 验证自定义数据
                int vali = YouyiSdk.M_VerifyUserData(m_Handle, md.Length, ref md[0]);
                LogHelper.ShowLog("验证结果:{0}", vali);
                if (vali != 0)
                {
                    _valiCode += 1;
                }
                else
                {
                    if (_valiCode != 0)
                    {
                        _valiCode = 0;
                    }
                }
            }
            else
            {
                _valiCode += 1;
            }
        }
示例#5
0
 public static BigInt[] StringToBigint(string data)
 {
     byte[] bytes = new UTF8Encoding().GetBytes(data);
     if (bytes.Length % 6 != 0)
     {
         int    l      = bytes.Length / 6;
         byte[] buffer = new byte[(l + 1) * 6];
         bytes.CopyTo(buffer, 0);
         bytes = buffer;
     }
     BigInt[] BI = new BigInt[bytes.Length / 6];
     for (int i = 0; i < bytes.Length / 6; i++)
     {
         byte[] bit = { bytes[i * 6], bytes[i * 6 + 1], bytes[i * 6 + 2], bytes[i * 6 + 3], bytes[i * 6 + 4], bytes[i * 6 + 5] };
         BI[i] = new BigInt(bit);
     }
     return(BI);
 }
        /// <inheritdoc/>
        public void Purge(out int count, out int before)
        {
            List <FileCabinetRecord> records = new List <FileCabinetRecord>();

            int beforePurge;
            int afterPurge;

            using (FileStream fs = new FileStream("cabinet-records.db", FileMode.Open))
            {
                int recordsCount = (int)fs.Length / RecordSize;
                beforePurge = recordsCount;
                fs.Seek(0, SeekOrigin.Begin);

                UTF8Encoding temp       = new UTF8Encoding(true);
                byte[]       recordByte = new byte[RecordSize];

                for (int i = 0; i < recordsCount; i++)
                {
                    fs.Read(recordByte, 0, RecordSize);

                    FileCabinetRecord record = new FileCabinetRecord();

                    byte[] arrayId = new byte[4];
                    Array.Copy(recordByte, 0, arrayId, 0, 4);
                    record.Id = Convert.ToInt32(temp.GetString(arrayId), new CultureInfo("en-US"));

                    byte[] arraySex = new byte[2];
                    Array.Copy(recordByte, 4, arraySex, 0, 2);
                    record.Sex = Convert.ToChar(temp.GetString(arraySex)[0], new CultureInfo("en-US"));

                    byte[] arrayFirstName = new byte[120];
                    Array.Copy(recordByte, 6, arrayFirstName, 0, 120);
                    string firstNameTmp = temp.GetString(arrayFirstName).Trim(' ');
                    for (int j = 0; j < firstNameTmp.IndexOf('\0', StringComparison.Ordinal); j++)
                    {
                        record.FirstName += firstNameTmp[j];
                    }

                    byte[] arrayLastName = new byte[120];
                    Array.Copy(recordByte, 126, arrayLastName, 0, 120);
                    string lastNameTmp = temp.GetString(arrayLastName).Trim(' ');
                    for (int j = 0; j < firstNameTmp.IndexOf('\0', StringComparison.Ordinal); j++)
                    {
                        record.LastName += lastNameTmp[j];
                    }

                    byte[] arrayAge = new byte[2];
                    Array.Copy(recordByte, 246, arrayAge, 0, 2);
                    record.Age = Convert.ToInt16(temp.GetString(arrayAge), new CultureInfo("en-US"));

                    byte[] arraySalary = new byte[16];
                    Array.Copy(recordByte, 248, arraySalary, 0, 16);
                    record.Salary = Convert.ToDecimal(temp.GetString(arraySalary), new CultureInfo("en-US"));

                    byte[] arrayYear = new byte[4];
                    Array.Copy(recordByte, 264, arrayYear, 0, 4);
                    int year = Convert.ToInt32(temp.GetString(arrayYear), new CultureInfo("en-US"));

                    byte[] arrayMonth = new byte[4];
                    Array.Copy(recordByte, 268, arrayMonth, 0, 4);
                    int month = Convert.ToInt32(temp.GetString(arrayMonth), new CultureInfo("en-US"));

                    byte[] arrayDay = new byte[4];
                    Array.Copy(recordByte, 272, arrayDay, 0, 4);
                    int day = Convert.ToInt32(temp.GetString(arrayDay), new CultureInfo("en-US"));

                    string date = month.ToString(new CultureInfo("en-US")) + "/" + day.ToString(new CultureInfo("en-US")) + "/" + year.ToString(new CultureInfo("en-US"));
                    record.DateOfBirth = DateTime.Parse(date, new CultureInfo("en-US"));

                    byte[] arrayIsDeleted = new byte[2];
                    Array.Copy(recordByte, 276, arrayIsDeleted, 0, 2);
                    short isDeleted = Convert.ToInt16(temp.GetString(arrayIsDeleted), new CultureInfo("en-US"));

                    if (isDeleted == 0)
                    {
                        records.Add(record);
                    }
                }
            }

            using (FileStream fileStream = new FileStream("cabinet-records.db", FileMode.Create))
            {
                int i = 0;
                foreach (var record in records)
                {
                    record.Id = i;
                    byte[] arrayRecord = new byte[RecordSize];

                    byte[] arrayId = new byte[4];
                    arrayId = new UTF8Encoding(true).GetBytes(record.Id.ToString(new CultureInfo("en-US")));
                    arrayId.CopyTo(arrayRecord, 0);

                    byte[] arraySex = new byte[2];
                    arraySex = new UTF8Encoding(true).GetBytes(record.Sex.ToString(new CultureInfo("en-US")));
                    arraySex.CopyTo(arrayRecord, 4);

                    byte[] arrayFirstName = new byte[120];
                    arrayFirstName = new UTF8Encoding(true).GetBytes(record.FirstName);
                    arrayFirstName.CopyTo(arrayRecord, 6);

                    byte[] arrayLastName = new byte[120];
                    arrayLastName = new UTF8Encoding(true).GetBytes(record.LastName);
                    arrayLastName.CopyTo(arrayRecord, 126);

                    byte[] arrayAge = new byte[2];
                    arrayAge = new UTF8Encoding(true).GetBytes(record.Age.ToString(new CultureInfo("en-US")));
                    arrayAge.CopyTo(arrayRecord, 246);

                    byte[] arraySalary = new byte[16];
                    arraySalary = new UTF8Encoding(true).GetBytes(record.Salary.ToString(new CultureInfo("en-US")));
                    arraySalary.CopyTo(arrayRecord, 248);

                    byte[] arrayYear = new byte[4];
                    arrayYear = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Year.ToString(new CultureInfo("en-US")));
                    arrayYear.CopyTo(arrayRecord, 264);

                    byte[] arrayMonth = new byte[4];
                    arrayMonth = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Month.ToString(new CultureInfo("en-US")));
                    arrayMonth.CopyTo(arrayRecord, 268);

                    byte[] arrayDay = new byte[4];
                    arrayDay = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Day.ToString(new CultureInfo("en-US")));
                    arrayDay.CopyTo(arrayRecord, 272);

                    byte[] arrayIsDeleted = new byte[2];
                    arrayIsDeleted = new UTF8Encoding(true).GetBytes(0.ToString(new CultureInfo("en-US")));
                    arrayIsDeleted.CopyTo(arrayRecord, 276);

                    fileStream.Seek(fileStream.Length, SeekOrigin.Begin);
                    fileStream.Write(arrayRecord, 0, arrayRecord.Length);

                    i++;
                }

                afterPurge = (int)fileStream.Length / RecordSize;
            }

            count  = beforePurge - afterPurge;
            before = beforePurge;
        }
        /// <inheritdoc/>
        public void EditRecord(FileCabinetRecord record)
        {
            if (record == null)
            {
                throw new ArgumentNullException(nameof(record));
            }

            if (this.dateOfBirthDictionary.ContainsKey(record.DateOfBirth))
            {
                this.dateOfBirthDictionary[record.DateOfBirth].Add(record.Id * RecordSize);
            }
            else
            {
                this.dateOfBirthDictionary.Add(record.DateOfBirth, new List <long> {
                    record.Id *RecordSize
                });
            }

            if (this.firstNameDictionaty.ContainsKey(record.FirstName))
            {
                this.firstNameDictionaty[record.FirstName].Add(record.Id * RecordSize);
            }
            else
            {
                this.firstNameDictionaty.Add(record.FirstName, new List <long> {
                    record.Id *RecordSize
                });
            }

            if (this.lastNameDictionary.ContainsKey(record.LastName))
            {
                this.lastNameDictionary[record.LastName].Add(record.Id * RecordSize);
            }
            else
            {
                this.lastNameDictionary.Add(record.LastName, new List <long> {
                    record.Id *RecordSize
                });
            }

            int editIndex = record.Id * RecordSize;

            using (FileStream fileStream = new FileStream("cabinet-records.db", FileMode.Open))
            {
                byte[] arrayRecord = new byte[RecordSize];

                byte[] arrayId = new byte[4];
                arrayId = new UTF8Encoding(true).GetBytes(record.Id.ToString(new CultureInfo("en-US")));
                arrayId.CopyTo(arrayRecord, 0);

                byte[] arraySex = new byte[2];
                arraySex = new UTF8Encoding(true).GetBytes(record.Sex.ToString(new CultureInfo("en-US")));
                arraySex.CopyTo(arrayRecord, 4);

                record.FirstName = record.FirstName.Trim(' ');
                byte[] arrayFirstName = new byte[120];
                arrayFirstName = new UTF8Encoding(true).GetBytes(record.FirstName);
                arrayFirstName.CopyTo(arrayRecord, 6);

                record.LastName = record.LastName.Trim(' ');
                byte[] arrayLastName = new byte[120];
                arrayLastName = new UTF8Encoding(true).GetBytes(record.LastName);
                arrayLastName.CopyTo(arrayRecord, 126);

                byte[] arrayAge = new byte[2];
                arrayAge = new UTF8Encoding(true).GetBytes(record.Age.ToString(new CultureInfo("en-US")));
                arrayAge.CopyTo(arrayRecord, 246);

                byte[] arraySalary = new byte[16];
                arraySalary = new UTF8Encoding(true).GetBytes(record.Salary.ToString(new CultureInfo("en-US")));
                arraySalary.CopyTo(arrayRecord, 248);

                byte[] arrayYear = new byte[4];
                arrayYear = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Year.ToString(new CultureInfo("en-US")));
                arrayYear.CopyTo(arrayRecord, 264);

                byte[] arrayMonth = new byte[4];
                arrayMonth = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Month.ToString(new CultureInfo("en-US")));
                arrayMonth.CopyTo(arrayRecord, 268);

                byte[] arrayDay = new byte[4];
                arrayDay = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Day.ToString(new CultureInfo("en-US")));
                arrayDay.CopyTo(arrayRecord, 272);

                byte[] arrayIsDeleted = new byte[2];
                arrayIsDeleted = new UTF8Encoding(true).GetBytes(0.ToString(new CultureInfo("en-US")));
                arrayIsDeleted.CopyTo(arrayRecord, 276);

                fileStream.Seek(editIndex, SeekOrigin.Begin);
                fileStream.Write(arrayRecord, 0, arrayRecord.Length);
            }
        }
        /// <inheritdoc/>
        public int CreateRecord(FileCabinetRecord record)
        {
            if (record == null)
            {
                throw new ArgumentNullException(nameof(record));
            }

            if (this.validator.ValidateParameters(record) == false)
            {
                throw new ArgumentException("Data validation is not successful.", nameof(record));
            }

            if (record.Id == 0)
            {
                if (this.list.Count > 0)
                {
                    record.Id = this.list[this.list.Count - 1].Id + 1;
                }
                else
                {
                    record.Id = this.GetStat(out int a);
                }
            }

            if (this.dateOfBirthDictionary.ContainsKey(record.DateOfBirth))
            {
                this.dateOfBirthDictionary[record.DateOfBirth].Add(record.Id * RecordSize);
            }
            else
            {
                this.dateOfBirthDictionary.Add(record.DateOfBirth, new List <long> {
                    record.Id *RecordSize
                });
            }

            if (this.firstNameDictionaty.ContainsKey(record.FirstName))
            {
                this.firstNameDictionaty[record.FirstName].Add(record.Id * RecordSize);
            }
            else
            {
                this.firstNameDictionaty.Add(record.FirstName, new List <long> {
                    record.Id *RecordSize
                });
            }

            if (this.lastNameDictionary.ContainsKey(record.LastName))
            {
                this.lastNameDictionary[record.LastName].Add(record.Id * RecordSize);
            }
            else
            {
                this.lastNameDictionary.Add(record.LastName, new List <long> {
                    record.Id *RecordSize
                });
            }

            bool rewriteFlag = false;
            int  index       = 0;

            if (this.list.Where(c => c.Id == record.Id).Count() == 1)
            {
                index = this.list.FindIndex(c => c.Id == record.Id);
                this.list.RemoveAt(index);
                this.list.Insert(index, record);
                rewriteFlag = true;
            }
            else
            {
                record.Id = this.list.Count;
                this.list.Add(record);
            }

            var recordToAdd = record;

            byte[] arrayRecord = new byte[RecordSize];

            byte[] arrayId = new byte[4];
            arrayId = new UTF8Encoding(true).GetBytes(record.Id.ToString(new CultureInfo("en-US")));
            arrayId.CopyTo(arrayRecord, 0);

            byte[] arraySex = new byte[2];
            arraySex = new UTF8Encoding(true).GetBytes(record.Sex.ToString(new CultureInfo("en-US")));
            arraySex.CopyTo(arrayRecord, 4);

            record.FirstName = record.FirstName.Trim(' ');
            byte[] arrayFirstName = new byte[120];
            arrayFirstName = new UTF8Encoding(true).GetBytes(record.FirstName);
            arrayFirstName.CopyTo(arrayRecord, 6);

            record.LastName = record.LastName.Trim(' ');
            byte[] arrayLastName = new byte[120];
            arrayLastName = new UTF8Encoding(true).GetBytes(record.LastName);
            arrayLastName.CopyTo(arrayRecord, 126);

            byte[] arrayAge = new byte[2];
            arrayAge = new UTF8Encoding(true).GetBytes(record.Age.ToString(new CultureInfo("en-US")));
            arrayAge.CopyTo(arrayRecord, 246);

            byte[] arraySalary = new byte[16];
            arraySalary = new UTF8Encoding(true).GetBytes(record.Salary.ToString(new CultureInfo("en-US")));
            arraySalary.CopyTo(arrayRecord, 248);

            byte[] arrayYear = new byte[4];
            arrayYear = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Year.ToString(new CultureInfo("en-US")));
            arrayYear.CopyTo(arrayRecord, 264);

            byte[] arrayMonth = new byte[4];
            arrayMonth = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Month.ToString(new CultureInfo("en-US")));
            arrayMonth.CopyTo(arrayRecord, 268);

            byte[] arrayDay = new byte[4];
            arrayDay = new UTF8Encoding(true).GetBytes(record.DateOfBirth.Day.ToString(new CultureInfo("en-US")));
            arrayDay.CopyTo(arrayRecord, 272);

            byte[] arrayIsDeleted = new byte[2];
            arrayIsDeleted = new UTF8Encoding(true).GetBytes(0.ToString(new CultureInfo("en-US")));
            arrayIsDeleted.CopyTo(arrayRecord, 276);

            if (rewriteFlag)
            {
                using (FileStream fileStream = new FileStream("cabinet-records.db", FileMode.Open))
                {
                    fileStream.Seek(index * RecordSize, SeekOrigin.Begin);
                    fileStream.Write(arrayRecord, 0, arrayRecord.Length);
                }
            }
            else
            {
                using (FileStream fileStream = new FileStream("cabinet-records.db", FileMode.Open))
                {
                    fileStream.Seek(fileStream.Length, SeekOrigin.Begin);
                    fileStream.Write(arrayRecord, 0, arrayRecord.Length);
                }
            }

            return(recordToAdd.Id);
        }
示例#9
0
        private void buttonConfirm_Click(object sender, EventArgs e)
        {
            var    audioFile = boxAudioFile.Text;
            string indexFile;

            try
            {
                if (!File.Exists(audioFile))
                {
                    throw new FileNotFoundException();
                }

                panelIndexingProgress.BringToFront();
                labelIndexingProgress.Text = "Hashing...";

                using (var md5 = MD5.Create())
                    using (var stream = File.OpenRead(audioFile))
                    {
                        var filename = new UTF8Encoding().GetBytes(Path.GetFileNameWithoutExtension(audioFile));
                        var buffer   = new byte[4096];

                        filename.CopyTo(buffer, 0);
                        stream.Read(buffer, filename.Length, 4096 - filename.Length);

                        var hash = BitConverter.ToString(md5.ComputeHash(buffer));
                        indexFile = Path.Combine(Path.GetTempPath(), hash + ".ffindex");
                    }

                if (File.Exists(indexFile))
                {
                    try
                    {
                        var index = new Index(indexFile);

                        if (index.BelongsToFile(audioFile))
                        {
                            DialogResult    = DialogResult.OK;
                            GeneratedFilter = new DubFilter(audioFile, indexFile, (DubMode)comboDubMode.SelectedIndex);
                            Close();
                            return;
                        }
                    }
                    catch
                    {
                        // ignored
                    }

                    File.Delete(indexFile);
                }

                labelIndexingProgress.Text     = "Indexing...";
                progressIndexingProgress.Value = 0;
                progressIndexingProgress.Style = ProgressBarStyle.Continuous;
                _worker = new BackgroundWorker
                {
                    WorkerSupportsCancellation = true,
                    WorkerReportsProgress      = true
                };
                _worker.ProgressChanged += (o, args) =>
                {
                    progressIndexingProgress.Value = args.ProgressPercentage;
                };
                _worker.DoWork += (bw, workArgs) =>
                {
                    var indexer = new Indexer(audioFile, Source.Lavf);

                    indexer.UpdateIndexProgress += (ind, updateArgs) =>
                    {
                        _worker.ReportProgress((int)(((double)updateArgs.Current / updateArgs.Total) * 100));
                        indexer.CancelIndexing = _worker.CancellationPending;
                    };

                    try
                    {
                        var index = indexer.Index();

                        try
                        {
                            index.GetFirstIndexedTrackOfType(TrackType.Audio);
                        }
                        catch (KeyNotFoundException)
                        {
                            throw new InvalidDataException("That file doesn't have any audio!");
                        }

                        index.WriteIndex(indexFile);
                    }
                    catch (OperationCanceledException)
                    {
                        workArgs.Cancel = true;
                    }
                };
                _worker.RunWorkerCompleted += (o, args) =>
                {
                    this.InvokeIfRequired(() =>
                    {
                        panelIndexingProgress.SendToBack();
                    });

                    if (args.Error != null)
                    {
                        this.InvokeIfRequired(() =>
                        {
                            SetFile(string.Empty);

                            if (
                                MessageBox.Show(args.Error.Message, "ERROR", MessageBoxButtons.OKCancel,
                                                MessageBoxIcon.Error) == DialogResult.Cancel)
                            {
                                Close();
                            }
                        });
                        return;
                    }

                    if (args.Cancelled)
                    {
                        DialogResult = DialogResult.Cancel;
                    }
                    else
                    {
                        DialogResult    = DialogResult.OK;
                        GeneratedFilter = new DubFilter(audioFile, indexFile, (DubMode)comboDubMode.SelectedIndex);
                    }

                    this.InvokeIfRequired(Close);
                };
                _worker.RunWorkerAsync();
            }
            catch (Exception err)
            {
                if (MessageBox.Show(err.Message, "ERROR", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) ==
                    DialogResult.Cancel)
                {
                    Close();
                }
            }
        }