コード例 #1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var picker = new CommonOpenFileDialog();

            picker.Filters.Add(new CommonFileDialogFilter("GIN/SNR Audio", "gin;snr"));

            if (picker.ShowDialog() == CommonFileDialogResult.Ok)
            {
                byte[] data  = File.ReadAllBytes(picker.FileName);
                int    index = tmx.audioEntries.IndexOf(currentEntry);
                int    header;

                using (var stream = new BinaryStream(new MemoryStream(data)))
                {
                    header = stream.ReadInt32();

                    switch ((long)header)
                    {
                    case 0x75736E47L:
                        tmx.audioEntries[index].type    = "GIN";
                        tmx.audioEntries[index].ginData = GIN.ReadGIN(data);
                        break;

                    // No actual header so just read the channel/sample rate info, should only ever be one of these for NFS
                    case 0x80BB0004L:
                    case 0x80BB0404L:
                    case 0x44AC0004L:
                    case 0x44AC0404L:
                        tmx.audioEntries[index].type      = "SNR";
                        tmx.audioEntries[index].audioData = data;
                        break;
                    }
                }


                RefreshAudioInfo(tmx);

                lstAudio.SelectedIndex = index;
            }
        }
コード例 #2
0
        public TMX ReadTMX(string path)
        {
            var bytes = File.ReadAllBytes(path);

            fileName = path;

            using (var stream = new BinaryStream(new MemoryStream(bytes)))
            {
                audioEntries = new List <AudioEntry>();

                if (stream.ReadUInt32() != 2292214557)
                {
                    MessageBox.Show("Not a valid TMX file. Please open a valid TMX file and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                    return(null);
                }
                else
                {
                    bool       searching = true;
                    int        offset = -1;
                    List <int> ginOffsets = new List <int>(), snrOffsets = new List <int>();
                    byte[]     ginHeader = new byte[] { 0x20, 0x4E, 0x49, 0x47 };
                    byte[]     snrHeader = new byte[] { 0x20, 0x52, 0x4E, 0x53 };

                    // Search for GIN headers
                    while (searching)
                    {
                        offset = FindSequenceMultiple(bytes, ginHeader, ginOffsets);
                        if (offset != -1)
                        {
                            ginOffsets.Add(offset);
                        }
                        else
                        {
                            searching = false;
                        }
                    }

                    searching = true;

                    // Search for SNR headers
                    while (searching)
                    {
                        offset = FindSequenceMultiple(bytes, snrHeader, snrOffsets);
                        if (offset != -1)
                        {
                            snrOffsets.Add(offset);
                        }
                        else
                        {
                            searching = false;
                        }
                    }

                    numSounds = ginOffsets.Count + snrOffsets.Count;

                    stream.Position = 0;

                    tmxHeader1      = stream.ReadBytes(88);
                    ginHeaderOffset = stream.ReadInt32();
                    audioDataOffset = stream.ReadInt32();
                    tmxHeader2      = stream.ReadBytes(ginHeaderOffset - 96);

                    for (int i = 0; i < numSounds; i++)
                    {
                        AudioEntry entry = new AudioEntry();

                        entry.entrySize = (byte)stream.ReadByte();

                        entry.type = new string(stream.ReadString(3).Reverse().ToArray());

                        entry.index          = stream.ReadInt32();
                        entry.relativeOffset = stream.ReadInt32();
                        entry.absoluteOffset = entry.relativeOffset + audioDataOffset;
                        entry.unk1           = stream.ReadInt32();
                        entry.unk2           = stream.ReadInt32();
                        entry.version        = stream.ReadInt32();
                        entry.unk3           = stream.ReadInt32();
                        entry.unk4           = BitConverter.ToSingle(stream.ReadBytes(4), 0);
                        entry.unk5           = stream.ReadInt32();

                        audioEntries.Add(entry);
                    }

                    for (int i = 0; i < audioEntries.Count; i++)
                    {
                        int readTo = 0;

                        stream.Position = audioEntries[i].absoluteOffset;

                        // If last sound file, read to end, otherwise to next header
                        if (i == audioEntries.Count - 1)
                        {
                            readTo = bytes.Length;
                        }
                        else
                        {
                            readTo = audioEntries[i + 1].absoluteOffset;
                        }

                        if (audioEntries[i].type == "GIN")
                        {
                            audioEntries[i].ginData = GIN.ReadGIN(stream.ReadBytes((int)(readTo - stream.Position)));
                        }
                        else
                        {
                            audioEntries[i].audioData = stream.ReadBytes((int)(readTo - stream.Position));
                        }
                    }
                }
            }

            return(this);
        }