/// <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;
         }
     }
 }
        /// <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);
        }
예제 #3
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.Error.WriteLine("Unable to find stringID \"{0}\".", stringIdStr);
                return true;
            }
            var stringId = _info.StringIds.GetStringId(stringIdIndex);
            if (stringId == StringId.Null)
            {
                Console.Error.WriteLine("Failed to resolve the stringID.");
                return true;
            }
            var newValue = ArgumentParser.Unescape(args[2]);

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

            // Save the tag data
            _unic.SetString(localizedStr, language, newValue);
            using (var stream = _info.OpenCacheReadWrite())
                _info.Serializer.Serialize(new TagSerializationContext(stream, _info.Cache, _tag), _unic);

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