Beispiel #1
0
    void InstantiateHelpLine(ePlayerAction action, KeyCode keyboardKey)
    {
        HelpLine helpLine = Instantiate(_helpLine, transform);

        helpLine._actionText.text     = action.ToString();
        helpLine._keyBoardText.text   = keyboardKey.ToString();
        helpLine._controllerText.text = "";
    }
        //private static void DecodeLine(Stream stream, out HelpLine line)
        //{
        //    line = null;

        //    // Read text length in bytes.
        //    int textLength = stream.ReadByte();
        //    if (textLength < 0)
        //        throw new EndOfStreamException("Cannot read text length byte.");
        //    byte[] textBytes = new byte[textLength];
        //    stream.ReadFull(textBytes, 0, textBytes.Length);
        //    string text = Graphic437.GetString(textBytes);
        //    line = new HelpLine(text);

        //    // Read byte count of attributes.
        //    int attrLength = reader.ReadByte();
        //    BufferReader attrReader = reader.ReadBuffer(attrLength - 1);
        //    DecodeLineAttributes(line, attrReader);

        //    // Read hyperlinks.
        //    while (!attrReader.IsEOF)
        //    {
        //        DecodeLineHyperlink(line, attrReader);
        //    }
        //}

        private static void DecodeLineAttributes(HelpLine line, BufferReader reader)
        {
            int charIndex = 0;

            for (int chunkIndex = 0; !reader.IsEOF; chunkIndex++)
            {
                TextStyle textStyle = TextStyle.None;

                // Read attribute byte except for the first chunk (for which
                // default attributes are applied).
                if (chunkIndex > 0)
                {
                    byte a = reader.ReadByte();
                    if (a == 0xFF) // marks the beginning of hyperlinks
                    {
                        break;
                    }

                    textStyle = TextStyle.None;
                    if ((a & 1) != 0)
                    {
                        textStyle |= TextStyle.Bold;
                    }
                    if ((a & 2) != 0)
                    {
                        textStyle |= TextStyle.Italic;
                    }
                    if ((a & 4) != 0)
                    {
                        textStyle |= TextStyle.Underline;
                    }
                    if ((a & 0xF8) != 0)
                    {
                        // should exit
                        //System.Diagnostics.Debug.WriteLine(string.Format(
                        //    "Text attribute bits {0:X2} is not recognized and is ignored.",
                        //    a & 0xF8));
                        throw new InvalidDataException("Invalid text attribute");
                    }
                }

                // Read chunk length to apply this attribute to.
                int charCount = reader.ReadByte();
                if (charCount > line.Length - charIndex)
                {
                    // TODO: issue warning
                    charCount = line.Length - charIndex;
                }
                if (textStyle != TextStyle.None)
                {
                    for (int j = 0; j < charCount; j++)
                    {
                        line.Attributes[charIndex + j] = new TextAttribute(textStyle, null);
                    }
                }
                charIndex += charCount;
            }
        }
Beispiel #3
0
 /// <summary>
 /// Formats a help line as HTML and returns the HTML source.
 /// </summary>
 /// <remarks>
 /// This method produces properly structured HTML. That is, it avoids
 /// markup such as
 ///
 ///   ...<b>...<i>...</b>...</i>...
 ///
 /// The generated HTML is not the most compact possible, but is quite
 /// compact in practice.
 ///
 /// For a formal discussion about unpaired tags, see
 /// http://www.w3.org/html/wg/drafts/html/master/syntax.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser
 /// </remarks>
 private void FormatLine(StringBuilder html, HelpTopic topic, HelpLine line)
 {
     if (this.FixLinks)
     {
         line = FixLine(line);
     }
     for (int index = 0; index < line.Length;)
     {
         index = FormatLineSegment(html, topic, line, index);
     }
 }
Beispiel #4
0
 private static HelpLine FixLine(HelpLine line)
 {
     TextAttribute[] attributes = new TextAttribute[line.Length];
     for (int i = 0; i < line.Length; i++)
     {
         if (line.Text[i] == '►' &&
             line.Attributes[i].Link != null &&
             (i == line.Length - 1 || line.Attributes[i + 1].Link == null))
         {
             attributes[i] = new TextAttribute(line.Attributes[i].Style, null);
         }
         else
         {
             attributes[i] = line.Attributes[i];
         }
     }
     return(new HelpLine(line.Text, attributes));
 }
        // TODO: compression is topic/database independent, so move them
        // into a separate namespace/class.

        static HelpTopic DecompileTopic(byte[] buffer, char controlCharacter)
        {
            HelpTopic    topic  = new HelpTopic();
            BufferReader reader = new BufferReader(buffer, Graphic437);

            while (!reader.IsEOF)
            {
                HelpLine line = null;
                try
                {
                    DecodeLine(reader, out line);
                }
                catch (Exception)
                {
                    if (line != null)
                    {
                        topic.Lines.Add(line);
                    }
                    throw;
                }

                bool isCommand = true;
                try
                {
                    isCommand = HelpCommandConverter.ProcessCommand(
                        line.Text, controlCharacter, topic);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(string.Format(
                                                           "Unable to process command '{0}': {1}",
                                                           line.Text, ex.Message));
                }

                if (!isCommand)
                {
                    topic.Lines.Add(line);
                }
            }
            return(topic);
        }
        internal static void DecodeLine(BufferReader reader, out HelpLine line)
        {
            line = null;

            // Read text length in bytes.
            int    textLength = reader.ReadByte();
            string text       = reader.ReadFixedLengthString(textLength - 1);

            line = new HelpLine(text);

            // Read byte count of attributes.
            int          attrLength = reader.ReadByte();
            BufferReader attrReader = reader.ReadBuffer(attrLength - 1);

            DecodeLineAttributes(line, attrReader);

            // Read hyperlinks.
            while (!attrReader.IsEOF)
            {
                DecodeLineHyperlink(line, attrReader);
            }
        }
        private static void DecodeLineHyperlink(HelpLine line, BufferReader reader)
        {
            // Read link location.
            int linkStartIndex = reader.ReadByte(); // one-base, inclusive
            int linkEndIndex   = reader.ReadByte(); // one-base, inclusive

            if (linkStartIndex == 0 || linkStartIndex > linkEndIndex)
            {
                throw new InvalidDataException("Invalid link location.");
            }
            if (linkEndIndex > line.Length)
            {
                System.Diagnostics.Debug.WriteLine(string.Format(
                                                       "WARNING: Link end {0} is past line end {1}.",
                                                       linkEndIndex, line.Length));
                linkEndIndex = line.Length;
            }
            //if (linkStartIndex

            // Read NULL-terminated context string.
            string context = reader.ReadNullTerminatedString();

            if (context == "")                        // link is WORD topic index
            {
                int numContext = reader.ReadUInt16(); // 0x8000 | topicIndex
                context = "@L" + numContext.ToString("X4");
            }

            // Add hyperlink to the line.
            HelpUri link = new HelpUri(context);

            for (int j = linkStartIndex; j <= linkEndIndex; j++)
            {
                line.Attributes[j - 1] = new TextAttribute(line.Attributes[j - 1].Style, link);
            }
        }
Beispiel #8
0
        private int FormatLineSegment(StringBuilder html, HelpTopic topic, HelpLine line, int startIndex)
        {
            HelpUri link = line.Attributes[startIndex].Link;

            if (link != null)
            {
                html.AppendFormat("<a href=\"{0}\">", FormatUri(topic, link));
            }

            Stack <TextStyle> openTags = new Stack <TextStyle>();
            int index = startIndex;

            while (index < line.Length && line.Attributes[index].Link == link)
            {
                TextAttribute oldAttrs = (index == startIndex) ?
                                         TextAttribute.Default : line.Attributes[index - 1];
                TextAttribute newAttrs       = line.Attributes[index];
                TextStyle     stylesToAdd    = newAttrs.Style & ~oldAttrs.Style;
                TextStyle     stylesToRemove = oldAttrs.Style & ~newAttrs.Style;

                while (stylesToRemove != TextStyle.None)
                {
                    TextStyle top = openTags.Pop();
                    FormatRemovedStyles(html, top);
                    if ((stylesToRemove & top) != 0)
                    {
                        stylesToRemove &= ~top;
                    }
                    else
                    {
                        stylesToAdd |= top;
                    }
                }

                if ((stylesToAdd & TextStyle.Bold) != 0)
                {
                    html.Append("<b>");
                    openTags.Push(TextStyle.Bold);
                }
                if ((stylesToAdd & TextStyle.Italic) != 0)
                {
                    html.Append("<i>");
                    openTags.Push(TextStyle.Italic);
                }
                if ((stylesToAdd & TextStyle.Underline) != 0)
                {
                    html.Append("<u>");
                    openTags.Push(TextStyle.Underline);
                }

                html.Append(Escape("" + line.Text[index]));
                index++;
            }

            while (openTags.Count > 0)
            {
                FormatRemovedStyles(html, openTags.Pop());
            }
            if (link != null)
            {
                html.Append("</a>");
            }

            return(index);
        }