Esempio n. 1
0
        /// <inheritdoc cref="IFileUploader"/>
        /// <summary>
        /// Uploads the file to mega.nz.
        /// </summary>
        /// <returns>The result as <see cref="string"/>.</returns>
        /// <seealso cref="IFileUploader"/>
        public async Task <string> UploadToMega()
        {
            var client           = new MegaApiClient();
            var possibleAccounts = this.GetPossibleAccounts("mega");

            foreach (var account in possibleAccounts)
            {
                LoginMega(client, account);
                var info = await client.GetAccountInformationAsync();

                if (info.UsedQuota + GetFileSize(this.FileName) > info.TotalQuota)
                {
                    continue;
                }

                var root     = GetRootFolderMega(client);
                var progress = this.GetProgress();
                var node     = await client.UploadFileAsync(this.FileName, root, progress, null);

                var link = await client.GetDownloadLinkAsync(node);

                await client.LogoutAsync();

                return(link.AbsoluteUri);
            }

            return(string.Empty);
        }
        private async static Task FetchMegaFile(string inSaveLocation, DownloadProgressCallback delProgress)
        {
            string megaUrl = await FetchMegaUrl();

            if (File.Exists(inSaveLocation))
            {
                File.Delete(inSaveLocation);
            }

            // Because the MegaApiClient just Task.Run()s (rather than actually parking until the native events are over)
            // I might want to use the single-threaded API all wrapped in a single Task.Run(). Probably negligible gains
            // though (only save ~number of commands - 1 threadpool requests) and it could be annoying if I want to handle
            // user input for ex: "Do you want to retry?"
            MegaApiClient client = new MegaApiClient();

            try
            {
                await client.LoginAnonymousAsync();

                INodeInfo node = await client.GetNodeFromLinkAsync(new Uri(megaUrl));

                await client.DownloadFileAsync(new Uri(megaUrl), inSaveLocation, new ProgressReporter(delProgress, node.Size));
            }
            catch (Exception ex)
            {
                // Check to see if we can split up the errors any further.
                throw new CannotConnectToMegaException("", ex);;
            }
            finally
            {
                await client.LogoutAsync();
            }
        }
Esempio n. 3
0
        private async void installLoginHelperClick(object sender, RoutedEventArgs e)
        {
            _progressControl        = new ProgressControl();
            ProgressGrid.Visibility = Visibility.Visible;
            MainGrid.Visibility     = Visibility.Collapsed;
            ProgressPanel.Children.Add(_progressControl);

            string pluginName = "loginhelper";

            if (((Button)sender).Name.Contains("CHARSELECT"))
            {
                pluginName = "charselect";
            }

            await Task.Run(async() =>
            {
                try
                {
                    if (!Directory.Exists("modpolice"))
                    {
                        Directory.CreateDirectory("modpolice");
                    }

                    ProgressControl.updateProgressLabel("Logging into Mega anonymously...");
                    var client = new MegaApiClient();
                    await client.LoginAnonymousAsync();

                    if (!Directory.Exists("modpolice"))
                    {
                        Directory.CreateDirectory("modpolice");
                    }

                    ProgressControl.updateProgressLabel("Retrieving file list...");
                    IEnumerable <INode> nodes = await client.GetNodesFromLinkAsync(new Uri("https://mega.nz/folder/4EUF2IhL#Ci1Y-sbbyw7nwwMGvHV2_w"));

                    INode currentNode           = null;
                    IProgress <double> progress = new Progress <double>(x => ProgressControl.updateProgressLabel(String.Format("Downloading: {0} ({1}%)", currentNode.Name, Math.Round(x))));

                    //Find our latest nodes for download
                    INode loginhelper_node = nodes.Where(x => x.Type == NodeType.File && x.Name.Contains(pluginName)).OrderByDescending(t => t.ModificationDate).FirstOrDefault();

                    if (loginhelper_node == null)
                    {
                        ProgressControl.errorSadPeepo(Visibility.Visible);
                        ProgressControl.updateProgressLabel("Something went wrong getting the node");
                        await Task.Delay(7000);
                        return;
                    }

                    if (File.Exists(@"modpolice\" + loginhelper_node.Name))
                    {
                        File.Delete(@"modpolice\" + loginhelper_node.Name);
                    }

                    currentNode = loginhelper_node;
                    await client.DownloadFileAsync(currentNode, @"modpolice\" + loginhelper_node.Name, progress);

                    ProgressControl.updateProgressLabel("Unzipping: " + loginhelper_node.Name);
                    await Task.Delay(750);
                    Modpolice.ExtractZipFileToDirectory(@".\modpolice\" + loginhelper_node.Name, @".\modpolice", true);

                    ProgressControl.updateProgressLabel("Installing " + pluginName + " x86");
                    await Task.Delay(750);

                    if (!Directory.Exists(plugins_x86))
                    {
                        Directory.CreateDirectory(plugins_x86);
                    }

                    ProgressControl.updateProgressLabel("Installing " + pluginName + " x86");
                    if (File.Exists(Path.Combine(plugins_x86, pluginName + ".dll")))
                    {
                        File.Delete(Path.Combine(plugins_x86, pluginName + ".dll"));
                    }

                    File.Move(@".\modpolice\bin\plugins\" + pluginName + ".dll", Path.Combine(plugins_x86, pluginName + ".dll"));

                    ProgressControl.updateProgressLabel("Installing " + pluginName + " x64");
                    await Task.Delay(750);

                    if (!Directory.Exists(plugins_x64))
                    {
                        Directory.CreateDirectory(plugins_x64);
                    }

                    ProgressControl.updateProgressLabel("Installing " + pluginName + " x64");
                    if (File.Exists(Path.Combine(plugins_x64, pluginName + ".dll")))
                    {
                        File.Delete(Path.Combine(plugins_x64, pluginName + ".dll"));
                    }

                    File.Move(@".\modpolice\bin64\plugins\" + pluginName + ".dll", Path.Combine(plugins_x64, pluginName + ".dll"));

                    if (pluginName == "loginhelper")
                    {
                        ProgressControl.updateProgressLabel("Searching for use-ingame-login.xml");
                        await Task.Delay(750);

                        if (!File.Exists(login_xml))
                        {
                            ProgressControl.updateProgressLabel("patches.xml not found, installing...");
                            if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BnS", "patches")))
                            {
                                Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BnS", "patches"));
                            }

                            File.WriteAllText(login_xml, Properties.Resources.use_ingame_login);
                        }
                    }

                    ProgressControl.updateProgressLabel("All done, just tidying up.");
                    await client.LogoutAsync();
                } catch (Exception ex)
                {
                    ProgressControl.errorSadPeepo(Visibility.Visible);
                    ProgressControl.updateProgressLabel(ex.Message);
                    await Task.Delay(7000);
                }
            });

            ProgressGrid.Visibility = Visibility.Hidden;
            MainGrid.Visibility     = Visibility.Visible;
            ProgressPanel.Children.Clear();
            _progressControl = null;

            //Check if loginhelper is installed
            loginHelper_installed = (File.Exists(Path.Combine(plugins_x86, pluginName + ".dll")) && File.Exists(Path.Combine(plugins_x64, pluginName + ".dll")));
            charselect_installed  = (File.Exists(Path.Combine(plugins_x86, "charselect.dll")) && File.Exists(Path.Combine(plugins_x64, "charselect.dll")));
            await Task.Run(async() => await checkOnlineVersion());
        }
Esempio n. 4
0
        private async void installAdditional(object sender, RoutedEventArgs e)
        {
            string pluginName;

            switch (((Button)sender).Name)
            {
            case "simplemodeInstall":
                pluginName = "simplemodetrainingroom";
                break;

            case "lessloadingInstall":
                pluginName = "lessloadingscreens";
                break;

            default:
                pluginName = "highpriority";
                break;
            }

            toggleControl           = false;
            _progressControl        = new ProgressControl();
            ProgressGrid.Visibility = Visibility.Visible;
            MainGrid.Visibility     = Visibility.Collapsed;
            ProgressPanel.Children.Add(_progressControl);

            try
            {
                if (!Directory.Exists("modpolice"))
                {
                    Directory.CreateDirectory("modpolice");
                }

                ProgressControl.updateProgressLabel("Logging into Mega");
                var client = new MegaApiClient();
                await client.LoginAnonymousAsync();

                ProgressControl.updateProgressLabel("Retrieving file list...");
                IEnumerable <INode> nodes = await client.GetNodesFromLinkAsync(new Uri("https://mega.nz/folder/WXhzUZ7Y#XzlqkPa8DU4X8xrILQDdZA"));

                INode currentNode           = null;
                IProgress <double> progress = new Progress <double>(x => ProgressControl.updateProgressLabel(String.Format("Downloading: {0} ({1}%)", currentNode.Name, Math.Round(x))));
                INode pluginNode            = nodes.Where(x => x.Type == NodeType.File && x.Name.Contains(pluginName)).OrderByDescending(t => t.ModificationDate).FirstOrDefault();

                if (pluginNode == null)
                {
                    ProgressControl.errorSadPeepo(Visibility.Visible);
                    ProgressControl.updateProgressLabel("Something went wrong getting the node");
                    await Task.Delay(5000);

                    toggleControl = true;
                    return;
                }

                if (File.Exists(@"modpolice\" + pluginNode.Name))
                {
                    File.Delete(@"modpolice\" + pluginNode.Name);
                }

                currentNode = pluginNode;
                await client.DownloadFileAsync(currentNode, @"modpolice\" + pluginNode.Name, progress);

                ProgressControl.updateProgressLabel("Unzipping: " + pluginNode.Name);
                await Task.Delay(750);

                Modpolice.ExtractZipFileToDirectory(@".\modpolice\" + pluginNode.Name, @".\modpolice", true);

                ProgressControl.updateProgressLabel("Installing " + pluginName + " x86");
                await Task.Delay(750);

                if (!Directory.Exists(SystemConfig.SYS.BNS_DIR + plugins_x86))
                {
                    Directory.CreateDirectory(SystemConfig.SYS.BNS_DIR + plugins_x86);
                }

                //Delete the current plugin dll
                if (File.Exists(SystemConfig.SYS.BNS_DIR + plugins_x86 + pluginName + ".dll"))
                {
                    File.Delete(SystemConfig.SYS.BNS_DIR + plugins_x86 + pluginName + ".dll");
                }

                //Make sure there isn't a plugin dll that is in an off state
                if (File.Exists(SystemConfig.SYS.BNS_DIR + plugins_x86 + pluginName + ".dll.off"))
                {
                    File.Delete(SystemConfig.SYS.BNS_DIR + plugins_x86 + pluginName + ".dll.off");
                }

                File.Move(@".\modpolice\bin\plugins\" + pluginName + ".dll", SystemConfig.SYS.BNS_DIR + plugins_x86 + pluginName + ".dll");

                ProgressControl.updateProgressLabel("Installing " + pluginName + " x64");
                await Task.Delay(750);

                if (!Directory.Exists(SystemConfig.SYS.BNS_DIR + plugins_x64))
                {
                    Directory.CreateDirectory(SystemConfig.SYS.BNS_DIR + plugins_x64);
                }

                //Delete the current plugin dll
                if (File.Exists(SystemConfig.SYS.BNS_DIR + plugins_x64 + pluginName + ".dll"))
                {
                    File.Delete(SystemConfig.SYS.BNS_DIR + plugins_x64 + pluginName + ".dll");
                }

                //Make sure there isn't a plugin dll that is in an off state
                if (File.Exists(SystemConfig.SYS.BNS_DIR + plugins_x64 + pluginName + ".dll.off"))
                {
                    File.Delete(SystemConfig.SYS.BNS_DIR + plugins_x64 + pluginName + ".dll.off");
                }

                File.Move(@".\modpolice\bin64\plugins\" + pluginName + ".dll", SystemConfig.SYS.BNS_DIR + plugins_x64 + pluginName + ".dll");

                ProgressControl.updateProgressLabel("All done");
                await Task.Delay(750);

                await client.LogoutAsync();
            } catch (Exception ex)
            {
                ProgressControl.errorSadPeepo(Visibility.Visible);
                ProgressControl.updateProgressLabel(ex.Message);
                await Task.Delay(7000);
            }
            try
            {
                ProgressGrid.Visibility = Visibility.Hidden;
                MainGrid.Visibility     = Visibility.Visible;
                ProgressPanel.Children.Clear();
                _progressControl = null;
                toggleControl    = true;

                if (pluginName == "simplemodetrainingroom")
                {
                    simplemodeTraining = new pluginFileInfo(SystemConfig.SYS.BNS_DIR + plugins_x86 + "simplemodetrainingroom.dll");
                    Dispatchers.toggleIsChecked(simplemodeToggle, true);
                    Dispatchers.labelContent(SimplemodeCurrentLbl, String.Format("Current: {0}", (simplemodeTraining != null) ? simplemodeTraining.modificationTime.ToString("MM-dd-yy") : "Not Installed"));
                }
                else if (pluginName == "lessloadingscreens")
                {
                    lessLoadingScreen = new pluginFileInfo(SystemConfig.SYS.BNS_DIR + plugins_x86 + "lessloadingscreens.dll");
                    Dispatchers.toggleIsChecked(lessloadingToggle, true);
                    Dispatchers.labelContent(lessloadingCurrentLbl, String.Format("Current: {0}", (lessLoadingScreen != null) ? lessLoadingScreen.modificationTime.ToString("MM-dd-yy") : "Not Installed"));
                }
                else
                {
                    highpriorityplugin = new pluginFileInfo(SystemConfig.SYS.BNS_DIR + plugins_x86 + "highpriority.dll");
                    Dispatchers.toggleIsChecked(HighpriorityToggle, true);
                    Dispatchers.labelContent(HighpriorityCurrentLbl, String.Format("Current: {0}", (highpriorityplugin != null) ? highpriorityplugin.modificationTime.ToString("MM-dd-yy") : "Not Installed"));
                }

                Dispatchers.btnIsEnabled((Button)sender, false);
            } catch (Exception)
            {
                //F**K ASS C**T
            }
        }
        private void DownloadFileFromMega(DownloadItemViewModel newDownload, string megaFileId)
        {
            bool wasCanceled = false;

            newDownload.PerformCancel = () =>
            {
                wasCanceled = true;

                try
                {
                    _megaDownloadCancelTokenSource?.Cancel();
                }
                catch (Exception dex)
                {
                    Logger.Error(dex);
                }
                finally
                {
                    _megaDownloadCancelTokenSource?.Dispose();
                    _megaDownloadCancelTokenSource = null;
                }

                newDownload.OnCancel?.Invoke();
            };

            var client = new MegaApiClient();

            client.LoginAnonymousAsync().ContinueWith((loginResult) =>
            {
                if (wasCanceled)
                {
                    return; // don't continue after async login since user already canceled download
                }

                if (loginResult.IsFaulted)
                {
                    newDownload.OnError?.Invoke(loginResult.Exception.GetBaseException());
                    return;
                }


                // get nodes from mega folder
                Uri fileLink   = new Uri($"https://mega.nz/file/{megaFileId}");
                INodeInfo node = client.GetNodeFromLink(fileLink);

                if (wasCanceled)
                {
                    return; // don't continue after async login since user already canceled download
                }

                if (node == null)
                {
                    newDownload.OnError?.Invoke(new Exception($"could not find node from link {fileLink}"));
                    client.LogoutAsync();
                    return;
                }

                if (File.Exists(newDownload.SaveFilePath))
                {
                    File.Delete(newDownload.SaveFilePath); //delete old temp file if it exists (throws exception otherwise)
                }


                IProgress <double> progressHandler = new Progress <double>(x =>
                {
                    double estimatedBytesReceived = (double)node.Size * (x / 100);
                    UpdateDownloadProgress(newDownload, (int)x, (long)estimatedBytesReceived);
                });

                _megaDownloadCancelTokenSource = new CancellationTokenSource();
                Task downloadTask = client.DownloadFileAsync(fileLink, newDownload.SaveFilePath, progressHandler, _megaDownloadCancelTokenSource.Token);

                downloadTask.ContinueWith((downloadResult) =>
                {
                    _megaDownloadCancelTokenSource?.Dispose();
                    _megaDownloadCancelTokenSource = null;
                    client.LogoutAsync();

                    if (downloadResult.IsCanceled)
                    {
                        return;
                    }

                    if (downloadResult.IsFaulted)
                    {
                        newDownload.OnError?.Invoke(downloadResult.Exception.GetBaseException());
                        return;
                    }

                    _wc_DownloadFileCompleted(client, new AsyncCompletedEventArgs(null, false, newDownload));
                });
            });
        }
Esempio n. 6
0
        private async void downloadClick(object sender, RoutedEventArgs e)
        {
            _progressControl        = new ProgressControl();
            ProgressGrid.Visibility = Visibility.Visible;
            MainGrid.Visibility     = Visibility.Collapsed;
            ProgressPanel.Children.Add(_progressControl);

            await Task.Run(async() =>
            {
                try
                {
                    ProgressControl.updateProgressLabel("Logging into Mega anonymously...");
                    var client = new MegaApiClient();
                    await client.LoginAnonymousAsync();

                    if (!Directory.Exists("modpolice"))
                    {
                        Directory.CreateDirectory("modpolice");
                    }

                    ProgressControl.updateProgressLabel("Retrieving file list...");
                    IEnumerable <INode> nodes = await client.GetNodesFromLinkAsync(new Uri("https://mega.nz/folder/WXhzUZ7Y#XzlqkPa8DU4X8xrILQDdZA"));

                    INode currentNode           = null;
                    IProgress <double> progress = new Progress <double>(x => ProgressControl.updateProgressLabel(String.Format("Downloading: {0} ({1}%)", currentNode.Name, Math.Round(x))));

                    //Find our latest nodes for download
                    INode bnspatch_node     = nodes.Where(x => x.Type == NodeType.File && x.Name.Contains("bnspatch")).OrderByDescending(t => t.ModificationDate).FirstOrDefault();
                    INode pluginloader_node = nodes.Where(x => x.Type == NodeType.File && x.Name.Contains("pluginloader")).OrderByDescending(t => t.ModificationDate).FirstOrDefault();

                    if (pluginloader_node == null)
                    {
                        ProgressControl.errorSadPeepo(Visibility.Visible);
                        ProgressControl.updateProgressLabel("Errorr retrieving pluginloader");
                    }
                    else
                    {
                        currentNode = pluginloader_node;
                        if (File.Exists(@"modpolice\" + pluginloader_node.Name))
                        {
                            File.Delete(@"modpolice\" + pluginloader_node.Name);
                        }

                        ProgressControl.errorSadPeepo(Visibility.Hidden);
                        await client.DownloadFileAsync(currentNode, @"modpolice\" + pluginloader_node.Name, progress);
                    }

                    if (pluginloader_node == null)
                    {
                        ProgressControl.errorSadPeepo(Visibility.Visible);
                        ProgressControl.updateProgressLabel("Errorr retrieving pluginloader");
                    }
                    else
                    {
                        currentNode = bnspatch_node;
                        if (File.Exists(@"modpolice\" + bnspatch_node.Name))
                        {
                            File.Delete(@"modpolice\" + bnspatch_node.Name);
                        }

                        ProgressControl.errorSadPeepo(Visibility.Hidden);
                        await client.DownloadFileAsync(currentNode, @"modpolice\" + bnspatch_node.Name, progress);
                    }

                    ProgressControl.updateProgressLabel("All done, logging out...");
                    await client.LogoutAsync();
                    ProgressControl.updateProgressLabel("Peepo berry happy!");
                    Thread.Sleep(1500);
                }
                catch (Exception ex)
                {
                    ProgressControl.errorSadPeepo(Visibility.Visible);
                    ProgressControl.updateProgressLabel(ex.Message);
                    Thread.Sleep(4000);
                }
            });

            ProgressGrid.Visibility = Visibility.Hidden;
            MainGrid.Visibility     = Visibility.Visible;
            ProgressPanel.Children.Clear();
            _progressControl = null;
        }