示例#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();
        }
示例#2
0
        private void AssetManagerForm_Load(object sender, EventArgs e)
        {
            app    = (Program.DiscordApplication) this.Tag;
            assets = app.Assets;

            foreach (Program.Asset asset in assets)
            {
                string imageLocation = Path.Combine(Program.appIconsPath, app.Id + "_" + asset.Icon);
                CreateAssetPanel(asset.Name, imageLocation);
            }
        }
示例#3
0
        private void AppEditingForm_Load(object sender, EventArgs e)
        {
            oldApp = Program.Applications.Find(v => v.Name == (string)this.Tag);

            textBoxId.Text   = oldApp.Id;
            textBoxName.Text = oldApp.Name;

            if (oldApp.Icon != "")
            {
                pictureBoxIcon.ImageLocation = Path.Combine(Program.appIconsPath, oldApp.Id + oldApp.Icon);
            }
        }
示例#4
0
        private void comboBoxApp_SelectedIndexChanged(object sender, EventArgs e)
        {
            //await Task.Delay(2000);

            // Clear fields
            comboBoxImageLarge.Items.Clear();
            comboBoxImageLarge.Items.Add("(None)");
            comboBoxImageLarge.SelectedIndex = 0;

            comboBoxImageSmall.Items.Clear();
            comboBoxImageSmall.Items.Add("(None)");
            comboBoxImageSmall.SelectedIndex = 0;

            pictureBoxApp.ImageLocation = null;


            if (comboBoxApp.SelectedIndex == -1)
            {
                return;
            }


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

            if (app.Icon != "")
            {
                pictureBoxApp.ImageLocation = Path.Combine(Program.appIconsPath, app.Id + app.Icon);
            }


            // Load app assets
            foreach (Program.Asset asset in app.Assets)
            {
                comboBoxImageLarge.Items.Add(asset.Name);
                comboBoxImageSmall.Items.Add(asset.Name);
            }


            // Reload preset when needed
            if (loadAppPreset)
            {
                loadAppPreset = false;

                Program.Preset preset = Program.Presets.Find(v => v.Name == (string)comboBoxPreset.SelectedItem);

                comboBoxImageLarge.SelectedIndex = comboBoxImageLarge.Items.IndexOf(preset.LargeKey);
                textBoxImageLarge.Text           = preset.LargeText;
                comboBoxImageSmall.SelectedIndex = comboBoxImageSmall.Items.IndexOf(preset.SmallKey);
                textBoxImageSmall.Text           = preset.SmallText;
            }
        }
        private void buttonCreate_Click(object sender, EventArgs e)
        {
            Program.DiscordApplication app = new Program.DiscordApplication
            {
                Id     = textBoxId.Text,
                Name   = textBoxName.Text,
                Icon   = Path.GetExtension(pictureBoxIcon.ImageLocation) ?? "",
                Assets = new List <Program.Asset>()
            };

            if (app.Id == "")
            {
                MessageBox.Show(null, "Application id cannot be empty.", "Discord RPC Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

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

            else if (Program.Applications.Find(v => v.Id == app.Id) != null)
            {
                MessageBox.Show(null, "An application with this Id already exists.", "Discord RPC Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else if (Program.Applications.Find(v => v.Name == app.Name) != null)
            {
                MessageBox.Show(null, "An application with this name already exists.", "Discord RPC Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {
                if (app.Icon != "")
                {
                    File.Copy(pictureBoxIcon.ImageLocation, Path.Combine(Program.appIconsPath, app.Id + app.Icon), true);
                }

                Program.Applications.Add(app);
                Program.Applications.Sort((a, b) => a.Name.CompareTo(b.Name));

                Program.SaveData();
                this.Close();
            }
        }
示例#6
0
        private void buttonEditAssets_Click(object sender, EventArgs e)
        {
            Program.DiscordApplication app = Program.Applications.Find(v => v.Name == comboBoxApp.Text);

            if (app == null)
            {
                return;
            }

            AssetManagerForm assetManagerForm = new AssetManagerForm
            {
                Tag = app
            };

            assetManagerForm.ShowDialog(this);
            assetManagerForm.Dispose();

            ReloadComboBoxApp();
        }
示例#7
0
        private void ReloadCurrentPreset()
        {
            int index = comboBoxPreset.SelectedIndex;

            Program.Preset             preset = Program.Presets.Find(v => v.Name == (string)comboBoxPreset.SelectedItem);
            Program.DiscordApplication app    = Program.Applications.Find(v => v.Id == preset.AppId);

            textBoxName.Text = preset.Name;

            textBoxDetails.Text = preset.Details;
            textBoxState.Text   = preset.State;

            loadAppPreset             = true;
            comboBoxApp.SelectedIndex = comboBoxApp.Items.IndexOf(app?.Name ?? ""); // app = null!

            comboBoxImageLarge.SelectedIndex = comboBoxImageLarge.Items.IndexOf(preset.LargeKey);
            textBoxImageLarge.Text           = preset.LargeText;
            comboBoxImageSmall.SelectedIndex = comboBoxImageSmall.Items.IndexOf(preset.SmallKey);
            textBoxImageSmall.Text           = preset.SmallText;

            if (app != null)
            {
                /*int appIndex = comboBoxApp.Items.IndexOf(app.Name);
                 * comboBoxApp.SelectedIndex = appIndex;*/

                /*if (appIndex != -1 && app.Icon != "")
                 *  pictureBoxApp.ImageLocation = Path.Combine(Program.appIconsPath, app.Id + app.Icon);
                 * else
                 *  pictureBoxApp.ImageLocation = null;*/
            }

            else
            {
                //comboBoxApp.SelectedIndex = -1;
                //comboBoxImageLarge.SelectedIndex = 0;
                //comboBoxImageSmall.SelectedIndex = 0;

                /*pictureBoxApp.ImageLocation = null;
                 * pictureBoxImageLarge.ImageLocation = null;
                 * pictureBoxImageSmall.ImageLocation = null;*/
            }
        }
示例#8
0
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            Program.DiscordApplication newApp = new Program.DiscordApplication
            {
                Id     = textBoxId.Text,
                Name   = textBoxName.Text,
                Icon   = Path.GetExtension(pictureBoxIcon.ImageLocation) ?? "",
                Assets = oldApp.Assets
            };


            if (newApp.Id == "")
            {
                MessageBox.Show(null, "Application id cannot be empty.", "Discord RPC Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

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

            else if (Program.Applications.Find(v => v.Id == newApp.Id && v.Id != oldApp.Id) != null)
            {
                MessageBox.Show(null, "An application with this Id already exists.", "Discord RPC Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else if (Program.Applications.Find(v => v.Name == newApp.Name && v.Name != oldApp.Name) != null)
            {
                MessageBox.Show(null, "An application with this name already exists.", "Discord RPC Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


            else
            {
                // Save new icon
                if (newApp.Icon != "")
                {
                    try
                    {
                        string iconDestpath = Path.Combine(Program.appIconsPath, newApp.Id + newApp.Icon);
                        File.Copy(pictureBoxIcon.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;
                    }
                }


                // Application ID changed
                if (newApp.Id != oldApp.Id)
                {
                    // Update presets using the old ID
                    foreach (Program.Preset preset in Program.Presets)
                    {
                        if (preset.AppId == oldApp.Id)
                        {
                            preset.AppId = newApp.Id;
                        }
                    }


                    // Update asset filenames
                    foreach (Program.Asset asset in newApp.Assets)
                    {
                        string oldPath = Path.Combine(Program.appIconsPath, oldApp.Id + "_" + asset.Icon);
                        string newPath = Path.Combine(Program.appIconsPath, newApp.Id + "_" + asset.Icon);

                        try
                        {
                            File.Move(oldPath, newPath);
                        }
                        catch (IOException) { }
                        catch (Exception ex)
                        {
                            MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                }


                // Update app
                int index = Program.Applications.FindIndex(v => v.Id == oldApp.Id);
                Program.Applications[index] = newApp;
                Program.Applications.Sort((a, b) => a.Name.CompareTo(b.Name));

                Program.SaveData();
                this.Close();
            }
        }
示例#9
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;
            }
        }