/// <summary>
        /// Gets the value of a string in a given language.
        /// </summary>
        /// <param name="str">The string.</param>
        /// <param name="language">The language.</param>
        /// <returns>The value of the string, or <c>null</c> if the string is not available.</returns>
        public string GetString(LocalizedString str, GameLanguage language)
        {
            var offset = str.Offsets[(int)language];
            if (offset < 0 || offset >= Data.Length)
                return null; // String not available

            var length = GetStringLength(offset);
            return Encoding.UTF8.GetString(Data, offset, length);
        }
Exemple #2
0
        public override bool Execute(List<string> args)
        {
            if (args.Count != 3)
                return false;

            GameLanguage language;
            if (!ArgumentParser.ParseLanguage(args[0], out language))
                return false;

            // Look up the stringID that was passed in
            var stringIdStr = args[1];
            var stringIdIndex = Info.StringIDs.Strings.IndexOf(stringIdStr);
            if (stringIdIndex < 0)
            {
                Console.WriteLine("Unable to find stringID \"{0}\".", stringIdStr);
                return true;
            }
            var stringId = Info.StringIDs.GetStringID(stringIdIndex);
            if (stringId == StringID.Null)
            {
                Console.WriteLine("Failed to resolve the stringID.");
                return true;
            }
            var newValue = ArgumentParser.Unescape(args[2]);

            // Look up or create a localized string entry
            var localizedStr = Definition.Strings.FirstOrDefault(s => s.StringID == stringId);
            var added = false;
            if (localizedStr == null)
            {
                // Add a new string
                localizedStr = new LocalizedString { StringID = stringId, StringIDStr = stringIdStr };
                Definition.Strings.Add(localizedStr);
                added = true;
            }

            // Save the tag data
            Definition.SetString(localizedStr, language, newValue);

            if (added)
                Console.WriteLine("String added successfully.");
            else
                Console.WriteLine("String changed successfully.");

            return true;
        }
        /// <summary>
        /// Sets the value of a string for a given language.
        /// </summary>
        /// <param name="str">The string.</param>
        /// <param name="language">The language.</param>
        /// <param name="newValue">The new value. Can be <c>null</c>.</param>
        public void SetString(LocalizedString str, GameLanguage language, string newValue)
        {
            // Replace the string
            var offset = str.Offsets[(int)language];
            if (offset < 0 || offset >= Data.Length)
                offset = Data.Length; // Add the string at the end
            var oldLength = GetStringLength(offset);
            var bytes = (newValue != null) ? Encoding.UTF8.GetBytes(newValue) : new byte[0];
            if (bytes.Length > 0 && offset == Data.Length)
            {
                // If it's a new string, null-terminate it
                var nullTerminated = new byte[bytes.Length + 1];
                Buffer.BlockCopy(bytes, 0, nullTerminated, 0, bytes.Length);
                bytes = nullTerminated;
            }
            Data = ArrayUtil.Replace(Data, offset, oldLength, bytes);

            // Update string offsets
            str.Offsets[(int)language] = (bytes.Length > 0) ? offset : -1;
            var sizeDelta = bytes.Length - oldLength;
            foreach (var adjustStr in Strings)
            {
                for (var i = 0; i < adjustStr.Offsets.Length; i++)
                {
                    if (adjustStr.Offsets[i] > offset)
                        adjustStr.Offsets[i] += sizeDelta;
                }
            }
        }