示例#1
0
        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);
                    }
                }
            }
        }