OSDN Git Service

RAD-7 RAD-172: Complete inventory search, recent and worn filters
[radegast/radegast.git] / Radegast / Core / RadegastInstance.cs
index 4aca46b..70cbd78 100644 (file)
@@ -1,6 +1,6 @@
 // 
 // Radegast Metaverse Client
-// Copyright (c) 2009, Radegast Development Team
+// Copyright (c) 2009-2010, Radegast Development Team
 // All rights reserved.
 // 
 // Redistribution and use in source and binary forms, with or without
@@ -52,7 +52,8 @@ namespace Radegast
         {
             if (RadegastFormCreated != null) RadegastFormCreated(radForm);
         }
-        #endregion        
+        #endregion
+
         private GridClient client;
         private RadegastNetcom netcom;
 
@@ -74,6 +75,16 @@ namespace Radegast
             }
         }
 
+        /// <summary>
+        /// When was Radegast started (UTC)
+        /// </summary>
+        public readonly DateTime StartupTimeUTC;
+
+        /// <summary>
+        /// Time zone of the current world (currently hard coded to US Pacific time)
+        /// </summary>
+        public TimeZoneInfo WordTimeZone;
+
         private string userDir;
         /// <summary>
         /// System (not grid!) user's dir
@@ -127,7 +138,9 @@ namespace Radegast
 
         public readonly bool advancedDebugging = false;
 
-        public readonly List<IRadegastPlugin> PluginsLoaded = new List<IRadegastPlugin>();
+        private PluginManager pluginManager;
+        /// <summary> Handles loading plugins and scripts</summary>
+        public PluginManager PluginManager { get { return pluginManager; } }
 
         private MediaManager mediaManager;
         /// <summary>
@@ -176,6 +189,10 @@ namespace Radegast
         /// </summary>
         public RLVManager RLV { get { return rlv; } }
 
+        private GridManager gridManager;
+        /// <summary>Manages default params for different grids</summary>
+        public GridManager GridManger { get { return gridManager; } }
+
         #region Events
 
         #region ClientChanged event
@@ -243,6 +260,10 @@ namespace Radegast
 
             client = client0;
 
+            // Initialize current time zone, and mark when we started
+            GetWorldTimeZone();
+            StartupTimeUTC = DateTime.UtcNow;
+
             // Are we running mono?
             monoRuntime = Type.GetType("Mono.Runtime") != null;
 
@@ -257,12 +278,15 @@ namespace Radegast
             InitializeClient(client);
 
             rlv = new RLVManager(this);
+            gridManager = new GridManager(this);
+            gridManager.LoadGrids();
 
             mainForm = new frmMain(this);
             mainForm.InitializeControls();
 
             mainForm.Load += new EventHandler(mainForm_Load);
-            ScanAndLoadPlugins();
+            pluginManager = new PluginManager(this);
+            pluginManager.ScanAndLoadPlugins();
         }
 
         private void InitializeClient(GridClient client)
@@ -283,7 +307,7 @@ namespace Radegast
             client.Assets.Cache.AutoPruneEnabled = false;
 
             client.Throttle.Total = 5000000f;
-            client.Settings.THROTTLE_OUTGOING_PACKETS = true;
+            client.Settings.THROTTLE_OUTGOING_PACKETS = false;
             client.Settings.LOGIN_TIMEOUT = 120 * 1000;
             client.Settings.SIMULATOR_TIMEOUT = 120 * 1000;
             client.Settings.MAX_CONCURRENT_TEXTURE_DOWNLOADS = 20;
@@ -298,8 +322,9 @@ 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)
         {
@@ -308,9 +333,46 @@ 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 GetWorldTimeZone()
+        {
+            try
+            {
+                foreach (TimeZoneInfo tz in TimeZoneInfo.GetSystemTimeZones())
+                {
+                    if (tz.Id == "Pacific Standard Time" || tz.Id == "America/Los_Angeles")
+                    {
+                        WordTimeZone = tz;
+                        break;
+                    }
+                }
+            }
+            catch (Exception) { }
+        }
+
+        public DateTime GetWorldTime()
+        {
+            DateTime now;
+            
+            try
+            {
+                if (WordTimeZone != null)
+                    now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, WordTimeZone);
+                else
+                    now = DateTime.UtcNow.AddHours(-7);
+            }
+            catch (Exception)
+            {
+                now = DateTime.UtcNow.AddHours(-7);
+            }
+
+            return now;
         }
 
+
         public void Reconnect()
         {
             TabConsole.DisplayNotificationInChat("Attempting to reconnect...", ChatBufferTextStyle.StatusDarkBlue);
@@ -325,6 +387,12 @@ namespace Radegast
 
         public void CleanUp()
         {
+            if (gridManager != null)
+            {
+                gridManager.Dispose();
+                gridManager = null;
+            }
+
             if (rlv != null)
             {
                 rlv.Dispose();
@@ -336,21 +404,10 @@ namespace Radegast
                 UnregisterClientEvents(client);
             }
 
-            lock (PluginsLoaded)
+            if (pluginManager != null)
             {
-                List<IRadegastPlugin> unload = new List<IRadegastPlugin>(PluginsLoaded);
-                unload.ForEach(plug =>
-               {
-                   PluginsLoaded.Remove(plug);
-                   try
-                   {
-                       plug.StopPlugin(this);
-                   }
-                   catch (Exception ex)
-                   {
-                       Logger.Log("ERROR in Shutdown Plugin: " + plug + " because " + ex, Helpers.LogLevel.Debug, ex);
-                   }
-               });
+                pluginManager.Dispose();
+                pluginManager = null;
             }
 
             if (movement != null)
@@ -392,107 +449,7 @@ namespace Radegast
 
         void mainForm_Load(object sender, EventArgs e)
         {
-            StartPlugins();
-        }
-
-        private void StartPlugins()
-        {
-            lock (PluginsLoaded)
-            {
-                foreach (IRadegastPlugin plug in PluginsLoaded)
-                {
-                    try
-                    {
-                        plug.StartPlugin(this);
-                    }
-                    catch (Exception ex)
-                    {
-                        Logger.Log("ERROR in Starting Radegast Plugin: " + plug + " because " + ex, Helpers.LogLevel.Debug);
-                    }
-                }
-            }
-        }
-
-        private void ScanAndLoadPlugins()
-        {
-            string dirName = Application.StartupPath;
-
-            if (!Directory.Exists(dirName)) return;
-
-            foreach (string loadfilename in Directory.GetFiles(dirName))
-            {
-                if (loadfilename.ToLower().EndsWith(".dll") || loadfilename.ToLower().EndsWith(".exe"))
-                {
-                    try
-                    {
-                        Assembly assembly = Assembly.LoadFile(loadfilename);
-                        LoadAssembly(loadfilename, assembly);
-                    }
-                    catch (BadImageFormatException)
-                    {
-                        // non .NET .dlls
-                    }
-                    catch (ReflectionTypeLoadException)
-                    {
-                        // Out of date or dlls missing sub dependencies
-                    }
-                    catch (TypeLoadException)
-                    {
-                        // Another version of: Out of date or dlls missing sub dependencies
-                    }
-                    catch (Exception ex)
-                    {
-                        Logger.Log("ERROR in Radegast Plugin: " + loadfilename + " because " + ex, Helpers.LogLevel.Debug);
-                    }
-                }
-            }
-        }
-
-        public void LoadAssembly(string loadfilename, Assembly assembly)
-        {
-            foreach (Type type in assembly.GetTypes())
-            {
-                if (typeof(IRadegastPlugin).IsAssignableFrom(type))
-                {
-                    if  (type.IsInterface) continue;
-                    try
-                    {
-                        IRadegastPlugin plug;
-                        ConstructorInfo constructorInfo = type.GetConstructor(new Type[] {typeof (RadegastInstance)});
-                        if (constructorInfo != null)
-                            plug = (IRadegastPlugin) constructorInfo.Invoke(new[] {this});
-                        else
-                        {
-                            constructorInfo = type.GetConstructor(new Type[] {});
-                            if (constructorInfo != null)
-                                plug = (IRadegastPlugin) constructorInfo.Invoke(new object[0]);
-                            else
-                            {
-                                Logger.Log("ERROR Constructing Radegast Plugin: " + loadfilename + " because "+type+ " has no usable constructor.",Helpers.LogLevel.Debug);
-                                continue;
-                            }
-                        }
-                        lock (PluginsLoaded) PluginsLoaded.Add(plug);
-                    }
-                    catch (Exception ex)
-                    {
-                        Logger.Log("ERROR Constructing Radegast Plugin: " + loadfilename + " because " + ex,
-                                   Helpers.LogLevel.Debug);
-                    }
-                }
-                else
-                {
-                    try
-                    {
-                        commandsManager.LoadType(type);
-                    }
-                    catch (Exception ex)
-                    {
-                        Logger.Log("ERROR in Radegast Plugin: " + loadfilename + " Command: " + type +
-                                   " because " + ex.Message + " " + ex.StackTrace, Helpers.LogLevel.Debug);
-                    }
-                }
-            }
+            pluginManager.StartPlugins();
         }
 
         void netcom_ClientConnected(object sender, EventArgs e)
@@ -525,6 +482,50 @@ namespace Radegast
             }
         }
 
+        /// <summary>
+        /// Fetches avatar name
+        /// </summary>
+        /// <param name="key">Avatar UUID</param>
+        /// <param name="blocking">Should we wait until the name is retrieved</param>
+        /// <returns>Avatar name</returns>
+        public string getAvatarName(UUID key, bool blocking)
+        {
+            if (!blocking)
+                return getAvatarName(key);
+
+            string name = null;
+
+            using (ManualResetEvent gotName = new ManualResetEvent(false))
+            {
+
+                EventHandler<UUIDNameReplyEventArgs> handler = (object sender, UUIDNameReplyEventArgs e) =>
+                    {
+                        if (e.Names.ContainsKey(key))
+                        {
+                            name = e.Names[key];
+                            gotName.Set();
+                        }
+                    };
+
+                client.Avatars.UUIDNameReply += handler;
+                name = getAvatarName(key);
+
+                if (name == INCOMPLETE_NAME)
+                {
+                    gotName.WaitOne(10 * 1000, false);
+                }
+
+                client.Avatars.UUIDNameReply -= handler;
+            }
+            return name;
+
+        }
+
+        /// <summary>
+        /// Fetches avatar name from cache, if not in cache will requst name from the server
+        /// </summary>
+        /// <param name="key">Avatar UUID</param>
+        /// <returns>Avatar name</returns>
         public string getAvatarName(UUID key)
         {
             lock (nameCache)
@@ -659,7 +660,7 @@ namespace Radegast
 
         public void HandleThreadException(object sender, ThreadExceptionEventArgs e)
         {
-            Logger.Log("Unhandled Thread Exception: " 
+            Logger.Log("Unhandled Thread Exception: "
                 + e.Exception.Message + Environment.NewLine
                 + e.Exception.StackTrace + Environment.NewLine,
                 Helpers.LogLevel.Error,