SHARE
TWEET

Untitled

a guest Jun 25th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace Tlk
  8. {
  9.     class TlkFile
  10.     {
  11.         public TLKHeader Header;
  12.         public SortedList<int, TLKString> Strings = new SortedList<int, TLKString>();
  13.  
  14.         private TlkFile() { }
  15.         public TlkFile(byte[] data)
  16.         {
  17.             byte[] entry = new byte[26];
  18.             Header = Tools.ByteArrayToStructure<TLKHeader>(data);
  19.             if (Header.Version.StartsWith("V1"))
  20.             {
  21.                 Encoding enc = Encoding.GetEncoding(1252);
  22.                 for (int i = 0; i < Header.NumberOfStrings; i++)
  23.                 {
  24.                     Array.Copy(data, 18 + i * 26, entry, 0, 26);
  25.                     TLKV1Entry tlkEntry = Tools.ByteArrayToStructure<TLKV1Entry>(entry);
  26.                     string s = enc.GetString(data, Header.Offset + tlkEntry.StringOffset, tlkEntry.StringLength);
  27.                     Strings.Add(i, new TLKString(tlkEntry, s));
  28.                 }
  29.             }
  30.         }
  31.     }
  32.  
  33.     #region TLKHeader
  34.     [StructLayout(LayoutKind.Sequential, Size = 18, Pack = 1)]
  35.     public struct TLKHeader
  36.     {
  37.         /// <summary>
  38.         /// Signature ('TLK ')
  39.         /// </summary>
  40.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
  41.         public string Signature;
  42.  
  43.         /// <summary>
  44.         /// Version ('V1')
  45.         /// </summary>
  46.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
  47.         public string Version;
  48.  
  49.         [MarshalAs(UnmanagedType.I2)]
  50.         public Int16 Unknown;
  51.  
  52.         /// <summary>
  53.         /// Number of Strref entries in this file.
  54.         /// </summary>
  55.         [MarshalAs(UnmanagedType.U4)]
  56.         public uint NumberOfStrings;
  57.         /// <summary>
  58.         /// Offset to string data
  59.         /// </summary>
  60.         [MarshalAs(UnmanagedType.I4)]
  61.         public int Offset;
  62.     }
  63.     #endregion
  64.     #region TLKV1Entry
  65.     [StructLayout(LayoutKind.Sequential, Size = 26, Pack = 1)]
  66.     public struct TLKV1Entry
  67.     {
  68.         /// <summary>
  69.         /// 2 == entry unused? (i.e. ignore other fields), 3 == normal?
  70.         /// </summary>
  71.         [MarshalAs(UnmanagedType.I2)]
  72.         public Int16 Flag;
  73.  
  74.         /// <summary>
  75.         /// Resource name of the associated sound, if any
  76.         /// </summary>
  77.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
  78.         public string SoundRessourceName;
  79.  
  80.         /// <summary>
  81.         /// Volume variance.
  82.         /// </summary>
  83.         [MarshalAs(UnmanagedType.I4)]
  84.         public int VolumeVariance;
  85.  
  86.         /// <summary>
  87.         /// Pitch variance.
  88.         /// </summary>
  89.         [MarshalAs(UnmanagedType.I4)]
  90.         public int PitchVariance;
  91.  
  92.         /// <summary>
  93.         /// Offset of this string relative to the strings section
  94.         /// </summary>
  95.         [MarshalAs(UnmanagedType.I4)]
  96.         public int StringOffset;
  97.  
  98.         /// <summary>
  99.         /// length of this string
  100.         /// </summary>
  101.         [MarshalAs(UnmanagedType.I4)]
  102.         public int StringLength;
  103.     }
  104.     #endregion
  105.  
  106.     class TLKString
  107.     {
  108.         public TLKV1Entry Entry;
  109.         public string Text;
  110.         public TLKString(TLKV1Entry e, string t)
  111.         {
  112.             Entry = e;
  113.             Text = t;
  114.         }
  115.     }
  116.  
  117.     static class Tools
  118.     {
  119.         public static T ByteArrayToStructure<T>(byte[] bytes) where T : struct
  120.         {
  121.             GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  122.             T stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
  123.                 typeof(T));
  124.             handle.Free();
  125.             return stuff;
  126.         }
  127.     }
  128. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top