Example #1
0
        public static DialogResult ShowTimeout()
        {
            ScriptTimeoutForm form = new ScriptTimeoutForm();

            if (ScriptMode.bScriptDone)
            {
                return(DialogResult.OK);
            }
            form.time.Start();
            return(form.ShowDialog());
        }
Example #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;
            }
        }