OSDN Git Service

RAD-7 RAD-172: Complete inventory search, recent and worn filters
[radegast/radegast.git] / Radegast / Core / RadegastInstance.cs
index 194b9ba..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
@@ -53,6 +53,7 @@ namespace Radegast
             if (RadegastFormCreated != null) RadegastFormCreated(radForm);
         }
         #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
@@ -178,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
@@ -245,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;
 
@@ -259,6 +278,8 @@ namespace Radegast
             InitializeClient(client);
 
             rlv = new RLVManager(this);
+            gridManager = new GridManager(this);
+            gridManager.LoadGrids();
 
             mainForm = new frmMain(this);
             mainForm.InitializeControls();
@@ -286,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;
@@ -301,7 +322,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,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);
@@ -328,6 +387,12 @@ namespace Radegast
 
         public void CleanUp()
         {
+            if (gridManager != null)
+            {
+                gridManager.Dispose();
+                gridManager = null;
+            }
+
             if (rlv != null)
             {
                 rlv.Dispose();
@@ -417,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)