/// <summary>
        /// Translates a stringID into an index into the global debug strings array.
        /// </summary>
        /// <param name="id">The StringID to translate.</param>
        /// <returns>The index of the string in the global debug strings array.</returns>
        public int StringIDToIndex(StringID id)
        {
            int closestSetIndex = ListSearching.BinarySearch<short>(_setsByID.Keys, id.Set);
            if (closestSetIndex < 0)
            {
                // BinarySearch returns the bitwise complement of the index of the next highest value if not found
                // So, use the set that comes before it...
                closestSetIndex = ~closestSetIndex - 1;
                if (closestSetIndex < 0)
                    return id.Value; // No previous set defined - just return the handle
            }

            // If the index falls outside the set's min value, then put it into the previous set
            if (id.Index < _setsByID.Values[closestSetIndex].MinIndex)
            {
                closestSetIndex--;
                if (closestSetIndex < 0)
                    return id.Value;
            }

            // Calculate the array index by subtracting the value of the first ID in the set
            // and then adding the index in the global array of the set's first string
            StringIDSet set = _setsByID.Values[closestSetIndex];
            StringID firstId = new StringID(set.ID, set.MinIndex);
            return id.Value - firstId.Value + set.GlobalIndex;
        }
        /// <summary>
        /// Translates a string index into a stringID which can be written to the file.
        /// </summary>
        /// <param name="index">The index of the string in the global strings array.</param>
        /// <returns>The stringID associated with the index.</returns>
        public StringID IndexToStringID(int index)
        {
            // Determine which set the index belongs to by finding the set with the closest global index that comes before it
            int closestSetIndex = ListSearching.BinarySearch<int>(_setsByGlobalIndex.Keys, index);
            if (closestSetIndex < 0)
            {
                // BinarySearch returns the bitwise complement of the index of the next highest value if not found
                // So negate it and subtract 1 to get the closest global index that comes before it
                closestSetIndex = ~closestSetIndex - 1;
                if (closestSetIndex < 0)
                    return new StringID(index); // No previous set defined - just return the index
            }

            // Calculate the StringID by subtracting the set's global array index
            // and then adding the value of the first stringID in the set
            StringIDSet set = _setsByGlobalIndex.Values[closestSetIndex];
            StringID firstId = new StringID(set.ID, set.MinIndex);
            return new StringID(index - set.GlobalIndex + firstId.Value);
        }
        /// <summary>
        /// Translates a stringID into an index into the global debug strings array.
        /// </summary>
        /// <param name="id">The StringID to translate.</param>
        /// <returns>The index of the string in the global debug strings array.</returns>
        public int StringIDToIndex(StringID id)
        {
            foreach (StringIDModifier modifier in _modifiers)
            {
                bool modify = false;

                if (modifier.IsGreaterThan)
                    modify = (id.Value > modifier.Identifier);
                else
                    modify = (id.Value < modifier.Identifier);

                if (modify)
                {
                    if (modifier.IsAddition)
                        return id.Value + modifier.Modifier;
                    else
                        return id.Value - modifier.Modifier;
                }
            }
            return id.Value;
        }
Example #4
0
 /// <summary>
 /// Constructs a new Locale.
 /// </summary>
 /// <param name="id">The locale's stringID.</param>
 /// <param name="value">The localized string.</param>
 public Locale(StringID id, string value)
 {
     ID = id;
     Value = value;
 }