private void TextSearch()
        {
            string searchTerm = boxSearch.Text.Trim().ToLower();

            if (searchTerm == "")
            {
                return;                   //don't search blank
            }
            int pos = DisplayedString_ListBox.SelectedIndex;

            pos += 1; //search this and 1 forward
            for (int i = 0; i < CleanedStrings.Count; i++)
            {
                int curIndex = (i + pos) % CleanedStrings.Count;
                ME1TalkFile.TLKStringRef node = CleanedStrings[curIndex];

                if (CleanedStrings[curIndex].StringID.ToString().Contains(searchTerm))
                {
                    //ID Search
                    DisplayedString_ListBox.SelectedIndex = curIndex;
                    return;
                }
                else if (CleanedStrings[curIndex].Data != null && CleanedStrings[curIndex].Data.ToLower().Contains(searchTerm))
                {
                    DisplayedString_ListBox.SelectedIndex = curIndex;
                    DisplayedString_ListBox.ScrollIntoView(DisplayedString_ListBox.SelectedItem);
                    return;
                }
            }
            //Not found
            SystemSounds.Beep.Play();
        }
        private static void FindSkipRanges(ME1TalkFile.TLKStringRef sref, List <int> skipRanges)
        {
            var  str         = sref.Data;
            int  startPos    = -1;
            char openingChar = (char)0x00;

            for (int i = 0; i < sref.Data.Length; i++)
            {
                if (startPos < 0 && (sref.Data[i] == '[' || sref.Data[i] == '<' || sref.Data[i] == '{'))
                {
                    startPos    = i;
                    openingChar = sref.Data[i];
                }
                else if (startPos >= 0 && openingChar == '[' && sref.Data[i] == ']') // ui control token
                {
                    var insideStr = sref.Data.Substring(startPos + 1, i - startPos - 1);
                    if (insideStr.StartsWith("Xbox", StringComparison.InvariantCultureIgnoreCase))
                    {
                        skipRanges.Add(startPos);
                        skipRanges.Add(i + 1);
                    }
                    //else
                    //    Debug.WriteLine(insideStr);

                    startPos    = -1;
                    openingChar = (char)0x00;
                }
                else if (startPos >= 0 && openingChar == '<' && sref.Data[i] == '>') //cust token
                {
                    var insideStr = sref.Data.Substring(startPos + 1, i - startPos - 1);
                    if (insideStr.StartsWith("CUSTOM") || insideStr.StartsWith("font") || insideStr.StartsWith("/font") || insideStr.Equals("br", StringComparison.InvariantCultureIgnoreCase))
                    {
                        // custom token. Do not modify it
                        skipRanges.Add(startPos);
                        skipRanges.Add(i + 1);
                    }
                    //else
                    //Debug.WriteLine(insideStr);

                    startPos    = -1;
                    openingChar = (char)0x00;
                }
                else if (startPos >= 0 && openingChar == '{' && sref.Data[i] == '}') // token for powers (?)
                {
                    //var insideStr = sref.Data.Substring(startPos + 1, i - startPos - 1);
                    //Debug.WriteLine(insideStr);
                    // { } brackets are for ui tokens in powers, saves, I think.
                    skipRanges.Add(startPos);
                    skipRanges.Add(i + 1);

                    startPos    = -1;
                    openingChar = (char)0x00;
                }

                // it's nothing.
            }
        }
        private void Evt_AddString(object sender, RoutedEventArgs e)
        {
            var blankstringref = new ME1TalkFile.TLKStringRef(100, 1, "New Blank Line");

            LoadedStrings.Add(blankstringref);
            CleanedStrings.Add(blankstringref);
            DisplayedString_ListBox.SelectedIndex = CleanedStrings.Count() - 1;           //Set focus to new line (which is the last one)
            DisplayedString_ListBox.ScrollIntoView(DisplayedString_ListBox.SelectedItem); //Scroll to last item
            SetNewID();
            FileModified = true;
        }
        /// <summary>
        /// Loads TLK data from a stream. The position must be properly set.
        /// </summary>
        /// <param name="fs"></param>
        public void LoadTlkDataFromStream(Stream fs)
        {
            //Magic: "Tlk " on Little Endian
            EndianReader r = EndianReader.SetupForReading(fs, 0x006B6C54, out var magic);

            r.Position = 0;
            Header     = new TLKHeader(r);

            //DebugTools.PrintHeader(Header);

            /* **************** STEP TWO ****************
             *  -- read and store Huffman Tree nodes --
             */
            /* jumping to the beginning of Huffmann Tree stored in TLK file */
            long pos = r.BaseStream.Position;

            r.BaseStream.Seek(pos + (Header.MaleEntryCount + Header.FemaleEntryCount) * 8, SeekOrigin.Begin);

            CharacterTree = new List <HuffmanNode>();
            for (int i = 0; i < Header.treeNodeCount; i++)
            {
                CharacterTree.Add(new HuffmanNode(r));
            }

            /* **************** STEP THREE ****************
             *  -- read all of coded data into memory --
             */
            byte[] data = new byte[Header.dataLen];
            r.BaseStream.Read(data, 0, data.Length);
            /* and store it as raw bits for further processing */
            Bits = new BitArray(data);

            /* rewind BinaryReader just after the Header
             * at the beginning of TLK Entries data */
            r.BaseStream.Seek(pos, SeekOrigin.Begin);

            /* **************** STEP FOUR ****************
             * -- decode (basing on Huffman Tree) raw bits data into actual strings --
             * and store them in a Dictionary<int, string> where:
             *   int: bit offset of the beginning of data (offset starting at 0 and counted for Bits array)
             *        so offset == 0 means the first bit in Bits array
             *   string: actual decoded string */
            Dictionary <int, string> rawStrings = new Dictionary <int, string>();
            int offset = 0;
            // int maxOffset = 0;
            var builder = new StringBuilder(); //reuse the same stringbuilder to avoid allocations

            while (offset < Bits.Length)
            {
                int key = offset;
                // if (key > maxOffset)
                // maxOffset = key;
                /* read the string and update 'offset' variable to store NEXT string offset */
                string s = GetString(ref offset, builder);
                rawStrings.Add(key, s);
            }

            // Console.WriteLine("Max offset = " + maxOffset);

            /* **************** STEP FIVE ****************
             *         -- bind data to String IDs --
             * go through Entries in TLK file and read it's String ID and offset
             * then check if offset is a key in rawStrings and if it is, then bind data.
             * Sometimes there's no such key, in that case, our String ID is probably a substring
             * of another String present in rawStrings.
             */
            StringRefs = new List <ME1TalkFile.TLKStringRef>();
            for (int i = 0; i < Header.MaleEntryCount + Header.FemaleEntryCount; i++)
            {
                ME1TalkFile.TLKStringRef sref = new ME1TalkFile.TLKStringRef(r, false);
                sref.Index = i;
                if (sref.BitOffset >= 0)
                {
                    if (!rawStrings.ContainsKey(sref.BitOffset))
                    {
                        int    tmpOffset  = sref.BitOffset;
                        string partString = GetString(ref tmpOffset, builder);

                        /* actually, it should store the fullString and subStringOffset,
                         * but as we don't have to use this compression feature,
                         * we will store only the part of string we need */

                        /* int key = rawStrings.Keys.Last(c => c < sref.BitOffset);
                         * string fullString = rawStrings[key];
                         * int subStringOffset = fullString.LastIndexOf(partString);
                         * sref.StartOfString = subStringOffset;
                         * sref.Data = fullString;
                         */
                        sref.Data = partString;
                    }
                    else
                    {
                        sref.Data = rawStrings[sref.BitOffset];
                    }
                }
                StringRefs.Add(sref);
            }
            r.Close();
        }
        /* for sorting */
        private static int CompareTlkStringRef(ME1TalkFile.TLKStringRef strRef1, ME1TalkFile.TLKStringRef strRef2)
        {
            int result = strRef1.StringID.CompareTo(strRef2.StringID);

            return(result);
        }