Beispiel #1
0
        private void comboBoxImageSmall_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBoxImageSmall.ImageLocation = null;
            pictureBoxImageSmall.Hide();

            if (comboBoxApp.SelectedIndex == -1 || comboBoxImageSmall.SelectedIndex <= 0)
            {
                return;
            }

            Program.DiscordApplication app = Program.Applications.Find(v => v.Name == comboBoxApp.Text);

            if (app == null)
            {
                return;
            }

            Program.Asset asset = app.Assets.Find(v => v.Name == comboBoxImageSmall.Text);

            if (asset == null || asset.Icon == "")
            {
                return;
            }

            pictureBoxImageSmall.ImageLocation = Path.Combine(Program.appIconsPath, app.Id + "_" + asset.Icon);
            pictureBoxImageSmall.Show();
        }
Beispiel #2
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            List <Program.Asset> assets = new List <Program.Asset>();

            foreach (Panel panel in flowLayoutPanel.Controls)
            {
                PictureBox pictureBox = (PictureBox)panel.Controls[0];
                TextBox    textBox    = (TextBox)panel.Controls[1];

                Program.Asset asset = new Program.Asset
                {
                    Name = textBox.Text,
                    Icon = ""
                };


                if (asset.Name == "")
                {
                    MessageBox.Show(null, "Asset name cannot be empty.", "Discord RPC Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                else if (assets.FindIndex(v => v.Name == asset.Name) != -1)
                {
                    MessageBox.Show(null, "An asset with this name already exists: " + asset.Name, "Discord RPC Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


                else
                {
                    if (pictureBox.ImageLocation != null)
                    {
                        // Edited
                        if ((string)pictureBox.Tag == "*")
                        {
                            string icon = Path.GetFileName(pictureBox.ImageLocation);
                            asset.Icon = icon;
                        }

                        // Unedited
                        else
                        {
                            string icon = Path.GetFileName(pictureBox.ImageLocation);
                            asset.Icon = icon.Substring(icon.IndexOf("_") + 1);
                        }

                        string iconDestPath = Path.Combine(Program.appIconsPath, app.Id + "_" + asset.Icon);

                        try
                        {
                            File.Copy(pictureBox.ImageLocation, iconDestPath, true);
                        } // If already in use (by PictureBox), then no change = no need to update (IOException)
                        catch (IOException) { }
                        catch (Exception ex)
                        {
                            MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    assets.Add(asset);
                }
            }

            int index = Program.Applications.FindIndex(v => v.Name == app.Name);

            Program.Applications[index].Assets = assets;

            Program.SaveData();
            this.Close();
        }
Beispiel #3
0
        private async void WebView_NavigationCompletedAsync(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            // Show login page
            if (webView.Source.LocalPath == "/login")
            {
                webView.Show();
                webView.BringToFront();

                if (loadingForm.Visible)
                {
                    loadingForm.Hide();
                }

                MessageBox.Show(this, "You must log in to Discord to continue.", "Login required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            // Get user applications
            else if (webView.Source.LocalPath == "/developers/applications")
            {
                if (!loadingForm.Visible)
                {
                    loadingForm.Tag = 1;
                    loadingForm.Show(this);
                }

                webView.Hide();

                bool   foundApps = false;
                string apps_text = "";

                for (int i = 0; i < 20; i++)
                {
                    apps_text = await webView.ExecuteScriptAsync("{let obj = [];"
                                                                 + "for (const app of document.querySelectorAll('div[class^=cardWrapper]')) {"
                                                                 + "const id = app.firstChild.href.replace('https://discord.com/developers/applications/', '');"
                                                                 + "const name = app.querySelector('div[class^=cardLabel]').textContent;"
                                                                 + "const _icon = app.querySelector('div[class^=cardImage]').style.backgroundImage;"
                                                                 + "const icon = _icon.substring(_icon.lastIndexOf('/') + 1, _icon.lastIndexOf('?') != - 1 ? _icon.lastIndexOf('?') : _icon.lastIndexOf('\"'));"
                                                                 + "obj.push({ id, name, icon });"
                                                                 + "}"
                                                                 + "obj;}");

                    if (apps_text != "[]")
                    {
                        foundApps = true;
                        break;
                    }

                    // Redirected to login page, stop
                    else if (webView.Visible)
                    {
                        break;
                    }

                    await Task.Delay(500);
                }


                if (!foundApps)
                {
                    loadingForm.Hide();

                    if (!webView.Visible)
                    {
                        MessageBox.Show(this, "No Application found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return;
                }


                List <AppImportForm.ImportedApp> userApps = JsonConvert.DeserializeObject <List <AppImportForm.ImportedApp> >(apps_text);


                // Wait for all icons to be downloaded
                loadingForm.Hide();

                loadingForm.Tag = 2;
                loadingForm.Show();
                loadingForm.SetMaximumProgress(userApps.Count);
                loadingForm.SetProgress(0);


                for (int i = 0; i < 20; i++)
                {
                    bool allIconsLoaded = true;

                    foreach (AppImportForm.ImportedApp app in userApps)
                    {
                        if (app.icon == "")
                        {
                            continue;
                        }

                        int index = web_loadedAppIcons.FindIndex(v => v == app.id);

                        if (index == -1)
                        {
                            allIconsLoaded = false;
                            break;
                        }
                    }

                    loadingForm.SetProgress(web_loadedAppIcons.Count);

                    if (allIconsLoaded)
                    {
                        break;
                    }

                    await Task.Delay(1000);
                }

                web_loadedAppIcons.Clear();


                // Let the user select which apps to import
                AppImportForm appImportForm = new AppImportForm
                {
                    Tag = apps_text
                };
                loadingForm.Hide();
                appImportForm.ShowDialog(this);

                string        tag            = (string)appImportForm.Tag;
                List <string> appIdsToImport = JsonConvert.DeserializeObject <List <string> >((string)tag);
                appImportForm.Dispose();


                // Import chosen applications (create or update)
                foreach (string appId in appIdsToImport)
                {
                    AppImportForm.ImportedApp userApp = userApps.Find(v => v.id == appId);
                    int appIndex = Program.Applications.FindIndex(v => v.Id == appId);

                    Program.DiscordApplication newApp = new Program.DiscordApplication
                    {
                        Id     = appId,
                        Name   = userApp.name,
                        Icon   = Path.GetExtension(userApp.icon),
                        Assets = new List <Program.Asset>()
                    };

                    if (appIndex != -1)
                    {
                        Program.Applications[appIndex] = newApp;
                    }
                    else
                    {
                        Program.Applications.Add(newApp);
                    }
                }


                // Then get assets for each selected app
                loadingForm.Tag = 3;
                loadingForm.Show();
                loadingForm.SetMaximumProgress(appIdsToImport.Count);
                loadingForm.SetProgress(0);


                // Find assets (icons + names), if any
                foreach (string appId in appIdsToImport)
                {
                    web_assetFetched = false;

                    // Wait before going to the next url
                    webView.CoreWebView2.Navigate("https://discord.com/developers/applications/" + appId + "/rich-presence/assets");

                    for (int i = 0; i < 20; i++)
                    {
                        if (web_assetFetched)
                        {
                            break;
                        }

                        await Task.Delay(1000);
                    }

                    loadingForm.IncrementProgress();
                }

                Program.SaveData();

                web_loadedAppIcons.Clear();
                loadingForm.Hide();

                MessageBox.Show(this, "Applications and assets imported successfully.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                ReloadComboBoxApp();
            }

            // Get application assets
            else if (webView.Source.LocalPath.Contains("/rich-presence/assets"))
            {
                bool   foundAssets = false;
                string assets_text = "";

                for (int i = 0; i < 20; i++)
                {
                    assets_text = await webView.ExecuteScriptAsync("{let obj = [];"
                                                                   + "const labels = document.querySelectorAll('label[class*=label]');"
                                                                   + "const icons = document.querySelectorAll('div[class*=imageWrapper]');"
                                                                   + "const names = document.querySelectorAll('div[class*=labelWrapper]');"
                                                                   + "for (let i = 0; i < names.length; i++) {"
                                                                   + "const name = names[i].firstChild.textContent;"
                                                                   + "const _icon = icons[i].firstChild.style.backgroundImage;"
                                                                   + "const icon = _icon.substring(_icon.lastIndexOf('/') + 1, _icon.lastIndexOf('?') != -1 ? _icon.lastIndexOf('?') : _icon.lastIndexOf('\"'));"
                                                                   + "obj.push({name, icon});"
                                                                   + "}" // page loaded and no asset found
                                                                   + "if (labels.length != 0 && obj.length == 0) {obj = null;}"
                                                                   + "obj;}");


                    // No asset, no need to import
                    if (assets_text == "null")
                    {
                        break;
                    }

                    else if (assets_text != "[]")
                    {
                        foundAssets = true;
                        break;
                    }

                    await Task.Delay(1000);
                }


                // Waiting until all icons are downloaded
                if (foundAssets)
                {
                    string appId = webView.Source.LocalPath;
                    appId = appId.Substring(appId.IndexOf("/applications/") + 14);
                    appId = appId.Substring(0, appId.IndexOf("/"));

                    // Wait for asset icons
                    List <AppImportForm.ImportedApp> assets = JsonConvert.DeserializeObject <List <AppImportForm.ImportedApp> >((string)assets_text);
                    int appIndex = Program.Applications.FindIndex(v => v.Id == appId);

                    for (int i = 0; i < 20; i++)
                    {
                        bool allAssetIconsLoaded = true;

                        foreach (AppImportForm.ImportedApp asset in assets)
                        {
                            string iconName = appId + "_" + Path.GetFileNameWithoutExtension(asset.icon);
                            int    index    = web_loadedAppIcons.FindIndex(v => v == iconName);

                            if (index == -1)
                            {
                                allAssetIconsLoaded = false;
                                break;
                            }
                        }

                        if (allAssetIconsLoaded)
                        {
                            break;
                        }

                        await Task.Delay(1000);
                    }

                    web_loadedAppIcons.Clear();


                    // Save application assets
                    Program.Applications[appIndex].Assets.Clear();

                    foreach (AppImportForm.ImportedApp asset in assets)
                    {
                        Program.Asset newAsset = new Program.Asset
                        {
                            Icon = asset.icon,
                            Name = asset.name
                        };

                        Program.Applications[appIndex].Assets.Add(newAsset);
                    }
                }

                web_assetFetched = true;
            }
        }