Exemplo n.º 1
0
        /// <summary>
        /// Creates a Tlk object for the specified tlk file.
        /// </summary>
        /// <param name="fileName">The tlk file</param>
        /// <returns>A Tlk object representing the tlk file</returns>
        public static Tlk LoadTlk(string fileName)
        {
            // Open the tlk file.
            Tlk tlk = new Tlk();
            using (FileStream reader =
                new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // Save the name of the tlk file.
                FileInfo info = new FileInfo(fileName);
                tlk.name = info.Name;

                // Read the header and decode it.
                byte[] buffer = new byte[Tlk.headerSize];
                if (reader.Read(buffer, 0, buffer.Length) != buffer.Length)
                    ThrowException("Tlk file {0} is corrupt", fileName);
                tlk.DeserializeHeader(buffer);

                // Do a reality check on the tlk file.
                if (tlk.header.fileType != tlkFile)
                    ThrowException("{0} is not a tlk file", fileName);
                if (tlk.header.fileVersion != tlkVersion)
                    ThrowException("{0} is an unsupported tlk file", fileName);

                // Read the RawResRef array and decode it.
                int size = tlk.header.stringCount * Tlk.RawResRefSize;
                buffer = new byte[size];
                if (reader.Read(buffer, 0, buffer.Length) != buffer.Length)
                    ThrowException("Tlk file {0} is corrupt", fileName);
                tlk.DeserializeRawResRefs(buffer);

                // Read the raw string data.
                buffer = new byte[reader.Length - tlk.header.stringOffset];
                if (reader.Read(buffer, 0, buffer.Length) != buffer.Length)
                    ThrowException("Tlk file {0} is corrupt", fileName);

                // Load the strings from the raw bytes into our string array.
                tlk.strings = new string[tlk.header.stringCount];
                for (int i = 0; i < tlk.header.stringCount; i++)
                    tlk.strings[i] = tlk.GetStringFromBuffer(buffer, i);
            }

            return tlk;
        }