private void btnAddAccount_Click(object sender, EventArgs e)
        {
            // Get the current server
            Server server = Switcher.GetCurrentServer();

            // Check if the current server can be identified; If not, you cannot save account data for that
            if (server.IsUnidentified)
            {
                MessageBox.Show("You are currently connected to an unknown server.\n\nYou can only save account data for servers that are featured in the switcher.\n\nTo add an account to the switcher, please follow these steps:\n\n1. Connect to the server you would like to save your account data from.\n2. In osu, login with that account on the server.\n3. Close osu to force it writing the account data in the config file\n4. Click on 'Add Account in the switcher.", "Add Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Check if you already have an account saved for the current connected server
            List <Account> accounts = JsonConvert.DeserializeObject <List <Account> >(m_accounts["accounts"]);

            if (accounts.Any(x => x.ServerUID == server.UID))
            {
                MessageBox.Show($"You already have account data saved for the server you are currently connected to. ({server.ServerName})\n\nIf you want to update your data, remove the old one by selecting it and clicking on 'Remove Account", "Add Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // get the account details from the osu config file
            (string username, string password) = OsuConfigFile.GetAccount();
            // GetAccount() returns null if there was an error. If so, just return, the GetAccount() method already shows an error message
            if (username == null || password == null)
            {
                return;
            }

            // Check if the username and password could both be extracted from the config file. Otherwise show error message
            if (username == "" || password == "")
            {
                MessageBox.Show("Could not find the username and password in your osu config file.\nPlease make sure you connected to the server which account data you want to save and you logged in successfully.", "Add Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Create an account instance and add it to the list, then save the json
            Account account = new Account(server.UID, username, password);

            accounts.Add(account);
            m_accounts["accounts"] = JsonConvert.SerializeObject(accounts);

            // Add an account item to the UI
            AddAccountItem(account);

            // Display informations about the added account
            MessageBox.Show($"Your account was successfully added.\n\nServer: {server.ServerName}\nUsername: {username}", "Add Account", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Ejemplo n.º 2
0
        private void richPresenceUpdateTimer_Tick(object sender, EventArgs e)
        {
            // Get all osu instances to check if osu is running
            Process[] osuInstances = Process.GetProcessesByName("osu!");
            // If no osu is running but a presence is set, remove that presence
            if (osuInstances.Length == 0 && Discord.IsPrecenseSet)
            {
                Discord.RemovePresence();
            }
            else if (osuInstances.Length > 0) // If osu is running
            {
                // Get the current server
                Server currentServer = Switcher.GetCurrentServer();

                // Get the osu window title to determine what the user is currently doing
                string title = osuInstances[0].MainWindowTitle;

                // get the username to display in the rpc
                string username = OsuConfigFile.GetAccount().username;

                // If title is just "osu!" the user is in idle state
                if (title == "osu!")
                {
                    Discord.SetPresence("Idle", $"{username ?? "?"} - {currentServer.ServerName}");
                }
                else if (title.StartsWith("osu!  -"))                                                                             // If title starts with "osu!  -" there is a map name behind it that the user is playing/watching/editing
                {
                    if (title.EndsWith(".osu"))                                                                                   // If the title ends with .osu the user is in the editor
                    {
                        Discord.SetPresence($"Editing: {title.Substring(8)}", $"{username ?? "?"} - {currentServer.ServerName}"); // substring 8 to remove the "osu!  - "
                    }
                    else // Otherwise hes playing or watching
                    {
                        Discord.SetPresence($"Playing: {title.Substring(8)}", $"{username ?? "?"} - {currentServer.ServerName}"); // substring 8 to remove the "osu!  - "
                    }
                }
            }
        }
        /// <summary>
        /// Switch the server
        /// </summary>
        /// <param name="server">The server to switch to</param>
        /// <returns>If the switch was successful</returns>
        public static bool SwitchServer(Server server)
        {
            // Remember the old server for telemetry
            Server from = GetCurrentServer();

            // Get the hosts file to edit it
            List <string> hosts = HostsUtils.GetHosts().ToList();

            // Check if reading the hosts file was successful
            if (hosts == null)
            {
                return(false);
            }
            hosts.RemoveAll(x => x.Contains(Variables.HostsIdentifier));

            // Edit the hosts file if the server has a custom ip
            if (server.HasIP)
            {
                // Change the hosts file by adding the server's ip and the osu domain
                foreach (string domain in Variables.OsuDomains)
                {
                    hosts.Add(server.IP + " " + domain);
                }
            }

            // Apply the changes to the hosts file
            bool successful = HostsUtils.SetHosts(hosts.ToArray());

            // check if writing to the hosts file was successful
            if (!successful)
            {
                return(false);
            }

            try
            {
                // Get rid of all uneccessary certificates
                CertificateUtils.UninstallAllCertificates(Servers);
            }
            catch (Exception ex)
            {
                // Show error
                MessageBox.Show($"Error whilst trying to uninstall certificates.\n\n{ex.Message}\n\nPlease switch back to bancho and try again.\nIf the issue persists, please visit our Discord server.", "Ultimate Osu Server Switcher", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Install the certificate if needed (not needed for bancho and localhost)
            if (server.HasCertificate)
            {
                try
                {
                    CertificateUtils.InstallCertificate(server);
                }
                catch (Exception ex)
                {
                    // Show error
                    MessageBox.Show($"Error whilst trying to install certificates.\n\n{ex.Message}\n\nPlease switch back to bancho and try again.\nOtherwise you will experience desynchronizations between your hosts file and your installed certificates.\n\nIf the issue persists, please visit our Discord server.", "Ultimate Osu Server Switcher", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            // switch the account in the osu config file if that option is enabled
            if (m_settings["switchAccount"] == "true")
            {
                // Only switch when an account is saved for that server
                List <Account> accounts = JsonConvert.DeserializeObject <List <Account> >(m_accounts["accounts"]);

                // get the account that belongs to the server the user switched to
                Account account = accounts.FirstOrDefault(x => x.ServerUID == server.UID);
                if (account != null)
                {
                    // Try to change the account details in the osu config file
                    OsuConfigFile.SetAccount(account);
                }
            }

            // Add the switch to the history by getting the history, adding the element and re-saving it
            List <HistoryElement> history = JsonConvert.DeserializeObject <List <HistoryElement> >(m_history["history"]);

            history.Add(new HistoryElement(DateTime.Now, from.UID, server.UID));
            string json = JsonConvert.SerializeObject(history.ToArray());

            m_history["history"] = json;

            // Send telemetry if enabled
            if (m_settings["sendTelemetry"] == "true")
            {
                SendTelemetry(from, server);
            }

            return(true);
        }