示例#1
0
        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"));
            }
        }
示例#2
0
 /// <summary>
 /// Removes a variable from the global scope.
 /// </summary>
 internal bool TryRemoveGlobalVariable(string name)
 {
     return(GlobalDict.Remove(name));
 }
示例#3
0
 internal bool TryRemoveGlobalVariable(string name)
 => GlobalDict.Remove(name);