コード例 #1
0
        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);
        }
コード例 #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!  - "
                    }
                }
            }
        }