public static void Switch(Server server) { // Only close osu if the feature is enabled if (m_settings["closeOsuBeforeSwitching"] == "true") { // Find the osu process and, if found, kill it Process osu = Process.GetProcessesByName("osu!").FirstOrDefault(); if (osu != null) { osu.Kill(); // Wait till osu is completely closed osu.WaitForExit(); } } bool successful = false; // Switch only if the user is not already connected Server current = Switcher.GetCurrentServer(); if (current.UID != server.UID) { // Try to switch the server successful = Switcher.SwitchServer(server); // Only check connection if switching was successful if (successful) { // Check if the server is available (server that has been switched to is reachable) if (!Switcher.CheckServerAvailability()) { // If not, show a warning MessageBox.Show("The connection test failed. Please try again.\r\n\r\nIf it's still not working the server either didn't update their mirror yet or their server is currently not running (for example due to maintenance).\nIn this case, please visit our Discord server.", "Ultimate Osu Server Switcher", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } // Only start osu etc. if switching the server was successful if (successful) { // Check if osu ise not running (it may be when close when switching feature is disabled) if (Process.GetProcessesByName("osu!").FirstOrDefault() == null) { // Check if the osu path could be found string osuDir = Paths.OsuFolder; if (osuDir == null) { MessageBox.Show("The path to the osu!.exe file could not be found.\n(server was still switched)\n\nPlease make sure you installed osu correctly by starting it as an admin.\n\nIf this issue persists, please visit our Discord server.", "Ultimate Osu Server Switcher", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // build the osu path string osuExecutablePath = Path.Combine(osuDir, "osu!.exe"); // run osu WinUtils.StartProcessUnelevated(osuExecutablePath); } } }
private void pctrAlreadyConnected_Click(object sender, EventArgs e) { // Open osu if ctrl is pressed if (System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftCtrl) || System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.RightCtrl)) { // Get osu dir and check if successful string osuDir = Paths.OsuFolder; if (osuDir != null) { WinUtils.StartProcessUnelevated(Path.Combine(osuDir, "osu!.exe")); } } }
private void BtnConnect_Click(object sender, EventArgs e) { // Change the shown button to the "connecting" one btnConnect.Visible = false; pctrConnecting.Visible = true; Application.DoEvents(); // Check if the user is holding ctrl to quick start osu bool pressingCtrl = System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftCtrl) || System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.RightCtrl); // Save the osu executable path for the reopen feature later string osuExecutablePath = ""; // Only close osu if the feature is enabled if (m_settings["closeOsuBeforeSwitching"] == "true") { // Find the osu process and, if found, save the executable path and kill it Process osu = Process.GetProcessesByName("osu!").FirstOrDefault(); if (osu != null) { osuExecutablePath = osu.MainModule.FileName; osu.Kill(); // Wait till osu is completely closed osu.WaitForExit(); } } // Try to switch the server to the currently selected one // if failed the method handles outputting the error if (Switcher.SwitchServer(m_currentSelectedServer)) { // Check if the server is available (server that has been switched to is reachable) if (!Switcher.CheckServerAvailability()) { // If not, show a warning MessageBox.Show("The connection test failed. Please restart the switcher and try again.\r\n\r\nIf it's still not working the server either didn't update their mirror yet or their server is currently not running (for example due to maintenance).\nIn this case, please visit our Discord server.", "Ultimate Osu Server Switcher", MessageBoxButtons.OK, MessageBoxIcon.Warning); } // Start osu if the reopen feature is enabled and an osu instance was found before switching // osuExecutablePath can only be != "" if close before switching feature is enabled // so a check for the closeOsuBeforeSwitching setting is not necessary if (m_settings["reopenOsuAfterSwitching"] == "true" && osuExecutablePath != "") { WinUtils.StartProcessUnelevated(osuExecutablePath); } else if (pressingCtrl) // Start osu if ctrl was pressed when clicking on connect and it has not started already ue to the reopen feature { // If we dont have the exe path yet because osu was not killed before get it from the registry if (osuExecutablePath == "") { // Check if the osu path could be found string osuDir = Paths.OsuFolder; if (osuDir == null) { MessageBox.Show("The path to the osu!.exe file could not be found.\n\nPlease make sure you installed osu correctly by starting it as an admin.\n\nIf this issue persists, please visit our Discord server.", "Ultimate Osu Server Switcher", MessageBoxButtons.OK, MessageBoxIcon.Error); } else // build the path { osuExecutablePath = Path.Combine(osuDir, "osu!.exe"); } } // Run osu if a path has been found if (osuExecutablePath != "") { WinUtils.StartProcessUnelevated(osuExecutablePath); } } } // Hide the "connecting" button and update the UI (update UI will show the already connected (or connect if switching failed) button then) pctrConnecting.Visible = false; UpdateUI(); }