Esempio n. 1
0
        /// <summary>
        /// Check hash mods
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="isSteam"></param>
        /// <returns></returns>
        private static void generateHashFiles(List <ModelFileInfo> result, ref string rootFolder, string folder, bool level0 = false)
        {
            if (!level0)
            {
                var dirs = Directory.GetDirectories(folder);
                foreach (var subDir in dirs)
                {
                    generateHashFiles(result, ref rootFolder, subDir);
                }
            }

            var files       = Directory.GetFiles(folder);
            int fileNamePos = rootFolder.Length + 1;
            var computeHash = new FastComputeHash();

            foreach (var file in files.Where(x => ApproveExt(x)))
            {
                var mfi = new ModelFileInfo()
                {
                    FileName = file.Substring(fileNamePos)
                };
                GetCheckSum(mfi, file, computeHash);
#if DEBUG
                /*
                 * if (mfi.FileName.Contains("armony"))
                 * {
                 *  Loger.Log($"generateHashFiles Harmony: {FileName} {Hash}");
                 * }*/
#endif
                result.Add(mfi);
            }
            computeHash.Wait();
        }
Esempio n. 2
0
        private static void GetCheckSum(ModelFileInfo mfi, string fileName, FastComputeHash computeHash)
        {
            try
            {
                if (computeHash.ReadFile != null)
                {
                    computeHash.ReadFile.Wait();
                }

                computeHash.ReadFile = Task.Run(() =>
                {
                    try
                    {
                        if (!File.Exists(fileName))
                        {
                            return(null);
                        }
                        var fileData = File.ReadAllBytes(fileName);
                        mfi.Size     = fileData.Length;
                        return(fileData);
                    }
                    catch (Exception exp)
                    {
                        ExceptionUtil.ExceptionLog(exp, "GetCheckSum 2 " + fileName);
                    }
                    return(null);
                });
                computeHash.GetHash = computeHash.ReadFile.ContinueWith((task) =>
                {
                    try
                    {
                        if (task.Result == null)
                        {
                            mfi.Hash = null;
                            return;
                        }
                        var sha  = SHA512.Create();
                        mfi.Hash = sha.ComputeHash(task.Result);
                    }
                    catch (Exception exp)
                    {
                        ExceptionUtil.ExceptionLog(exp, "GetCheckSum 3 " + fileName);
                    }
                });

                /*
                 * var sha = SHA512.Create();
                 * using (var fs = new FileStream(fileName, FileMode.Open))
                 * {
                 *  return sha.ComputeHash(fileData);
                 * }
                 */
            }
            catch (Exception exp)
            {
                ExceptionUtil.ExceptionLog(exp, "GetCheckSum 1 " + fileName);
            }
        }
Esempio n. 3
0
        public static void ReHashFiles(List <ModelFileInfo> rep, string folder, List <string> fileNames)
        {
            var dir         = rep.ToDictionary(f => f.FileName);
            var computeHash = new FastComputeHash();

            foreach (var fileName in fileNames)
            {
                ModelFileInfo mfi;
                if (!dir.TryGetValue(fileName, out mfi))
                {
                    mfi = new ModelFileInfo()
                    {
                        FileName = fileName,
                    };
                    rep.Add(mfi);
                }
                var file    = Path.Combine(folder, fileName);
                var oldHash = mfi.Hash;

                //if (MainHelper.DebugMode)
                Loger.Log($"ReHashFile b {file} {(oldHash == null ? "" : Convert.ToBase64String(oldHash))}" + $"->{(mfi.Hash == null ? "" : Convert.ToBase64String(mfi.Hash))}");

                GetCheckSum(mfi, file, computeHash);

                //if (MainHelper.DebugMode)
                Loger.Log($"ReHashFile e {file} {(oldHash == null ? "" : Convert.ToBase64String(oldHash))}" + $"->{(mfi.Hash == null ? "" : Convert.ToBase64String(mfi.Hash))}");
            }
            computeHash.Wait();

            for (int i = 0; i < rep.Count; i++)
            {
                if (rep[i].Hash == null)
                {
                    rep.RemoveAt(i--);
                }
            }
            computeHash.Wait();
        }