Exemple #1
0
        /// @brief Load plugin editor from file.
        /// @details Load a plugin definition into a new editor window, from user selected file,
        /// remembering independently from last binary directory.
        /// @param[in] sender Reference to object where event was raised.
        /// @param[in] e Event data arguments.
        private void OpenPluginButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Gear plug-in component (*.xml)|*.xml|All Files (*.*)|*.*";
            openFileDialog.Title  = "Open Gear Plug-in...";
            if (!String.IsNullOrEmpty(Properties.Settings.Default.LastPlugin))
            {
                openFileDialog.InitialDirectory =
                    Path.GetDirectoryName(Properties.Settings.Default.LastPlugin);
            }

            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                PluginEditor plugin = new PluginEditor(false);

                if (plugin.OpenFile(openFileDialog.FileName, false))
                {
                    //remember plugin successfully loaded
                    plugin.UpdateLastPluginOpened();
                    //show plugin editor loaded with selected one
                    plugin.MdiParent = this;
                    plugin.ShowErrorGrid(false);    //hide it by default
                    plugin.Show();
                }
            }
        }
Exemple #2
0
        /// @brief Load plugin editor from file.
        /// @details Load a plugin definition into a new editor window, from user selected file,
        /// remembering independently from last binary directory.
        private void OpenPluginButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Gear plug-in component (*.xml)|*.xml|All Files (*.*)|*.*";
            openFileDialog.Title  = "Open Gear Plug-in...";
            if ((Properties.Settings.Default.LastPlugin != null) &&
                (Properties.Settings.Default.LastPlugin.Length > 0))
            {
                openFileDialog.InitialDirectory =
                    Path.GetDirectoryName(Properties.Settings.Default.LastPlugin);
            }

            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                PluginEditor plugin = new PluginEditor(false);

                if (plugin.OpenFile(openFileDialog.FileName, false))
                {
                    //show plugin editor loaded with selected one
                    plugin.MdiParent = this;
                    plugin.Show();
                    //remember plugin succesfully loaded
                    Properties.Settings.Default.LastPlugin = plugin.GetLastPlugin;
                    Properties.Settings.Default.Save();
                }
            }
        }
Exemple #3
0
        /// @brief Open a window with the plugin editor to create a new plugin.
        private void newPluginButton_Click(object sender, EventArgs e)
        {
            //load default plugin template
            PluginEditor plugin = new PluginEditor(!Properties.Settings.Default.UseNoTemplate);

            plugin.MdiParent = this;
            plugin.Show();
        }
Exemple #4
0
        /// @brief Load a plugin from XML file.
        /// @details Try to open the XML definition for the plugin from the file name given as
        /// parameter. Then extract information from the XML (class name, auxiliary references
        /// and source code to compile), trying to compile the C# source code (based on
        /// Gear.PluginSupport.PluginBase class) and returning the new class instance. If the
        /// compilation fails, then it opens the plugin editor to show errors and source code.
        /// @param[in] FileName Name and path to the XML plugin file to open
        /// @returns Reference to the new plugin instance (on success) or NULL (on fail).
        public PluginBase LoadPlugin(string FileName)
        {
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;
            settings.IgnoreWhitespace             = true;
            XmlReader tr       = XmlReader.Create(FileName, settings);
            bool      ReadText = false;

            List <string> references   = new List <string>();
            string        instanceName = "";
            string        code         = "";

            try
            {
                while (tr.Read())
                {
                    if (tr.NodeType == XmlNodeType.Text && ReadText)
                    {
                        code     = tr.Value;
                        ReadText = false;
                    }

                    switch (tr.Name.ToLower())
                    {
                    case "reference":
                        if (!tr.IsEmptyElement)         //prevent empty element generates error
                        {
                            references.Add(tr.GetAttribute("name"));
                        }
                        break;

                    case "instance":
                        instanceName = tr.GetAttribute("class");
                        break;

                    case "code":
                        ReadText = true;
                        break;
                    }
                }

                //Dynamic load and compile the plugin module as a class, giving the chip
                // instance as a parameter.
                PluginBase plugin = ModuleCompiler.LoadModule(
                    code,
                    instanceName,
                    references.ToArray(),
                    Chip
                    );

                if (plugin == null)     //if it fails...
                {
                    // ...open plugin editor in other window
                    PluginEditor pe = new PluginEditor(false);
                    pe.OpenFile(FileName, true);
                    pe.MdiParent = this.MdiParent;
                    pe.Show();
                }
                else               //if success compiling & instantiate the new class...
                {
                    //...add the reference to the plugin list of the emulator instance
                    AttachPlugin(plugin);
                    Properties.Settings.Default.LastPlugin = FileName;  //update location of last plugin
                    Properties.Settings.Default.Save();
                }

                return(plugin);
            }
            catch (IOException ioe)
            {
                MessageBox.Show(this,
                                ioe.Message,
                                "Failed to load program binary",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);

                return(null);
            }
            catch (XmlException xmle)
            {
                MessageBox.Show(this,
                                xmle.Message,
                                "Failed to load program binary",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);

                return(null);
            }
            finally
            {
                tr.Close();
            }
        }
Exemple #5
0
        /// @brief Load a plugin from file.
        /// @details Try to open the XML definition for the plugin from the file name given as
        /// parameter. Then extract information from the XML (class name, auxiliary references
        /// and source code to compile), trying to compile the C# source code (based on
        /// Gear.PluginSupport.PluginCommon class) and returning the new class instance. If the
        /// compilation fails, then it opens the plugin editor to show errors and source code.
        /// @param[in] FileName Name and path to the XML plugin file to open
        /// @returns Reference to the new plugin instance (on success) or NULL (on fail).
        /// @throws Exception
        // TODO Modify the method to receive a PluginData, and delete the validation
        public void LoadPlugin(string FileName)
        {
            object objInst = null;
            //create the structure to fill data from file
            PluginData pluginCandidate = new PluginData();

            //Determine if the XML is valid, and for which DTD version
            if (!pluginCandidate.ValidatePluginFile(FileName))
            {
                string allMessages = string.Empty;
                //case not valid file, so show the errors.
                foreach (string strText in pluginCandidate.ValidationErrors)
                {
                    allMessages += (strText.Trim() + "\r\n");
                }
                /// @todo Add a custom dialog to show every error message in a grid.
                //show messages
                MessageBox.Show(allMessages,
                                "Emulator - OpenPlugin.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else  //...XML plugin file is valid & system version is determined
            {
                //determine time to use to build the plugin
                DateTime TimeOfBuild = AssemblyUtils.GetFileDateTime(FileName);
                for (int i = 0; i < pluginCandidate.UseExtFiles.Length; i++)
                {
                    if (pluginCandidate.UseExtFiles[i] == true)
                    {
                        TimeOfBuild = DateTime.FromBinary(Math.Max(
                                                              TimeOfBuild.ToBinary(),
                                                              AssemblyUtils.GetFileDateTime(pluginCandidate.ExtFiles[i]).ToBinary()));
                    }
                }
                //generate the full name of the assembly corresponding to the plugin candidate
                string candidateAssemblyFullName =
                    pluginCandidate.PluginAssemblyFullName(TimeOfBuild).FullName;

                //determine the version to look for the correct method to load it
                switch (pluginCandidate.PluginSystemVersion)
                {
                case "0.0":
                    if (PluginPersistence.GetDataFromXML_v0_0(FileName, ref pluginCandidate))
                    {
                        //Search and replace plugin class declarations for V0.0 plugin
                        // system compatibility.
                        pluginCandidate.Codes[0] =
                            PluginSystem.ReplacePropellerClassV0_0(
                                PluginSystem.ReplaceBaseClassV0_0(pluginCandidate.Codes[0]));
                    }
                    break;

                case "1.0":
                    PluginPersistence.GetDataFromXML_v1_0(FileName, ref pluginCandidate);
                    objInst = this.Chip;
                    break;

                default:
                    MessageBox.Show(string.Format("Plugin system version '{0}' not recognized " +
                                                  "on file \"{1}\".", pluginCandidate.PluginSystemVersion, FileName),
                                    "Emulator - Open File.",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    return;
                }
                //add information into plugin's code to generate with assembly attributes
                pluginCandidate.Codes[0] = PluginSystem.InsertAssemblyDetails(
                    pluginCandidate.Codes[0],
                    TimeOfBuild,
                    pluginCandidate.InstanceName,
                    pluginCandidate.Description,
                    pluginCandidate.PluginVersion);
                try
                {
                    //Dynamic load and compile the plugin module as a class, giving the chip
                    // instance as a parameter, and casting to appropriate class
                    PluginCommon plugin = ModuleCompiler.LoadModule(
                        pluginCandidate.Codes,                  //string[] codeTexts
                        pluginCandidate.ExtFiles,               //string[] sourceFiles
                        pluginCandidate.InstanceName,           //string module
                        pluginCandidate.References,             //string[] references
                        objInst,                                //object objInstance
                        pluginCandidate.PluginSystemVersion);   //string pluginSystemVersion,
                    if (plugin == null)
                    {
                        throw new Exception("Emulator - OpenPlugin: plugin object not generated!" +
                                            " (null response from memory loading).");
                    }
                    else //if success compiling & instantiate the new instance...
                    {
                        //...add to the corresponding plugin list of the emulator instance
                        AttachPlugin(plugin);
                        //update location of last plugin
                        Properties.Settings.Default.LastPlugin = FileName;
                        Properties.Settings.Default.Save();
                    }
                }
                catch (Exception)
                {
                    //open plugin editor in other window
                    PluginEditor errorPlugin = new PluginEditor(false);
                    if (errorPlugin.OpenFile(FileName, true))
                    {
                        //remember plugin successfully loaded
                        errorPlugin.UpdateLastPluginOpened();
                        //show plugin editor loaded with the faultly one
                        errorPlugin.MdiParent = this.MdiParent;
                        //the compilation errors are displayed in the error grid
                        ModuleCompiler.EnumerateErrors(errorPlugin.EnumErrors);
                        //show the error list
                        errorPlugin.ShowErrorGrid(true);
                        errorPlugin.Show();
                    }
                }
            }
        }