public static void Initialize(string titleIdText, string displayVersion, bool enabled) { Wait(); ClearMemoryStreams(); PtcJumpTable.Clear(); PtcProfiler.Stop(); PtcProfiler.Wait(); PtcProfiler.ClearEntries(); if (String.IsNullOrEmpty(titleIdText) || titleIdText == TitleIdTextDefault) { TitleIdText = TitleIdTextDefault; DisplayVersion = DisplayVersionDefault; CachePathActual = string.Empty; CachePathBackup = string.Empty; Disable(); return; } Logger.PrintInfo(LogClass.Ptc, $"Initializing Profiled Persistent Translation Cache (enabled: {enabled})."); TitleIdText = titleIdText; DisplayVersion = !String.IsNullOrEmpty(displayVersion) ? displayVersion : DisplayVersionDefault; if (enabled) { string workPathActual = Path.Combine(_basePath, "games", TitleIdText, "cache", "cpu", ActualDir); string workPathBackup = Path.Combine(_basePath, "games", TitleIdText, "cache", "cpu", BackupDir); if (!Directory.Exists(workPathActual)) { Directory.CreateDirectory(workPathActual); } if (!Directory.Exists(workPathBackup)) { Directory.CreateDirectory(workPathBackup); } CachePathActual = Path.Combine(workPathActual, DisplayVersion); CachePathBackup = Path.Combine(workPathBackup, DisplayVersion); Enable(); PreLoad(); PtcProfiler.PreLoad(); } else { CachePathActual = string.Empty; CachePathBackup = string.Empty; Disable(); } }
private static void Save(string fileName) { int translatedFuncsCount; using (MemoryStream stream = new MemoryStream()) using (MD5 md5 = MD5.Create()) { int hashSize = md5.HashSize / 8; stream.Seek((long)hashSize, SeekOrigin.Begin); WriteHeader(stream); _infosStream.WriteTo(stream); _codesStream.WriteTo(stream); _relocsStream.WriteTo(stream); _unwindInfosStream.WriteTo(stream); PtcJumpTable.Serialize(stream, PtcJumpTable); translatedFuncsCount = GetInfosEntriesCount(); ClearMemoryStreams(); PtcJumpTable.Clear(); stream.Seek((long)hashSize, SeekOrigin.Begin); byte[] hash = md5.ComputeHash(stream); stream.Seek(0L, SeekOrigin.Begin); stream.Write(hash, 0, hashSize); using (FileStream compressedStream = new FileStream(fileName, FileMode.OpenOrCreate)) using (DeflateStream deflateStream = new DeflateStream(compressedStream, SaveCompressionLevel, true)) { try { stream.WriteTo(deflateStream); } catch { compressedStream.Position = 0L; } if (compressedStream.Position < compressedStream.Length) { compressedStream.SetLength(compressedStream.Position); } } } long fileSize = new FileInfo(fileName).Length; Logger.Info?.Print(LogClass.Ptc, $"Saved Translation Cache (size: {fileSize} bytes, translated functions: {translatedFuncsCount})."); }