public static void DownloadAndInstall(Mod m, bool isUpdatingMod = false)
        {
            // validate mod is not already downloading/updating
            ModStatus status = Sys.GetStatus(m.ID);

            if (status == ModStatus.Downloading)
            {
                Sys.Message(new WMessage($"{m.Name} [{StringKey.IsAlreadyDownloading}]", true)
                {
                    TextTranslationKey = StringKey.IsAlreadyDownloading
                });
                return;
            }

            if (status == ModStatus.Updating)
            {
                Sys.Message(new WMessage($"{m.Name} [{StringKey.IsAlreadyUpdating}]", true)
                {
                    TextTranslationKey = StringKey.IsAlreadyUpdating
                });
                return;
            }

            if (status == ModStatus.Installed && !isUpdatingMod)
            {
                Sys.Message(new WMessage($"{m.Name} [{StringKey.IsAlreadyDownloadedAndInstalled}]", true)
                {
                    TextTranslationKey = StringKey.IsAlreadyDownloadedAndInstalled
                });
                return;
            }

            Action             onCancel = () => Sys.RevertStatus(m.ID);
            Action <Exception> onError  = ex =>
            {
                Sys.Message(new WMessage($"[{StringKey.ErrorOccurredDownloadingInstalling}] {m.Name} - {ex.Message}", WMessageLogLevel.Error, ex)
                {
                    TextTranslationKey = StringKey.ErrorOccurredDownloadingInstalling
                });
                Sys.RevertStatus(m.ID);
            };

            string file     = String.Format("{0}_{1}_{2}.iro", m.ID, SafeStr(m.Name), m.LatestVersion.Version);
            string temppath = Path.Combine(Sys.Settings.LibraryLocation, "temp");

            Directory.CreateDirectory(temppath);

            var install = Sys.Library.GetItem(m.ID);

            if (install != null)
            {
                // mod is installed so download update files for updating mod
                Sys.SetStatus(m.ID, ModStatus.Updating);

                List <ModPatch> patches = m.GetPatchesFromTo(install.LatestInstalled.VersionDetails.Version, m.LatestVersion.Version);
                if (patches.Any())
                {
                    // download patches to update the mod
                    string       pfile    = String.Format("{0}_{1}_{2}.irop", m.ID, SafeStr(m.Name), m.LatestVersion.Version);
                    DownloadItem download = new DownloadItem()
                    {
                        Links                  = patches.Select(p => p.Link).ToList(),
                        SaveFilePath           = Path.Combine(temppath, pfile),
                        Category               = DownloadCategory.ModPatch,
                        ItemName               = $"[{StringKey.Downloading}] {m.Name} patch {install.LatestInstalled.VersionDetails.Version} -> {m.LatestVersion.Version}",
                        ItemNameTranslationKey = StringKey.Downloading,
                        OnCancel               = onCancel
                    };

                    download.IProc = new PatchModProcedure()
                    {
                        Filename = pfile,
                        Install  = install,
                        Mod      = m,
                        Error    = onError
                    };

                    Sys.Downloads.AddToDownloadQueue(download);
                    return;
                }
                else
                {
                    // no patches available to update so download entire new mod version .iro
                    DownloadAndInstallMod(m, temppath, file);
                    return;
                }
            }


            Sys.SetStatus(m.ID, ModStatus.Downloading);

            if (m.Patches.Any())
            {
                // mod is not installed and the latest version has patches available
                // so first download and install mod then download all patches for mod

                Guid downloadId = DownloadAndInstallMod(m, temppath, file);

                int pCount = 0;
                foreach (IGrouping <decimal, ModPatch> linksPerVersion in m.Patches.GroupBy(mp => mp.VerTo))
                {
                    string pfile = String.Format("{0}_{1}_{2}_patch{3}.irop", m.ID, SafeStr(m.Name), m.LatestVersion.Version, pCount);

                    DownloadItem patchDownload = new DownloadItem()
                    {
                        ParentUniqueID         = downloadId, // this is set to know these patch downloads are dependent of the main mod download or previous patch.
                        Links                  = linksPerVersion.Select(mp => mp.Link).ToList(),
                        SaveFilePath           = Path.Combine(temppath, pfile),
                        ItemName               = $"[{StringKey.Downloading}] {m.Name} patch {linksPerVersion.Key}",
                        ItemNameTranslationKey = StringKey.Downloading,
                        Category               = DownloadCategory.ModPatch,
                        OnCancel               = onCancel
                    };

                    patchDownload.IProc = new PatchModProcedure()
                    {
                        Filename = pfile,
                        Error    = onError,
                        Mod      = m
                    };

                    downloadId = patchDownload.UniqueId; // get the id of the patch download so it can be set as the parent for the following patch

                    Sys.Downloads.AddToDownloadQueue(patchDownload);
                    pCount++;
                }
            }
            else
            {
                // mod is not installed in library so just download using the links of latest version
                DownloadAndInstallMod(m, temppath, file);
            }
        }
Exemple #2
0
        public static void DownloadAndInstall(Mod m, bool isUpdatingMod = false)
        {
            // validate mod is not already downloading/updating
            ModStatus status = Sys.GetStatus(m.ID);

            if (status == ModStatus.Downloading)
            {
                Sys.Message(new WMessage($"{m.Name} is already downloading!", true));
                return;
            }

            if (status == ModStatus.Updating)
            {
                Sys.Message(new WMessage($"{m.Name} is already updating!", true));
                return;
            }

            if (status == ModStatus.Installed && !isUpdatingMod)
            {
                Sys.Message(new WMessage($"{m.Name} is already downloaded and installed!", true));
                return;
            }

            Action             onCancel = () => Sys.RevertStatus(m.ID);
            Action <Exception> onError  = ex =>
            {
                Sys.Message(new WMessage($"Error occurred downloading/installing {m.Name} - {ex.Message}", WMessageLogLevel.Error, ex));
                Sys.RevertStatus(m.ID);
            };

            string file     = String.Format("{0}_{1}_{2}.iro", m.ID, SafeStr(m.Name), m.LatestVersion.Version);
            string temppath = System.IO.Path.Combine(Sys.Settings.LibraryLocation, "temp");

            System.IO.Directory.CreateDirectory(temppath);

            var install = Sys.Library.GetItem(m.ID);

            if (install != null)
            {
                // mod is installed so download update files for updating mod
                Sys.SetStatus(m.ID, ModStatus.Updating);

                List <ModPatch> patches = m.GetPatchesFromTo(install.LatestInstalled.VersionDetails.Version, m.LatestVersion.Version);
                if (patches.Any())
                {
                    // download patches to update the mod
                    string       pfile    = String.Format("{0}_{1}_{2}.irop", m.ID, SafeStr(m.Name), m.LatestVersion.Version);
                    DownloadItem download = new DownloadItem()
                    {
                        Links        = patches.Select(p => p.Link).ToList(),
                        SaveFilePath = System.IO.Path.Combine(temppath, pfile),
                        Category     = DownloadCategory.Mod,
                        ItemName     = "Downloading " + m.Name,
                        OnCancel     = onCancel
                    };

                    download.IProc = new PatchModProcedure()
                    {
                        File    = pfile,
                        Install = install,
                        Mod     = m,
                        Error   = onError
                    };

                    Sys.Downloads.AddToDownloadQueue(download);
                    return;
                }
                else
                {
                    // no patches available to update so download entire new mod version .iro
                    DownloadItem installDownload = new DownloadItem()
                    {
                        Links        = m.LatestVersion.Links,
                        SaveFilePath = System.IO.Path.Combine(temppath, file),
                        Category     = DownloadCategory.Mod,
                        ItemName     = "Downloading " + m.Name,
                        OnCancel     = onCancel
                    };

                    installDownload.IProc = new InstallModProcedure()
                    {
                        File             = file,
                        Mod              = m,
                        ExtractSubFolder = m.LatestVersion.ExtractSubFolder,
                        ExtractInto      = m.LatestVersion.ExtractInto,
                        Error            = onError
                    };

                    Sys.Downloads.AddToDownloadQueue(installDownload);
                    return;
                }
            }



            Sys.SetStatus(m.ID, ModStatus.Downloading);

            if (m.LatestVersion.PatchLinks.Any())
            {
                // mod is not installed and the latest version has patches available
                // so first download and install mod then download all patches for mod
                PatchController pc = new PatchController(m.LatestVersion.PatchLinks.Count);

                DownloadItem download = new DownloadItem()
                {
                    Links        = m.LatestVersion.Links,
                    SaveFilePath = System.IO.Path.Combine(temppath, file),
                    ItemName     = "Downloading " + m.Name,
                    Category     = DownloadCategory.Mod,
                    OnCancel     = onCancel
                };

                download.IProc = new DownloadThenPatchProcedure()
                {
                    File       = file,
                    Mod        = m,
                    Controller = pc,
                    Error      = onError
                };

                Sys.Downloads.AddToDownloadQueue(download);

                int pCount = 0;
                foreach (string p in m.LatestVersion.PatchLinks)
                {
                    string pfile = String.Format("{0}_{1}_{2}_patch{3}.iro", m.ID, SafeStr(m.Name), m.LatestVersion.Version, pCount);
                    pc.PatchFiles.Add(pfile);

                    DownloadItem patchDownload = new DownloadItem()
                    {
                        Links = new List <string>()
                        {
                            p
                        },
                        SaveFilePath = System.IO.Path.Combine(temppath, pfile),
                        ItemName     = "Downloading " + m.Name + " patch " + pCount,
                        Category     = DownloadCategory.Mod,
                        OnCancel     = () =>
                        {
                            pc.PatchFailed();
                            Sys.RevertStatus(m.ID);
                        }
                    };

                    patchDownload.IProc = new DownloadPatchProcedure()
                    {
                        File       = pfile,
                        Controller = pc,
                        Error      = ex =>
                        {
                            pc.PatchFailed();
                            Sys.RevertStatus(m.ID);
                        }
                    };

                    Sys.Downloads.AddToDownloadQueue(patchDownload);
                    pCount++;
                }
            }
            else
            {
                // mod is not installed in library so just download using the links of latest version
                DownloadItem installDownload = new DownloadItem()
                {
                    Links        = m.LatestVersion.Links,
                    SaveFilePath = System.IO.Path.Combine(temppath, file),
                    Category     = DownloadCategory.Mod,
                    ItemName     = "Downloading " + m.Name,
                    OnCancel     = onCancel
                };

                installDownload.IProc = new InstallModProcedure()
                {
                    File             = file,
                    Mod              = m,
                    ExtractSubFolder = m.LatestVersion.ExtractSubFolder,
                    ExtractInto      = m.LatestVersion.ExtractInto,
                    Error            = onError
                };

                Sys.Downloads.AddToDownloadQueue(installDownload);
            }
        }
Exemple #3
0
        public static void DownloadAndInstall(Mod m)
        {
            string temppath = System.IO.Path.Combine(Sys.Settings.LibraryLocation, "temp");

            System.IO.Directory.CreateDirectory(temppath);

            var install = Sys.Library.GetItem(m.ID);

            if (install != null)
            {
                var patches = m.GetPatchesFromTo(install.LatestInstalled.VersionDetails.Version, m.LatestVersion.Version);
                if (patches.Any())
                {
                    string pfile = String.Format("{0}_{1}_{2}.irop", m.ID, SafeStr(m.Name), m.LatestVersion.Version);
                    Sys.SetStatus(m.ID, ModStatus.Downloading);
                    Sys.Downloads.Download(patches.Select(p => p.Link),
                                           System.IO.Path.Combine(temppath, pfile),
                                           "Downloading " + m.Name,
                                           new PatchModProcedure()
                    {
                        File = pfile, Install = install, Mod = m
                    },
                                           () => Sys.RevertStatus(m.ID)
                                           );
                    return;
                }
            }

            string file = String.Format("{0}_{1}_{2}.iro", m.ID, SafeStr(m.Name), m.LatestVersion.Version);

            Sys.SetStatus(m.ID, ModStatus.Downloading);

            if (m.LatestVersion.PatchLinks.Any())
            {
                PatchController pc = new PatchController(m.LatestVersion.PatchLinks.Count);

                Sys.Downloads.Download(m.LatestVersion.Links,
                                       System.IO.Path.Combine(temppath, file),
                                       "Downloading " + m.Name,
                                       new DownloadThenPatchProcedure()
                {
                    File = file, Mod = m, Controller = pc
                },
                                       () => Sys.RevertStatus(m.ID)
                                       );
                int pCount = 0;
                foreach (string p in m.LatestVersion.PatchLinks)
                {
                    string pfile = String.Format("{0}_{1}_{2}_patch{3}.iro", m.ID, SafeStr(m.Name), m.LatestVersion.Version, pCount);
                    pc.PatchFiles.Add(pfile);
                    Sys.Downloads.Download(p,
                                           System.IO.Path.Combine(temppath, pfile),
                                           "Downloading " + m.Name + " patch " + pCount,
                                           new DownloadPatchProcedure()
                    {
                        File = pfile, Controller = pc
                    },
                                           () => {
                        pc.PatchFailed();
                        Sys.RevertStatus(m.ID);
                    }
                                           );

                    pCount++;
                }
            }
            else
            {
                Sys.Downloads.Download(m.LatestVersion.Links,
                                       System.IO.Path.Combine(temppath, file),
                                       "Downloading " + m.Name,
                                       new InstallModProcedure()
                {
                    File = file, Mod = m, ExtractSubFolder = m.LatestVersion.ExtractSubFolder, ExtractInto = m.LatestVersion.ExtractInto
                },
                                       () => Sys.RevertStatus(m.ID)
                                       );
            }
        }