예제 #1
0
        public static List <LogInfo> Visible(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>(1);

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Visible));
            CodeInfo_Visible info = cmd.Info as CodeInfo_Visible;

            string visibilityStr = StringEscaper.Preprocess(s, info.Visibility);
            bool   visibility    = false;

            if (visibilityStr.Equals("True", StringComparison.OrdinalIgnoreCase))
            {
                visibility = true;
            }
            else if (visibilityStr.Equals("False", StringComparison.OrdinalIgnoreCase) == false)
            {
                logs.Add(new LogInfo(LogState.Error, $"Invalid boolean value [{visibilityStr}]"));
                return(logs);
            }

            Script        p     = cmd.Addr.Script;
            ScriptSection iface = p.GetInterface(out string ifaceSecName);

            if (iface == null)
            {
                logs.Add(new LogInfo(LogState.Error, $"Script [{cmd.Addr.Script.ShortPath}] does not have section [{ifaceSecName}]"));
                return(logs);
            }

            List <UIControl> uiCtrls = iface.GetUICtrls(true);
            UIControl        uiCtrl  = uiCtrls.Find(x => x.Key.Equals(info.InterfaceKey, StringComparison.OrdinalIgnoreCase));

            if (uiCtrl == null)
            {
                logs.Add(new LogInfo(LogState.Error, $"Cannot find interface control [{info.InterfaceKey}] in section [{ifaceSecName}]"));
                return(logs);
            }

            if (uiCtrl.Visibility != visibility)
            {
                uiCtrl.Visibility = visibility;
                uiCtrl.Update();

                // Re-render Script
                Application.Current.Dispatcher.Invoke(() =>
                {
                    MainWindow w = (Application.Current.MainWindow as MainWindow);
                    if (w.CurMainTree.Script == cmd.Addr.Script)
                    {
                        w.DrawScript(cmd.Addr.Script);
                    }
                });
            }

            logs.Add(new LogInfo(LogState.Success, $"Interface control [{info.InterfaceKey}]'s visibility set to [{visibility}]"));

            return(logs);
        }
예제 #2
0
        public static List <LogInfo> Set(EngineState s, CodeCommand cmd)
        {
            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Set));
            CodeInfo_Set info = cmd.Info as CodeInfo_Set;

            Variables.VarKeyType varType = Variables.DetermineType(info.VarKey);
            if (varType == Variables.VarKeyType.None)
            {
                // Check Macro
                if (Regex.Match(info.VarKey, Macro.MacroNameRegex, RegexOptions.Compiled | RegexOptions.CultureInvariant).Success) // Macro Name Validation
                {
                    string macroCommand = StringEscaper.Preprocess(s, info.VarValue);

                    if (macroCommand.Equals("NIL", StringComparison.OrdinalIgnoreCase))
                    {
                        macroCommand = null;
                    }

                    LogInfo log = s.Macro.SetMacro(info.VarKey, macroCommand, cmd.Addr, info.Permanent, false);
                    return(new List <LogInfo>(1)
                    {
                        log
                    });
                }
            }

            // [WB082 Behavior]
            // If PERMANENT was used but the key exists in interface command, the value will not be written to script.project but in interface.
            // Need to investigate where the logs are saved in this case.
            switch (info.Permanent)
            {
            case true:
            {         // Check if interface contains VarKey
                List <LogInfo> logs = new List <LogInfo>();

                if (Variables.DetermineType(info.VarKey) != Variables.VarKeyType.Variable)
                {
                    goto case false;
                }

                string varKey     = Variables.TrimPercentMark(info.VarKey);
                string finalValue = StringEscaper.Preprocess(s, info.VarValue);

                #region Set UI
                Script        p     = cmd.Addr.Script;
                ScriptSection iface = p.GetInterface(out string sectionName);
                if (iface == null)
                {
                    goto case false;
                }

                List <UIControl> uiCmds = iface.GetUICtrls(true);
                UIControl        uiCmd  = uiCmds.Find(x => x.Key.Equals(varKey, StringComparison.OrdinalIgnoreCase));
                if (uiCmd == null)
                {
                    goto case false;
                }

                bool match = uiCmd.SetValue(finalValue, false, out List <LogInfo> varLogs);
                logs.AddRange(varLogs);

                if (match)
                {
                    uiCmd.Update();

                    logs.AddRange(Variables.SetVariable(s, info.VarKey, info.VarValue, false, false));
                    return(logs);
                }
                else
                {
                    goto case false;
                }
                #endregion
            }

            case false:
            default:
                return(Variables.SetVariable(s, info.VarKey, info.VarValue, info.Global, info.Permanent));
            }
        }
예제 #3
0
        public static List <LogInfo> ReadInterface(EngineState s, CodeCommand cmd)
        { // ReadInterface,<Element>,<ScriptFile>,<Section>,<Key>,<DestVar>
            List <LogInfo> logs = new List <LogInfo>(1);

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_ReadInterface));
            CodeInfo_ReadInterface info = cmd.Info as CodeInfo_ReadInterface;

            string scriptFile = StringEscaper.Preprocess(s, info.ScriptFile);
            string section    = StringEscaper.Preprocess(s, info.Section);
            string key        = StringEscaper.Preprocess(s, info.Key);

            Script p = Engine.GetScriptInstance(s, cmd, s.CurrentScript.FullPath, scriptFile, out bool inCurrentScript);

            if (!p.Sections.ContainsKey(section))
            {
                logs.Add(new LogInfo(LogState.Error, $"Script [{scriptFile}] does not have section [{section}]"));
                return(logs);
            }

            ScriptSection    iface  = p.Sections[section];
            List <UIControl> uiCmds = iface.GetUICtrls(true);
            UIControl        uiCmd  = uiCmds.Find(x => x.Key.Equals(key, StringComparison.OrdinalIgnoreCase));

            if (uiCmd == null)
            {
                logs.Add(new LogInfo(LogState.Error, $"Interface control [{key}] does not exist"));
                return(logs);
            }

            string destStr;

            switch (info.Element)
            {
            case InterfaceElement.Text:
                destStr = uiCmd.Text;
                break;

            case InterfaceElement.Visible:
                destStr = uiCmd.Visibility.ToString();
                break;

            case InterfaceElement.PosX:
                destStr = ((int)uiCmd.Rect.X).ToString();
                break;

            case InterfaceElement.PosY:
                destStr = ((int)uiCmd.Rect.Y).ToString();
                break;

            case InterfaceElement.Width:
                destStr = ((int)uiCmd.Rect.Width).ToString();
                break;

            case InterfaceElement.Height:
                destStr = ((int)uiCmd.Rect.Height).ToString();
                break;

            case InterfaceElement.Value:
                destStr = uiCmd.GetValue();
                if (destStr == null)
                {
                    logs.Add(new LogInfo(LogState.Error, $"Reading [Value] from [{uiCmd.Type}] is not supported"));
                    return(logs);
                }
                break;

            default:
                throw new InternalException($"Internal Logic Error at ReadInterface");
            }

            // Do not expand read values
            List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, destStr, false, false, false);

            logs.AddRange(varLogs);

            return(logs);
        }
예제 #4
0
        public static List <LogInfo> WriteInterface(EngineState s, CodeCommand cmd)
        { // WriteInterface,<Element>,<ScriptFile>,<Section>,<Key>,<Value>
            List <LogInfo> logs = new List <LogInfo>(2);

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_WriteInterface));
            CodeInfo_WriteInterface info = cmd.Info as CodeInfo_WriteInterface;

            string scriptFile = StringEscaper.Preprocess(s, info.ScriptFile);
            string section    = StringEscaper.Preprocess(s, info.Section);
            string key        = StringEscaper.Preprocess(s, info.Key);
            string finalValue = StringEscaper.Preprocess(s, info.Value);

            Script p = Engine.GetScriptInstance(s, cmd, s.CurrentScript.FullPath, scriptFile, out bool inCurrentScript);

            if (!p.Sections.ContainsKey(section))
            {
                logs.Add(new LogInfo(LogState.Error, $"Script [{scriptFile}] does not have section [{section}]"));
                return(logs);
            }

            ScriptSection    iface  = p.Sections[section];
            List <UIControl> uiCmds = iface.GetUICtrls(true);
            UIControl        uiCmd  = uiCmds.Find(x => x.Key.Equals(key, StringComparison.OrdinalIgnoreCase));

            if (uiCmd == null)
            {
                logs.Add(new LogInfo(LogState.Error, $"Interface control [{key}] does not exist"));
                return(logs);
            }

            switch (info.Element)
            {
            case InterfaceElement.Text:
                uiCmd.Text = finalValue;
                break;

            case InterfaceElement.Visible:
            {
                bool visibility = false;
                if (finalValue.Equals("True", StringComparison.OrdinalIgnoreCase))
                {
                    visibility = true;
                }
                else if (!finalValue.Equals("False", StringComparison.OrdinalIgnoreCase))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid boolean value"));
                    return(logs);
                }

                uiCmd.Visibility = visibility;
            }
            break;

            case InterfaceElement.PosX:
            {
                if (!NumberHelper.ParseInt32(finalValue, out int x))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid integer"));
                    return(logs);
                }

                uiCmd.Rect.X = x;
            }
            break;

            case InterfaceElement.PosY:
            {
                if (!NumberHelper.ParseInt32(finalValue, out int y))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid integer"));
                    return(logs);
                }

                uiCmd.Rect.Y = y;
            }
            break;

            case InterfaceElement.Width:
            {
                if (!NumberHelper.ParseInt32(finalValue, out int width))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid integer"));
                    return(logs);
                }

                uiCmd.Rect.Width = width;
            }
            break;

            case InterfaceElement.Height:
            {
                if (!NumberHelper.ParseInt32(finalValue, out int height))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid integer"));
                    return(logs);
                }

                uiCmd.Rect.Height = height;
            }
            break;

            case InterfaceElement.Value:
            {
                bool success = uiCmd.SetValue(finalValue, false, out List <LogInfo> varLogs);
                logs.AddRange(varLogs);

                if (success == false && varLogs.Count == 0)
                {
                    logs.Add(new LogInfo(LogState.Error, $"Writing [Value] to [{uiCmd.Type}] is not supported"));
                    return(logs);
                }
            }
            break;

            default:
                throw new InternalException($"Internal Logic Error at WriteInterface");
            }

            // Update uiCmd into file
            uiCmd.Update();

            // Rerender Script
            Application.Current?.Dispatcher.Invoke(() =>
            { // Application.Current is null in unit test
                MainWindow w = (Application.Current.MainWindow as MainWindow);
                if (w.CurMainTree.Script == cmd.Addr.Script)
                {
                    w.DrawScript(cmd.Addr.Script);
                }
            });

            return(logs);
        }
예제 #5
0
        public static List <LogInfo> VisibleOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>(8);

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_VisibleOp));
            CodeInfo_VisibleOp infoOp = cmd.Info as CodeInfo_VisibleOp;

            Script        p     = cmd.Addr.Script;
            ScriptSection iface = p.GetInterface(out string ifaceSecName);

            if (iface == null)
            {
                logs.Add(new LogInfo(LogState.Error, $"Script [{cmd.Addr.Script.ShortPath}] does not have section [{ifaceSecName}]"));
                return(logs);
            }

            List <UIControl> uiCtrls = iface.GetUICtrls(true);

            List <Tuple <string, bool> > prepArgs = new List <Tuple <string, bool> >();

            foreach (CodeInfo_Visible info in infoOp.InfoList)
            {
                string visibilityStr = StringEscaper.Preprocess(s, info.Visibility);
                bool   visibility    = false;
                if (visibilityStr.Equals("True", StringComparison.OrdinalIgnoreCase))
                {
                    visibility = true;
                }
                else if (visibilityStr.Equals("False", StringComparison.OrdinalIgnoreCase) == false)
                {
                    throw new ExecuteException($"Invalid boolean value [{visibilityStr}]");
                }

                prepArgs.Add(new Tuple <string, bool>(info.InterfaceKey, visibility));
            }

            List <UIControl> uiCmdList = new List <UIControl>();

            foreach (Tuple <string, bool> args in prepArgs)
            {
                UIControl uiCmd = uiCtrls.Find(x => x.Key.Equals(args.Item1, StringComparison.OrdinalIgnoreCase));
                if (uiCmd == null)
                {
                    logs.Add(new LogInfo(LogState.Error, $"Cannot find interface control [{args.Item1}] in section [{ifaceSecName}]"));
                    continue;
                }

                uiCmd.Visibility = args.Item2;
                uiCmdList.Add(uiCmd);
            }

            UIControl.Update(uiCmdList);

            foreach (Tuple <string, bool> args in prepArgs)
            {
                logs.Add(new LogInfo(LogState.Success, $"Interface control [{args.Item1}]'s visibility set to [{args.Item2}]"));
            }

            // Re-render Script
            Application.Current.Dispatcher.Invoke(() =>
            {
                MainWindow w = (Application.Current.MainWindow as MainWindow);
                if (w.CurMainTree.Script == cmd.Addr.Script)
                {
                    w.DrawScript(cmd.Addr.Script);
                }
            });

            return(logs);
        }