public InnerTabPage() { InitializeComponent(); panelDesktop.BackColor = Color.FromArgb(114, 106, 112); _params = new Params(this); explorerPainter = new ExplorerPainter(tabPageExplorer); stack = new UndoRedoStack(); jSONParser = new JSONParser(_params, explorerPainter, this); label = new Label(); label.Text = ""; label.Location = new Point(defaultWidth, defaultHeight); label.Size = new Size(0, 0); panelDesktop.Controls.Add(label); innerHeader = new InnerHeader(this); innerHeader.Location = new Point(0, 0); innerHeader.Visible = false; panelDesktop.Controls.Add(innerHeader); panelDesktop.ControlAdded += PanelDesktop_ControlAdded; panelDesktop.Click += PanelDesktop_Click; toolStripTextBoxHeight.Text = MaxY().ToString(); toolStripTextBoxHeight.LostFocus += ToolStripTextBoxHeight_LostFocus; toolStripTextBoxHeight.KeyDown += ToolStripTextBoxHeight_KeyDown; toolStripTextBoxScale.Text = "1"; toolStripTextBoxScale.LostFocus += ToolStripTextBoxScale_LostFocus; toolStripTextBoxScale.KeyDown += ToolStripTextBoxScale_KeyDown; }
private static unsafe void Save(string fileName) { int translatedFuncsCount; InnerHeader innerHeader = new InnerHeader(); innerHeader.Magic = _innerHeaderMagic; innerHeader.InfosLength = (int)_infosStream.Length; innerHeader.CodesLength = _codesList.Length(); innerHeader.RelocsLength = (int)_relocsStream.Length; innerHeader.UnwindInfosLength = (int)_unwindInfosStream.Length; innerHeader.PtcJumpTableLength = PtcJumpTable.GetSerializeSize(PtcJumpTable); OuterHeader outerHeader = new OuterHeader(); outerHeader.Magic = _outerHeaderMagic; outerHeader.CacheFileVersion = InternalVersion; outerHeader.Endianness = GetEndianness(); outerHeader.FeatureInfo = GetFeatureInfo(); outerHeader.MemoryManagerMode = GetMemoryManagerMode(); outerHeader.OSPlatform = GetOSPlatform(); outerHeader.UncompressedStreamSize = (long)Unsafe.SizeOf <InnerHeader>() + innerHeader.InfosLength + innerHeader.CodesLength + innerHeader.RelocsLength + innerHeader.UnwindInfosLength + innerHeader.PtcJumpTableLength; outerHeader.SetHeaderHash(); IntPtr intPtr = IntPtr.Zero; try { intPtr = Marshal.AllocHGlobal(new IntPtr(outerHeader.UncompressedStreamSize)); using (UnmanagedMemoryStream stream = new((byte *)intPtr.ToPointer(), outerHeader.UncompressedStreamSize, outerHeader.UncompressedStreamSize, FileAccess.ReadWrite)) { stream.Seek((long)Unsafe.SizeOf <InnerHeader>(), SeekOrigin.Begin); ReadOnlySpan <byte> infosBytes = new(stream.PositionPointer, innerHeader.InfosLength); _infosStream.WriteTo(stream); ReadOnlySpan <byte> codesBytes = (int)innerHeader.CodesLength > 0 ? new(stream.PositionPointer, (int)innerHeader.CodesLength) : ReadOnlySpan <byte> .Empty; _codesList.WriteTo(stream); ReadOnlySpan <byte> relocsBytes = new(stream.PositionPointer, innerHeader.RelocsLength); _relocsStream.WriteTo(stream); ReadOnlySpan <byte> unwindInfosBytes = new(stream.PositionPointer, innerHeader.UnwindInfosLength); _unwindInfosStream.WriteTo(stream); ReadOnlySpan <byte> ptcJumpTableBytes = new(stream.PositionPointer, innerHeader.PtcJumpTableLength); PtcJumpTable.Serialize(stream, PtcJumpTable); Debug.Assert(stream.Position == stream.Length); innerHeader.InfosHash = XXHash128.ComputeHash(infosBytes); innerHeader.CodesHash = XXHash128.ComputeHash(codesBytes); innerHeader.RelocsHash = XXHash128.ComputeHash(relocsBytes); innerHeader.UnwindInfosHash = XXHash128.ComputeHash(unwindInfosBytes); innerHeader.PtcJumpTableHash = XXHash128.ComputeHash(ptcJumpTableBytes); innerHeader.SetHeaderHash(); stream.Seek(0L, SeekOrigin.Begin); SerializeStructure(stream, innerHeader); translatedFuncsCount = GetEntriesCount(); ResetCarriersIfNeeded(); PtcJumpTable.ClearIfNeeded(); using (FileStream compressedStream = new(fileName, FileMode.OpenOrCreate)) using (DeflateStream deflateStream = new(compressedStream, SaveCompressionLevel, true)) { try { SerializeStructure(compressedStream, outerHeader); stream.Seek(0L, SeekOrigin.Begin); stream.CopyTo(deflateStream); } catch { compressedStream.Position = 0L; } if (compressedStream.Position < compressedStream.Length) { compressedStream.SetLength(compressedStream.Position); } } } } finally { if (intPtr != IntPtr.Zero) { Marshal.FreeHGlobal(intPtr); } } long fileSize = new FileInfo(fileName).Length; if (fileSize != 0L) { Logger.Info?.Print(LogClass.Ptc, $"Saved Translation Cache (size: {fileSize} bytes, translated functions: {translatedFuncsCount})."); } }
private static unsafe bool Load(string fileName, bool isBackup) { using (FileStream compressedStream = new(fileName, FileMode.Open)) using (DeflateStream deflateStream = new(compressedStream, CompressionMode.Decompress, true)) { OuterHeader outerHeader = DeserializeStructure <OuterHeader>(compressedStream); if (!outerHeader.IsHeaderValid()) { InvalidateCompressedStream(compressedStream); return(false); } if (outerHeader.Magic != _outerHeaderMagic) { InvalidateCompressedStream(compressedStream); return(false); } if (outerHeader.CacheFileVersion != InternalVersion) { InvalidateCompressedStream(compressedStream); return(false); } if (outerHeader.Endianness != GetEndianness()) { InvalidateCompressedStream(compressedStream); return(false); } if (outerHeader.FeatureInfo != GetFeatureInfo()) { InvalidateCompressedStream(compressedStream); return(false); } if (outerHeader.MemoryManagerMode != GetMemoryManagerMode()) { InvalidateCompressedStream(compressedStream); return(false); } if (outerHeader.OSPlatform != GetOSPlatform()) { InvalidateCompressedStream(compressedStream); return(false); } IntPtr intPtr = IntPtr.Zero; try { intPtr = Marshal.AllocHGlobal(new IntPtr(outerHeader.UncompressedStreamSize)); using (UnmanagedMemoryStream stream = new((byte *)intPtr.ToPointer(), outerHeader.UncompressedStreamSize, outerHeader.UncompressedStreamSize, FileAccess.ReadWrite)) { try { deflateStream.CopyTo(stream); } catch { InvalidateCompressedStream(compressedStream); return(false); } Debug.Assert(stream.Position == stream.Length); stream.Seek(0L, SeekOrigin.Begin); InnerHeader innerHeader = DeserializeStructure <InnerHeader>(stream); if (!innerHeader.IsHeaderValid()) { InvalidateCompressedStream(compressedStream); return(false); } if (innerHeader.Magic != _innerHeaderMagic) { InvalidateCompressedStream(compressedStream); return(false); } ReadOnlySpan <byte> infosBytes = new(stream.PositionPointer, innerHeader.InfosLength); stream.Seek(innerHeader.InfosLength, SeekOrigin.Current); Hash128 infosHash = XXHash128.ComputeHash(infosBytes); if (innerHeader.InfosHash != infosHash) { InvalidateCompressedStream(compressedStream); return(false); } ReadOnlySpan <byte> codesBytes = (int)innerHeader.CodesLength > 0 ? new(stream.PositionPointer, (int)innerHeader.CodesLength) : ReadOnlySpan <byte> .Empty; stream.Seek(innerHeader.CodesLength, SeekOrigin.Current); Hash128 codesHash = XXHash128.ComputeHash(codesBytes); if (innerHeader.CodesHash != codesHash) { InvalidateCompressedStream(compressedStream); return(false); } ReadOnlySpan <byte> relocsBytes = new(stream.PositionPointer, innerHeader.RelocsLength); stream.Seek(innerHeader.RelocsLength, SeekOrigin.Current); Hash128 relocsHash = XXHash128.ComputeHash(relocsBytes); if (innerHeader.RelocsHash != relocsHash) { InvalidateCompressedStream(compressedStream); return(false); } ReadOnlySpan <byte> unwindInfosBytes = new(stream.PositionPointer, innerHeader.UnwindInfosLength); stream.Seek(innerHeader.UnwindInfosLength, SeekOrigin.Current); Hash128 unwindInfosHash = XXHash128.ComputeHash(unwindInfosBytes); if (innerHeader.UnwindInfosHash != unwindInfosHash) { InvalidateCompressedStream(compressedStream); return(false); } ReadOnlySpan <byte> ptcJumpTableBytes = new(stream.PositionPointer, innerHeader.PtcJumpTableLength); stream.Seek(innerHeader.PtcJumpTableLength, SeekOrigin.Current); Debug.Assert(stream.Position == stream.Length); Hash128 ptcJumpTableHash = XXHash128.ComputeHash(ptcJumpTableBytes); if (innerHeader.PtcJumpTableHash != ptcJumpTableHash) { InvalidateCompressedStream(compressedStream); return(false); } stream.Seek((long)Unsafe.SizeOf <InnerHeader>(), SeekOrigin.Begin); _infosStream.Write(infosBytes); stream.Seek(innerHeader.InfosLength, SeekOrigin.Current); _codesList.ReadFrom(stream); _relocsStream.Write(relocsBytes); stream.Seek(innerHeader.RelocsLength, SeekOrigin.Current); _unwindInfosStream.Write(unwindInfosBytes); stream.Seek(innerHeader.UnwindInfosLength, SeekOrigin.Current); PtcJumpTable = PtcJumpTable.Deserialize(stream); Debug.Assert(stream.Position == stream.Length); } } finally { if (intPtr != IntPtr.Zero) { Marshal.FreeHGlobal(intPtr); } } } long fileSize = new FileInfo(fileName).Length; Logger.Info?.Print(LogClass.Ptc, $"{(isBackup ? "Loaded Backup Translation Cache" : "Loaded Translation Cache")} (size: {fileSize} bytes, translated functions: {GetEntriesCount()})."); return(true); }