示例#1
0
        protected override bool ProcessMarkupHandler(ProjectLayoutElement zElement, FormattedTextData zData, FormattedTextProcessData zProcessData,
                                                     Graphics zGraphics)
        {
            var arrayComponents = m_sVariable.Split(new char[] { ';' });

            var fMarkupXOffset      = 0f;
            var fMarkupYOffset      = 0f;
            var fMarkupHeightAdjust = 0f;
            var fMarkupYWidthAdjust = 0f;

            m_fAdditionalVerticalPixels = 0f;

            if (arrayComponents.Length > 0)
            {
                m_zBrush = new SolidBrush(ProjectLayoutElement.TranslateColorString(arrayComponents[0], zElement.opacity));

                if (arrayComponents.Length == 2)
                {
                    ParseUtil.ParseFloat(arrayComponents[1], out m_fAdditionalVerticalPixels);
                }
                else if (arrayComponents.Length > 4)
                {
                    ParseUtil.ParseFloat(arrayComponents[1], out fMarkupXOffset);
                    ParseUtil.ParseFloat(arrayComponents[2], out fMarkupYOffset);
                    ParseUtil.ParseFloat(arrayComponents[3], out m_fAdditionalHorizontalPixels);
                    ParseUtil.ParseFloat(arrayComponents[4], out m_fAdditionalVerticalPixels);
                }
            }

            m_fXOffset = zProcessData.CurrentXOffset + fMarkupXOffset;
            m_fYOffset = zProcessData.CurrentYOffset + fMarkupYOffset;
            return(true);
        }
示例#2
0
        public object Deserialize(StructuredText storage)
        {
            RenderProfile prof = new RenderProfile();

            prof.FontName    = storage.Get("font-name", "Courier New");
            prof.CJKFontName = storage.Get("cjk-font-name",
                                           storage.Get("japanese-font-name",
                                                       storage.Get("chinese-font-name", "Courier New")));
            prof.FontSize                = ParseUtil.ParseFloat(storage.Get("font-size"), 10.0f);
            prof.LineSpacing             = ParseUtil.ParseInt(storage.Get("line-spacing"), 0);
            prof.UseClearType            = ParseUtil.ParseBool(storage.Get("clear-type"), false);
            prof.EnableBoldStyle         = ParseUtil.ParseBool(storage.Get("enable-bold-style"), true);
            prof.ForceBoldStyle          = ParseUtil.ParseBool(storage.Get("force-bold-style"), false);
            prof.ForeColor               = ParseUtil.ParseColor(storage.Get("text-color"), Color.FromKnownColor(KnownColor.WindowText));
            prof.BackColor               = ParseUtil.ParseColor(storage.Get("back-color"), Color.FromKnownColor(KnownColor.Window));
            prof.ImageStyle              = ParseUtil.ParseEnum <ImageStyle>(storage.Get("back-style"), ImageStyle.Center);
            prof.BackgroundImageFileName = storage.Get("back-image", "");

            prof.ESColorSet = new EscapesequenceColorSet();
            string escolor = storage.Get("escape-sequence-color");

            if (escolor != null)
            {
                prof.ESColorSet.Load(escolor);
            }
            prof.DarkenEsColorForBackground = ParseUtil.ParseBool(storage.Get("darken-escolor-for-background"), true);

            return(prof);
        }
示例#3
0
    /// <summary>
    /// 解析CameraShake事件字符串
    /// "camera_shake 2 0 1.5,0.3 4 0.2"
    /// </summary>
    static public void ParseShakeEvent(string evt, out CameraShake.ShakeType shakeType,
                                       out CameraShake.NoiseType noiseType, out Vector3 moveExt,
                                       out float speed,
                                       out float duration)
    {
        // 参数使用空白分割
        var args = evt.Split(' ');

        try {
            // 参数0是指令参数,通常为camera_shake
            shakeType = (CameraShake.ShakeType)ParseUtil.ParseInt(args [1]);
            noiseType = (CameraShake.NoiseType)ParseUtil.ParseInt(args [2]);
            moveExt   = ParseUtil.ParseVector2(args [3]);
            speed     = ParseUtil.ParseFloat(args [4]);
            duration  = ParseUtil.ParseFloat(args [5]);
        }
        catch (System.Exception ex) {
            shakeType = CameraShake.ShakeType.Constant;
            noiseType = CameraShake.NoiseType.Perlin;
            moveExt   = default(Vector3);
            speed     = 0f;
            duration  = 0f;
            Debug.LogError($"解析CameraShake事件字符串失败:{evt}");
        }
    }
示例#4
0
 protected override bool ProcessMarkupHandler(ProjectLayoutElement zElement, FormattedTextData zData, FormattedTextProcessData zProcessData, Graphics zGraphics)
 {
     float fXOffset;
     if (ParseUtil.ParseFloat(m_sVariable, out fXOffset))
     {
         m_fPreviousOffset = zProcessData.CurrentXOffset;
         zProcessData.CurrentXOffset = fXOffset;
     }
     return false;
 }
示例#5
0
        public bool VerifyFloatParse(string sInput, float fExpectedValue)
        {
            float fValue;

            if (!ParseUtil.ParseFloat(sInput, out fValue))
            {
                return(false);
            }
            Assert.AreEqual(fExpectedValue, fValue);
            return(true);
        }
示例#6
0
        public override bool ProcessMarkup(ProjectLayoutElement zElement, FormattedTextData zData, FormattedTextProcessData zProcessData, Graphics zGraphics)
        {
            float fNewSize;

            if (ParseUtil.ParseFloat(m_sVariable, out fNewSize) && fNewSize > 0)
            {
                m_zPreviousFont = zProcessData.Font;
                zProcessData.SetFont(new Font(zProcessData.Font.FontFamily, fNewSize, zProcessData.Font.Style), zGraphics);
            }
            return(false);
        }
示例#7
0
        public override bool ProcessMarkup(ProjectLayoutElement zElement, FormattedTextData zData, FormattedTextProcessData zProcessData, Graphics zGraphics)
        {
            float fLineHeight;

            if (ParseUtil.ParseFloat(m_sVariable, out fLineHeight))
            {
                m_fPreviousLineHeight          = zProcessData.CurrentLineHeight;
                zProcessData.CurrentLineHeight = fLineHeight;
            }
            return(false);
        }
        /// <summary>
        /// Processes the image markup and if the image is valid associates it with the current process data.
        /// </summary>
        /// <param name="zElement"></param>
        /// <param name="zData"></param>
        /// <param name="zProcessData"></param>
        /// <param name="zGraphics"></param>
        /// <returns>false -  The BackgroundImageMarkup.Render is called as part of a TextMarkup</returns>
        protected override bool ProcessMarkupHandler(ProjectLayoutElement zElement, FormattedTextData zData, FormattedTextProcessData zProcessData, Graphics zGraphics)
        {
            var arrayComponents = m_sVariable.Split(new char[] { ';' });

            if (1 > arrayComponents.Length)
            {
                return(false);
            }

            LineNumber = zProcessData.CurrentLine;

            m_sImageFile = arrayComponents[0];
            m_colorImage = zProcessData.ImageColor;

            var zBmp = LoadImage(zElement);

            m_fXOffset = zProcessData.CurrentXOffset;
            m_fYOffset = zProcessData.CurrentYOffset;

            if (null != zBmp)
            {
                switch (arrayComponents.Length)
                {
                case 1:
                    m_nWidth   = zBmp.Width;
                    m_nHeight  = zBmp.Height;
                    TargetRect = new RectangleF(zProcessData.CurrentX, zProcessData.CurrentY, 0, 0);
                    return(true);

                case 5:
                {
                    if (ParseUtil.ParseFloat(arrayComponents[1], out m_fXOffset) &&
                        ParseUtil.ParseFloat(arrayComponents[2], out m_fYOffset) &&
                        int.TryParse(arrayComponents[3], out m_nWidth) &&
                        int.TryParse(arrayComponents[4], out m_nHeight))
                    {
                        TargetRect = new RectangleF(zProcessData.CurrentX, zProcessData.CurrentY, 0, 0);
                        return(true);
                    }
                }
                break;
                }
            }

            return(false);
        }
        /// <summary>
        /// Translates a font string to a font
        /// </summary>
        /// <param name="fontString">The font string to translate</param>
        /// <returns>The font, otherwise null on error</returns>
        public static Font TranslateFontString(string fontString)
        {
            var   arraySplit = fontString.Split(new char[] { ';' });
            float fFontSize;

            if (6 != arraySplit.Length || !ParseUtil.ParseFloat(arraySplit[1], out fFontSize))
            {
                return(null);
            }
            return(FontLoader.GetFont(
                       arraySplit[0],
                       fFontSize,
                       (arraySplit[2].Equals("1") ? FontStyle.Bold : FontStyle.Regular) |
                       (arraySplit[3].Equals("1") ? FontStyle.Underline : FontStyle.Regular) |
                       (arraySplit[4].Equals("1") ? FontStyle.Italic : FontStyle.Regular) |
                       (arraySplit[5].Equals("1") ? FontStyle.Strikeout : FontStyle.Regular)));
        }
示例#10
0
        public override bool ProcessMarkup(ProjectLayoutElement zElement, FormattedTextData zData, FormattedTextProcessData zProcessData,
                                           Graphics zGraphics)
        {
            var arrayComponents = m_sVariable.Split(new char[] { ';' });

            if (arrayComponents.Length > 0)
            {
                m_zBrush = new SolidBrush(ProjectLayoutElement.TranslateColorString(arrayComponents[0], zElement.opacity));

                if (arrayComponents.Length > 1)
                {
                    ParseUtil.ParseFloat(arrayComponents[1], out m_fAdditionalPixels);
                }
            }

            m_fXOffset = zProcessData.CurrentXOffset;
            m_fYOffset = zProcessData.CurrentYOffset;
            return(true);
        }
示例#11
0
    static public void ParseBackgroundFadeEvent(string evt, out float fadeInTime,
                                                out float afterInDelayTime,
                                                out float fadeOutTime, out float afterOutDelayTime)
    {
        // 参数使用空白分割
        var args = evt.Split(' ');

        try {
            // 参数0是指令参数,通常为bgfade
            fadeInTime        = ParseUtil.ParseFloat(args [1]);
            afterInDelayTime  = ParseUtil.ParseFloat(args [2]);
            fadeOutTime       = ParseUtil.ParseFloat(args [3]);
            afterOutDelayTime = ParseUtil.ParseFloat(args [4]);
        }
        catch (Exception e) {
            fadeInTime        = 0.5f;
            afterInDelayTime  = 0.2f;
            fadeOutTime       = 0.3f;
            afterOutDelayTime = 0f;
            Debug.LogError($"解析BackgroundFade事件字符串失败:{ evt } {e.Message}");
        }
    }
示例#12
0
        private static void UpdateElementField(ProjectLayoutElement zElement, PropertyInfo zPropertyInfo, string sValue)
        {
            var zMethod = zPropertyInfo.GetSetMethod();

            if (!string.IsNullOrEmpty(sValue))
            {
                if (zPropertyInfo.PropertyType == typeof(string))
                {
                    zMethod.Invoke(zElement, new object[] { sValue });
                }
                else if (zPropertyInfo.PropertyType == typeof(float))
                {
                    float fValue;
                    if (ParseUtil.ParseFloat(sValue, out fValue))
                    {
                        zMethod.Invoke(zElement, new object[] { fValue });
                    }
                }
                else if (zPropertyInfo.PropertyType == typeof(bool))
                {
                    bool bValue;
                    if (bool.TryParse(sValue, out bValue))
                    {
                        zMethod.Invoke(zElement, new object[] { bValue });
                    }
                }
                else if (zPropertyInfo.PropertyType == typeof(Int32))
                {
                    int nValue;
                    if (int.TryParse(sValue, out nValue) ||
                        int.TryParse(sValue.Replace("0x", string.Empty), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out nValue))
                    {
                        zMethod.Invoke(zElement, new object[] { nValue });
                    }
                }
            }
        }
示例#13
0
        protected override bool ProcessMarkupHandler(ProjectLayoutElement zElement, FormattedTextData zData, FormattedTextProcessData zProcessData, Graphics zGraphics)
        {
            var arrayComponents = m_sVariable.Split(new char[] { ';' });

            if (1 > arrayComponents.Length)
            {
                return(false);
            }
            LineNumber = zProcessData.CurrentLine;

            m_sImageFile = arrayComponents[0];

            m_zBmp = ImageCache.LoadCustomImageFromCache(m_sImageFile, zElement);

            if (null == m_zBmp ||
                m_zBmp.Width == 0 ||
                m_zBmp.Height == 0)
            {
                return(false);
            }

            var fLineHeightPercent = -1f;

            // SOOOOO much duplication
            switch (arrayComponents.Length)
            {
            case 1:     // <img=[filename]>
                fLineHeightPercent = 1.0f;
                TargetRect         = new RectangleF(zProcessData.CurrentX, zProcessData.CurrentY, m_zBmp.Width, m_zBmp.Height);
                break;

            case 2:     // <img=[filename];[percent]>
                ParseUtil.ParseFloat(arrayComponents[1], out fLineHeightPercent);
                TargetRect = new RectangleF(zProcessData.CurrentX, zProcessData.CurrentY, m_zBmp.Width, m_zBmp.Height);
                break;

            case 3:     // <img=[filename];[xoffset];[yoffset]>
                if (ParseUtil.ParseFloat(arrayComponents[1], out m_fXOffset) &&
                    ParseUtil.ParseFloat(arrayComponents[2], out m_fYOffset))
                {
                    TargetRect = new RectangleF(zProcessData.CurrentX, zProcessData.CurrentY, m_zBmp.Width, m_zBmp.Height);
                }
                break;

            case 4:     // <img=[filename];[percent];[xoffset];[yoffset]>
                ParseUtil.ParseFloat(arrayComponents[1], out fLineHeightPercent);
                if (ParseUtil.ParseFloat(arrayComponents[2], out m_fXOffset) &&
                    ParseUtil.ParseFloat(arrayComponents[3], out m_fYOffset))
                {
                    TargetRect = new RectangleF(zProcessData.CurrentX, zProcessData.CurrentY, m_zBmp.Width, m_zBmp.Height);
                }
                break;

            case 5:     // <img=[filename];[xoffset];[yoffset];[width];[height]>
            {
                int nWidth;
                int nHeight;
                if (ParseUtil.ParseFloat(arrayComponents[1], out m_fXOffset) &&
                    ParseUtil.ParseFloat(arrayComponents[2], out m_fYOffset) &&
                    int.TryParse(arrayComponents[3], out nWidth) &&
                    int.TryParse(arrayComponents[4], out nHeight))
                {
                    TargetRect = new RectangleF(zProcessData.CurrentX, zProcessData.CurrentY, nWidth, nHeight);
                }
            }
            break;
            }

            if (TargetRect == RectangleF.Empty)
            {
                return(false);
            }

            m_fXOffset += zProcessData.CurrentXOffset;
            m_fYOffset += zProcessData.CurrentYOffset;

            var fAspectRatio = (float)m_zBmp.Width / (float)m_zBmp.Height;

            if (-1f != fLineHeightPercent)
            {
                var fNewHeight = fLineHeightPercent * (zProcessData.FontHeight == 0f ? 1f : (float)zProcessData.FontHeight);
                var fNewWidth  = fNewHeight * fAspectRatio;
                TargetRect = new RectangleF(zProcessData.CurrentX, zProcessData.CurrentY, fNewWidth, fNewHeight);
            }

            // newline eval
            if (TargetRect.Width + TargetRect.X > zElement.width)
            {
                zProcessData.MoveToNextLine(zElement);
                TargetRect = new RectangleF(0, zProcessData.CurrentY, TargetRect.Width, TargetRect.Height);
            }

            // cap off excessively wide images
            if (TargetRect.Width + TargetRect.X > zElement.width)
            {
                var fNewHeight = zElement.width / fAspectRatio;
                TargetRect = new RectangleF(TargetRect.X, TargetRect.Y, zElement.width, fNewHeight);
            }

            // Center the image on the line based on font height or line height (todo figure out which...)
            TargetRect = new RectangleF(TargetRect.X,
                                        TargetRect.Y + (((float)zProcessData.FontHeight - (float)TargetRect.Height) / 2f), TargetRect.Width, TargetRect.Height);

            zProcessData.CurrentX += TargetRect.Width;

            return(true);
        }
示例#14
0
        /// <summary>
        /// Translates the string representing the element. (also handles any nodraw text input)
        /// </summary>
        /// <param name="zDeck"></param>
        /// <param name="sRawString"></param>
        /// <param name="nCardIndex"></param>
        /// <param name="zDeckLine"></param>
        /// <param name="zElement"></param>
        /// <returns></returns>
        protected override ElementString TranslateToElementString(Deck zDeck, string sRawString, int nCardIndex, DeckLine zDeckLine, ProjectLayoutElement zElement)
        {
#warning Investigate using method references instead of anonymous methods (optimization/code easier to read)

            var listLine = zDeckLine.LineColumns;
            var sOutput  = sRawString;

            sOutput = sOutput.Replace("#empty", string.Empty);

            var zElementString = new ElementString();

            // TODO: maybe move these into classes so this isn't one mammoth blob

            LogTranslation(zElement, sOutput);

            // Translate card variables (non-reference information
            // Groups
            //     1    2    3   4   5
            // @"(.*)(!\[)(.+?)(\])(.*)"
            Func <Match, string> funcCardVariableProcessor =
                (zMatch =>
            {
                string sDefineValue;
                var sKey = zMatch.Groups[3].ToString().ToLower();

                // NOTE: if this expands into more variables move all this into some other method and use a dictionary lookup
                if (sKey.Equals("cardindex"))
                {
                    sDefineValue = (nCardIndex + 1).ToString();
                }
                else if (sKey.Equals("deckindex"))
                {
                    sDefineValue = (zDeckLine.RowSubIndex + 1).ToString();
                }
                else if (sKey.Equals("cardcount"))
                {
                    sDefineValue = zDeck.CardCount.ToString();
                }
                else if (sKey.Equals("elementname"))
                {
                    sDefineValue = zElement.name;
                }
                else
                {
                    IssueManager.Instance.FireAddIssueEvent("Bad card variable: " + sKey);
                    sDefineValue = "[BAD NAME: " + sKey + "]";
                }

                return(zMatch.Groups[1] + sDefineValue + zMatch.Groups[5]);
            });

            // Translate named items (column names / defines)
            //Groups
            //    1    2    3   4   5
            //@"(.*)(@\[)(.+?)(\])(.*)"
            Func <Match, string> funcDefineProcessor =
                zMatch =>
            {
                int    nIndex;
                string sDefineValue;
                var    sKey = zMatch.Groups[3].ToString();

                // check the key for define parameters
                var arrayParams = sKey.Split(new char[] { ',' });
                if (arrayParams.Length > 1)
                {
                    sKey = arrayParams[0];
                }

                sKey = sKey.ToLower();

                if (DictionaryDefines.TryGetValue(sKey, out sDefineValue))
                {
                }
                else if (DictionaryColumnNameToIndex.TryGetValue(sKey, out nIndex))
                {
                    sDefineValue = (nIndex >= listLine.Count ? string.Empty : (listLine[nIndex] ?? "").Trim());
                }
                else
                {
                    IssueManager.Instance.FireAddIssueEvent("Bad reference name: " + sKey);
                    sDefineValue = "[BAD NAME: " + sKey + "]";
                }
                if (arrayParams.Length > 1)
                {
                    for (int nIdx = 1; nIdx < arrayParams.Length; nIdx++)
                    {
                        sDefineValue = sDefineValue.Replace("{" + nIdx + "}", arrayParams[nIdx]);
                    }
                }
                var result = zMatch.Groups[1] + sDefineValue + zMatch.Groups[5];
                // perform the #empty replace every time a define is unwrapped
                return(result.Replace("#empty", string.Empty));
            };

            // Translate substrings (column names / defines)
            //Groups
            //    1  2    3    4  5    6  7    8   9
            //@"(.*)(%\[)(.+?)(,)(\d+)(,)(\d+)(\])(.*)
            Func <Match, string> funcDefineSubstringProcessor =
                zMatch =>
            {
                var sValue = zMatch.Groups[3].ToString();
                int nStartIdx;
                int nLength;
                if (!int.TryParse(zMatch.Groups[5].ToString(), out nStartIdx) ||
                    !int.TryParse(zMatch.Groups[7].ToString(), out nLength))
                {
                    sValue = "[Invalid substring parameters]";
                }
                else
                {
                    sValue = sValue.Length >= nStartIdx + nLength
                            ? sValue.Substring(nStartIdx, nLength)
                            : string.Empty;
                }


                var result = zMatch.Groups[1] + sValue + zMatch.Groups[9];
                // perform the #empty replace every time a define is unwrapped
                return(result.Replace("#empty", string.Empty));
            };

            // define and define substring processing
            sOutput = LoopTranslationMatchMap(sOutput, zElement,
                                              new Dictionary <Regex, Func <Match, string> >
            {
                { s_regexColumnVariable, funcDefineProcessor },
                { s_regexColumnVariableSubstring, funcDefineSubstringProcessor },
                { s_regexCardVariable, funcCardVariableProcessor }
            });


            // Translate card counter/index
            // Groups
            //     1   2    3  4    5  6    7  8   9
            //(@"(.*)(##)(\d+)(;)(\d+)(;)(\d+)(#)(.*)");
            sOutput = LoopTranslateRegex(s_regexCardCounter, sOutput, zElement,
                                         (zMatch =>
            {
                var nStart = Int32.Parse(zMatch.Groups[3].ToString());
                var nChange = Int32.Parse(zMatch.Groups[5].ToString());
                var nLeftPad = Int32.Parse(zMatch.Groups[7].ToString());

                return(zMatch.Groups[1] +
                       // nIndex is left as is (not adding 1)
                       (nStart + (nCardIndex * nChange)).ToString(CultureInfo.InvariantCulture)
                       .PadLeft(nLeftPad, '0') +
                       zMatch.Groups[9]);
            }));

            // Translate sub card counter/index
            // Groups
            //     1   2    3  4    5  6    7  8   9
            //(@"(.*)(#sc;)(\d+)(;)(\d+)(;)(\d+)(#)(.*)");
            sOutput = LoopTranslateRegex(s_regexSubCardCounter, sOutput, zElement,
                                         (zMatch =>
            {
                var nStart = Int32.Parse(zMatch.Groups[3].ToString());
                var nChange = Int32.Parse(zMatch.Groups[5].ToString());
                var nLeftPad = Int32.Parse(zMatch.Groups[7].ToString());

                var nIndex = zDeckLine.RowSubIndex;

                return(zMatch.Groups[1] +
                       // nIndex is left as is (not adding 1)
                       (nStart + (nIndex * nChange)).ToString(CultureInfo.InvariantCulture).PadLeft(nLeftPad, '0') +
                       zMatch.Groups[9]);
            }));

            // Translate random number
            // Groups
            //    1  2         3      4  5      6  7
            //@"(.*)(#random;)(-?\d+)(;)(-?\d+)(#)(.*)"
            sOutput = LoopTranslateRegex(s_regexRandomNumber, sOutput, zElement,
                                         (zMatch =>
            {
                int nMin;
                int nMax;

                if (!int.TryParse(zMatch.Groups[3].ToString(), out nMin) ||
                    !int.TryParse(zMatch.Groups[5].ToString(), out nMax))
                {
                    return("Failed to parse random min/max");
                }

                if (nMin >= nMax)
                {
                    return("Invalid random specified. Min >= Max");
                }

                // max is not inclusive
                return(zMatch.Groups[1] + CardMakerInstance.Random.Next(nMin, nMax + 1).ToString() +
                       zMatch.Groups[7]);
            }));

            // Translate math (float support)
            // https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
            // Groups
            //   1   2            3                  4         5                           6    7  8
            //@"(.*)(#math;)([+-]?[0-9]*[.,]?[0-9]+)([+\-*/%])([+-]?[0-9]*[.,]?[0-9]+)[;]?(.*?)(#)(.*)"
            sOutput = LoopTranslateRegex(s_regexMath, sOutput, zElement,
                                         (zMatch =>
            {
                var sResult = "";
                if (ParseUtil.ParseFloat(zMatch.Groups[3].ToString(), out var fAValue) &&
                    ParseUtil.ParseFloat(zMatch.Groups[5].ToString(), out var fBValue))
                {
                    try
                    {
                        var sFormat = zMatch.Groups[6].ToString();
                        var bUseFormat = !string.IsNullOrWhiteSpace(sFormat);
                        float fResult = 0;
                        switch (zMatch.Groups[4].ToString()[0])
                        {
                        case '+':
                            fResult = fAValue + fBValue;
                            break;

                        case '-':
                            fResult = fAValue - fBValue;
                            break;

                        case '*':
                            fResult = fAValue * fBValue;
                            break;

                        case '/':
                            if (fBValue == 0)
                            {
                                throw new Exception("Cannot divide by zero.");
                            }
                            fResult = fAValue / fBValue;
                            break;

                        case '%':
                            fResult = fAValue % fBValue;
                            break;
                        }

                        sResult = bUseFormat
                                ? fResult.ToString(sFormat)
                                : fResult.ToString();
                    }
                    catch (Exception e)
                    {
                        Logger.AddLogLine("Math float translator failed: {0}".FormatString(e));
                    }
                }
                return(zMatch.Groups[1] + sResult + zMatch.Groups[8]);
            }));


            // Translate repeat
            // Groups
            //    1  2         3    4  5    6  7
            //@"(.*)(#repeat;)(\d+)(;)(.+?)(#)(.*)"
            sOutput = LoopTranslateRegex(s_regexRepeat, sOutput, zElement,
                                         (zMatch =>
            {
                int nRepeatCount;
                var zBuilder = new StringBuilder();
                if (int.TryParse(zMatch.Groups[3].ToString(), out nRepeatCount))
                {
                    for (var nIdx = 0; nIdx < nRepeatCount; nIdx++)
                    {
                        zBuilder.Append(zMatch.Groups[5].ToString());
                    }
                }
                else
                {
                    Logger.AddLogLine("Unable to parse repeat count: " + zMatch.Groups[3].ToString());
                }

                return(zMatch.Groups[1] + zBuilder.ToString() + zMatch.Groups[7]);
            }));

            // Translate If Logic
            //Groups
            //    1     2    3    4   5
            //@"(.*)(#\()(if.+)(\)#)(.*)");
            Func <Match, string> funcIfProcessor =
                (match =>
            {
                var sLogicResult = TranslateIfLogic(match.Groups[3].ToString());
                return(match.Groups[1] +
                       sLogicResult +
                       match.Groups[5]);
            });

            // Translate Switch Logic
            //Groups
            //    1    2         3    4   5
            //@"(.*)(#\()(switch.+)(\)#)(.*)");
            Func <Match, string> funcSwitchProcessor =
                match =>
            {
                var sLogicResult = TranslateSwitchLogic(match.Groups[3].ToString());
                return(match.Groups[1] +
                       sLogicResult +
                       match.Groups[5]);
            };

            // if / switch processor
            sOutput = LoopTranslationMatchMap(sOutput, zElement,
                                              new Dictionary <Regex, Func <Match, string> >
            {
                { s_regexIfLogic, funcIfProcessor },
                { s_regexSwitchLogic, funcSwitchProcessor }
            });

            var dictionaryOverrideFieldToValue = new Dictionary <string, string>();
            // Override evaluation:
            // Translate card variables (non-reference information
            // Groups
            //     1     2    3    4    5   6
            // @"(.*)(\$\[)(.+?):(.+?)(\])(.*)
            sOutput = LoopTranslateRegex(s_regexElementOverride, sOutput, zElement,
                                         (zMatch =>
            {
                var sField = zMatch.Groups[3].ToString().ToLower();
                var sValue = zMatch.Groups[4].ToString();

                if (IsDisallowedOverrideField(sField))
                {
                    Logger.AddLogLine(
                        "[{1}] override not allowed on element: [{0}]".FormatString(zElement.name, sField));
                }
                // empty override values are discarded (matches reference overrides)
                else if (!string.IsNullOrWhiteSpace(sValue))
                {
                    dictionaryOverrideFieldToValue[sField] = sValue;
                }

                return(zMatch.Groups[1].Value + zMatch.Groups[6].Value);
            }
                                         ));

            zElementString.String = sOutput;
            zElementString.OverrideFieldToValueDictionary = dictionaryOverrideFieldToValue == null
                ? null
                : dictionaryOverrideFieldToValue;
            return(zElementString);
        }
示例#15
0
        public ProjectLayoutElement GetOverrideElement(ProjectLayoutElement zElement, int nCardIndex, List <string> arrayLine, DeckLine zDeckLine)
        {
            Dictionary <string, int> dictionaryOverrideColumns;
            string sNameLower = zElement.name.ToLower();

            DictionaryElementOverrides.TryGetValue(sNameLower, out dictionaryOverrideColumns);
            if (null == dictionaryOverrideColumns)
            {
                return(zElement);
            }

            var zOverrideElement = new ProjectLayoutElement();

            zOverrideElement.DeepCopy(zElement, false);
            zOverrideElement.name = zElement.name;

            foreach (string sKey in dictionaryOverrideColumns.Keys)
            {
                Type         zType     = typeof(ProjectLayoutElement);
                PropertyInfo zProperty = zType.GetProperty(sKey);
                if (null != zProperty && zProperty.CanWrite)
                {
                    MethodInfo zMethod = zProperty.GetSetMethod();
                    int        nOverrideValueColumnIdx = dictionaryOverrideColumns[sKey];
                    if (arrayLine.Count <= nOverrideValueColumnIdx)
                    {
                        continue;
                    }
                    string sValue = arrayLine[nOverrideValueColumnIdx].Trim();

                    // Note: TranslateString maintains an element name based cache, the key is critical to make this translation unique
                    sValue = TranslateString(sValue, nCardIndex, zDeckLine, zOverrideElement, sKey).String;

                    if (!string.IsNullOrEmpty(sValue))
                    {
                        if (zProperty.PropertyType == typeof(string))
                        {
                            zMethod.Invoke(zOverrideElement, new object[] { sValue });
                        }
                        else if (zProperty.PropertyType == typeof(float))
                        {
                            float fValue;
                            if (ParseUtil.ParseFloat(sValue, out fValue))
                            {
                                zMethod.Invoke(zOverrideElement, new object[] { fValue });
                            }
                        }
                        else if (zProperty.PropertyType == typeof(bool))
                        {
                            bool bValue;
                            if (bool.TryParse(sValue, out bValue))
                            {
                                zMethod.Invoke(zOverrideElement, new object[] { bValue });
                            }
                        }
                        else if (zProperty.PropertyType == typeof(Int32))
                        {
                            int nValue;
                            if (int.TryParse(sValue, out nValue))
                            {
                                zMethod.Invoke(zOverrideElement, new object[] { nValue });
                            }
                        }
                    }
                }
            }
            zOverrideElement.InitializeCache(); // any cached items must be recached
            return(zOverrideElement);
        }