OSDN Git Service

Added ability to run C# scripts, load command definitions, and command interpreters...
authorLatif Khalifa <latifer@streamgrid.net>
Tue, 16 Mar 2010 16:26:05 +0000 (16:26 +0000)
committerLatif Khalifa <latifer@streamgrid.net>
Tue, 16 Mar 2010 16:26:05 +0000 (16:26 +0000)
git-svn-id: https://radegast.googlecode.com/svn/trunk@518 f7a694da-4d33-11de-9ad6-1127a62b9fcd

Radegast/Core/Commands/LoadPluginCommand.cs
Radegast/Core/PluginManager.cs
Radegast/Core/RadegastInstance.cs
Radegast/GUI/Dialogs/MainForm.Designer.cs
Radegast/GUI/Dialogs/MainForm.cs
plugins/Radegast.Plugin.Demo/DemoPlugin.cs

index 0484934..2f21e47 100644 (file)
@@ -68,7 +68,7 @@ namespace Radegast.Commands
             try
             {
                 Assembly assembly = Assembly.LoadFile(loadfilename);
-                instance.PluginManager.LoadAssembly(loadfilename, assembly);
+                instance.PluginManager.LoadAssembly(loadfilename, assembly, true);
             }
             catch (Exception ex)
             {
index deff323..f93119e 100644 (file)
@@ -33,9 +33,12 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.IO;
+using System.Threading;
 using System.Reflection;
+using System.CodeDom.Compiler;
 using System.Windows.Forms;
 using OpenMetaverse;
+using Microsoft.CSharp;
 
 namespace Radegast
 {
@@ -125,8 +128,78 @@ namespace Radegast
             }
         }
 
+        public void LoadCSharpScriptFile(string filename)
+        {
+            try { LoadCSharpScript(File.ReadAllText(filename)); }
+            catch (Exception ex)
+            {
+                Logger.Log("Failed loading C# script " + filename + ": ", Helpers.LogLevel.Warning, ex);
+            }
+        }
+
+        public void LoadCSharpScript(string code)
+        {
+            ThreadPool.QueueUserWorkItem(sender =>
+            {
+                try
+                {
+                    // *** Generate dynamic compiler
+                    Dictionary<string, string> loCompilerOptions = new Dictionary<string, string>();
+                    loCompilerOptions.Add("CompilerVersion", "v3.5");
+                    CSharpCodeProvider loCompiler = new CSharpCodeProvider(loCompilerOptions);
+                    CompilerParameters loParameters = new CompilerParameters();
+
+                    // *** Start by adding any referenced assemblies
+                    loParameters.ReferencedAssemblies.Add("OpenMetaverse.StructuredData.dll");
+                    loParameters.ReferencedAssemblies.Add("OpenMetaverseTypes.dll");
+                    loParameters.ReferencedAssemblies.Add("OpenMetaverse.dll");
+                    loParameters.ReferencedAssemblies.Add("Radegast.exe");
+                    loParameters.ReferencedAssemblies.Add("System.dll");
+                    loParameters.ReferencedAssemblies.Add("System.Core.dll");
+                    loParameters.ReferencedAssemblies.Add("System.Drawing.dll");
+                    loParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
+
+                    // *** Load the resulting assembly into memory
+                    loParameters.GenerateInMemory = true;
+                    loParameters.GenerateExecutable = false;
+
+                    // *** Now compile the whole thing
+                    CompilerResults loCompiled =
+                            loCompiler.CompileAssemblyFromSource(loParameters, code);
+
+                    // *** Check for compilation erros
+                    if (loCompiled.Errors.HasErrors)
+                    {
+                        string lcErrorMsg = "";
+                        lcErrorMsg = "Compilation failed: " + loCompiled.Errors.Count.ToString() + " errors:";
+
+                        for (int x = 0; x < loCompiled.Errors.Count; x++)
+                            lcErrorMsg += "\r\nLine: " +
+                                         loCompiled.Errors[x].Line.ToString() + " - " +
+                                         loCompiled.Errors[x].ErrorText;
+
+                        instance.TabConsole.DisplayNotificationInChat(lcErrorMsg, ChatBufferTextStyle.Alert);
+                        return;
+                    }
+
+                    instance.TabConsole.DisplayNotificationInChat("Compilation successful.");
+                    Assembly loAssembly = loCompiled.CompiledAssembly;
+                    instance.MainForm.Invoke(new MethodInvoker(() => LoadAssembly("Dynamically compiled", loAssembly, true)));
+                }
+                catch (Exception ex)
+                {
+                    Logger.Log("Failed loading C# script: ", Helpers.LogLevel.Warning, ex);
+                }
+            });
+        }
+
         public void LoadAssembly(string loadfilename, Assembly assembly)
         {
+            LoadAssembly(loadfilename, assembly, false);
+        }
+
+        public void LoadAssembly(string loadfilename, Assembly assembly, bool startPlugins)
+        {
             foreach (Type type in assembly.GetTypes())
             {
                 if (typeof(IRadegastPlugin).IsAssignableFrom(type))
@@ -134,10 +207,10 @@ namespace Radegast
                     if (type.IsInterface) continue;
                     try
                     {
-                        IRadegastPlugin plug;
+                        IRadegastPlugin plug = null;
                         ConstructorInfo constructorInfo = type.GetConstructor(new Type[] { typeof(RadegastInstance) });
                         if (constructorInfo != null)
-                            plug = (IRadegastPlugin)constructorInfo.Invoke(new[] { this });
+                            plug = (IRadegastPlugin)constructorInfo.Invoke(new[] { instance });
                         else
                         {
                             constructorInfo = type.GetConstructor(new Type[] { });
@@ -150,6 +223,10 @@ namespace Radegast
                             }
                         }
                         lock (PluginsLoaded) PluginsLoaded.Add(plug);
+                        if (startPlugins && plug != null)
+                        {
+                            plug.StartPlugin(instance);
+                        }
                     }
                     catch (Exception ex)
                     {
index 194b9ba..7c77a8a 100644 (file)
@@ -301,7 +301,8 @@ namespace Radegast
             client.Groups.GroupDropped += new EventHandler<GroupDroppedEventArgs>(Groups_GroupsChanged);
             client.Groups.GroupJoinedReply += new EventHandler<GroupOperationEventArgs>(Groups_GroupsChanged);
             client.Avatars.UUIDNameReply += new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
-            netcom.ClientConnected += new EventHandler<EventArgs>(netcom_ClientConnected);
+            if (netcom != null)
+                netcom.ClientConnected += new EventHandler<EventArgs>(netcom_ClientConnected);
         }
 
         private void UnregisterClientEvents(GridClient client)
@@ -311,7 +312,8 @@ namespace Radegast
             client.Groups.GroupDropped -= new EventHandler<GroupDroppedEventArgs>(Groups_GroupsChanged);
             client.Groups.GroupJoinedReply -= new EventHandler<GroupOperationEventArgs>(Groups_GroupsChanged);
             client.Avatars.UUIDNameReply -= new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
-            netcom.ClientConnected -= new EventHandler<EventArgs>(netcom_ClientConnected);
+            if (netcom != null)
+                netcom.ClientConnected -= new EventHandler<EventArgs>(netcom_ClientConnected);
         }
 
         public void Reconnect()
index 8e24340..102bf88 100644 (file)
@@ -101,9 +101,11 @@ namespace Radegast
             this.homeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.autopilotToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.cleanCacheToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+            this.reloadInventoryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.setAppearanceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.rebakeTexturesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.tbnPlugins = new System.Windows.Forms.ToolStripDropDownButton();
+            this.btnLoadScript = new System.Windows.Forms.ToolStripMenuItem();
             this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
             this.tbtnFriends = new System.Windows.Forms.ToolStripButton();
             this.tbtnGroups = new System.Windows.Forms.ToolStripButton();
@@ -135,7 +137,6 @@ namespace Radegast
             this.toolStripContainer1 = new System.Windows.Forms.ToolStripContainer();
             this.pnlDialog = new System.Windows.Forms.Panel();
             this.timerWorldClock = new System.Windows.Forms.Timer(this.components);
-            this.reloadInventoryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.toolStrip1.SuspendLayout();
             this.statusStrip1.SuspendLayout();
             this.toolStripContainer1.TopToolStripPanel.SuspendLayout();
@@ -409,6 +410,13 @@ namespace Radegast
             this.cleanCacheToolStripMenuItem.Text = "Clean Cache";
             this.cleanCacheToolStripMenuItem.Click += new System.EventHandler(this.cleanCacheToolStripMenuItem_Click);
             // 
+            // reloadInventoryToolStripMenuItem
+            // 
+            this.reloadInventoryToolStripMenuItem.Name = "reloadInventoryToolStripMenuItem";
+            this.reloadInventoryToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
+            this.reloadInventoryToolStripMenuItem.Text = "Reload Inventory";
+            this.reloadInventoryToolStripMenuItem.Click += new System.EventHandler(this.reloadInventoryToolStripMenuItem_Click);
+            // 
             // setAppearanceToolStripMenuItem
             // 
             this.setAppearanceToolStripMenuItem.Name = "setAppearanceToolStripMenuItem";
@@ -426,12 +434,21 @@ namespace Radegast
             // tbnPlugins
             // 
             this.tbnPlugins.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
+            this.tbnPlugins.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
+            this.btnLoadScript});
             this.tbnPlugins.Image = ((System.Drawing.Image)(resources.GetObject("tbnPlugins.Image")));
             this.tbnPlugins.ImageTransparentColor = System.Drawing.Color.Magenta;
             this.tbnPlugins.Name = "tbnPlugins";
             this.tbnPlugins.Size = new System.Drawing.Size(59, 22);
             this.tbnPlugins.Text = "&Plugins";
-            this.tbnPlugins.Visible = false;
+            // 
+            // btnLoadScript
+            // 
+            this.btnLoadScript.Name = "btnLoadScript";
+            this.btnLoadScript.Size = new System.Drawing.Size(160, 22);
+            this.btnLoadScript.Text = "Load C# Script...";
+            this.btnLoadScript.Visible = false;
+            this.btnLoadScript.Click += new System.EventHandler(this.btnLoadScript_Click);
             // 
             // toolStripSeparator1
             // 
@@ -750,13 +767,6 @@ namespace Radegast
             this.timerWorldClock.Interval = 1000;
             this.timerWorldClock.Tick += new System.EventHandler(this.timerWorldClock_Tick);
             // 
-            // reloadInventoryToolStripMenuItem
-            // 
-            this.reloadInventoryToolStripMenuItem.Name = "reloadInventoryToolStripMenuItem";
-            this.reloadInventoryToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
-            this.reloadInventoryToolStripMenuItem.Text = "Reload Inventory";
-            this.reloadInventoryToolStripMenuItem.Click += new System.EventHandler(this.reloadInventoryToolStripMenuItem_Click);
-            // 
             // frmMain
             // 
             this.AutoSavePosition = true;
@@ -855,6 +865,7 @@ namespace Radegast
         private System.Windows.Forms.ToolStripButton tbtnVoice;
         public System.Windows.Forms.ToolStripMenuItem testToolStripMenuItem;
         private System.Windows.Forms.ToolStripMenuItem reloadInventoryToolStripMenuItem;
+        private System.Windows.Forms.ToolStripMenuItem btnLoadScript;
     }
 }
 
index 577423e..b3ba2e2 100644 (file)
@@ -199,11 +199,17 @@ namespace Radegast
 
         void frmMain_Disposed(object sender, EventArgs e)
         {
-            netcom.NetcomSync = null;
-            netcom.ClientLoginStatus -= new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
-            netcom.ClientLoggedOut -= new EventHandler(netcom_ClientLoggedOut);
-            netcom.ClientDisconnected -= new EventHandler<DisconnectedEventArgs>(netcom_ClientDisconnected);
-            UnregisterClientEvents(client);
+            if (netcom != null)
+            {
+                netcom.NetcomSync = null;
+                netcom.ClientLoginStatus -= new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
+                netcom.ClientLoggedOut -= new EventHandler(netcom_ClientLoggedOut);
+                netcom.ClientDisconnected -= new EventHandler<DisconnectedEventArgs>(netcom_ClientDisconnected);
+            }
+            if (client != null)
+            {
+                UnregisterClientEvents(client);
+            }
             this.instance.CleanUp();
         }
         #endregion
@@ -1308,6 +1314,10 @@ namespace Radegast
             }
         }
 
+        private void btnLoadScript_Click(object sender, EventArgs e)
+        {
+            //instance.PluginManager.LoadCSharpScriptFile("DemoPlugin.cs");
+        }
         #endregion
     }
 }
\ No newline at end of file
index ec2f1be..792c92f 100644 (file)
@@ -30,7 +30,6 @@
 //
 using System;
 using System.Collections.Generic;
-using System.Linq;
 using System.Text;
 using System.Threading;
 using System.Windows.Forms;