public int VerifyTask(string[] path, ulong[] hashes) { var CP77_DIR = System.Environment.GetEnvironmentVariable("CP77_DIR", EnvironmentVariableTarget.User); var gameDirectory = new DirectoryInfo(CP77_DIR); var gameArchiveDir = Path.Combine(gameDirectory.FullName, "archive", "pc", "content"); var exe = new FileInfo(Path.Combine(gameArchiveDir, Constants.Red4Exe)); if (path != null) { foreach (var s in path) { if (!File.Exists(s)) { continue; } using var fs = new FileStream(s, FileMode.Open, FileAccess.Read); if (VerifyFile(fs, s)) { _loggerService.Success($"{s} - No problems found."); } else { _loggerService.Error($"{s} - Verification failed, files not binary equal."); } } } if (hashes != null) { var bm = new ArchiveManager(_hashService); bm.LoadAll(exe, false); foreach (var hash in hashes) { if (!_hashService.Contains(hash)) { continue; } var file = bm.Items[hash]; foreach (var ifileEntry in file) { var fileEntry = ifileEntry as FileEntry; var ar = bm.Archives[fileEntry.Archive.ArchiveAbsolutePath] as Archive; using var ms = new MemoryStream(); ar.CopyFileToStream(ms, fileEntry.NameHash64, false); if (VerifyFile(ms)) { _loggerService.Success($"{fileEntry.NameOrHash} - No errors found."); } else { _loggerService.Error($"{fileEntry.NameOrHash} - Verification failed, files not binary equal."); } } } }
public ArchiveManager LoadArchiveManager() { var _settings = ServiceLocator.Default.ResolveType <ISettingsManager>(); var _logger = ServiceLocator.Default.ResolveType <ILoggerService>(); _logger.LogString("Loading archive Manager ... ", Logtype.Important); try { if (File.Exists(Tw3Controller.GetManagerPath(EManagerType.ArchiveManager))) { using (StreamReader file = File.OpenText(Cp77Controller.GetManagerPath(EManagerType.ArchiveManager))) { JsonSerializer serializer = new JsonSerializer(); serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects; serializer.TypeNameHandling = TypeNameHandling.Auto; archiveManager = (ArchiveManager)serializer.Deserialize(file, typeof(ArchiveManager)); } } else { archiveManager = new ArchiveManager(); archiveManager.LoadAll(Path.GetDirectoryName(_settings.ExecutablePath)); File.WriteAllText(Cp77Controller.GetManagerPath(EManagerType.ArchiveManager), JsonConvert.SerializeObject(archiveManager, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling = TypeNameHandling.Auto })); _settings.ManagerVersions[(int)EManagerType.ArchiveManager] = ArchiveManager.SerializationVersion; } } catch (System.Exception ex) { if (File.Exists(Cp77Controller.GetManagerPath(EManagerType.ArchiveManager))) { File.Delete(Cp77Controller.GetManagerPath(EManagerType.ArchiveManager)); } archiveManager = new ArchiveManager(); archiveManager.LoadAll(Path.GetDirectoryName(_settings.ExecutablePath)); } _logger.LogString("Finished loading archive manager.", Logtype.Success); return(archiveManager); }
private void DumpBufferInfo() { var gameDirectory = Environment.GetEnvironmentVariable("CP77_DIR", EnvironmentVariableTarget.User); var exe = new FileInfo(Path.Combine(gameDirectory, "bin", "x64", Constants.Red4Exe)); var bm = new ArchiveManager(_hashService); bm.LoadAll(exe, false); var groupedFiles = bm.GroupedFiles; _logger.LogInformation("ArchiveManager loaded"); var exclude = new List <String>() { ".wem" }; foreach (var(key, fileEntries) in groupedFiles) { if (exclude.Contains(key)) { continue; } InspectFiles(groupedFiles, bm, key); } void InspectFiles(Dictionary <string, IEnumerable <FileEntry> > groupedFiles, ArchiveManager bm, string ext) { var fileslist = groupedFiles[ext].ToList(); _logger.LogInformation("Loaded {fileslist.Count} {ext} files", fileslist.Count, ext); var logpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "logs", $"blog_{ext.TrimStart('.')}.csv"); using var fs = new FileStream(logpath, FileMode.Create, FileAccess.Write); using var sw = new StreamWriter(fs); sw.WriteLine($"name;flags"); int latest = 0; int unknown = 0; int pre = 0; for (var i = 1; i < fileslist.Count + 1; i++) { var file = fileslist[i - 1]; var ar = bm.Archives[file.Archive.ArchiveAbsolutePath] as Archive; using var ms = new MemoryStream(); ar.CopyFileToStreamWithoutBuffers(ms, file.NameHash64); var c = new CR2WFile { FileName = file.NameOrHash }; ms.Seek(0, SeekOrigin.Begin); using var br = new BinaryReader(ms); try { var readResult = c.ReadHeaders(br); } catch (FormatException) { continue; } var buffers = c.Buffers; foreach (var buffer in buffers) { sw.WriteLine($"{file.Name};{buffer.Flags.ToString("X")}"); } var percf = (float)i / (float)fileslist.Count * 100; Console.Write($"\r{i}/{fileslist.Count}-{(int)percf}%"); } _logger.LogInformation("[{ext}] unknown: {unknown}, pre: {pre}, latest: {latest}, ", ext, unknown, pre, latest); } }
private void DumpNameHashInfo() { var gameDirectory = Environment.GetEnvironmentVariable("CP77_DIR", EnvironmentVariableTarget.User); var gameBinDir = new DirectoryInfo(Path.Combine(gameDirectory, "bin", "x64")); var bm = new ArchiveManager(_hashService); bm.LoadAll(gameBinDir.FullName, false); var groupedFiles = bm.GroupedFiles; _logger.LogInformation("ArchiveManager loaded"); var exclude = new List <String>() { ".wem" }; foreach (var(key, fileEntries) in groupedFiles) { if (exclude.Contains(key)) { continue; } InspectFiles(groupedFiles, bm, key); } void InspectFiles(Dictionary <string, IEnumerable <FileEntry> > groupedFiles, ArchiveManager bm, string ext) { var fileslist = groupedFiles[ext].ToList(); _logger.LogInformation("Loaded {fileslist.Count} {ext} files", fileslist.Count, ext); var logpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "logs", $"log_{ext.TrimStart('.')}.csv"); using var fs = new FileStream(logpath, FileMode.Create, FileAccess.Write); using var sw = new StreamWriter(fs); sw.WriteLine($"version;buildVersion;hashversion"); int latest = 0; int unknown = 0; int pre = 0; for (var i = 1; i < fileslist.Count + 1; i++) { var file = fileslist[i - 1]; var ar = bm.Archives[file.Archive.ArchiveAbsolutePath] as Archive; using var ms = new MemoryStream(); ar.CopyFileToStreamWithoutBuffers(ms, file.NameHash64); var c = new CR2WFile { FileName = file.NameOrHash }; ms.Seek(0, SeekOrigin.Begin); using var br = new BinaryReader(ms); try { var readResult = c.ReadHeaders(br); } catch (FormatException) { continue; } var header = c.Header; var hashversion = c.IdentifyHash(); switch (hashversion) { case CR2WFile.EHashVersion.Unknown: unknown++; break; case CR2WFile.EHashVersion.Pre120: pre++; break; case CR2WFile.EHashVersion.Latest: latest++; break; default: throw new ArgumentOutOfRangeException(); } sw.WriteLine($"{header.version};{header.buildVersion};{hashversion.ToString()}"); var percf = (float)i / (float)fileslist.Count * 100; Console.Write($"\r{i}/{fileslist.Count}-{(int)percf}%"); } _logger.LogInformation("[{ext}] unknown: {unknown}, pre: {pre}, latest: {latest}, ", ext, unknown, pre, latest); } }