public void AddInstalledMod(ModApiObject mod, bool forceSave = false)
        {
            // search mod and update
            var installedMod = _settings.InstalledMods.FirstOrDefault(x =>
            {
                if (x.Name != mod.Name)
                {
                    return(false);
                }

                x.SetMod(mod);
                return(true);
            });

            // mod not found -> new
            if (installedMod == null)
            {
                installedMod = new SettingsModObject();
                installedMod.SetMod(mod);

                var installedMods = _settings.InstalledMods.ToList();
                installedMods.Add(installedMod);

                _settings.InstalledMods = installedMods.AsEnumerable();
            }

            if (forceSave)
            {
                SaveSettings();
            }
        }
        public void SearchMods(ModApiObject mod, List <ModApiObject> modList)
        {
            if (!modList.Contains(mod))
            {
                modList.Add(mod);
            }

            Console.WriteLine(mod.Name);

            foreach (var depMod in mod.Dependencies)
            {
                var latestMod = GetLatestVersion(depMod.Name, depMod.Version);
                if (latestMod != null)
                {
                    if (modList.Contains(latestMod))
                    {
                        continue;
                    }

                    modList.Add(latestMod);

                    // search the dependencies
                    SearchMods(latestMod, modList);
                }
                else
                {
                    _logger.LogError("mod " + depMod.Name + " not found o.o");
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public async Task <bool> DownloadMod(ModApiObject mod)
        {
            // check if mod need to download
            if (mod.IsInstalled() && !string.IsNullOrEmpty(mod.InstalledVersion) && !mod.IsNew())
            {
                _logger.LogInformation("{0} v{1} ({2}) already installed.", mod.Name, mod.Version, mod.GameVersion);

                return(true);
            }

            _logger.LogInformation("downloading {0} v{1}...", mod.Name, mod.Version);
            Console.WriteLine(mod.Name);
            StatusHandler?.Invoke(null, new StatusEvent($"Downloading {mod.Name}..."));

            var tmpFileName = $"mod-{mod.Name}-{mod.Version}.zip";

            if (!await _httpHelper.DownloadFile(new Uri(ModApiBasicUrl + mod.Downloads.First().Url), tmpFileName))
            {
                _logger.LogError("Download failed");
                return(false);
            }

            // extract
            _logger.LogInformation("Downloading finished. Extracting...");
            _fileHelper.Extract(tmpFileName, SettingsHandler.Instance.GetSettings().GamePath);

            // delete tmp file
            _logger.LogInformation("Deleting temporary download file...");
            File.Delete(tmpFileName);

            SettingsHandler.Instance.AddInstalledMod(mod);

            _logger.LogInformation("{0} v{1} installed", mod.Name, mod.Version);

            return(true);
        }
示例#4
0
 public void SetMod(ModApiObject mod)
 {
     Name    = mod.Name;
     Version = mod.Version;
 }