/// <summary> /// Default constructor. /// </summary> /// <param name="count">The initial number of entries in the tlk file.</param> public Tlk(int count) { name = string.Empty; header = new TlkHeader(); resRefs = new RawResRef[count]; strings = new string[count]; header.fileType = tlkFile; header.fileVersion = tlkVersion; header.stringCount = count; header.stringOffset = 0; for (int i = 0; i < count; i++) { resRefs[i] = new RawResRef(); resRefs[i].flags = (int) ResRefFlags.None; resRefs[i].offsetToString = 0; resRefs[i].pitchVariance = 0; resRefs[i].soundLength = 0; resRefs[i].soundResRef = string.Empty; resRefs[i].stringSize = 0; resRefs[i].volumeVariance = 0; strings[i] = string.Empty; } }
/// <summary> /// Private constructor, instances of this class must /// </summary> private Tlk() { header = new TlkHeader(); resRefs = null; strings = null; }
/// <summary> /// Deserializes the RawResRef array from the given byte array. /// </summary> /// <param name="bytes"></param> private void DeserializeRawResRefs(byte[] bytes) { // Alloc a hglobal to store the bytes. IntPtr buffer = Marshal.AllocHGlobal(RawResRefSize); try { // Create a RawResRef array for all of the entries and loop // through the array populating it. resRefs = new RawResRef[header.stringCount]; for (int i = 0; i < resRefs.Length; i++) { // Copy the bytes of the i'th RawResRef to unprotected memory // and convert it to a RawResRef structure. Marshal.Copy(bytes, i * RawResRefSize, buffer, RawResRefSize); object o = Marshal.PtrToStructure(buffer, typeof(RawResRef)); resRefs[i] = (RawResRef) o; } } finally { // Free the hglobal before exiting. Marshal.FreeHGlobal(buffer); } }
/// <summary> /// Pads the tlk to have at least the specified number of entries. If the tlk file /// has less entries than what is given, blank entries are inserted to pad. /// </summary> /// <param name="length">The new number of entries</param> public void Pad(int length) { // If the tlk file is larger than the pad count then do nothing. if (header.stringCount >= length) return; // Add blank entries to the tlk file to pad. RawResRef[] padded = new RawResRef[length]; resRefs.CopyTo(padded, 0); for (int i = resRefs.Length; i < padded.Length; i++) { padded[i].flags = 0; padded[i].offsetToString = 0; padded[i].pitchVariance = 0; padded[i].stringSize = 0; padded[i].volumeVariance = 0; padded[i].soundLength = 0.0f; padded[i].soundResRef = string.Empty; } string[] paddedStrings = new string[length]; paddedStrings.CopyTo(strings, 0); for (int i = strings.Length; i < paddedStrings.Length; i++) paddedStrings[i] = string.Empty; // Save the new RawResRef array and update the number of entries in // the header. header.stringCount = length; resRefs = padded; strings = paddedStrings; }