Example #1
0
        public static TimerCaps QueryTimerResolution()
        {
            TimerCaps caps = new TimerCaps();

            _ = NtQueryTimerResolution(out caps.PeriodMin, out caps.PeriodMax, out caps.PeriodCurrent);
            return(caps);
        }
        public SystemPerformanceManagerForm(ModManagerForm form)
        {
            InitializeComponent();

            modManager = form;

            timer = new Timer()
            {
                Enabled  = true,
                Interval = 1000
            };
            timer.Tick += new EventHandler(TimerTick);

            // Use the same icon as executable
            Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);

            TimerCaps caps = WinApiCalls.QueryTimerResolution();

            minimumTimerResolutionTextBox.Text = caps.PeriodMax / 10000.0 + " ms";
            maximumTimerResolutionTextBox.Text = caps.PeriodMin / 10000.0 + " ms";
            currentTimerResolutionTextBox.Text = caps.PeriodCurrent / 10000.0 + " ms";

            if (caps.PeriodCurrent / 10000.0 < 0.8)
            {
                modManager.IsTimerResolutionLowered = true;
            }

            REG_DOW_PATH = modManager.CurrentDir + "\\" + modManager.CurrentGameEXE;

            // We are checking for Compatibility settings in Registry
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(REG_COMPATIBILITY_PATH, false))
            {
                if (key != null)
                {
                    object valueObject = key.GetValue(REG_DOW_PATH);

                    if (valueObject != null)
                    {
                        string value = valueObject.ToString();

                        if (value.Contains(REG_VALUE_FULLSCREEN_OPTIMIZATIONS))
                        {
                            fullscreenOptimizationsCheckBox.Checked = true;
                        }
                        if (value.Contains(REG_VALUE_RUN_AS_ADMIN))
                        {
                            runAsAdministratorCheckBox.Checked = true;
                        }
                        if (value.Contains(REG_VALUE_HDPI_AWARE))
                        {
                            HDPIiScalingCheckBox.Checked = true;
                        }
                        if (value.Contains(REG_VALUE_COMPATIBILITY_WITH))
                        {
                            comatibilityModeCheckBox.Checked = true;
                        }
                    }
                }
            }

            // We are checking for Power Settings in Registry
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_ULTIMATE_PERFORMANCE_2, false))
            {
                if (key != null)
                {
                    powerPlanComboBox.Items.Add(NAME_ULTIMATE_PERFORMANCE);
                    unlockUltimatePerformanceButton.Enabled = false;
                    ultimatePerformanceGUIDIndex            = 2;
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_ULTIMATE_PERFORMANCE, false))
            {
                if (key != null && ultimatePerformanceGUIDIndex == 0)
                {
                    powerPlanComboBox.Items.Add(NAME_ULTIMATE_PERFORMANCE);
                    unlockUltimatePerformanceButton.Enabled = false;
                    ultimatePerformanceGUIDIndex            = 1;
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_MAX_PERFORMANCE, false))
            {
                if (key != null)
                {
                    powerPlanComboBox.Items.Add(NAME_MAX_PERFORMANCE);
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_BALANCED, false))
            {
                if (key != null)
                {
                    powerPlanComboBox.Items.Add(NAME_BALANCED);
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_POWER_SAVER, false))
            {
                if (key != null)
                {
                    powerPlanComboBox.Items.Add(NAME_POWER_SAVER);
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH, false))
            {
                if (key != null)
                {
                    object valueObject = key.GetValue("ActivePowerScheme");

                    if (valueObject != null)
                    {
                        string value = valueObject.ToString();

                        switch (value)
                        {
                        case GUID_ULTIMATE_PERFORMANCE:
                        case GUID_ULTIMATE_PERFORMANCE_2:
                            powerPlanComboBox.SelectedItem = NAME_ULTIMATE_PERFORMANCE;
                            break;

                        case GUID_MAX_PERFORMANCE:
                            powerPlanComboBox.SelectedItem = NAME_MAX_PERFORMANCE;
                            break;

                        case GUID_BALANCED:
                            powerPlanComboBox.SelectedItem = NAME_BALANCED;
                            break;

                        case GUID_POWER_SAVER:
                            powerPlanComboBox.SelectedItem = NAME_POWER_SAVER;
                            break;
                        }
                    }
                }
            }

            // We have to add those methods to the EventHandler here so we could avoid accidental firing of those methods after we would change the state of the CheckBox
            runAsAdministratorCheckBox.CheckedChanged      += new EventHandler(CheckBox_CheckedChanged);
            HDPIiScalingCheckBox.CheckedChanged            += new EventHandler(CheckBox_CheckedChanged);
            comatibilityModeCheckBox.CheckedChanged        += new EventHandler(CheckBox_CheckedChanged);
            fullscreenOptimizationsCheckBox.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);

            powerPlanComboBox.SelectedIndexChanged += new EventHandler(PowerPlanComboBox_SelectedIndexChanged);
        }
        private void TimerTick(object sender, EventArgs e)
        {
            TimerCaps caps = WinApiCalls.QueryTimerResolution();

            currentTimerResolutionTextBox.Text = caps.PeriodCurrent / 10000.0 + " ms";
        }