public void RawRead(string file, uint offset, uint length, IntPtr dest, ref uint bytesRead) { DirectoryEntry e; if (_lookup.TryGetValue(file, out e)) { if ((e.Flags & FileFlags.COMPRESSION_FLAGS) != 0) { var cache = GetCache(e); uint readLen = Math.Min(length, (uint)cache.Data.Length - offset); System.Runtime.InteropServices.Marshal.Copy(cache.Data, (int)offset, dest, (int)readLen); bytesRead = readLen; if (readLen == 0) { System.Diagnostics.Debug.WriteLine("IrosArc RawRead file {0} offset {1} length {2} read {3} bytes - cache data size {4}", file, offset, length, readLen, cache.Data.Length); } } else { uint readLen = Math.Min(length, (uint)e.Length - offset); long Loffset = offset + e.Offset; Win32.OVERLAPPED ov = new Win32.OVERLAPPED() { EventHandle = IntPtr.Zero, Internal = UIntPtr.Zero, InternalHigh = UIntPtr.Zero, Offset = (uint)(Loffset & 0xffffffff), OffsetHigh = (uint)(Loffset >> 32) }; Win32.ReadFile(_data.Handle, dest, readLen, ref bytesRead, ref ov); } } else bytesRead = 0; }
public override void Read(uint offset, uint length, IntPtr dest, ref uint bytesRead) { Win32.OVERLAPPED ov = new Win32.OVERLAPPED() { EventHandle = IntPtr.Zero, Internal = UIntPtr.Zero, InternalHigh = UIntPtr.Zero, Offset = Offset + offset, OffsetHigh = 0 }; Win32.ReadFile(Handle, dest, length, ref bytesRead, ref ov); }
private unsafe void Recalculate() { DebugLogger.WriteLine($"Chunked file {Name} recalculating contents..."); byte[] original = new byte[_fbLen]; Win32.OVERLAPPED ov = new Win32.OVERLAPPED() { EventHandle = IntPtr.Zero, Internal = UIntPtr.Zero, InternalHigh = UIntPtr.Zero, Offset = _fbOffset, OffsetHigh = 0 }; fixed(byte *bp = &original[0]) { uint bytesRead = 0; //TODO should loop... Win32.ReadFile(_fbHandle, new IntPtr(bp), (uint)_fbLen, ref bytesRead, ref ov); } DebugLogger.WriteLine($"Original read {_fbLen} from {_fbOffset} sig {original[0]} {original[1]} {original[2]} {original[3]}"); var orig = FieldFile.Unchunk(original); foreach (int i in Enumerable.Range(0, orig.Count)) { string fn = Name + ".chunk." + (i + 1); foreach (var of in _mods.SelectMany(m => m.GetOverrides(fn))) { if (of.CFolder == null || of.CFolder.IsActive(of.CName)) { if (of.Archive == null) { orig[i] = System.IO.File.ReadAllBytes(of.File); } else { orig[i] = of.Archive.GetBytes(of.File); } break; } } } _calculated = FieldFile.Chunk(orig); Bytes.WriteInt(_header, 20, _calculated.Length); DebugLogger.WriteLine($"New length of {Name} is {_calculated.Length}"); }
public bool TryReadFile(string path, string name, uint offset, uint length, IntPtr data, ref uint numBytesRead) { LoadedFile lf = GetLF(path, name); if (lf.Missing) { return(false); } lf.Tag = DateTime.Now; Win32.OVERLAPPED ov = new Win32.OVERLAPPED() { EventHandle = IntPtr.Zero, Internal = UIntPtr.Zero, InternalHigh = UIntPtr.Zero, Offset = offset, OffsetHigh = 0 }; System.Diagnostics.Debug.WriteLine("Attempting to read {0} bytes at offset {1} from {2}", length, offset, name); Win32.ReadFile(lf.Handle, data, length, ref numBytesRead, ref ov); System.Diagnostics.Debug.WriteLine("# bytes read: " + numBytesRead); return(true); }
public override void Read(uint offset, uint length, IntPtr dest, ref uint bytesRead) { //DebugLogger.WriteLine("Conditional {2} reading from OS {0} L {1}", offset, length, Name); bool header = (offset < 24); bool init = (offset == 24); if ((!_lastHeader && header) || (init && !_lastInit)) //re-evaluate { _current = null; if (!_handle.Equals(IntPtr.Zero)) { Win32.CloseHandle(_handle); } _handle = IntPtr.Zero; foreach (var of in _files) { if (of.CFolder == null || of.CFolder.IsActive(of.CName)) { DebugLogger.WriteLine($"Conditional {Name} switching to {of.File}"); _current = of; Bytes.WriteInt(_header, 20, of.Size); break; } } if (_current == null) { DebugLogger.WriteLine($"Conditional {Name} switching to fallback"); } _lastInit = true; //we're ready to read file headers } else { _lastInit = init; } if (_current != null && _handle.Equals(IntPtr.Zero) && _current.Archive == null) { _handle = Wrap.CreateFileW(_current.File, System.IO.FileAccess.Read, System.IO.FileShare.Read, IntPtr.Zero, System.IO.FileMode.Open, System.IO.FileAttributes.Normal, IntPtr.Zero); } _lastHeader = header; _access = DateTime.Now; if (_current == null) //read from fallback //DebugLogger.WriteLine("Conditional reading from fallback handle {0} with extra offset {1}", _fallbackHandle, _fallbackOffset); { Win32.OVERLAPPED ov = new Win32.OVERLAPPED() { EventHandle = IntPtr.Zero, Internal = UIntPtr.Zero, InternalHigh = UIntPtr.Zero, Offset = _fallbackOffset + offset, OffsetHigh = 0 }; Win32.ReadFile(_fallbackHandle, dest, length, ref bytesRead, ref ov); //DebugLogger.WriteLine("Conditional {1} reading from fallback - {0} bytes read", bytesRead, Name); } else { if (offset < 24) { length = Math.Min(length, 24 - offset); System.Runtime.InteropServices.Marshal.Copy(_header, (int)offset, dest, (int)length); bytesRead = length; //DebugLogger.WriteLine("Conditional {2} reading from cheader - {0} bytes read [current size is {1}]", bytesRead, BitConverter.ToInt32(_header, 20), Name); return; } else { offset -= 24; if (_current.Archive == null) { Wrap.SetFilePointer(_handle, (int)offset, IntPtr.Zero, Wrap.EMoveMethod.Begin); Win32.ReadFile(_handle, dest, length, ref bytesRead, IntPtr.Zero); //DebugLogger.WriteLine("Conditional {1} reading from cfile - {0} bytes read", bytesRead, Name); } else { _current.Archive.RawRead(_current.File, offset, length, dest, ref bytesRead); //DebugLogger.WriteLine("Conditional {1} reading from cfile archive offset {2} length {3} - {0} bytes read", bytesRead, Name, offset, length); } } } }