Exemplo n.º 1
0
        public static Dictionary <string, string> ShowParamsDialog(LuaUIParameters ui_parameters)
        {
            // ano - safer to assume data coming from a script is suspect
            if (ui_parameters.keys == null || ui_parameters.labels == null)
            {
                throw new ScriptRuntimeException("Keys/labels were nil for taking script parameters");
            }
            if (ui_parameters.keys.Count != ui_parameters.labels.Count)
            {
                throw new ScriptRuntimeException("# keys must be equal to # labels for parameters dialog");
            }

            // ano - really suspect
            if (ui_parameters.keys.Count > 384)
            {
                throw new ScriptRuntimeException("# keys for script parameters can't be greater than 384");
            }

            if (ui_parameters.defaultvalues == null)
            {
                ui_parameters.defaultvalues = new List <string>();
            }

            while (ui_parameters.defaultvalues.Count < ui_parameters.keys.Count)
            {
                ui_parameters.defaultvalues.Add("");
            }

            ScriptParamForm form = new ScriptParamForm();

            for (int i = 0; i < ui_parameters.labels.Count; i++)
            {
                form.paramsview.Rows.Add(ui_parameters.labels[i], ui_parameters.defaultvalues[i]);
            }
            DialogResult result = form.ShowDialog();

            if (result == DialogResult.OK)
            {
                Dictionary <string, string> output = new Dictionary <string, string>(ui_parameters.labels.Count);
                for (int i = 0; i < ui_parameters.labels.Count; i++)
                {
                    output.Add(ui_parameters.keys[i], form.paramsview.Rows[i].Cells[1].Value.ToString());
                }
                return(output);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        public void DoScriptAt(Vector2D mappos)
        {
            if (scriptPath == "")
            {
                General.Interface.DisplayStatus(StatusType.Warning,
                                                "No Lua file selected.");
                // TODO - think about if this is really the correct way to react
                ChooseScript();
                return;
            }
            if (!File.Exists(scriptPath))
            {
                General.Interface.DisplayStatus(StatusType.Warning,
                                                "Can't find Lua file '" + scriptPath + "'");
                ChooseScript();
                return;
            }

            bool snaptogrid    = General.Interface.ShiftState ^ General.Interface.SnapToGrid;
            bool snaptonearest = General.Interface.CtrlState ^ General.Interface.AutoMerge;

            stopwatch = new Stopwatch();
            stopwatch.Start();

            //string scriptPath = Path.Combine(General.SettingsPath, @"scripts\test.lua");
            string scriptShortName = Path.GetFileName(scriptPath);

            // Make undo for the draw
            General.Map.UndoRedo.CreateUndo("Run script '" + scriptShortName + "'");

            string title = "";

            if (General.Interface is MainForm)
            {
                title = ((MainForm)General.Interface).Text;
                ((MainForm)General.Interface).Text = "Running Lua...";
            }

            General.Interface.SetCursor(Cursors.AppStarting);

            General.Interface.DisplayStatus(StatusType.Busy, "Executing script '" + scriptShortName + "'!");
            CancelReason           = eCancelReason.Unknown;
            bScriptParamsRequested = false;
            bScriptSuccess         = true;
            bScriptDone            = false;
            bScriptCancelled       = false;

            scriptRunner          = new ScriptContext(renderer, mappos, snaptogrid, snaptonearest);
            scriptThread          = new Thread(new ThreadStart(RunScriptThread));
            scriptThread.Priority = ThreadPriority.Highest;
            scriptThread.Start();

            Thread.Sleep(4);
            int scriptTime = 4;

            while (!bScriptDone && !bScriptCancelled && scriptTime < 2800)
            {
                Thread.Sleep(10);
                scriptTime += 10;

                if (bScriptParamsRequested)
                {
                    bScriptParamsRequested = false;
                    stopwatch.Stop();
                    returned_parameters = ScriptParamForm.ShowParamsDialog(scriptRunner.ui_parameters);
                    stopwatch.Start();
                    bScriptUIDone = true;
                }
            }

            General.Map.IsMapBeingEdited = true;
            // if our script isnt done
            // let's ask the user if they wanna keep waiting
            if (!bScriptDone && !bScriptCancelled)
            {
                General.Interface.SetCursor(Cursors.Default);
                General.Interface.DisplayStatus(StatusType.Busy, "Executing script '" + scriptShortName + "', but it's being slow.");

                ScriptTimeoutForm.ShowTimeout();
            }

            scriptThread.Join();
            General.Map.IsMapBeingEdited = false;

            General.Map.ThingsFilter.Update();

            // Snap to map format accuracy
            General.Map.Map.SnapAllToAccuracy();

            // Update cached values
            General.Map.Map.Update();

            // Update the used textures
            General.Map.Data.UpdateUsedTextures();

            // Map is changed
            General.Map.IsChanged = true;

            General.Interface.SetCursor(Cursors.Default);

            General.Interface.RedrawDisplay();

            stopwatch.Stop();

            // check for warnings
            if (bScriptSuccess)
            {
                string warningsText = scriptRunner.GetWarnings();
                if (warningsText.Length > 0)
                {
                    string debugLog = scriptRunner.DebugLog;
                    if (debugLog.Length > 0)
                    {
                        warningsText += "\nSCRIPT DEBUG LOG:\n" + debugLog;
                    }
                    warningsText += debugLog;

                    if (ScriptWarningForm.AskUndo(warningsText))
                    {
                        bScriptSuccess = false;
                    }
                }
            }

            // actual success
            if (bScriptSuccess)
            {
                General.Interface.DisplayStatus(StatusType.Info,
                                                "Lua script  '" + scriptShortName + "' success in "
                                                + (stopwatch.Elapsed.TotalMilliseconds / 1000d).ToString("########0.00")
                                                + " seconds.");

                string scriptLog = scriptRunner.ScriptLog;
                if (scriptLog.Length > 0)
                {
                    ScriptMessageForm.ShowMessage(scriptLog);
                }
            }
            else
            {
                // okay failure
                General.Map.UndoRedo.WithdrawUndo();

                General.Interface.DisplayStatus(StatusType.Warning,
                                                "Lua script '" + scriptShortName + "' failed in "
                                                + (stopwatch.Elapsed.TotalMilliseconds / 1000d).ToString("########0.00")
                                                + " seconds.");

                string errorText = scriptRunner.errorText;

                string warnings = scriptRunner.GetWarnings();

                if (warnings.Length > 0)
                {
                    errorText += "\nSCRIPT WARNING:\n" + warnings;
                }

                string scriptLog = scriptRunner.ScriptLog;
                if (scriptLog.Length > 0)
                {
                    errorText += "\nSCRIPT LOG:\n" + scriptLog;
                }

                string debugLog = scriptRunner.DebugLog;
                if (debugLog.Length > 0)
                {
                    errorText += "\nSCRIPT DEBUG LOG:\n" + debugLog;
                }

                if (errorText.Length > 0)
                {
                    ScriptErrorForm.ShowError(errorText);
                }
                else
                {
                    ScriptErrorForm.ShowError("unable to produce error message. possibly a big problem");
                }
            } // else error

            if (General.Interface is MainForm)
            {
                ((MainForm)General.Interface).Text = title;
            }
        }
Exemplo n.º 3
0
        public static Dictionary <string, string> ShowParamsDialog(LuaUIParameters ui_parameters)
        {
            // ano - safer to assume data coming from a script is suspect
            if (ui_parameters.keys == null || ui_parameters.labels == null)
            {
                ScriptContext.context.Warn("Keys/labels were nil for taking script parameters. Possibly Lua API bug?");
                return(null);
            }
            if (ui_parameters.keys.Count != ui_parameters.labels.Count)
            {
                ScriptContext.context.Warn("# keys must be equal to # labels for parameters dialog. Possibly Lua API bug?");
                return(null);
            }

            // ano - really suspect
            if (ui_parameters.keys.Count > 384)
            {
                ScriptContext.context.Warn("# keys for script parameters can't be greater than 384");
                return(null);
            }

            if (ui_parameters.defaultvalues == null)
            {
                ui_parameters.defaultvalues = new List <string>();
            }

            while (ui_parameters.defaultvalues.Count < ui_parameters.keys.Count)
            {
                ui_parameters.defaultvalues.Add(null);
            }

            ScriptParamForm form = new ScriptParamForm();

            for (int i = 0; i < ui_parameters.labels.Count; i++)
            {
                if (ui_parameters.defaultvalues[i] == null)
                {
                    form.paramsview.Rows.Add(ui_parameters.labels[i], "");
                }
                else
                {
                    form.paramsview.Rows.Add(ui_parameters.labels[i], ui_parameters.defaultvalues[i]);
                }

                if (ui_parameters.tooltips[i] != null && ui_parameters.tooltips[i].Length > 0)
                {
                    form.paramsview.Rows[i].Cells[0].ToolTipText = ui_parameters.tooltips[i];
                    form.paramsview.Rows[i].Cells[1].ToolTipText = ui_parameters.tooltips[i];
                }
            }

            DialogResult result = form.ShowDialog();

            if (result == DialogResult.OK)
            {
                Dictionary <string, string> output = new Dictionary <string, string>(ui_parameters.labels.Count);

                for (int i = 0; i < ui_parameters.labels.Count; i++)
                {
                    output.Add(ui_parameters.keys[i], form.paramsview.Rows[i].Cells[1].Value.ToString());
                }

                return(output);
            }
            else
            {
                return(null);
            }
        }