Example #1
0
        public static void InstallMod(object state)
        {
            var op = state as InstallMod;

            if (isInstalling)
            {
                notifier.Notify(NotificationType.Warning, "Already downloading a mod", "Please try again after a few seconds.");
                return;
            }
            isInstalling = true;
            notifier.Notify(NotificationType.Progress, "Downloading mod", "Downloading " + op.Mod.name + "...");

            log.Info("Server requested that we install mod " + op.Mod.name + " from download " + op.url);

            //delete if already exists
            string targetDir = Path.Combine(d2mpDir, op.Mod.name);

            if (Directory.Exists(targetDir))
            {
                Directory.Delete(targetDir, true);
            }
            //Make the dir again
            Directory.CreateDirectory(targetDir);
            //Stream the ZIP to the folder
            try
            {
                using (var wc = new WebClient())
                {
                    int lastProgress = -1;
                    wc.DownloadProgressChanged += (sender, e) =>
                    {
                        notifier.reportProgress(e.ProgressPercentage);
                        if (e.ProgressPercentage % 5 == 0 && e.ProgressPercentage > lastProgress)
                        {
                            lastProgress = e.ProgressPercentage;
                            log.Info(String.Format("Downloading mod: {0}% complete.", e.ProgressPercentage));
                        }
                    };
                    wc.DownloadDataCompleted += (sender, e) =>
                    {
                        byte[] buffer = { 0 };
                        try
                        {
                            buffer = e.Result;
                        }
                        catch (Exception ex)
                        {
                            log.Error("Error downloading mod", ex);
                            AskTryAgain(op, "Error Downloading Mod", "The connection was forcibly closed by the remote host");
                            return;
                        }
                        notifier.Notify(NotificationType.Info, "Extracting mod", "Download completed, extracting files...");
                        Stream s = new MemoryStream(buffer);
                        if (UnzipWithTemp(op, s, targetDir))
                        {
                            refreshMods();
                            log.Info("Mod installed!");
                            notifier.Notify(NotificationType.Success, "Mod installed",
                                            "The following mod has been installed successfully: " + op.Mod.name);
                            var msg = new OnInstalledMod()
                            {
                                Mod = op.Mod
                            };
                            Send(JObject.FromObject(msg).ToString(Formatting.None));
                            var existing = modController.clientMods.FirstOrDefault(m => m.name == op.Mod.name);
                            if (existing != null)
                            {
                                modController.clientMods.Remove(existing);
                            }
                            modController.clientMods.Add(op.Mod);
                            isInstalling = false;
                            dlRetry      = false;
                        }
                    };
                    wc.DownloadDataAsync(new Uri(op.url));
                }
            }
            catch (Exception ex)
            {
                log.Error("Failed to download mod " + op.Mod.name + ".", ex);
                AskTryAgain(op, "Error Downloading Mod", "Error downloading mod " + op.Mod.name);
            }
        }