示例#1
0
        private static void InstallMod(object state)
        {
            var op = state as InstallMod;

            if (isInstalling)
            {
                icon.DisplayBubble("Already downloading a mod!");
                return;
            }
            isInstalling = true;
            icon.DisplayBubble("Attempting to download " + 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 client = new WebClient())
                {
                    UnzipFromStream(client.OpenRead(op.url), targetDir);
                }
            }
            catch (Exception ex)
            {
                isInstalling = false;
                icon.DisplayBubble("Failed to download mod " + op.Mod.name + ".");
                return;
            }
            log.Info("Mod installed!");
            icon.DisplayBubble("Mod downloaded successfully: " + op.Mod.name + ".");
            var msg = new OnInstalledMod()
            {
                Mod = op.Mod
            };

            ws.Send(JObject.FromObject(msg).ToString(Formatting.None));
            var existing = mods.FirstOrDefault(m => m.name == op.Mod.name);

            if (existing != null)
            {
                mods.Remove(existing);
            }
            mods.Add(op.Mod);
            isInstalling = false;
        }
示例#2
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);
            }
        }
示例#3
0
        internal static void manualInstallMod()
        {
            if (isInstalling)
            {
                notifier.Notify(NotificationType.Warning, "Already downloading a mod", "Please try again after a few seconds.");
                return;
            }
            isInstalling = true;

            using (var dlg = new OpenFileDialog()
            {
                CheckFileExists = true,
                CheckPathExists = true,
                DefaultExt = "zip",
                Filter = "Zip Files|*.zip",
                FilterIndex = 1,
                Multiselect = false,
                Title = "Choose the mod zip to install"
            })
            {
                //user pressed ok
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    //open the file in a stream
                    using (var fileStream = dlg.OpenFile())
                    {
                        ZipFile zip = new ZipFile(fileStream);

                        //check integrity
                        if (zip.TestArchive(true))
                        {
                            //look for the map file. It contains the mod name
                            ZipEntry map = zip.Cast <ZipEntry>().FirstOrDefault(a => a.Name.ToLower().EndsWith(".bsp"));

                            if (map != null)
                            {
                                //look for the version file
                                int entry = zip.FindEntry("addoninfo.txt", true);
                                if (entry >= 0)
                                {
                                    string allText = string.Empty;

                                    using (var infoStream = new StreamReader(zip.GetInputStream(entry)))
                                        allText = infoStream.ReadToEnd();

                                    string version = modController.ReadAddonVersion(allText);

                                    if (!string.IsNullOrEmpty(version))
                                    {
                                        Version v    = new Version(version);
                                        string  name = Path.GetFileNameWithoutExtension(map.Name).ToLower();

                                        //check if this same mod is already installed and if it needs an update
                                        if (modController.clientMods.Any(
                                                a => a.name.ToLower().Equals(name) && new Version(a.version) >= v))
                                        {
                                            MessageBox.Show("The mod you are trying to install is already installed or outdated.", "Mod Manual Install",
                                                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        }
                                        else
                                        {
                                            string targetDir = Path.Combine(d2mpDir, name);
                                            if (Directory.Exists(targetDir))
                                            {
                                                Directory.Delete(targetDir, true);
                                            }
                                            //Make the dir again
                                            Directory.CreateDirectory(targetDir);

                                            if (UnzipWithTemp(null, fileStream, targetDir))
                                            {
                                                refreshMods();
                                                log.Info("Mod manually installed!");
                                                notifier.Notify(NotificationType.Success, "Mod installed", "The following mod has been installed successfully: " + name);

                                                var mod = new ClientMod()
                                                {
                                                    name = name, version = v.ToString()
                                                };
                                                var msg = new OnInstalledMod()
                                                {
                                                    Mod = mod
                                                };

                                                Send(JObject.FromObject(msg).ToString(Formatting.None));

                                                var existing = modController.clientMods.FirstOrDefault(m => m.name == mod.name);
                                                if (existing != null)
                                                {
                                                    modController.clientMods.Remove(existing);
                                                }

                                                modController.clientMods.Add(mod);
                                            }
                                            else
                                            {
                                                MessageBox.Show("The mod could not be installed. Read the log file for details.", "Mod Manual Install",
                                                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("Could not read the mod version from the zip file.", "Mod Manual Install",
                                                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("No mod info was found in the zip file.", "Mod Manual Install",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                            else
                            {
                                MessageBox.Show("No mod map was found in the zip file.", "Mod Manual Install",
                                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        else
                        {
                            MessageBox.Show("The zip file you selected seems to be invalid.", "Mod Manual Install",
                                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
            }

            isInstalling = false;
        }
示例#4
0
        private static void InstallMod(object state)
        {
            var op = state as InstallMod;

            if (isInstalling)
            {
                notifier.Notify(3, "Already downloading a mod", "Please try again after a few seconds.");
                //icon.DisplayBubble("Already downloading a mod!");
                return;
            }
            isInstalling = true;
            notifier.Notify(2, "Downloading mod", "Attempting to download " + op.Mod.name + "...");
            //icon.DisplayBubble("Attempting to download "+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) => {
                        if (e.ProgressPercentage % 5 == 0 && e.ProgressPercentage > lastProgress)
                        {
                            lastProgress = e.ProgressPercentage;
                            log.Info(String.Format("Downloading mod: {0}% complete.", e.ProgressPercentage.ToString()));
                        }
                    };
                    wc.DownloadDataCompleted += (sender, e) =>
                    {
                        byte[] buffer = e.Result;
                        Stream s      = new MemoryStream(buffer);
                        UnzipFromStream(s, targetDir);
                        log.Info("Mod installed!");
                        notifier.Notify(1, "Mod installed", "The following mod has been installed successfully: " + op.Mod.name);
                        //icon.DisplayBubble("Mod downloaded successfully: " + op.Mod.name + ".");
                        var msg = new OnInstalledMod()
                        {
                            Mod = op.Mod
                        };
                        Send(JObject.FromObject(msg).ToString(Formatting.None));
                        var existing = mods.FirstOrDefault(m => m.name == op.Mod.name);
                        if (existing != null)
                        {
                            mods.Remove(existing);
                        }
                        mods.Add(op.Mod);
                        isInstalling = false;
                    };
                    wc.DownloadDataAsync(new Uri(op.url));
                }
            }
            catch (Exception ex)
            {
                isInstalling = false;
                notifier.Notify(4, "Error downloading mod", "Failed to download mod " + op.Mod.name + ".");
                //icon.DisplayBubble("Failed to download mod " + op.Mod.name + ".");
                return;
            }
        }