protected override ElementString TranslateToElementString(string sRawString, int nCardIndex, DeckLine zDeckLine, ProjectLayoutElement zElement)
 {
     using (var engine = new V8ScriptEngine())
     {
         var sScript = GetJavaScript(nCardIndex, zDeckLine, sRawString);
         try
         {
             var sValue = engine.Evaluate(sScript);
             if (sValue is string || sValue is int)
             {
                 return new ElementString()
                 {
                     String = sValue.ToString()
                 };
             }
             else
             {
                 Logger.AddLogLine(sValue.GetType().ToString());
             }
         }
         catch (Exception e)
         {
             Logger.AddLogLine(e.Message);
         }
     }
     return new ElementString()
     {
         String = string.Empty
     };
 }
Example #2
0
        /// <summary>
        /// Translates a file export string for naming a file
        /// </summary>
        /// <param name="sRawString"></param>
        /// <param name="nCardNumber"></param>
        /// <param name="nLeftPad"></param>
        /// <returns></returns>
        public static string TranslateFileNameString(string sRawString, int nCardNumber, int nLeftPad, DeckLine zCurrentPrintLine, Dictionary<string, string> dictionaryDefines,
            Dictionary<string, int> dictionaryColumnNameToIndex, ProjectLayout zLayout)
        {
            string sOutput = sRawString;
            var listLine = zCurrentPrintLine.LineColumns;

            // Translate named items (column names / defines)
            //Groups
            //    1    2    3   4   5
            //@"(.*)(@\[)(.+?)(\])(.*)"
            while (s_regexColumnVariable.IsMatch(sOutput))
            {
                var zMatch = s_regexColumnVariable.Match(sOutput);
                int nIndex;
                string sDefineValue;
                string sKey = zMatch.Groups[3].ToString().ToLower();
                if (dictionaryDefines.TryGetValue(sKey, out sDefineValue))
                {
                    sOutput = zMatch.Groups[1] + sDefineValue.Trim() + zMatch.Groups[5];
                }
                else if (dictionaryColumnNameToIndex.TryGetValue(sKey, out nIndex))
                {
                    sOutput = zMatch.Groups[1] + listLine[nIndex].Trim() + zMatch.Groups[5];
                }
                else
                {
                    sOutput = zMatch.Groups[1] + "[UNKNOWN]" + zMatch.Groups[5];
                }
            }
            // replace ##, #L, Newlines
            sOutput = sOutput.Replace("##", nCardNumber.ToString(CultureInfo.InvariantCulture).PadLeft(nLeftPad, '0')).Replace("#L", zLayout.Name).Replace(Environment.NewLine, String.Empty);

            // last chance: replace unsupported characters (for file name)
            var zBuilder = new StringBuilder();
            foreach (char c in sOutput)
            {
                string sReplace;
                if (s_dictionaryCharReplacement.TryGetValue(c, out sReplace))
                {
                    // quadruple check against bad chars!
                    if (-1 == sReplace.IndexOfAny(DISALLOWED_FILE_CHARS_ARRAY, 0))
                    {
                        zBuilder.Append(sReplace);
                    }
                }
                else
                {
                    if (-1 == c.ToString().IndexOfAny(DISALLOWED_FILE_CHARS_ARRAY))
                    {
                        zBuilder.Append(c);
                    }
                }
            }
            return zBuilder.ToString();
        }
Example #3
0
        public ElementString TranslateString(string sRawString, int nCardIndex, DeckLine zDeckLine,
            ProjectLayoutElement zElement, string sCacheSuffix = "")
        {
            string sCacheKey = zElement.name + sCacheSuffix;
            ElementString zCached;
            if (m_dictionaryElementStringCache.TryGetValue(sCacheKey, out zCached))
            {
                return zCached;
            }

            var zElementString = TranslateToElementString(sRawString, nCardIndex, zDeckLine, zElement);

            if (zElementString.String.Contains("#nodraw"))
            {
                zElementString.DrawElement = false;
            }

            // all translators perform this post replacement operation
            switch ((ElementType)Enum.Parse(typeof(ElementType), zElement.type))
            {
                case ElementType.Text:
                    zElementString.String = zElementString.String.Replace("\\n", Environment.NewLine);
                    zElementString.String = zElementString.String.Replace("\\q", "\"");
                    zElementString.String = zElementString.String.Replace("\\c", ",");
                    zElementString.String = zElementString.String.Replace("&gt;", ">");
                    zElementString.String = zElementString.String.Replace("&lt;", "<");
                    break;
                case ElementType.FormattedText:
                    zElementString.String = zElementString.String.Replace("<c>", ",");
                    zElementString.String = zElementString.String.Replace("<q>", "\"");
                    zElementString.String = zElementString.String.Replace("&gt;", ">");
                    zElementString.String = zElementString.String.Replace("&lt;", "<");
                    break;
            }

            AddStringToTranslationCache(sCacheKey, zElementString);

            return zElementString;
        }
        private string GetJavaScript(int nCardIndex, DeckLine zDeckLine, string sDefintion)
        {
            var zBuilder = new StringBuilder();
            if (string.IsNullOrWhiteSpace(sDefintion))
            {
                return "''";
            }

            AddNumericVar(zBuilder, "deckIndex", (nCardIndex + 1).ToString());
            AddNumericVar(zBuilder, "cardIndex", (zDeckLine.RowSubIndex + 1).ToString());

            foreach (var sKey in DictionaryDefines.Keys)
            {
                AddVar(zBuilder, sKey, DictionaryDefines[sKey]);
            }

            for (int nIdx = 0; nIdx < ListColumnNames.Count; nIdx++)
            {
                AddVar(zBuilder, ListColumnNames[nIdx], zDeckLine.LineColumns[nIdx]);
            }
            zBuilder.Append(sDefintion);
            return zBuilder.ToString();
        }
Example #5
0
 protected abstract ElementString TranslateToElementString(string sRawString, int nCardIndex, DeckLine zDeckLine,
     ProjectLayoutElement zElement);
Example #6
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 (float.TryParse(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;
        }
Example #7
0
        /// <summary>
        /// Translates the string representing the element. (also handles any nodraw text input)
        /// </summary>
        /// <param name="sRawString"></param>
        /// <param name="zDeckLine"></param>
        /// <param name="zElement"></param>
        /// <param name="sCacheSuffix"></param>
        /// <returns></returns>
        protected override ElementString TranslateToElementString(string sRawString, int nCardIndex, DeckLine zDeckLine, ProjectLayoutElement zElement)
        {
            List<string> listLine = zDeckLine.LineColumns;

            string sOutput = sRawString;

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

            var zElementString = new ElementString();

            // Translate card variables (non-reference information
            // Groups
            //     1    2    3   4   5
            // @"(.*)(!\[)(.+?)(\])(.*)"
            Match zMatch;
            while (s_regexCardVariable.IsMatch(sOutput))
            {
                zMatch = s_regexCardVariable.Match(sOutput);
                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
                {
                    IssueManager.Instance.FireAddIssueEvent("Bad card variable: " + sKey);
                    sDefineValue = "[BAD NAME: " + sKey + "]";
                }

                sOutput = zMatch.Groups[1] + sDefineValue + zMatch.Groups[5];
            }

            // Translate named items (column names / defines)
            //Groups
            //    1    2    3   4   5
            //@"(.*)(@\[)(.+?)(\])(.*)"
            while (s_regexColumnVariable.IsMatch(sOutput))
            {
                zMatch = s_regexColumnVariable.Match(sOutput);
                int nIndex;
                string sDefineValue;
                var sKey = zMatch.Groups[3].ToString().ToLower();

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

                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]);
                    }
                }
                sOutput = zMatch.Groups[1] + sDefineValue + zMatch.Groups[5];
            }

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

                sOutput = 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+)(#)(.*)");
            while (s_regexSubCardCounter.IsMatch(sOutput))
            {
                zMatch = s_regexSubCardCounter.Match(sOutput);
                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;

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

            // Translate If Logic
            //Groups
            //    1     2    3    4   5 
            //@"(.*)(#\()(if.+)(\)#)(.*)");
            while (s_regexIfLogic.IsMatch(sOutput))
            {
                zMatch = s_regexIfLogic.Match(sOutput);
                string sLogicResult = TranslateIfLogic(zMatch.Groups[3].ToString());
                sOutput = zMatch.Groups[1] +
                    sLogicResult +
                    zMatch.Groups[5];
            }

            // Translate Switch Logic
            //Groups                  
            //    1     2        3    4   5 
            //@"(.*)(#\()(switch.+)(\)#)(.*)");
            while (s_regexSwitchLogic.IsMatch(sOutput))
            {
                zMatch = s_regexSwitchLogic.Match(sOutput);
                string sLogicResult = TranslateSwitchLogic(zMatch.Groups[3].ToString());
                sOutput = zMatch.Groups[1] +
                    sLogicResult +
                    zMatch.Groups[5];
            }

            zElementString.String = sOutput;

            return zElementString;
        }