public static List <LogInfo> IniDelete(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); CodeInfo_IniDelete info = cmd.Info.Cast <CodeInfo_IniDelete>(); string fileName = StringEscaper.Preprocess(s, info.FileName); string sectionName = StringEscaper.Preprocess(s, info.Section); string key = StringEscaper.Preprocess(s, info.Key); Debug.Assert(fileName != null, $"{nameof(fileName)} != null"); Debug.Assert(sectionName != null, $"{nameof(sectionName)} != null"); Debug.Assert(key != null, $"{nameof(key)} != null"); if (sectionName.Length == 0) { return(LogInfo.LogErrorMessage(logs, "Section name cannot be empty")); } if (key.Length == 0) { return(LogInfo.LogErrorMessage(logs, "Key name cannot be empty")); } if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg)) { return(LogInfo.LogErrorMessage(logs, errorMsg)); } bool result; if (s.CompatAutoCompactIniWriteCommand) { result = IniReadWriter.DeleteCompactKey(fileName, sectionName, key); } else { result = IniReadWriter.DeleteKey(fileName, sectionName, key); } if (result) { logs.Add(new LogInfo(LogState.Success, $"Key [{key}] deleted from [{fileName}]", cmd)); } else { logs.Add(new LogInfo(LogState.Ignore, $"Could not delete key [{key}] from [{fileName}]", cmd)); } return(logs); }
public static void SetDelPermanent(EngineState s) { string scPath = s.Project.MainScript.RealPath; IniReadWriter.DeleteKey(scPath, "Variables", "%PermDest%"); try { // Set EngineTests.Eval(s, "Set,%PermDest%,PEBakery,PERMANENT", CodeType.Set, ErrorCheck.Success); string dest = s.Variables.GetValue(VarsType.Global, "PermDest"); Assert.IsTrue(dest.Equals("PEBakery", StringComparison.Ordinal)); // Check memory-cached script section ScriptSection varSect = s.Project.MainScript.Sections["Variables"]; int idx = Array.FindIndex(varSect.Lines, x => x.StartsWith("%PermDest%=")); Assert.AreNotEqual(-1, idx); // Check script file string permanent = IniReadWriter.ReadKey(scPath, "Variables", "%PermDest%"); Assert.IsTrue(dest.Equals(permanent, StringComparison.Ordinal)); // Delete EngineTests.Eval(s, "Set,%PermDest%,NIL,PERMANENT", CodeType.Set, ErrorCheck.Success); // Check memory-cached script section idx = Array.FindIndex(varSect.Lines, x => x.StartsWith("%PermDest%=")); Assert.AreEqual(-1, idx); // Check script file permanent = IniReadWriter.ReadKey(scPath, "Variables", "%PermDest%"); Assert.IsNull(permanent); } finally { IniReadWriter.DeleteKey(scPath, "Variables", "%PermDest%"); } }
public bool Delete() { return(IniReadWriter.DeleteKey(Section.Script.RealPath, Section.Name, Key)); }
public LogInfo SetMacro(string macroName, string macroCommand, ScriptSection section, bool global, bool permanent) { // Macro Name Validation if (!Regex.Match(macroName, MacroNameRegex, RegexOptions.Compiled | RegexOptions.CultureInvariant).Success) { return(new LogInfo(LogState.Error, $"Invalid macro name [{macroName}]")); } if (macroCommand != null) { // Insert // Try parsing CodeParser parser = new CodeParser(section, Global.Setting, section.Project.Compat); CodeCommand cmd = parser.ParseStatement(macroCommand); if (cmd.Type == CodeType.Error) { CodeInfo_Error info = cmd.Info.Cast <CodeInfo_Error>(); return(new LogInfo(LogState.Error, info.ErrorMessage)); } // Put into dictionary if (permanent) // MacroDict { GlobalDict[macroName] = cmd; if (IniReadWriter.WriteKey(section.Project.MainScript.RealPath, ScriptSection.Names.Variables, macroName, cmd.RawCode)) { return(new LogInfo(LogState.Success, $"Permanent Macro [{macroName}] set to [{cmd.RawCode}]")); } else { return(new LogInfo(LogState.Error, $"Could not write macro into [{section.Project.MainScript.RealPath}]")); } } if (global) // MacroDict { GlobalDict[macroName] = cmd; return(new LogInfo(LogState.Success, $"Global Macro [{macroName}] set to [{cmd.RawCode}]")); } LocalDict[macroName] = cmd; return(new LogInfo(LogState.Success, $"Local Macro [{macroName}] set to [{cmd.RawCode}]")); } else { // Delete // Put into dictionary if (permanent) // MacroDict { if (GlobalDict.ContainsKey(macroName)) { GlobalDict.Remove(macroName); IniReadWriter.DeleteKey(section.Project.MainScript.RealPath, ScriptSection.Names.Variables, macroName); return(new LogInfo(LogState.Success, $"Permanent Macro [{macroName}] deleted")); } return(new LogInfo(LogState.Error, $"Permanent Macro [{macroName}] not found")); } if (global) // MacroDict { if (GlobalDict.ContainsKey(macroName)) { GlobalDict.Remove(macroName); return(new LogInfo(LogState.Success, $"Global Macro [{macroName}] deleted")); } return(new LogInfo(LogState.Error, $"Global Macro [{macroName}] not found")); } // LocalDict if (LocalDict.ContainsKey(macroName)) { LocalDict.Remove(macroName); return(new LogInfo(LogState.Success, $"Local Macro [{macroName}] deleted")); } return(new LogInfo(LogState.Error, $"Local Macro [{macroName}] not found")); } }