public LGPWrapper(IntPtr handle, string name, RuntimeProfile profile) { if (!AnyOverridesFor(name, profile)) { System.Diagnostics.Debug.WriteLine("LGPWrapper: no overrides for {0}, early out", (object)name); return; //optimisation, don't run anything else, if no override files } IsActive = true; System.Diagnostics.Debug.WriteLine(" LGPWrapper: Parsing"); var fs = new System.IO.FileStream(new Microsoft.Win32.SafeHandles.SafeFileHandle(handle, false), FileAccess.Read); ProcMonParser.DataFile df = ProcMonParser.FF7Files.LoadLGP(fs, name); fs.Position = 0; Dictionary <string, int> sortKeys = df.Items.ToDictionary(i => i.Name, i => i.Index, StringComparer.InvariantCulture); Dictionary <string, long> dataKeys = df.Items.ToDictionary(i => i.Name, i => i.Start, StringComparer.InvariantCulture); Dictionary <string, int> filesSizes = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase); Dictionary <string, int> filesOptions = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase); Dictionary <string, ProcMonParser.DataItem> lgpItems = df.Items.ToDictionary(i => i.Name, i => i, StringComparer.InvariantCulture); foreach (var item in df.Items) { filesOptions[item.Name] = 0; filesSizes[item.Name] = item.Length - 24; RuntimeLog.Write("Checking chunk support for {0}~{1}~", name, item.Name); if (profile.Mods.Any(m => m.SupportsChunks(System.IO.Path.Combine(name, item.Name)))) { filesSizes[item.Name] = Math.Max(filesSizes[item.Name], 1024 * 1024 * 2); //This is a horrible hack. TODO. filesOptions[item.Name] |= 0x1; } } System.Diagnostics.Debug.WriteLine(" LGPWrapper: Prepared structures"); List <string> names = profile.Mods.SelectMany(m => m.GetPathOverrideNames(name)).Distinct(StringComparer.InvariantCultureIgnoreCase).ToList(); foreach (string fname in names) { if (fname.IndexOf(".chunk.", StringComparison.InvariantCultureIgnoreCase) >= 0) { continue; } if (!filesSizes.ContainsKey(fname)) { filesSizes[fname] = 0; System.Diagnostics.Debug.WriteLine("Added LGP file {0} {1}", name, fname); } var overrides = profile.Mods.SelectMany(m => m.GetOverrides(System.IO.Path.Combine(name, fname))); foreach (var over in overrides) { filesSizes[fname] = Math.Max(filesSizes[fname], over.Size); if (over.CFolder == null) { break; } } } List <LGPEntry> entries = filesSizes.Select(kv => new LGPEntry(kv.Key, kv.Value)).ToList(); List <LGP.LGPEntryMade> newentries; System.Diagnostics.Debug.WriteLine(" LGPWrapper: creating new headers"); byte[] headers = LGP.CalculateHeaders(entries, s => { int index; sortKeys.TryGetValue(s, out index); return(index); }, s => { long index; dataKeys.TryGetValue(s, out index); return((uint)index); }, out newentries ); int datastart = headers.Length; System.Diagnostics.Debug.WriteLine(" LGPWrapper: Calculated new LGP headers for {0} with {1} file entries", name, entries.Count); /* * int datastart = df.Items[0].Start; * byte[] headers = new byte[datastart]; * fs.Read(headers, 0, datastart); * fs.Position = 0; */ uint offset = (uint)datastart; VFile = new VFile(); try { VFile.Add(new VRangeInline() { Start = 0, Length = offset, Data = headers, Tag = "Headers" }); int count = 0; foreach (var item in newentries.OrderBy(em => em.DataIndex)) { RuntimeLog.Write("LGPWrapper calculate {0}", item.Entry.Name); //Bytes.WriteUInt(headers, 16 + 20 + 27 * item.Index, offset); string fn = System.IO.Path.Combine(name, item.Entry.Name); var overrides = profile.Mods.SelectMany(m => m.GetOverrides(fn)); int fOptions; filesOptions.TryGetValue(item.Entry.Name, out fOptions); bool chunked = (fOptions & 0x1) != 0; //var overrides = Enumerable.Empty<OverrideFile>(); //System.Diagnostics.Debug.WriteLine("Virtualizing LGP entry {0} at offset {1}", item.Entry.Name, offset); if (item.DataOffset != offset) { throw new Exception("LGPWrapper mismatch on offset for " + item.Entry.Name + " offset=" + offset + " hoffset=" + item.DataOffset); } if (chunked) { long pos = lgpItems[item.Entry.Name].Start + 24; int len = lgpItems[item.Entry.Name].Length - 24; VFile.Add(new VRangeChunked(item.Entry.Name, profile.Mods, handle, (uint)pos, len) { Start = offset, Length = (uint)(item.Entry.MaxSize + 24), Name = fn, Tag = "Chunked" }); //System.Diagnostics.Debug.WriteLine("File {0} initialized with chunks", item.Entry.Name, 0); offset += (uint)item.Entry.MaxSize + 24; } else if (!overrides.Any()) //take from original LGP { long pos = lgpItems[item.Entry.Name].Start; VFile.Add(new VRangeHandle() { Start = offset, Length = (uint)(item.Entry.MaxSize + 24), Handle = handle, Offset = (uint)pos, Tag = item.Entry.Name }); offset += (uint)item.Entry.MaxSize + 24; //System.Diagnostics.Debug.WriteLine("--VRangeHandle"); } else if (overrides.First().CFolder == null) //only one override, replace directly { var ov = overrides.First(); byte[] fheader = new byte[24]; System.Text.Encoding.ASCII.GetBytes(item.Entry.Name, 0, item.Entry.Name.Length, fheader, 0); Bytes.WriteUInt(fheader, 20, (uint)ov.Size); VFile.Add(new VRangeInline() { Start = offset, Length = 24, Data = fheader, Tag = item.Entry.Name + "%header" }); offset += 24; VRange vr; if (ov.Archive == null) { var vf = new VRangeFile() { Start = offset, Length = (uint)ov.Size, Filename = ov.File, Tag = ov.File }; VFile.Add(vf); _wFiles.Add(vf); vr = vf; //System.Diagnostics.Debug.WriteLine("LGP entry {0} coming from file {1}", item.Entry.Name, ov.File); } else { vr = new VRangeArchive() { Start = offset, Length = (uint)ov.Size, File = ov.File, Archive = ov.Archive, Tag = ov.File }; VFile.Add(vr); //System.Diagnostics.Debug.WriteLine("LGP entry {0} coming from archive file {1} with size {2}", item.Entry.Name, ov.File, ov.Size); } //if (vr.Length != item.Entry.MaxSize) //System.Diagnostics.Debug.WriteLine("Entry {0} size difference {1} vs {2}", item.Entry.Name, vr.Length, item.Entry.MaxSize); if (vr.Length < item.Entry.MaxSize) { uint diff = (uint)item.Entry.MaxSize - vr.Length; VFile.Add(new VRangeNull() { Length = diff, Start = vr.Start + vr.Length, Tag = "Padding" }); } offset += (uint)item.Entry.MaxSize; //System.Diagnostics.Debug.WriteLine("--VRangeFile"); } else //multiple overrides; tricky! //System.Diagnostics.Debug.WriteLine("Add VRangeConditional for " + item.Entry.Name); { ProcMonParser.DataItem di; lgpItems.TryGetValue(item.Entry.Name, out di); uint fbOffset = (di == null) ? 0 : (uint)di.Start; var vcond = new VRangeConditional(item.Entry.Name, overrides.ToList(), handle, fbOffset) { Length = (uint)item.Entry.MaxSize + 24, Start = offset, Name = item.Entry.Name, Tag = item.Entry.Name }; VFile.Add(vcond); _wFiles.Add(vcond); offset += (uint)item.Entry.MaxSize + 24; //System.Diagnostics.Debug.WriteLine("--VRangeConditional"); } count++; /* * string file = MapFile(System.IO.Path.Combine(name, item.Name), profile); * if (file != null) { * uint length = (uint)new System.IO.FileInfo(file).Length; * byte[] fheader = new byte[24]; * System.Text.Encoding.ASCII.GetBytes(item.Name, 0, item.Name.Length, fheader, 0); * Bytes.WriteUInt(fheader, 20, length); * VFile.Add(new VRangeInline() { Start = offset, Length = 24, Data = fheader }); * offset += 24; * var vf = new VRangeFile() { Start = offset, Length = length, Filename = file }; * VFile.Add(vf); * _wFiles.Add(vf); * offset += length; * } else { * VFile.Add(new VRangeHandle() { Start = offset, Length = (uint)(item.Length), Handle = handle, Offset = (uint)item.Start }); * offset += (uint)item.Length; * }*/ } byte[] footer = System.Text.Encoding.ASCII.GetBytes("FINAL FANTASY7"); VFile.Add(new VRangeInline() { Start = offset, Length = (uint)footer.Length, Data = footer, Tag = "footer" }); System.Diagnostics.Debug.WriteLine("Created: " + VFile.ToString()); } catch { VFile.Dump(); throw; } }
public override void Read(uint offset, uint length, IntPtr dest, ref uint bytesRead) { //System.Diagnostics.Debug.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)) { System.Diagnostics.Debug.WriteLine("Conditional {0} switching to {1}", Name, of.File); _current = of; Bytes.WriteInt(_header, 20, of.Size); break; } } if (_current == null) { System.Diagnostics.Debug.WriteLine("Conditional {0} switching to fallback", Name, Name); } _lastInit = true; //we're ready to read file headers } else { _lastInit = init; } if (_current != null && _handle.Equals(IntPtr.Zero) && _current.Archive == null) { _handle = Wrap.CreateFile(_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 //System.Diagnostics.Debug.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); //System.Diagnostics.Debug.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; //System.Diagnostics.Debug.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); //System.Diagnostics.Debug.WriteLine("Conditional {1} reading from cfile - {0} bytes read", bytesRead, Name); } else { _current.Archive.RawRead(_current.File, offset, length, dest, ref bytesRead); //System.Diagnostics.Debug.WriteLine("Conditional {1} reading from cfile archive offset {2} length {3} - {0} bytes read", bytesRead, Name, offset, length); } } } }