Esempio n. 1
0
        // Called once an asyncronous compiling task has completed.
        private void OnCompileComplete(ScriptCompileResult results)
        {
            // Update the script object.
            script.Code     = codeEditor.Text;
            script.Errors   = results.Errors;
            script.Warnings = results.Warnings;

            // Update the error message status-strip.
            if (script.HasErrors)
            {
                displayedError          = script.Errors[0];
                labelErrorMessage.Text  = displayedError.ToString();
                labelErrorMessage.Image = ZeldaEditor.ResourceProperties.Resources.exclamation;
            }
            else
            {
                displayedError          = null;
                labelErrorMessage.Text  = "";
                labelErrorMessage.Image = null;
            }

            // Update the world-tree-view's scripts (if there is an error icon for a failed compile).
            if (!script.IsHidden)
            {
                editorControl.EditorForm.worldTreeView.RefreshScripts();
            }
        }
Esempio n. 2
0
        private Script ReadScript(BinaryReader reader)
        {
            Script script = new Script();

            // Read the name and source code.
            script.Name     = ReadString(reader);
            script.Code     = ReadString(reader);
            script.IsHidden = reader.ReadBoolean();

            // Read the parameters.
            int paramCount = reader.ReadInt32();

            for (int i = 0; i < paramCount; i++)
            {
                ScriptParameter param = new ScriptParameter();
                param.Type = ReadString(reader);
                param.Name = ReadString(reader);
                script.Parameters.Add(param);
            }

            // Read errors and warnings.
            int errorCount = reader.ReadInt32();

            for (int i = 0; i < errorCount; i++)
            {
                ScriptCompileError error = new ScriptCompileError();
                error.Line        = reader.ReadInt32();
                error.Column      = reader.ReadInt32();
                error.ErrorNumber = ReadString(reader);
                error.ErrorText   = ReadString(reader);
                error.IsWarning   = true;
                script.Errors.Add(error);
            }
            int warningCount = reader.ReadInt32();

            for (int i = 0; i < warningCount; i++)
            {
                ScriptCompileError warning = new ScriptCompileError();
                warning.Line        = reader.ReadInt32();
                warning.Column      = reader.ReadInt32();
                warning.ErrorNumber = ReadString(reader);
                warning.ErrorText   = ReadString(reader);
                warning.IsWarning   = true;
                script.Warnings.Add(warning);
            }

            return(script);
        }
Esempio n. 3
0
        //-----------------------------------------------------------------------------
        // Internal Methods
        //-----------------------------------------------------------------------------

        // Compile the final code for a script only to find any errors and/or warnings.
        private static ScriptCompileResult CompileScript(string code)
        {
            // Setup the compile options.
            CompilerParameters options = new CompilerParameters();

            options.GenerateExecutable = false;                  // We want a Dll (Class Library)
            options.GenerateInMemory   = true;

            // Add the assembly references.
            Assembly zeldaApiAssembly = Assembly.GetAssembly(typeof(ZeldaAPI.CustomScriptBase));

            options.ReferencedAssemblies.Add(zeldaApiAssembly.Location);

            // Create a C# code provider and compile the code.
            Microsoft.CSharp.CSharpCodeProvider csProvider = new Microsoft.CSharp.CSharpCodeProvider();
            CompilerResults csResult = csProvider.CompileAssemblyFromSource(options, code);

            // Copy warnings and errors into the ScriptComileResult.
            ScriptCompileResult result = new ScriptCompileResult();

            foreach (CompilerError csError in csResult.Errors)
            {
                ScriptCompileError error = new ScriptCompileError(csError.Line,
                                                                  csError.Column, csError.ErrorNumber, csError.ErrorText, csError.IsWarning);
                if (error.IsWarning)
                {
                    result.Warnings.Add(error);
                }
                else
                {
                    result.Errors.Add(error);
                }
            }

            return(result);
        }
Esempio n. 4
0
        //-----------------------------------------------------------------------------
        // Constructors
        //-----------------------------------------------------------------------------

        public ScriptEditor(Script script, EditorControl editorControl)
        {
            InitializeComponent();

            this.script           = script;
            this.editorControl    = editorControl;
            this.previousName     = script.Name;
            this.previousCode     = script.Code;
            this.autoCompile      = true;
            this.compileOnClose   = true;
            this.needsRecompiling = false;
            this.compileTask      = null;

            if (script.HasErrors)
            {
                displayedError          = script.Errors[0];
                labelErrorMessage.Text  = displayedError.ToString();
                labelErrorMessage.Image = ZeldaEditor.ResourceProperties.Resources.exclamation;
            }
            else
            {
                displayedError          = null;
                labelErrorMessage.Text  = "";
                labelErrorMessage.Image = null;
            }

            // Set some UI text.
            base.Text        = "Script Editor: " + script.Name;
            textBoxName.Text = script.Name;

            // Create the code editor text box.
            codeEditor = new FastColoredTextBox();
            panelCode.Controls.Add(codeEditor);
            codeEditor.Dock     = DockStyle.Fill;
            codeEditor.Language = Language.CSharp;
            codeEditor.Text     = script.Code;
            codeEditor.ReservedCountOfLineNumberChars = 4;
            codeEditor.IsChanged = false;
            codeEditor.ClearUndo();
            codeEditor.TextChanged          += codeEditor_TextChanged;
            codeEditor.SelectionChanged     += codeEditor_SelectionChanged;
            codeEditor.UndoRedoStateChanged += UpdateUndoRedoButtonStates;

            // Create the auto-complete menu.
            autoCompleteMenu               = new AutocompleteMenu(codeEditor);
            autoCompleteMenu.ForeColor     = Color.Black;
            autoCompleteMenu.BackColor     = Color.White;
            autoCompleteMenu.SelectedColor = Color.Orange;
            autoCompleteMenu.SearchPattern = @"[\w\.]";
            autoCompleteMenu.AllowTabKey   = true;
            autoCompleteMenu.Items.SetAutocompleteItems(new DynamicCollection(autoCompleteMenu, script, codeEditor));

            // Start a timer to auto-compile the script every 2 seconds.
            if (autoCompile)
            {
                /*
                 * timer = new System.Timers.Timer(2000);
                 * timer.AutoReset = true;
                 * timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e) {
                 *
                 *      //if (needsRecompiling && compileTask == null) {
                 *      if (needsRecompiling && !editorControl.IsBusyCompiling) {
                 *              //BeginCompilingScript();
                 *              editorControl.CompileScript(script, OnCompileComplete);
                 *              editorControl.NeedsRecompiling = true;
                 *      }
                 * };
                 * timer.Start();
                 */
                // Add an idle method to check for compile task completion.
                Application.Idle += RecompileUpdate;
            }

            // Setup the initial undo/redo button states.
            UpdateUndoRedoButtonStates(this, EventArgs.Empty);
        }