Ejemplo n.º 1
0
 public string GetFileHashSha1(string filePath)
 {
     // Use shared implementation from Core.
     // Also needs to be an instance method so it can be Moq'd for testing.
     return(NetModuleCache.GetFileHashSha1(filePath));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Import a list of files into the download cache, with progress bar and
        /// interactive prompts for installation and deletion.
        /// </summary>
        /// <param name="gameInst">Game instance to install into</param>
        /// <param name="files">Set of files to import</param>
        /// <param name="user">Object for user interaction</param>
        /// <param name="inst">Module installer object</param>
        /// <param name="installMod">Function to call to mark a mod for installation</param>
        public static void ImportFiles(KSP gameInst, HashSet <FileInfo> files,
                                       IUser user, ModuleInstaller inst, Action <string> installMod)
        {
            Registry         registry    = RegistryManager.Instance(gameInst).registry;
            HashSet <string> installable = new HashSet <string>();
            List <FileInfo>  deletable   = new List <FileInfo>();
            // Get the mapping of known hashes to modules
            Dictionary <string, List <CkanModule> > index = registry.GetSha1Index();
            int i = 0;

            foreach (FileInfo f in files)
            {
                int percent = i * 100 / files.Count;
                user.RaiseProgress($"Importing {f.Name}... ({percent}%)", percent);
                // Calc SHA-1 sum
                string sha1 = NetModuleCache.GetFileHashSha1(f.FullName);
                // Find SHA-1 sum in registry (potentially multiple)
                if (index.ContainsKey(sha1))
                {
                    deletable.Add(f);
                    List <CkanModule> matches = index[sha1];
                    foreach (CkanModule mod in matches)
                    {
                        if (mod.IsCompatibleKSP(gameInst.VersionCriteria()))
                        {
                            installable.Add(mod.identifier);
                        }
                        if (inst.Cache.IsMaybeCachedZip(mod))
                        {
                            user.RaiseMessage("Already cached: {0}", f.Name);
                        }
                        else
                        {
                            user.RaiseMessage($"Importing {mod.identifier} {Formatting.StripEpoch(mod.version)}...");
                            inst.Cache.Store(mod, f.FullName);
                        }
                    }
                }
                else
                {
                    user.RaiseMessage("Not found in index: {0}", f.Name);
                }
                ++i;
            }
            if (installable.Count > 0 && user.RaiseYesNoDialog($"Install {installable.Count} compatible imported mods?"))
            {
                // Install the imported mods
                foreach (string identifier in installable)
                {
                    installMod(identifier);
                }
            }
            if (user.RaiseYesNoDialog($"Import complete. Delete {deletable.Count} old files?"))
            {
                // Delete old files
                foreach (FileInfo f in deletable)
                {
                    f.Delete();
                }
            }
        }