Ejemplo n.º 1
0
 public VirtualWaveBankEntry(SemanticLogger logger, VirtualWaveBank waveBank, void *entry, int index, string cueName)
 {
     mLogger   = logger;
     WaveBank  = waveBank;
     NativePtr = entry;
     Index     = index;
     CueName   = cueName;
 }
Ejemplo n.º 2
0
        private NtStatus ReadEntryWaveData(VirtualWaveBank waveBank, long absOffset,
                                           IntPtr handle, IntPtr hEvent, IntPtr *apcRoutine, IntPtr *apcContext, ref Native.IO_STATUS_BLOCK ioStatus, byte *buffer, uint length, Native.LARGE_INTEGER *byteOffset, IntPtr key)
        {
            var status  = NtStatus.Success;
            var handled = false;

            var segBaseOffset = waveBank.Native.Header->Segments[(int)WaveBankSegmentIndex.EntryWaveData].Offset;
            var segOffset     = absOffset - segBaseOffset;

            for (int i = 0; i < waveBank.Entries.Count; i++)
            {
                var entry = waveBank.Entries[i];
                if (segOffset < entry.Native->PlayRegion.Offset || segOffset >= (entry.Native->PlayRegion.Offset + entry.Native->PlayRegion.Length))
                {
                    continue;
                }

                var fileDataOffset = segOffset - entry.Native->PlayRegion.Offset;
                var readEndOffset  = fileDataOffset + length;
                var nextDataOffset = i < waveBank.Entries.Count - 1 ? waveBank.Entries[i + 1].Native->PlayRegion.Offset : (waveBank.VirtualFileSize - segBaseOffset);
                if (readEndOffset > nextDataOffset)
                {
                    continue;
                }

                handled = true;
                if (!entry.IsRedirected)
                {
                    // Trigger cache miss
                    for (int j = 0; j < mCache.Length; j++)
                    {
                        var cacheEntry = mCache[j].Entry;
                        if (mCache[j].Miss())
                        {
                            mLogger.Debug($"{waveBank.FileName} Hnd: {handle} Index: {j} {cacheEntry.CueName} removed from cache");
                        }
                    }

                    mLogger.Info($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} Data access Offset: 0x{absOffset:X8} Length: 0x{length:X8}");
                    status = mHooks.NtReadFileHook.OriginalFunction(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key);
                }
                else
                {
                    mLogger.Info($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} Data access Offset: 0x{absOffset:X8} Length: 0x{length:X8} redirected to {entry.FilePath}");
                    status = NtStatus.Success;

                    if (fileDataOffset < 0)
                    {
                        mLogger.Error($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} Offset is before start of data!!!");
                        continue;
                    }
                    else if (fileDataOffset > entry.FileSize)
                    {
                        mLogger.Error($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} Offset is after end of data!!!");
                        //continue;
                    }

                    mLogger.Debug($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} Reading 0x{length:X8} bytes from redirected file at offset 0x{fileDataOffset:X8}");

                    // Get cached file stream if the file was previously opened or open a new file
                    Stream redirectedStream = null;
                    for (int j = 0; j < mCache.Length; j++)
                    {
                        if (mCache[j].Entry == entry)
                        {
                            // Found entry in cache, increase score
                            mCache[j].Hit();
                            mLogger.Debug($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} loaded from cache");
                            redirectedStream = mCache[j].Stream;
                            break;
                        }
                        else
                        {
                            // Entry is not the one we're looking for, so we lower its score
                            var cacheEntry = mCache[j].Entry;
                            if (mCache[j].Miss())
                            {
                                mLogger.Debug($"{waveBank.FileName} Hnd: {handle} Index: {j} {cacheEntry.CueName} removed from cache");
                            }
                        }
                    }

                    if (redirectedStream == null)
                    {
                        // Wasn't found in cache
                        mLogger.Debug($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} added to cache");
                        redirectedStream = entry.OpenRead();
                        for (int j = 0; j < mCache.Length; j++)
                        {
                            if (mCache[j].Entry == null)
                            {
                                mCache[j] = new CacheEntry()
                                {
                                    Entry = entry, Score = 1, Stream = redirectedStream
                                };
                                break;;
                            }
                        }
                    }

                    // Read from redirected file into the buffer
                    try
                    {
                        redirectedStream.Seek(fileDataOffset, SeekOrigin.Begin);
                        var readBytes = redirectedStream.Read(new Span <byte>(( void * )buffer, ( int )length));
                        SetBytesRead(handle, ( int )waveBank.FilePointer, ( int )length, ref ioStatus);

                        if (readBytes != length)
                        {
                            mLogger.Error($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} File read length doesnt match requested read length!! Expected 0x{length:X8}, Actual 0x{readBytes:X8}");
                        }

                        mLogger.Debug($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} Wrote redirected file to buffer");
                    }
                    catch (Exception e)
                    {
                        mLogger.Debug($"{waveBank.FileName} Hnd: {handle} Index: {i} {entry.CueName} Unhandled exception thrown during reading {entry.FileName}: {e}");
                    }
                }
            }

            if (!handled)
            {
                mLogger.Error($"{waveBank.FileName} Hnd: {handle} Unhandled file data read request!! Offset: 0x{absOffset:X8} Length: 0x{length:X8}");
                status = mHooks.NtReadFileHook.OriginalFunction(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key);
            }

            return(status);
        }
Ejemplo n.º 3
0
        public override Native.NtStatus NtCreateFileImpl(string newFilePath, out IntPtr handle, FileAccess access, ref Native.OBJECT_ATTRIBUTES objectAttributes,
                                                         ref Native.IO_STATUS_BLOCK ioStatus, ref long allocSize, uint fileAttributes, FileShare share, uint createDisposition, uint createOptions, IntPtr eaBuffer, uint eaLength)
        {
            var result = mHooks.NtCreateFileHook.OriginalFunction(out handle, access, ref objectAttributes, ref ioStatus, ref allocSize, fileAttributes,
                                                                  share, createDisposition, createOptions, eaBuffer, eaLength);

            var              fileName   = Path.GetFileNameWithoutExtension(newFilePath);
            var              ext        = Path.GetExtension(newFilePath);
            var              isWaveBank = ext.Equals(".xwb", StringComparison.OrdinalIgnoreCase);
            VirtualWaveBank  waveBank   = null;
            VirtualSoundBank soundBank  = null;

            if (isWaveBank && !mWaveBankByName.TryGetValue(fileName, out waveBank))
            {
                // Try get sound bank for cue names
                mSoundBankByName.TryGetValue(fileName, out soundBank);

                // Wave bank
                mWaveBankByName[fileName] = waveBank = new VirtualWaveBank(mLogger);

                // Load wave bank
                using (var fileStream = new FileStream(new SafeFileHandle(handle, true), FileAccess.Read, 1024 * 1024))
                    waveBank.LoadFromFile(newFilePath, fileStream);

                // Reopen file to reset it
                result = mHooks.NtCreateFileHook.OriginalFunction(out handle, access, ref objectAttributes, ref ioStatus, ref allocSize, fileAttributes,
                                                                  share, createDisposition, createOptions, eaBuffer, eaLength);

                ProcessWaveBankEntries(newFilePath, soundBank, waveBank);
                mLogger.Debug($"{newFilePath} registered");
            }
            else if (!mSoundBankByName.TryGetValue(fileName, out soundBank))
            {
                // Sound bank
                mSoundBankByName[fileName] = soundBank = new VirtualSoundBank(mLogger);

                // Load wave bank
                using (var fileStream = new FileStream(new SafeFileHandle(handle, true), FileAccess.Read, 1024 * 1024))
                    soundBank.LoadFromFile(newFilePath, fileStream);

                // Reopen file to reset it
                result = mHooks.NtCreateFileHook.OriginalFunction(out handle, access, ref objectAttributes, ref ioStatus, ref allocSize, fileAttributes,
                                                                  share, createDisposition, createOptions, eaBuffer, eaLength);

                // Find associated wave bank
                if (!mWaveBankByName.TryGetValue(soundBank.Native.WaveBankNames[0].Name, out waveBank))
                {
                    mLogger.Error($"{newFilePath} Can't find wavebank!");
                }
                else
                {
                    ProcessWaveBankEntries(newFilePath, soundBank, waveBank);
                }
            }

            if (isWaveBank)
            {
                mWaveBankByHandle.Add(handle, waveBank);
                mLogger.Debug($"{waveBank.FileName} Hnd {handle} registered");
            }
            else
            {
                mSoundBankByHandle.Add(handle, soundBank);
            }


            return(result);
        }
Ejemplo n.º 4
0
        private void ProcessWaveBankEntries(string filePath, VirtualSoundBank soundBank, VirtualWaveBank waveBank)
        {
            bool TryRedirectEntry(Mod mod, int i, string fileName)
            {
                var redirectFilePath = Path.Combine(Path.Combine(mod.LoadDirectory, "SND"), waveBank.FileName, fileName);

                if (File.Exists(redirectFilePath))
                {
                    if (waveBank.Entries[i].Redirect(redirectFilePath))
                    {
                        mLogger.Info($"{waveBank.FileName} Index: {i} Cue: {waveBank.Entries[i].CueName} redirected to {redirectFilePath}");
                    }
                    else
                    {
                        mLogger.Error($"{waveBank.FileName} Index: {i} Cue: {waveBank.Entries[i].CueName} redirecting to {redirectFilePath} failed!");
                    }

                    return(true);
                }

                return(false);
            }

            for (int i = 0; i < waveBank.Entries.Count; i++)
            {
                if (soundBank != null)
                {
                    waveBank.Entries[i].CueName = soundBank.GetTrackCueName(i, 0);
                }

                if (waveBank.Entries[i].IsRedirected)
                {
                    continue;
                }

                foreach (var mod in mModDb.Mods)
                {
                    // Try redirect from wave/track index
                    if (TryRedirectEntry(mod, i, $"{i}.raw"))
                    {
                        break;
                    }

                    if (waveBank.Entries[i].CueName != null)
                    {
                        // Try redirect from cue name
                        if (TryRedirectEntry(mod, i, waveBank.Entries[i].CueName + ".raw"))
                        {
                            break;
                        }
                    }
                }
            }
        }