Exemplo n.º 1
0
        void SetCurrentTlkFile(string tlkFilePath)
        {
            _tlkFile     = TlkFile.Load(tlkFilePath);
            _tlkFilePath = tlkFilePath;

            tvTlkFileContents.Nodes.Clear();
            TreeNode rootNode = tvTlkFileContents.Nodes.Add(tlkFilePath);

            rootNode.Tag = _tlkFile;
        }
Exemplo n.º 2
0
        public BinaryTlkFile(TlkFile other)
            : base(other)
        {
            if (other is BinaryTlkFile)
            {
                StringRefs = (other as BinaryTlkFile).StringRefs;
            }

            if (StringRefs == null)
            {
                StringRefs = new List <TlkStringRef>();
            }
        }
Exemplo n.º 3
0
            private TlkFile ParseFile(BinaryReader br)
            {
                var header = (TlkHeaderBinary)Common.ReadStruct(br, typeof(TlkHeaderBinary));

                var stringDataEntries = new List <TlkEntryBinary>();
                var stringEntries     = new List <string>();

                br.BaseStream.Seek(18, SeekOrigin.Begin);
                for (int i = 0; i < header.StringCount; i++)
                {
                    var stringDataEntry = (TlkEntryBinary)Common.ReadStruct(br, typeof(TlkEntryBinary));
                    stringDataEntries.Add(stringDataEntry);
                }

                Int64 stringLocation = header.StringOffset;

                br.BaseStream.Seek(header.StringOffset, SeekOrigin.Begin);
                for (int i = 0; i < header.StringCount; i++)
                {
                    br.BaseStream.Seek(stringLocation, SeekOrigin.Begin);
                    var stringEntry = br.ReadChars(stringDataEntries[i].StringLength);
                    stringLocation += stringDataEntries[i].StringLength;
                    stringEntries.Add(new string(stringEntry));
                }

                var tlk = new TlkFile
                {
                    LangugeId = header.LanguageId
                };

                int stringIndex = 0;

                foreach (var data in stringDataEntries)
                {
                    var stringInfo = new StringEntry
                    {
                        Strref         = stringIndex,
                        Flags          = (StringEntryType)data.Flags,
                        PitchVariance  = data.PitchVariance,
                        Sound          = data.Sound.ToString(),
                        Text           = stringEntries[stringIndex],
                        VolumeVariance = data.VolumeVariance
                    };
                    tlk.Strings.Add(stringInfo);
                    stringIndex++;
                }

                return(tlk);
            }
Exemplo n.º 4
0
        private static void TLK2WAV(string tlkFilePath, string outputFileOrFolder, uint?fileNum)
        {
            const string extension = ".WAV";
            var          tlkFile   = TlkFile.Load(tlkFilePath);

            var tlkFileCodec = tlkFile.DetectTlkFileCodecType();

            Console.WriteLine(string.Format("Extracting {0} file(s) from {1} using {2} codec...", extension, tlkFilePath,
                                            tlkFileCodec));
            var successful = 0;
            var failed     = 0;

            for (var i = 0; i < tlkFile.Records.Length; i++)
            {
                if (fileNum.HasValue && fileNum != i)
                {
                    continue;
                }
                try
                {
                    var    dataItem = tlkFile.Records[i];
                    string thisFileName;
                    if (fileNum.HasValue && !Directory.Exists(outputFileOrFolder))
                    {
                        thisFileName = outputFileOrFolder;
                    }
                    else
                    {
                        thisFileName = Path.Combine(outputFileOrFolder, i + extension);
                    }
                    Console.Write("Extracting " + thisFileName + "...");
                    tlkFile.DecompressRecordAndWriteToFile(tlkFileCodec, dataItem, thisFileName);
                    Console.Write("Completed.");
                    successful++;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(string.Format("Error: {0}", ex));
                    failed++;
                }
                Console.WriteLine();
            }
            if (failed == 0 && successful == 0)
            {
                failed = 1;
            }
            Console.WriteLine(string.Format("Finished extracting {0} successful / {1} failed {2} file(s) from {3}.",
                                            successful, failed, extension, tlkFilePath));
        }
Exemplo n.º 5
0
        private static void COMPRESSED2TLK(string compressedFileOrFolder, string tlkFilePath, CodecType codecType,
                                           uint?fileNum)
        {
            var extension = "." + codecType;
            var compressedFilesToInclude = new Dictionary <uint, string>();

            if (!extension.StartsWith("."))
            {
                extension = "." + extension;
            }
            if (fileNum.HasValue)
            {
                compressedFilesToInclude.Add(fileNum.Value, compressedFileOrFolder);
            }
            else
            {
                var files = Directory.GetFiles(compressedFileOrFolder, "*" + extension);
                Array.Sort(files, new NumericComparer());
                foreach (var file in files)
                {
                    uint rslt;
                    var  parsed = UInt32.TryParse(Path.GetFileNameWithoutExtension(file), out rslt);
                    if (parsed)
                    {
                        compressedFilesToInclude.Add(rslt, file);
                    }
                }
            }
            if (fileNum.HasValue || new FileInfo(tlkFilePath).Exists)
            {
                Console.WriteLine(string.Format("Importing {0} .{1} file(s) into {2}...", compressedFilesToInclude.Count,
                                                codecType, tlkFilePath));
                TlkFile.ImportCompressedFiles(compressedFilesToInclude, tlkFilePath, codecType, Console.Out);
                Console.WriteLine(string.Format("Finished importing {0} .{1} file(s) into {2}.",
                                                compressedFilesToInclude.Count, codecType, tlkFilePath));
            }
            else
            {
                Console.WriteLine(string.Format("Creating new .TLK file {0} from {1} file(s)...", tlkFilePath, extension));
                TlkFile.BuildFromCompressedAudioFiles(new List <string>(compressedFilesToInclude.Values).ToArray(),
                                                      tlkFilePath, Console.Out);
                Console.WriteLine(string.Format("Finished creating new .TLK file {0} from {1} file(s).", tlkFilePath,
                                                extension));
            }
        }
Exemplo n.º 6
0
        private static void WAV2TLK(string wavsFileOrFolder, string tlkFilePath, CodecType codecType, uint?fileNum)
        {
            var codecTypeDesc = codecType == CodecType.LH ? "LH" : codecType == CodecType.SPX ? "SPX" : "Unknown";

            var wavsToInclude = new Dictionary <uint, string>();

            if (fileNum.HasValue)
            {
                wavsToInclude.Add(fileNum.Value, wavsFileOrFolder);
            }
            else
            {
                var files = Directory.GetFiles(wavsFileOrFolder, "*.wav");
                Array.Sort(files, new NumericComparer());
                foreach (var file in files)
                {
                    uint rslt;
                    var  parsed = UInt32.TryParse(Path.GetFileNameWithoutExtension(file), out rslt);
                    if (parsed)
                    {
                        wavsToInclude.Add(rslt, file);
                    }
                }
            }
            if (fileNum.HasValue || new FileInfo(tlkFilePath).Exists)
            {
                Console.WriteLine(string.Format("Importing {0} .WAV(s) (using {1} codec) into {2}...",
                                                wavsToInclude.Count, codecTypeDesc, tlkFilePath));
                TlkFile.ImportWAVFiles(codecType, wavsToInclude, tlkFilePath, Console.Out);
                Console.WriteLine(string.Format("Finished importing {0} .WAV(s) (using {1} codec) into {2}.",
                                                wavsToInclude.Count, codecTypeDesc, tlkFilePath));
            }
            else
            {
                Console.WriteLine(string.Format("Creating new .TLK file {0} from .WAV(s) (using {1} codec)...",
                                                tlkFilePath, codecTypeDesc));
                TlkFile.BuildFromWAVFiles(codecType, new List <string>(wavsToInclude.Values).ToArray(), tlkFilePath,
                                          Console.Out);
                Console.WriteLine(string.Format("Finished creating new .TLK file {0} from .WAV(s) (using {1} codec).",
                                                tlkFilePath, codecTypeDesc));
            }
        }
Exemplo n.º 7
0
        private static void ConvertCompressedFileFormats(string sourceFileOrFolderSpec, string outputFileOrFolderSpec,
                                                         CodecType sourceCodec, CodecType targetCodec)
        {
            string[] inputFiles;
            var      sourceCodecString = sourceCodec.ToString();

            if (sourceCodecString == "Unknown")
            {
                sourceCodecString = "WAV";
            }
            var targetCodecString = targetCodec.ToString();

            if (targetCodecString == "Unknown")
            {
                targetCodecString = "WAV";
            }

            if (Directory.Exists(sourceFileOrFolderSpec))
            {
                inputFiles = Directory.GetFiles(sourceFileOrFolderSpec, "*." + sourceCodecString);
            }
            else
            {
                inputFiles = new[] { sourceFileOrFolderSpec };
            }
            Array.Sort(inputFiles, new NumericComparer());
            if (inputFiles.Length == 0)
            {
                Console.WriteLine(string.Format("No {0} file(s) were found to convert!", "." + sourceCodecString));
                return;
            }
            Console.WriteLine(string.Format("Converting {0} .{1} file(s) to .{2} format...", inputFiles.Length,
                                            sourceCodecString, targetCodecString));
            foreach (var inputFile in inputFiles)
            {
                var targetDirectory = Directory.Exists(outputFileOrFolderSpec)
                                          ? outputFileOrFolderSpec
                                          : Path.GetDirectoryName(outputFileOrFolderSpec);
                var targetFileName = Path.Combine(targetDirectory,
                                                  Path.GetFileNameWithoutExtension(Path.GetFileName(inputFile)) + "." +
                                                  targetCodecString);

                Console.Write(string.Format("Converting {0} to {1}...", inputFile, targetFileName));
                using (var sourceFileStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
                {
                    const int WAV_HEADER_SIZE = 44;

                    uint uncompressedLength = 0;
                    uint compressedLength   = 0;
                    if (sourceCodec == CodecType.Unknown)
                    {
                        uncompressedLength = (uint)new FileInfo(inputFile).Length - WAV_HEADER_SIZE;
                    }
                    else
                    {
                        var fieldBytes = new byte[4];
                        sourceFileStream.Read(fieldBytes, 0, 4);
                        uncompressedLength = BitConverter.ToUInt32(fieldBytes, 0);
                        sourceFileStream.Read(fieldBytes, 0, 4);
                        compressedLength = BitConverter.ToUInt32(fieldBytes, 0);
                    }
                    using (var sourceFilePcmStream = new MemoryStream((int)uncompressedLength))
                    {
                        if (sourceCodec == CodecType.Unknown)
                        {
                            sourceFileStream.Seek(WAV_HEADER_SIZE, SeekOrigin.Begin);
                            var sourceFilePcmBytes = new byte[uncompressedLength];
                            sourceFileStream.Read(sourceFilePcmBytes, 0, (int)uncompressedLength);
                            sourceFilePcmStream.Write(sourceFilePcmBytes, 0, (int)uncompressedLength);
                            sourceFilePcmStream.Seek(0, SeekOrigin.Begin);
                        }
                        else
                        {
                            TlkFile.DecompressAudioDataFromStream(sourceCodec, sourceFileStream, (int)compressedLength,
                                                                  (int)uncompressedLength, sourceFilePcmStream);
                            sourceFilePcmStream.Seek(0, SeekOrigin.Begin);
                        }
                        using (
                            var targetFileStream = new FileStream(targetFileName, FileMode.Create, FileAccess.ReadWrite)
                            )
                        {
                            if (targetCodec == CodecType.Unknown)
                            {
                                var pcmBuffer = new byte[uncompressedLength];
                                sourceFilePcmStream.Read(pcmBuffer, 0, pcmBuffer.Length);
                                TlkFile.WritePCMBitsAsWAVToStream(pcmBuffer, 0, pcmBuffer.Length, targetFileStream);
                            }
                            else
                            {
                                targetFileStream.Write(BitConverter.GetBytes(uncompressedLength), 0, 4);
                                targetFileStream.Seek(4, SeekOrigin.Current);
                                //leave room for writing the compressed file length
                                compressedLength =
                                    (uint)
                                    TlkFile.CompressAudioToStream(targetCodec, sourceFilePcmStream,
                                                                  (int)uncompressedLength, targetFileStream);
                                targetFileStream.Flush();
                                targetFileStream.Seek(4, SeekOrigin.Begin);
                                targetFileStream.Write(BitConverter.GetBytes(compressedLength), 0, 4);
                            }
                            targetFileStream.Flush();
                            targetFileStream.Close();
                        }
                    }
                }
                Console.WriteLine("Completed.");
            }
            Console.WriteLine(string.Format("Finished converting {0} .{1} file(s) to .{2} format.", inputFiles.Length,
                                            sourceCodecString, targetCodecString));
        }
Exemplo n.º 8
0
        private static int TLK2COMPRESSED(string tlkFilePath, string outputFileOrFolder, CodecType codecType,
                                          uint?fileNum)
        {
            var extension = "." + codecType;
            var tlkFile   = TlkFile.Load(tlkFilePath);

            var tlkFileCodecType = tlkFile.DetectTlkFileCodecType();

            if (tlkFileCodecType != codecType)
            {
                if (tlkFileCodecType != CodecType.Unknown)
                {
                    Console.Error.WriteLine(
                        string.Format(
                            "Invalid <codec> argument: {0} was built using files that were encoded using the {1} codec, not the {2} codec.",
                            tlkFilePath, tlkFileCodecType, codecType));
                    return(ERR_LEVEL__ERROR_OCCURRED);
                }
            }
            Console.WriteLine(string.Format("Extracting {0} file(s) from {1}...", extension, tlkFilePath));
            var successful = 0;
            var failed     = 0;

            for (var i = 0; i < tlkFile.Records.Length; i++)
            {
                if (fileNum.HasValue && fileNum != i)
                {
                    continue;
                }
                try
                {
                    var    dataItem = tlkFile.Records[i];
                    string thisFileName;
                    if (fileNum.HasValue && !Directory.Exists(outputFileOrFolder))
                    {
                        thisFileName = outputFileOrFolder;
                    }
                    else
                    {
                        thisFileName = Path.Combine(outputFileOrFolder, i + extension);
                    }
                    Console.Write("Extracting " + thisFileName + "...");
                    tlkFile.WriteRecordToFile(dataItem, thisFileName);
                    Console.Write("Completed.");
                    successful++;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(string.Format("Error: {0}", ex));
                    failed++;
                }
                Console.WriteLine();
            }
            if (failed == 0 && successful == 0)
            {
                failed = 1;
            }
            Console.WriteLine(string.Format("Finished extracting {0} successful / {1} failed {2} file(s) from {3}.",
                                            successful, failed, extension, tlkFilePath));
            return(failed == 0 ? ERR_LEVEL__SUCCESS : ERR_LEVEL__ERROR_OCCURRED);
        }
Exemplo n.º 9
0
 public XmlTlkFile(TlkFile other)
     : base(other)
 {
 }