/// <summary>
 /// Handles the <see cref="IBackgroundTaskSet.TaskSetCompleted"/> event of the mod deletion
 /// mod deativator.
 /// </summary>
 /// <param name="sender">The object that raised the event.</param>
 /// <param name="e">A <see cref="TaskSetCompletedEventArgs"/> describing the event arguments.</param>
 private void Deactivator_TaskSetCompleted(object sender, TaskSetCompletedEventArgs e)
 {
     if (e.Success)
     {
         ManagedModRegistry.UnregisterMod((IMod)e.ReturnValue);
     }
 }
 /// <summary>
 /// Returns the mod registered with the given downloadId.
 /// </summary>
 /// <param name="p_strDownloadID">The path of the mod to return</param>
 /// <returns>The mod registered with the given downloadId, or
 /// <c>null</c> if there is no registered mod with the given downloadId.</returns>
 public IMod GetModByDownloadID(string p_strDownloadID)
 {
     return(ManagedModRegistry.GetModByDownloadID(p_strDownloadID));
 }
 /// <summary>
 /// Returns the mod registered with the given file name.
 /// </summary>
 /// <param name="p_strFilename">The path of the mod to return</param>
 /// <returns>The mod registered with the given file name, or
 /// <c>null</c> if there is no registered mod with the given file name.</returns>
 public IMod GetModByFilename(string p_strFilename)
 {
     return(ManagedModRegistry.GetModByFilename(p_strFilename));
 }
        /// <summary>
        /// Loads the data from the Install Log file.
        /// </summary>
        private void LoadInstallLog()
        {
            Trace.TraceInformation(String.Format("Path: {0}", LogPath));
            if (!File.Exists(LogPath))
            {
                SaveInstallLog();
            }
            XDocument docLog = XDocument.Load(LogPath);

            Trace.TraceInformation("Loaded from XML.");

            string strLogVersion = docLog.Element("installLog").Attribute("fileVersion").Value;

            if (!CURRENT_VERSION.ToString().Equals(strLogVersion))
            {
                throw new Exception(String.Format("Invalid Install Log version: {0} Expecting {1}", strLogVersion, CURRENT_VERSION));
            }

            XElement xelModList = docLog.Descendants("modList").FirstOrDefault();

            if (xelModList != null)
            {
                foreach (XElement xelMod in xelModList.Elements("mod"))
                {
                    string strModPath = xelMod.Attribute("path").Value;
                    Trace.Write("Found " + strModPath + "...");
                    if (OriginalValueMod.ModArchivePath.Equals(strModPath))
                    {
                        m_amrModKeys.RegisterMod(OriginalValueMod, xelMod.Attribute("key").Value, true);
                        Trace.WriteLine("OK");
                    }
                    else if (ModManagerValueMod.ModArchivePath.Equals(strModPath))
                    {
                        m_amrModKeys.RegisterMod(ModManagerValueMod, xelMod.Attribute("key").Value, true);
                        Trace.WriteLine("OK");
                    }
                    else
                    {
                        string strModName     = xelMod.Element("name").Value;
                        string strInstallDate = "<No Data>";
                        if (!(xelMod.Element("installDate") == null))
                        {
                            strInstallDate = xelMod.Element("installDate").Value;
                        }
                        strModPath = Path.Combine(ModInstallDirectory, strModPath);
                        XElement xelVersion = xelMod.Element("version");
                        string   strVersion = xelVersion.Attribute("machineVersion").Value;
                        Version  verVersion = String.IsNullOrEmpty(strVersion) ? null : new Version(strVersion);
                        strVersion = xelVersion.Value;
                        IMod modMod = ManagedModRegistry.GetMod(strModPath) ?? new DummyMod(strModName, strModPath, verVersion, strVersion, "", strInstallDate);
                        modMod.InstallDate = strInstallDate;

                        try
                        {
                            m_amrModKeys.RegisterMod(modMod, xelMod.Attribute("key").Value);
                        }
                        catch (ArgumentException) { }

                        if (modMod is DummyMod)
                        {
                            Trace.WriteLine("Missing");
                        }
                        else
                        {
                            Trace.WriteLine("OK");
                        }
                    }
                }
            }

            XElement xelFiles = docLog.Descendants("dataFiles").FirstOrDefault();

            if (xelFiles != null)
            {
                foreach (XElement xelFile in xelFiles.Elements("file"))
                {
                    string strPath = xelFile.Attribute("path").Value;
                    foreach (XElement xelMod in xelFile.Descendants("mod"))
                    {
                        m_dicInstalledFiles[strPath].Push(xelMod.Attribute("key").Value, null);
                    }
                }
            }

            XElement xelIniEdits = docLog.Descendants("iniEdits").FirstOrDefault();

            if (xelIniEdits != null)
            {
                foreach (XElement xelIniEdit in xelIniEdits.Elements("ini"))
                {
                    string  strFile    = xelIniEdit.Attribute("file").Value;
                    string  strSection = xelIniEdit.Attribute("section").Value;
                    string  strKey     = xelIniEdit.Attribute("key").Value;
                    IniEdit iniEntry   = new IniEdit(strFile, strSection, strKey);
                    foreach (XElement xelMod in xelIniEdit.Descendants("mod"))
                    {
                        m_dicInstalledIniEdits[iniEntry].Push(xelMod.Attribute("key").Value, xelMod.Value);
                    }
                }
            }

            XElement xelGameSpecificValueEdits = docLog.Descendants("gameSpecificEdits").FirstOrDefault();

            if (xelGameSpecificValueEdits != null)
            {
                foreach (XElement xelGameSpecificValueEdit in xelGameSpecificValueEdits.Elements("edit"))
                {
                    string strKey = xelGameSpecificValueEdit.Attribute("key").Value;
                    foreach (XElement xelMod in xelGameSpecificValueEdit.Descendants("mod"))
                    {
                        m_dicInstalledGameSpecificValueEdits[strKey].Push(xelMod.Attribute("key").Value, Convert.FromBase64String(xelMod.Value));
                    }
                }
            }
        }
        /// <summary>
        /// Loads the data from the Install Log file.
        /// </summary>
        private void LoadInstallLog()
        {
            Trace.TraceInformation($"Path: {LogPath}");

            if (!File.Exists(LogPath))
            {
                SaveInstallLog();
            }

            var docLog = XDocument.Load(LogPath);

            Trace.TraceInformation("Loaded from XML.");

            var logVersion = docLog.Element("installLog")?.Attribute("fileVersion")?.Value;

            if (!CurrentVersion.ToString().Equals(logVersion))
            {
                throw new Exception($"Invalid Install Log version: \"{logVersion}\", expected \"{CurrentVersion}\"");
            }

            var modList = docLog.Descendants("modList").FirstOrDefault();

            if (modList != null)
            {
                foreach (var mod in modList.Elements("mod"))
                {
                    var modPath = mod.Attribute("path")?.Value;
                    Trace.Write("Found " + modPath + "...");

                    if (OriginalValueMod.ModArchivePath.Equals(modPath))
                    {
                        _activeModRegistry.RegisterMod(OriginalValueMod, mod.Attribute("key")?.Value, true);
                        Trace.WriteLine("OK");
                    }
                    else if (ModManagerValueMod.ModArchivePath.Equals(modPath))
                    {
                        _activeModRegistry.RegisterMod(ModManagerValueMod, mod.Attribute("key")?.Value, true);
                        Trace.WriteLine("OK");
                    }
                    else
                    {
                        var strModName  = mod.Element("name")?.Value;
                        var installDate = "<No Data>";

                        if (mod.Element("installDate") != null)
                        {
                            installDate = mod.Element("installDate")?.Value;
                        }

                        modPath = Path.Combine(ModInstallDirectory, modPath);

                        var version = mod.Element("version");
                        var humanReadableVersion = version.Attribute("machineVersion").Value;
                        var machineVersion       = string.IsNullOrEmpty(humanReadableVersion) ? null : new Version(humanReadableVersion);
                        humanReadableVersion = version.Value;

                        var modMod = ManagedModRegistry.GetMod(modPath) ?? new DummyMod(strModName, modPath, machineVersion, humanReadableVersion, "", installDate);
                        modMod.InstallDate = installDate;

                        try
                        {
                            _activeModRegistry.RegisterMod(modMod, mod.Attribute("key").Value);
                        }
                        catch (ArgumentException) { }

                        Trace.WriteLine(modMod is DummyMod ? "Missing" : "OK");
                    }
                }
            }

            var files = docLog.Descendants("dataFiles").FirstOrDefault();

            if (files != null)
            {
                foreach (var file in files.Elements("file"))
                {
                    var path = file.Attribute("path").Value;

                    foreach (var mod in file.Descendants("mod"))
                    {
                        _installedFiles[path].Push(mod.Attribute("key") != null ? mod.Attribute("key").Value : string.Empty, null);
                    }
                }
            }

            var iniEdits = docLog.Descendants("iniEdits").FirstOrDefault();

            if (iniEdits != null)
            {
                foreach (var iniEdit in iniEdits.Elements("ini"))
                {
                    var file     = iniEdit.Attribute("file").Value;
                    var section  = iniEdit.Attribute("section").Value;
                    var key      = iniEdit.Attribute("key").Value;
                    var iniEntry = new IniEdit(file, section, key);

                    foreach (var xelMod in iniEdit.Descendants("mod"))
                    {
                        _installedIniEdits[iniEntry].Push(xelMod.Attribute("key").Value, xelMod.Value);
                    }
                }
            }

            var gameSpecificValueEdits = docLog.Descendants("gameSpecificEdits").FirstOrDefault();

            if (gameSpecificValueEdits != null)
            {
                foreach (var gameSpecificValueEdit in gameSpecificValueEdits.Elements("edit"))
                {
                    var key = gameSpecificValueEdit.Attribute("key").Value;

                    foreach (var mod in gameSpecificValueEdit.Descendants("mod"))
                    {
                        _gameSpecificValueEdits[key].Push(mod.Attribute("key").Value, Convert.FromBase64String(mod.Value));
                    }
                }
            }
        }