Exemplo n.º 1
0
        /// <summary>
        /// <para>Replaces current sound effect in a message with a new one</para>
        ///
        /// Throws InvalidOperationException if current sound cannot be replaced
        /// </summary>
        public static void ReplaceInMessageWith(this SoundEffect source, SoundEffect newSound, MessageTable messageTable)
        {
            if (!source.IsReplacableInMessage())
            {
                throw new InvalidOperationException($"Sound effect {source} is not replacable!");
            }

            var replacableAttribute = source.GetAttribute <ReplacableInMessageAttribute>();

            foreach (var messageId in replacableAttribute.MessageIds)
            {
                var message = messageTable.GetMessage(messageId);

                var oldSoundId       = replacableAttribute.SoundId;
                var oldSoundEffect   = (ushort)(oldSoundId & 0x0E00);
                var oldSoundBytes    = new string(new char[] { Convert.ToChar((oldSoundId & 0xFF00) >> 8), Convert.ToChar(oldSoundId & 0xFF) });
                var oldSoundLocation = message.Message.IndexOf(oldSoundBytes);

                if (oldSoundLocation < 0)
                {
                    throw new InvalidProgramException($"Sound effect {source} has invalid sound replacement setup!");
                }

                var newSoundId    = (uint)newSound;
                var newSoundBytes = new string(new char[] {
                    Convert.ToChar(((newSoundId | oldSoundEffect) & 0xFF00) >> 8),
                    Convert.ToChar(newSoundId & 0xFF)
                });

                var newMessage = message.Message.Replace(oldSoundBytes, newSoundBytes);
                message.Message = newMessage;
            }
        }