/// <summary>
 /// A simple constructor that initializes the object with the required dependencies.
 /// </summary>
 /// <param name="p_modMod">The mod for which the script is running.</param>
 /// <param name="p_gmdGameMode">The game mode currently being managed.</param>
 /// <param name="p_igpInstallers">The utility class to use to install the mod items.</param>
 public XmlScriptInstaller(IMod p_modMod, IGameMode p_gmdGameMode, InstallerGroup p_igpInstallers, IVirtualModActivator p_ivaVirtualModActivator)
 {
     Mod        = p_modMod;
     GameMode   = p_gmdGameMode;
     Installers = p_igpInstallers;
     m_ivaVirtualModActivator = p_ivaVirtualModActivator;
     m_mliModLinkInstaller    = m_ivaVirtualModActivator.GetModLinkInstaller();
 }
Exemplo n.º 2
0
        /// <summary>
        /// The method that is called to start the backgound task.
        /// </summary>
        /// <remarks>
        /// This method installs all of the files in the <see cref="IMod"/> being installed.
        /// </remarks>
        /// <param name="args">Arguments to for the task execution.</param>
        /// <returns>A return value.</returns>
        protected override object DoWork(object[] args)
        {
            IModLinkInstaller ModLinkInstaller = VirtualModActivator.GetModLinkInstaller();

            char[] chrDirectorySeperators = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
            List <KeyValuePair <string, string> > lstFiles       = (FilesToInstall == null) ? Mod.GetFileList().Select(x => new KeyValuePair <string, string>(x, null)).ToList() : FilesToInstall;
            List <KeyValuePair <string, string> > lstFilesToLink = new List <KeyValuePair <string, string> >();

            OverallProgressMaximum = lstFiles.Count * 2;

            if (GameMode.RequiresSpecialFileInstallation && GameMode.IsSpecialFile(Mod.GetFileList()))
            {
                lstFiles = GameMode.SpecialFileInstall(Mod).Select(x => new KeyValuePair <string, string>(x, null)).ToList();
            }

            if (GameMode.RequiresModFileMerge)
            {
                GameMode.ModFileMerge(ActiveMods, Mod, false);
            }

            foreach (KeyValuePair <string, string> File in lstFiles)
            {
                string strFileTo = File.Value;
                if (string.IsNullOrWhiteSpace(strFileTo))
                {
                    strFileTo = File.Key;
                }


                if (Status == TaskStatus.Cancelling)
                {
                    return(false);
                }
                string strFixedPath = GameMode.GetModFormatAdjustedPath(Mod.Format, strFileTo, Mod, false);
                if (string.IsNullOrEmpty(strFixedPath))
                {
                    continue;
                }

                string strModFilenamePath   = Path.Combine(VirtualModActivator.VirtualPath, Path.GetFileNameWithoutExtension(Mod.Filename), GameMode.GetModFormatAdjustedPath(Mod.Format, strFileTo, true));
                string strModDownloadIDPath = (string.IsNullOrWhiteSpace(Mod.DownloadId) || (Mod.DownloadId.Length <= 1) || Mod.DownloadId.Equals("-1", StringComparison.OrdinalIgnoreCase)) ? string.Empty : Path.Combine(VirtualModActivator.VirtualPath, Mod.DownloadId, GameMode.GetModFormatAdjustedPath(Mod.Format, strFileTo, true));
                string strVirtualPath       = strModFilenamePath;

                if (!string.IsNullOrWhiteSpace(strModDownloadIDPath))
                {
                    strVirtualPath = strModDownloadIDPath;
                }

                string strLinkPath = string.Empty;
                if (VirtualModActivator.MultiHDMode)
                {
                    string strModFilenameLink   = Path.Combine(VirtualModActivator.HDLinkFolder, Path.GetFileNameWithoutExtension(Mod.Filename), GameMode.GetModFormatAdjustedPath(Mod.Format, strFileTo, true));
                    string strModDownloadIDLink = (string.IsNullOrWhiteSpace(Mod.DownloadId) || (Mod.DownloadId.Length <= 1) || Mod.DownloadId.Equals("-1", StringComparison.OrdinalIgnoreCase)) ? string.Empty : Path.Combine(VirtualModActivator.HDLinkFolder, Mod.DownloadId, GameMode.GetModFormatAdjustedPath(Mod.Format, strFileTo, true));
                    strLinkPath = strModFilenameLink;

                    if (!string.IsNullOrWhiteSpace(strModDownloadIDLink))
                    {
                        strLinkPath = strModDownloadIDLink;
                    }
                }

                string strFileType = Path.GetExtension(File.Key);
                if (!strFileType.StartsWith("."))
                {
                    strFileType = "." + strFileType;
                }
                bool booHardLinkFile = (VirtualModActivator.MultiHDMode && (GameMode.HardlinkRequiredFilesType(File.Key) || strFileType.Equals(".exe", StringComparison.InvariantCultureIgnoreCase) || strFileType.Equals(".jar", StringComparison.InvariantCultureIgnoreCase)));

                if (!string.IsNullOrEmpty(strFixedPath))
                {
                    if (!(GameMode.RequiresModFileMerge && (Path.GetFileName(File.Key) == GameMode.MergedFileName)))
                    {
                        if (!(SkipReadme && Readme.IsValidExtension(Path.GetExtension(File.Key).ToLower()) && Path.GetDirectoryName(strFixedPath).Equals(Path.GetFileName(GameMode.PluginDirectory), StringComparison.CurrentCultureIgnoreCase)))
                        {
                            FileInstaller.InstallFileFromMod(File.Key, ((booHardLinkFile) ? strLinkPath : strVirtualPath));
                            lstFilesToLink.Add(new KeyValuePair <string, string>(strFileTo, (booHardLinkFile) ? strLinkPath : strVirtualPath));
                        }
                    }
                }
                StepOverallProgress();
            }

            if ((lstFiles.Count > 0) && (lstFilesToLink.Count <= 0))
            {
                throw new InvalidDataException(string.Format("This mod does not have the correct file structure for a {0} mod that NMM can use. It will not work with NMM.", GameMode.Name));
            }

            foreach (KeyValuePair <string, string> strLink in lstFilesToLink)
            {
                if (!VirtualModActivator.DisableLinkCreation)
                {
                    string strFileLink = ModLinkInstaller.AddFileLink(Mod, strLink.Key, strLink.Value, false);

                    if (!string.IsNullOrEmpty(strFileLink))
                    {
                        ActivatePlugin(strFileLink);
                    }
                }
                StepOverallProgress();
            }

            VirtualModActivator.SaveList();
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// The method that is called to start the backgound task.
        /// </summary>
        /// <param name="p_objArgs">Arguments to for the task execution.</param>
        /// <returns>Always <c>null</c>.</returns>
        protected override object DoWork(object[] p_objArgs)
        {
            ConfirmActionMethod camConfirm = (ConfirmActionMethod)p_objArgs[0];
            bool   booLotsOfLinks          = false;
            int    intProgress             = 0;
            double dblRatio = 0;

            OverallMessage          = String.Format("{0} Mod Links: {1}", Disabling ? "Disabling" : "Activating", Mod.ModName);
            ItemMessage             = String.Format("{0}...", Disabling ? "Disabling" : "Activating");
            OverallProgress         = 0;
            OverallProgressStepSize = 1;
            OverallProgressMaximum  = 1;
            ShowItemProgress        = true;
            ItemProgress            = 0;

            if (!Disabling)
            {
                string strModFolderPath = Path.Combine(VirtualModActivator.VirtualPath, Path.GetFileNameWithoutExtension(Mod.Filename));

                if (Directory.Exists(strModFolderPath))
                {
                    string[] strFiles = Directory.GetFiles(strModFolderPath, "*", SearchOption.AllDirectories);

                    if (strFiles.Length <= 1000)
                    {
                        ItemProgressMaximum  = strFiles.Length;
                        ItemProgressStepSize = 1;
                    }
                    else
                    {
                        ItemProgressMaximum = 1000;
                        booLotsOfLinks      = true;
                        dblRatio            = 1000 / strFiles.Length;
                    }

                    if (strFiles.Length > 0)
                    {
                        IModLinkInstaller ModLinkInstaller = VirtualModActivator.GetModLinkInstaller();

                        foreach (string File in strFiles)
                        {
                            //if (m_booCancel)
                            //	break;
                            ItemMessage = String.Format("{0}: {1}", Disabling ? "Disabling" : "Activating", File);

                            string strFile = File.Replace((strModFolderPath + Path.DirectorySeparatorChar), String.Empty);

                            string strFileLink = ModLinkInstaller.AddFileLink(Mod, strFile, null, false);

                            if (!string.IsNullOrEmpty(strFileLink))
                            {
                                if (PluginManager != null)
                                {
                                    if (PluginManager.IsActivatiblePluginFile(strFileLink))
                                    {
                                        PluginManager.AddPlugin(strFileLink);
                                        PluginManager.ActivatePlugin(strFileLink);
                                    }
                                }
                            }

                            if (ItemProgress < ItemProgressMaximum)
                            {
                                if (booLotsOfLinks)
                                {
                                    ItemProgress = (int)Math.Floor(++intProgress * dblRatio);
                                }
                                else
                                {
                                    StepItemProgress();
                                }
                            }
                        }

                        VirtualModActivator.FinalizeModActivation(Mod);
                    }
                }
            }
            else
            {
                if (Mod != null)
                {
                    List <IVirtualModLink> ivlLinks = VirtualModActivator.VirtualLinks.Where(x => (x.ModInfo != null) && (x.ModInfo.ModFileName.ToLowerInvariant() == Path.GetFileName(Mod.Filename).ToLowerInvariant())).ToList();
                    if ((ivlLinks != null) && (ivlLinks.Count > 0))
                    {
                        if (ivlLinks.Count <= 1000)
                        {
                            ItemProgressMaximum  = ivlLinks.Count;
                            ItemProgressStepSize = 1;
                        }
                        else
                        {
                            ItemProgressMaximum = 1000;
                            booLotsOfLinks      = true;
                            dblRatio            = 1000 / ivlLinks.Count;
                        }

                        foreach (IVirtualModLink Link in ivlLinks)
                        {
                            ItemMessage = String.Format("{0}: {1}", Disabling ? "Disabling" : "Activating", Link.VirtualModPath);
                            VirtualModActivator.RemoveFileLink(Link, Mod);

                            if (ItemProgress < ItemProgressMaximum)
                            {
                                if (booLotsOfLinks)
                                {
                                    ItemProgress = (int)Math.Floor(++intProgress * dblRatio);
                                }
                                else
                                {
                                    StepItemProgress();
                                }
                            }
                        }
                    }

                    VirtualModActivator.FinalizeModDeactivation(Mod);
                }
            }


            if (OverallProgress < OverallProgressMaximum)
            {
                StepOverallProgress();
            }

            return(null);
        }