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 }; }
/// <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(); }
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(">", ">"); zElementString.String = zElementString.String.Replace("<", "<"); break; case ElementType.FormattedText: zElementString.String = zElementString.String.Replace("<c>", ","); zElementString.String = zElementString.String.Replace("<q>", "\""); zElementString.String = zElementString.String.Replace(">", ">"); zElementString.String = zElementString.String.Replace("<", "<"); 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(); }
/// <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); }
/// <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()); }
public void Setup() { _testDeck = new TestDeck(); _testLine = new DeckLine(new List <string>()); _testElement = new ProjectLayoutElement("testElement"); }
/// <summary> /// Translates the string representing the element. (also handles any nodraw text input) /// </summary> /// <param name="sRawString"></param> /// <param name="nCardIndex"></param> /// <param name="zDeckLine"></param> /// <param name="zElement"></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); }
public ElementString TranslateString(Deck zDeck, string sRawString, int nCardIndex, DeckLine zDeckLine, ProjectLayoutElement zElement, string sCacheSuffix = "") { var sCacheKey = zElement.name + sCacheSuffix; // pull from translated string cache ElementString zCached; if (m_dictionaryElementStringCache.TryGetValue(sCacheKey, out zCached)) { return(zCached); } var zElementString = TranslateToElementString(zDeck, 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(">", ">"); zElementString.String = zElementString.String.Replace("<", "<"); break; case ElementType.FormattedText: // NOTE: never convert \n => <br> here. This will affect file paths that include '\n' (ie. c:\newfile.png) zElementString.String = zElementString.String.Replace("<c>", ","); zElementString.String = zElementString.String.Replace("<q>", "\""); zElementString.String = zElementString.String.Replace(">", ">"); zElementString.String = zElementString.String.Replace("<", "<"); break; } AddStringToTranslationCache(sCacheKey, zElementString); return(zElementString); }
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; }
public void Setup() { _testDeck = new TestDeck(); _testLine = new DeckLine(new List <string>()); _testElement = new ProjectLayoutElement(TEST_ELEMENT_NAME); }
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); }
public void Setup() { _testDeck = new TestDeck(new JavaScriptTranslatorFactory()); _testLine = new DeckLine(new List <string>()); _testElement = new ProjectLayoutElement("testElement"); }
/// <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; }
protected abstract ElementString TranslateToElementString(string sRawString, int nCardIndex, DeckLine zDeckLine, ProjectLayoutElement zElement);
public ProjectLayoutElement GetOverrideElement(ProjectLayoutElement zElement, int nCardIndex, List <string> arrayLine, DeckLine zDeckLine) { Dictionary <string, int> dictionaryOverrideColumns; DictionaryElementToFieldColumnOverrides.TryGetValue(zElement.name.ToLower(), out dictionaryOverrideColumns); var zOverrideElement = new ProjectLayoutElement(); zOverrideElement.DeepCopy(zElement, false); zOverrideElement.name = zElement.name; if (null != dictionaryOverrideColumns) { foreach (var sKey in dictionaryOverrideColumns.Keys) { var zProperty = typeof(ProjectLayoutElement).GetProperty(sKey); if (null != zProperty && zProperty.CanWrite) { 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; UpdateElementField(zOverrideElement, zProperty, sValue); } else { Logger.AddLogLine("Unrecognized data-based override: [{0}] for element: [{1}]".FormatString(sKey, zElement.name)); } } } return(zOverrideElement); }
protected override ElementString TranslateToElementString(Deck zDeck, string sRawString, int nCardIndex, DeckLine zDeckLine, ProjectLayoutElement zElement) { using (var engine = new V8ScriptEngine()) { var hostFunctions = new JavascriptHostFunctions(zElement); engine.AddHostObject("host", Microsoft.ClearScript.HostItemFlags.GlobalMembers, hostFunctions); var sScript = GetJavaScript(nCardIndex, zDeckLine, sRawString); try { var sValue = engine.Evaluate(sScript); if (sValue is string || sValue is int || sValue is double) { return(new ElementString() { String = sValue.ToString(), OverrideFieldToValueDictionary = hostFunctions.dictionaryOverrideFieldToValue }); } else { Logger.AddLogLine(sValue.GetType().ToString()); } } catch (Exception e) { Logger.AddLogLine(e.Message); } } return(new ElementString() { String = string.Empty }); }
protected override ElementString TranslateToElementString(Deck zDeck, 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 }); }