Esempio n. 1
0
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            uint VCountMin  = uint.Parse(textBoxVCountMin.Text, NumberStyles.HexNumber);
            uint VCountMax  = uint.Parse(textBoxVCountMax.Text, NumberStyles.HexNumber);
            uint Timer0Min  = uint.Parse(textBoxTimer0Min.Text, NumberStyles.HexNumber);
            uint Timer0Max  = uint.Parse(textBoxTimer0Max.Text, NumberStyles.HexNumber);
            uint GxStatMin  = uint.Parse(textBoxGxStatMin.Text, NumberStyles.HexNumber);
            uint GxStatMax  = uint.Parse(textBoxGxStatMax.Text, NumberStyles.HexNumber);
            uint VFrameMin  = uint.Parse(textBoxVFrameMin.Text, NumberStyles.HexNumber);
            uint VFrameMax  = uint.Parse(textBoxVFrameMax.Text, NumberStyles.HexNumber);
            int  secondsMin = int.Parse(maskedTextBoxSecondsMin.Text);
            int  secondsMax = int.Parse(maskedTextBoxSecondsMax.Text);

            if (secondsMax < secondsMin)
            {
                secondsMax = secondsMax + 60;
            }

            bool minMaxGxStat = cbGxStat.Checked;

            dsParameters = new List <DSParameterCapture>();
            var listBinding = new BindingSource {
                DataSource = dsParameters
            };

            dgvResults.DataSource = listBinding;

            cpus = Settings.Default.CPUs;
            if (cpus < 1)
            {
                cpus = 1;
            }

            var jobs = new Thread[cpus];

            var progress = new Progress {
                Text = "DS Parameter Progress"
            };

            // this is wrong
            progressTotal = (ulong)((VCountMax - VCountMin + 1)
                                    * (Timer0Max - Timer0Min + 1)
                                    // if only min max gx stat search is 2 (or 1 if they're the same) else it's max-min
                                    * (minMaxGxStat ? (GxStatMax > GxStatMin ? 2u : 1) : (GxStatMax - GxStatMin + 1))
                                    * (VFrameMax - VFrameMin + 1)
                                    * (secondsMax - secondsMin + 1));

            progressSearched = 0;
            progressFound    = 0;
            var testTime = new DateTime(
                datePicker.Value.Year,
                datePicker.Value.Month,
                datePicker.Value.Day,
                int.Parse(maskedTextBoxHour.Text),
                int.Parse(maskedTextBoxMinute.Text),
                secondsMin);

            MAC_address = ulong.Parse(textBoxMACAddress.Text, NumberStyles.HexNumber);
            if (textBoxMACAddress.Text.Length < 12)
            {
                MessageBox.Show("Your MAC address is missing some digits.  Double-check your MAC address.");
            }

            var version  = (Version)(comboBoxVersion.SelectedIndex + 2);
            var language = (Language)comboBoxLanguage.SelectedIndex;
            var dstype   = (DSType)comboBoxDSType.SelectedIndex;

            var  interval       = (uint)((VCountMax - VCountMin) / (float)jobs.Length);
            uint VCountMinLower = VCountMin;
            uint VCountMinUpper = VCountMin + interval;

            var buttons = new int[3];

            buttons[0] = comboBoxButton1.SelectedIndex;
            buttons[1] = comboBoxButton2.SelectedIndex;
            buttons[2] = comboBoxButton3.SelectedIndex;
            uint button = Functions.buttonMashed(buttons);

            String[] arrows  = txtSpins.Text.Split(' ');
            var      pattern = new uint[arrows.Length];

            for (int i = 0; i < arrows.Length; ++i)
            {
                switch (arrows[i])
                {
                case "↑":
                    pattern[i] = 0;
                    break;

                case "↗":
                    pattern[i] = 1;
                    break;

                case "→":
                    pattern[i] = 2;
                    break;

                case "↘":
                    pattern[i] = 3;
                    break;

                case "↓":
                    pattern[i] = 4;
                    break;

                case "↙":
                    pattern[i] = 5;
                    break;

                case "←":
                    pattern[i] = 6;
                    break;

                case "↖":
                    pattern[i] = 7;
                    break;
                }
            }

            try
            {
                progress.SetupAndShow(this, 0, 0, false, true);
                for (int i = 0; i < jobs.Length; i++)
                {
                    uint lower = VCountMinLower;
                    uint upper = (i < (jobs.Length - 1)) ? VCountMinUpper : VCountMax;
                    jobs[i] =
                        new Thread(
                            () => Search(testTime, lower, upper, Timer0Min, Timer0Max, VFrameMin,
                                         VFrameMax, GxStatMin, GxStatMax, minMaxGxStat, secondsMin, secondsMax,
                                         checkBoxSoftReset.Checked, version, language, dstype, cbMemoryLink.Checked,
                                         MAC_address,
                                         button, pattern));

                    jobs[i].Start();
                    Thread.Sleep(100);
                    VCountMinLower = VCountMinLower + interval + 1;
                    VCountMinUpper = VCountMinUpper + interval + 1;
                }
                bool alive = true;
                while (alive)
                {
                    progress.ShowProgress(progressSearched / (float)progressTotal, progressSearched, progressFound);
                    if (refreshQueue)
                    {
                        listBinding.ResetBindings(false);
                        refreshQueue = false;
                    }
                    foreach (Thread job in jobs)
                    {
                        if (job != null && job.IsAlive)
                        {
                            alive = true;
                            break;
                        }
                        alive = false;
                    }
                }
            }
            catch (Exception exception)
            {
                if (exception.Message != "Operation Cancelled")
                {
                    throw;
                }
            }
            finally
            {
                progress.Finish();
                if (dsParameters.Count > 0)
                {
                    //btnSendProfile.Enabled = true;
                }
                for (int i = 0; i < jobs.Length; i++)
                {
                    if (jobs[i] != null)
                    {
                        jobs[i].Abort();
                    }
                }
            }
        }
        private void ManageProgress(BindingSource bindingSource, DoubleBufferedDataGridView grid, FrameType frameType,
                                    int sleepTimer)
        {
            var progress = new Progress();

            progress.SetupAndShow(this, 0, 0, false, true, waitHandle);

            progressSearched = 0;
            progressFound    = 0;

            UpdateGridDelegate gridUpdater = UpdateGrid;
            var updateParams = new object[] { bindingSource };
            ResortGridDelegate gridSorter       = ResortGrid;
            var            sortParams           = new object[] { bindingSource, grid, frameType };
            ThreadDelegate enableGenerateButton = EnableSeedGenerate;

            try
            {
                bool alive = true;
                while (alive)
                {
                    progress.ShowProgress(progressSearched / (float)progressTotal, progressSearched, progressFound);
                    if (refreshQueue)
                    {
                        Invoke(gridUpdater, updateParams);
                        refreshQueue = false;
                    }
                    if (jobs != null)
                    {
                        foreach (Thread job in jobs)
                        {
                            if (job != null && job.IsAlive)
                            {
                                alive = true;
                                break;
                            }
                            alive = false;
                        }
                    }
                    if (sleepTimer > 0)
                    {
                        Thread.Sleep(sleepTimer);
                    }
                }
            }
            catch (ObjectDisposedException)
            {
                // This keeps the program from crashing when the Time Finder progress box
                // is closed from the Windows taskbar.
            }
            catch (Exception exception)
            {
                if (exception.Message != "Operation Cancelled")
                {
                    throw;
                }
            }
            finally
            {
                progress.Finish();

                if (jobs != null)
                {
                    for (int i = 0; i < jobs.Length; i++)
                    {
                        if (jobs[i] != null)
                        {
                            jobs[i].Abort();
                        }
                    }
                }

                Invoke(enableGenerateButton);
                Invoke(gridSorter, sortParams);
            }
        }