SubstituteSpecialColors() public static method

public static SubstituteSpecialColors ( [ input ) : string
input [
return string
Example #1
0
        public static Packet MakeExtAddPlayerName(short nameId, string playerName, string listName, string groupName,
                                                  byte groupRank, bool useFallbacks, bool hasCP437)
        {
            if (playerName == null)
            {
                throw new ArgumentNullException("playerName");
            }
            if (listName == null)
            {
                throw new ArgumentNullException("listName");
            }
            if (groupName == null)
            {
                throw new ArgumentNullException("groupName");
            }
            Packet packet = new Packet(OpCode.ExtAddPlayerName);

            //Logger.Log(LogType.Debug, "Send: MakeExtAddPlayerName({0}, {1}, {2}, {3}, {4})", nameId, playerName, listName, groupName, groupRank);
            WriteI16(nameId, packet.Bytes, 1);
            PacketWriter.WriteString(Color.SubstituteSpecialColors(playerName, useFallbacks), packet.Bytes, 3, hasCP437);
            PacketWriter.WriteString(Color.SubstituteSpecialColors(listName, useFallbacks), packet.Bytes, 67, hasCP437);
            PacketWriter.WriteString(Color.SubstituteSpecialColors(groupName, useFallbacks), packet.Bytes, 131, hasCP437);
            packet.Bytes[195] = groupRank;
            return(packet);
        }
Example #2
0
        /// <summary> Creates a new Message (0x0D) packet. </summary>
        /// <param name="type"> Message type. </param>
        /// <param name="message"> Message. </param>
        /// <param name="useFallbacks"> whether or not to use color fallback codes. </param>
        /// <param name="hasCP437"> Whether client supports extended code page 437 characters. </param>
        public static Packet Message(byte type, string message, bool useFallbacks, bool hasCP437)
        {
            Packet packet = new Packet(OpCode.Message);

            packet.Bytes[1] = type;
            message         = Color.Sys + Color.SubstituteSpecialColors(message, useFallbacks);
            PacketWriter.WriteString(message, packet.Bytes, 2, hasCP437);
            return(packet);
        }
Example #3
0
 static Packet SpawnPacket(Player dst, sbyte id, string name, string skin, Position pos)
 {
     name = Color.SubstituteSpecialColors(name, dst.FallbackColors);
     if (dst.Supports(CpeExt.ExtPlayerList2))
     {
         return(Packet.MakeExtAddEntity2(id, name, skin, pos, dst.HasCP437, dst.supportsExtPositions));
     }
     else
     {
         return(Packet.MakeAddEntity(id, name, pos, dst.HasCP437, dst.supportsExtPositions));
     }
 }
Example #4
0
        /// <summary> Creates a new Kick (0x0E) packet. </summary>
        /// <param name="reason"> Given reason. Only first 64 characters will be sent. May not be null. </param>
        /// <param name="hasCP437"> Whether client supports extended code page 437 characters. </param>
        /// <exception cref="ArgumentNullException"> reason is null </exception>
        public static Packet MakeKick([NotNull] string reason, bool hasCP437)
        {
            if (reason == null)
            {
                throw new ArgumentNullException("reason");
            }
            reason = Color.SubstituteSpecialColors(reason, true);
            Packet packet = new Packet(OpCode.Kick);

            PacketWriter.WriteString(reason, packet.Bytes, 1, hasCP437);
            return(packet);
        }
Example #5
0
        internal static IEnumerable <Packet> MakeWrappedMessage(string prefix, string text, bool appendPrefixToFirstLine)
        {
            if (appendPrefixToFirstLine)
            {
                text = prefix + text;
            }

            /* STEP 1: Split by lines */
            if (text.Contains("&N"))
            {
                bool first = true;
                foreach (string subline in text.Split(NewlineSplitter, StringSplitOptions.None))
                {
                    foreach (Packet p in MakeWrappedMessage(prefix, subline, !first))
                    {
                        yield return(p);
                    }
                    first = false;
                }
                yield break;
            }

            /* STEP 2: Replace special colorcodes */
            text = Color.SubstituteSpecialColors(text);

            /* STEP 3: Remove consecutive colorcodes */
            for (int i = 0; i < text.Length - 3; i++)
            {
                if (text[i] == '&' && IsColorCode(text[i + 1]) && text[i + 2] == '&' && IsColorCode(text[i + 3]))
                {
                    text = text.Substring(0, i) + text.Substring(i + 2);
                    i--;
                }
            }

            /* STEP 4: Split */
            int lastIndex = 0;

            List <string> segments = new List <string>();

            for (int i = 0; i < text.Length; i++)
            {
                if (IsColorCode(text[i]) && i > 0 && text[i - 1] == '&')
                {
                    // split at color codes
                    if (i > 1)
                    {
                        segments.Add(text.Substring(lastIndex, i - lastIndex - 1));
                        lastIndex = i - 1;
                    }
                }
                else if (text[i] == ' ')
                {
                    for ( ; i < text.Length && text[i] == ' '; i++)
                    {
                    }
                    i--;
                    // split at spaces
                    segments.Add(text.Substring(lastIndex, i - lastIndex));
                    lastIndex = i;
                }
            }

            // add remainder of the string
            if (lastIndex != text.Length)
            {
                segments.Add(text.Substring(lastIndex));
            }


            /* STEP 5: Delete empty segments */
            for (int i = segments.Count - 1; i >= 0; i--)
            {
                if (segments[i].Length == 0)
                {
                    segments.RemoveAt(i);
                }
            }


            /* STEP 6: Join segments into strings */
            string        line          = "";
            string        lastColorCode = "";
            List <string> lines         = new List <string>();

            for (int i = 0; i < segments.Count; i++)
            {
                if (line.Length + segments[i].TrimEnd().Length + 1 > 64)
                {
                    // end of line, start new one
                    lines.Add(line);

                    if (segments[i].TrimStart().StartsWith("&"))
                    {
                        lastColorCode = segments[i].Substring(0, 2);
                        line          = prefix + segments[i].TrimStart();
                    }
                    else
                    {
                        line = prefix + lastColorCode + segments[i].TrimStart();
                    }
                }
                else
                {
                    // apending to line
                    if (segments[i].TrimStart().StartsWith("&"))
                    {
                        lastColorCode = segments[i].Substring(0, 2);
                        line         += segments[i];
                    }
                    else
                    {
                        line += segments[i];
                    }
                }
            }

            // last line
            lines.Add(line);


            /* STEP 7: Remove trailing whitespace and colorcodes */
            for (int l = lines.Count - 1; l >= 0; l--)
            {
                int i = lines[l].Length - 1;
                for ( ; i >= 0 && (lines[l][i] == ' ' || lines[l][i] == '&' || IsColorCode(lines[l][i]) && i > 0 && lines[l][i - 1] == '&'); i--)
                {
                }
                if (i == 0)
                {
                    lines.RemoveAt(l);
                }
                else
                {
                    lines[l] = lines[l].Substring(0, i + 1);
                }
            }

            /* STEP 8: DONE */
            foreach (string processedLine in lines)
            {
                yield return(MakeMessage(processedLine));
            }
        }