private List <LogInfo> ValidateUISection(ScriptSection section) { // Force parsing of code, bypassing caching by section.GetUICtrls() List <string> lines = section.GetLines(); SectionAddress addr = new SectionAddress(p, section); List <UIControl> uiCtrls = UIParser.ParseRawLines(lines, addr, out List <LogInfo> logs); foreach (UIControl uiCmd in uiCtrls) { switch (uiCmd.Type) { case UIControlType.CheckBox: { Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_CheckBox)); UIInfo_CheckBox info = uiCmd.Info as UIInfo_CheckBox; if (info.SectionName != null) { if (p.Sections.ContainsKey(info.SectionName)) // Only if section exists { logs.AddRange(ValidateCodeSection(p.Sections[info.SectionName])); } } } break; case UIControlType.Button: { Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_Button)); UIInfo_Button info = uiCmd.Info as UIInfo_Button; if (info.SectionName != null) { if (p.Sections.ContainsKey(info.SectionName)) // Only if section exists { logs.AddRange(ValidateCodeSection(p.Sections[info.SectionName])); } } } break; case UIControlType.RadioButton: { Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_RadioButton)); UIInfo_RadioButton info = uiCmd.Info as UIInfo_RadioButton; if (info.SectionName != null) { if (p.Sections.ContainsKey(info.SectionName)) // Only if section exists { logs.AddRange(ValidateCodeSection(p.Sections[info.SectionName])); } } } break; } } return(logs); }
public List <LogInfo> LoadLocalMacroDict(Script p, bool append, string sectionName = "Variables") { if (p.Sections.ContainsKey(sectionName)) { ScriptSection section = p.Sections[sectionName]; // [Variables]'s type is SectionDataType.Lines // Pick key-value only if key is not wrapped by % SectionAddress addr = new SectionAddress(p, section); Dictionary <string, string> dict = Ini.ParseIniLinesIniStyle(section.GetLines()); return(LoadLocalMacroDict(addr, dict, append)); } else { return(new List <LogInfo>()); } }
private List <LogInfo> ValidateCodeSection(ScriptSection section) { // Already processed, so skip if (visitedSections.Contains(section)) { return(new List <LogInfo>()); } // Force parsing of code, bypassing caching by section.GetCodes() List <string> lines = section.GetLines(); SectionAddress addr = new SectionAddress(p, section); List <CodeCommand> codes = CodeParser.ParseStatements(lines, addr, out List <LogInfo> logs); visitedSections.Add(section); InternalValidateCodes(codes, logs); return(logs); }
public Macro(Project project, Variables variables, out List <LogInfo> logs) { macroEnabled = true; logs = new List <LogInfo>(); if (project.MainScript.Sections.ContainsKey("Variables") == false) { macroEnabled = false; logs.Add(new LogInfo(LogState.Info, "Macro not defined")); return; } Dictionary <string, string> varDict = Ini.ParseIniLinesVarStyle(project.MainScript.Sections["Variables"].GetLines()); if ((varDict.ContainsKey("API") && varDict.ContainsKey("APIVAR")) == false) { macroEnabled = false; logs.Add(new LogInfo(LogState.Info, "Macro not defined")); return; } // Get macroScript string rawScriptPath = varDict["API"]; string macroScriptPath = variables.Expand(varDict["API"]); // Need Expansion macroScript = project.AllScripts.Find(x => x.FullPath.Equals(macroScriptPath, StringComparison.OrdinalIgnoreCase)); if (macroScript == null) { macroEnabled = false; logs.Add(new LogInfo(LogState.Error, $"Macro defined but unable to find macro script [{rawScriptPath}")); return; } // Get macroScript if (macroScript.Sections.ContainsKey(varDict["APIVAR"]) == false) { macroEnabled = false; logs.Add(new LogInfo(LogState.Error, $"Macro defined but unable to find macro section [{varDict["APIVAR"]}")); return; } macroSection = macroScript.Sections[varDict["APIVAR"]]; variables.SetValue(VarsType.Global, "API", macroScriptPath); if (macroScript.Sections.ContainsKey("Variables")) { variables.AddVariables(VarsType.Global, macroScript.Sections["Variables"]); } // Import Section [APIVAR]'s variables, such as '%Shc_Mode%=0' variables.AddVariables(VarsType.Global, macroSection); // Parse Section [APIVAR] into MacroDict { SectionAddress addr = new SectionAddress(macroScript, macroSection); Dictionary <string, string> rawDict = Ini.ParseIniLinesIniStyle(macroSection.GetLines()); foreach (var kv in rawDict) { try { if (Regex.Match(kv.Key, Macro.MacroNameRegex, RegexOptions.Compiled | RegexOptions.CultureInvariant).Success) // Macro Name Validation { macroDict[kv.Key] = CodeParser.ParseStatement(kv.Value, addr); } else { logs.Add(new LogInfo(LogState.Error, $"Invalid macro name [{kv.Key}]")); } } catch (Exception e) { logs.Add(new LogInfo(LogState.Error, e)); } } } // Parse MainScript's section [Variables] into MacroDict // (Written by SetMacro, ... ,PERMANENT if (project.MainScript.Sections.ContainsKey("Variables")) { ScriptSection permaSection = project.MainScript.Sections["Variables"]; SectionAddress addr = new SectionAddress(project.MainScript, permaSection); Dictionary <string, string> rawDict = Ini.ParseIniLinesIniStyle(permaSection.GetLines()); foreach (var kv in rawDict) { try { if (Regex.Match(kv.Key, Macro.MacroNameRegex, RegexOptions.Compiled | RegexOptions.CultureInvariant).Success) // Macro Name Validation { macroDict[kv.Key] = CodeParser.ParseStatement(kv.Value, addr); } else { logs.Add(new LogInfo(LogState.Error, $"Invalid macro name [{kv.Key}]")); } } catch (Exception e) { logs.Add(new LogInfo(LogState.Error, e)); } } } }