private static string GetStrongHashFile(string pAddonFile, out string pErrorText) { pErrorText = null; if (!File.Exists(pAddonFile)) { return(null); } string hash = null; string fileNameLower = null; try { SevenZipArchiver archiver = new SevenZipArchiver(pAddonFile); List <ArchiveFileInfo> fileList; archiver.ArchivedFileList(out fileList); List <string> fileData = new List <string>(); foreach (ArchiveFileInfo item in fileList) { if (item.IsDirectory) { continue; } fileNameLower = item.FileName.ToLower(); if ((fileNameLower == "assetdata.jar") || (fileNameLower == "meshdata.data")) { fileData.Add( $"{fileNameLower}^{ComputeArchivedFileHash(archiver, item)}"); continue; } if (!fileNameLower.StartsWith("data\\")) { continue; } string file = Path.GetFileName(fileNameLower); string extension = Path.GetExtension(fileNameLower) ?? ""; if ((file == "descriptor") || (extension == ".bodypart") || (extension == ".template") || (extension == ".part") || (extension == ".cmf") || (extension == ".crf")) { continue; } fileData.Add( $"{fileNameLower}^{ComputeArchivedFileHash(archiver, item)}"); } hash = CalcStrongHash(fileData); } catch (Exception exception) { pErrorText = $"GetStrongHashFile(): [{fileNameLower}]\n EXCEPTION: {exception.Message}\n{exception.StackTrace}"; } return(hash); }
private static string ComputeArchivedFileHash(SevenZipArchiver pArchiver, ArchiveFileInfo pFile) { if (pFile.Size == 0) { return("#ZERO-LENGTH"); } byte[] hash; using (Stream stream = pArchiver.ExtractArchivedFileToStream(pFile.FileName)) { SHA512Cng sha512 = new SHA512Cng(); hash = sha512.ComputeHash(stream); } return(Utils.HexaBinString(hash)); }
private static string GetStrongHashArchivedAddon(string pArchive, string pAddonName, out string pErrorText) { pErrorText = null; if (!File.Exists(pArchive)) { return(null); } SevenZipArchiver archiver = new SevenZipArchiver(pArchive); if (!archiver.FileExists(pAddonName)) { return(null); } string tempFolder = Utils.GetTempDirectory(); if (!Directory.Exists(tempFolder)) { return(null); } archiver.ArchivedFilesExtract(tempFolder, new List <string>() { pAddonName }); string unarchAddon = Path.Combine(tempFolder, pAddonName); if (!File.Exists(unarchAddon)) { return(null); } string hash = GetStrongHashFile(unarchAddon, out pErrorText); File.Delete(unarchAddon); return(hash); }
// --------------------------------------------------------------------------------------------------- /// <summary> /// Restore an addon file disguised as an archive /// </summary> /// <param name="pArchiver"></param> /// <param name="pDeleteSource">Delete source archive if restoration succeeds</param> /// <param name="pRootFolder">Root folder, in the case of a rooted addon</param> /// <param name="pErrorText">Text of error, if any</param> /// <returns>Name of the addon file, or null if error</returns> public static string CorrectAddonFile(SevenZipArchiver pArchiver, bool pDeleteSource, string pRootFolder, out string pErrorText) { pErrorText = null; string sourceFile = pArchiver.ArchiveName; string extension = Path.GetExtension(sourceFile); string destFile = pRootFolder == null ? sourceFile.Replace(extension, ".addon") : Path.Combine(Path.GetDirectoryName(sourceFile) ?? "", pRootFolder + ".addon"); string tempPath = Utils.GetTempDirectory(); string destFolder = null; bool processOk = false; try { SevenZipExtractor extractor = pArchiver.GetExtractor(); if (extractor == null) { pErrorText = "Couldn't determine archive format"; return(null); } bool isZipArchive = (extractor.Format == InArchiveFormat.Zip); bool isRooted = (pRootFolder != null); if (!isRooted && isZipArchive) { if (File.Exists(destFile)) { pDeleteSource = false; return(destFile); } File.Copy(sourceFile, destFile); if (!File.Exists(destFile)) { pErrorText = "Couldn't create addon file"; return(null); } processOk = true; return(destFile); } if (!isRooted) { destFolder = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(sourceFile)); Directory.CreateDirectory(destFolder); pArchiver.ArchivedFilesExtract(destFolder, null); } else { destFolder = Path.Combine(tempPath, pRootFolder); pArchiver.ArchivedFilesExtract(tempPath, null); } SevenZipArchiver newArchiver = new SevenZipArchiver(destFile); newArchiver.ArchiveFolder(destFolder); if (!File.Exists(destFile)) { pErrorText = newArchiver.LastErrorText ?? "???"; return(null); } processOk = true; return(destFile); } catch (Exception exception) { pErrorText = $"CorrectAddonFile(), Exception: {exception.Message}"; } finally { if (destFolder != null) { string errorText; Utils.DeleteTempFolder(destFolder, out errorText); } if (processOk) { if ((pDeleteSource) && File.Exists(sourceFile)) { File.Delete(sourceFile); } } else { if (File.Exists(destFile)) { File.Delete(destFile); } } } return(null); }