2 // Radegast Metaverse Client
\r
3 // Copyright (c) 2009, Radegast Development Team
\r
4 // All rights reserved.
\r
6 // Redistribution and use in source and binary forms, with or without
\r
7 // modification, are permitted provided that the following conditions are met:
\r
9 // * Redistributions of source code must retain the above copyright notice,
\r
10 // this list of conditions and the following disclaimer.
\r
11 // * Redistributions in binary form must reproduce the above copyright
\r
12 // notice, this list of conditions and the following disclaimer in the
\r
13 // documentation and/or other materials provided with the distribution.
\r
14 // * Neither the name of the application "Radegast", nor the names of its
\r
15 // contributors may be used to endorse or promote products derived from
\r
16 // this software without specific prior written permission.
\r
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
32 using System.Collections.Generic;
\r
34 using System.Reflection;
\r
35 using System.Threading;
\r
36 using System.Windows.Forms;
\r
37 using Radegast.Commands;
\r
38 using Radegast.Netcom;
\r
39 using Radegast.Media;
\r
40 using OpenMetaverse;
\r
44 public class RadegastInstance
\r
46 private GridClient client;
\r
47 private RadegastNetcom netcom;
\r
49 private StateManager state;
\r
51 private frmMain mainForm;
\r
52 private TabsConsole tabsConsole;
\r
54 // Singleton, there can be only one instance
\r
55 private static RadegastInstance globalInstance = null;
\r
56 public static RadegastInstance GlobalInstance
\r
60 if (globalInstance == null)
\r
62 globalInstance = new RadegastInstance(new GridClient());
\r
64 return globalInstance;
\r
68 private string userDir;
\r
70 /// System (not grid!) user's dir
\r
72 public string UserDir { get { return userDir; } }
\r
75 /// Grid client's user dir for settings and logs
\r
77 public string ClientDir
\r
81 if (client != null && client.Self != null && !string.IsNullOrEmpty(client.Self.Name))
\r
83 return Path.Combine(userDir, client.Self.Name);
\r
87 return Environment.CurrentDirectory;
\r
92 public string InventoryCacheFileName { get { return Path.Combine(ClientDir, "inventory.cache"); } }
\r
94 private string animCacheDir;
\r
95 public string AnimCacheDir { get { return animCacheDir; } }
\r
97 private string globalLogFile;
\r
98 public string GlobalLogFile { get { return globalLogFile; } }
\r
100 private bool monoRuntime;
\r
101 public bool MonoRuntime { get { return monoRuntime; } }
\r
103 private Dictionary<UUID, Group> groups;
\r
104 public Dictionary<UUID, Group> Groups { get { return groups; } }
\r
106 private Settings globalSettings;
\r
108 /// Global settings for the entire application
\r
110 public Settings GlobalSettings { get { return globalSettings; } }
\r
112 private Settings clientSettings;
\r
114 /// Per client settings
\r
116 public Settings ClientSettings { get { return clientSettings; } }
\r
118 public Dictionary<UUID, string> nameCache = new Dictionary<UUID, string>();
\r
120 public const string INCOMPLETE_NAME = "Loading...";
\r
122 public readonly bool advancedDebugging = false;
\r
124 public readonly List<IRadegastPlugin> PluginsLoaded = new List<IRadegastPlugin>();
\r
126 private MediaManager mediaManager;
\r
128 /// Radegast media manager for playing streams and in world sounds
\r
130 public MediaManager MediaManager { get { return mediaManager; } }
\r
133 private CommandsManager commandsManager;
\r
135 /// Radegast command manager for executing textual console commands
\r
137 public CommandsManager CommandsManager { get { return commandsManager; } }
\r
140 /// Radegast ContextAction manager for context sensitive actions
\r
142 public ContextActionsManager ContextActionManager { get; private set; }
\r
144 private SleekMovement movement;
\r
146 /// Allows key emulation for moving avatar around
\r
148 public SleekMovement Movement { get { return movement; } }
\r
150 public RadegastInstance(GridClient client0)
\r
152 // incase something else calls GlobalInstance while we are loading
\r
153 globalInstance = this;
\r
154 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
\r
155 Application.ThreadException += HandleThreadException;
\r
158 // Are we running mono?
\r
159 monoRuntime = Type.GetType("Mono.Runtime") != null;
\r
161 netcom = new RadegastNetcom(client);
\r
162 state = new StateManager(this);
\r
163 mediaManager = new MediaManager(this);
\r
164 commandsManager = new CommandsManager(this);
\r
165 ContextActionManager = new ContextActionsManager(this);
\r
166 movement = new SleekMovement(client);
\r
168 InitializeLoggingAndConfig();
\r
170 client.Settings.MULTIPLE_SIMS = true;
\r
172 client.Settings.USE_INTERPOLATION_TIMER = false;
\r
173 client.Settings.ALWAYS_REQUEST_OBJECTS = true;
\r
174 client.Settings.ALWAYS_DECODE_OBJECTS = true;
\r
175 client.Settings.OBJECT_TRACKING = true;
\r
176 client.Settings.ENABLE_SIMSTATS = true;
\r
177 client.Settings.FETCH_MISSING_INVENTORY = true;
\r
178 client.Settings.SEND_AGENT_THROTTLE = true;
\r
179 client.Settings.SEND_AGENT_UPDATES = true;
\r
181 client.Settings.USE_ASSET_CACHE = true;
\r
182 client.Settings.ASSET_CACHE_DIR = Path.Combine(userDir, "cache");
\r
183 client.Assets.Cache.AutoPruneEnabled = false;
\r
185 client.Throttle.Texture = 2446000.0f;
\r
186 client.Throttle.Asset = 2446000.0f;
\r
187 client.Settings.THROTTLE_OUTGOING_PACKETS = true;
\r
188 client.Settings.LOGIN_TIMEOUT = 120 * 1000;
\r
189 client.Settings.SIMULATOR_TIMEOUT = 120 * 1000;
\r
190 client.Settings.MAX_CONCURRENT_TEXTURE_DOWNLOADS = 20;
\r
192 mainForm = new frmMain(this);
\r
193 mainForm.InitializeControls();
\r
194 tabsConsole = mainForm.TabConsole;
\r
196 groups = new Dictionary<UUID, Group>();
\r
198 client.Groups.OnCurrentGroups += new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
\r
199 client.Groups.OnGroupLeft += new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);
\r
200 client.Groups.OnGroupDropped += new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);
\r
201 client.Groups.OnGroupJoined += new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);
\r
202 client.Avatars.OnAvatarNames += new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
203 client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected);
\r
204 ScanAndLoadPlugins();
\r
207 public void CleanUp()
\r
209 if (client != null)
\r
211 client.Groups.OnCurrentGroups -= new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
\r
212 client.Groups.OnGroupLeft -= new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);
\r
213 client.Groups.OnGroupDropped -= new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);
\r
214 client.Groups.OnGroupJoined -= new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);
\r
215 client.Avatars.OnAvatarNames -= new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
216 client.Network.OnConnected -= new NetworkManager.ConnectedCallback(Network_OnConnected);
\r
219 lock (PluginsLoaded)
\r
221 List<IRadegastPlugin> unload = new List<IRadegastPlugin>(PluginsLoaded);
\r
222 unload.ForEach(plug =>
\r
224 PluginsLoaded.Remove(plug);
\r
227 plug.StopPlugin(this);
\r
229 catch (Exception ex)
\r
231 Logger.Log("ERROR in Shutdown Plugin: " + plug + " because " + ex, Helpers.LogLevel.Debug, ex);
\r
236 if (movement != null)
\r
238 movement.Dispose();
\r
241 if (commandsManager != null)
\r
243 commandsManager.Dispose();
\r
244 commandsManager = null;
\r
246 if (ContextActionManager != null)
\r
248 ContextActionManager.Dispose();
\r
249 ContextActionManager = null;
\r
251 if (mediaManager != null)
\r
253 mediaManager.Dispose();
\r
254 mediaManager = null;
\r
261 if (netcom != null)
\r
267 Logger.Log("RadegastInstance finished cleaning up.", Helpers.LogLevel.Debug);
\r
270 private void ScanAndLoadPlugins()
\r
272 string dirName = Application.StartupPath;
\r
274 if (!Directory.Exists(dirName)) return;
\r
276 foreach (string loadfilename in Directory.GetFiles(dirName))
\r
278 if (loadfilename.ToLower().EndsWith(".dll") || loadfilename.ToLower().EndsWith(".exe"))
\r
282 Assembly assembly = Assembly.LoadFile(loadfilename);
\r
283 LoadAssembly(loadfilename, assembly);
\r
285 catch (BadImageFormatException)
\r
289 catch (ReflectionTypeLoadException)
\r
291 // Out of date or dlls missing sub dependencies
\r
293 catch (Exception ex)
\r
295 Logger.Log("ERROR in Radegast Plugin: " + loadfilename + " because " + ex.Message + " " + ex.StackTrace, Helpers.LogLevel.Debug);
\r
301 public void LoadAssembly(string loadfilename, Assembly assembly)
\r
303 foreach (Type type in assembly.GetTypes())
\r
305 if (typeof(IRadegastPlugin).IsAssignableFrom(type))
\r
307 foreach (var ci in type.GetConstructors())
\r
309 if (ci.GetParameters().Length > 0) continue;
\r
312 IRadegastPlugin plug = (IRadegastPlugin)ci.Invoke(new object[0]);
\r
313 plug.StartPlugin(this);
\r
314 lock (PluginsLoaded) PluginsLoaded.Add(plug);
\r
317 catch (Exception ex)
\r
319 Logger.Log("ERROR Constructing Radegast Plugin: " + loadfilename + " because " + ex.Message, Helpers.LogLevel.Debug);
\r
328 commandsManager.LoadType(type);
\r
329 ContextActionManager.LoadType(type);
\r
331 catch (Exception ex)
\r
333 Logger.Log("ERROR in Radegast Plugin: " + loadfilename + " Command: " + type +
\r
334 " because " + ex.Message + " " + ex.StackTrace, Helpers.LogLevel.Debug);
\r
340 void Network_OnConnected(object sender)
\r
344 if (!Directory.Exists(ClientDir))
\r
345 Directory.CreateDirectory(ClientDir);
\r
347 catch (Exception ex)
\r
349 Logger.Log("Failed to create client directory", Helpers.LogLevel.Warning, ex);
\r
352 clientSettings = new Settings(Path.Combine(ClientDir, "client_settings.xml"));
\r
355 void Avatars_OnAvatarNames(Dictionary<UUID, string> names)
\r
359 foreach (KeyValuePair<UUID, string> av in names)
\r
361 if (!nameCache.ContainsKey(av.Key))
\r
363 nameCache.Add(av.Key, av.Value);
\r
369 public string getAvatarName(UUID key)
\r
373 if (key == UUID.Zero)
\r
375 return "(???) (???)";
\r
377 if (nameCache.ContainsKey(key))
\r
379 return nameCache[key];
\r
383 client.Avatars.RequestAvatarName(key);
\r
384 return INCOMPLETE_NAME;
\r
389 public void getAvatarNames(List<UUID> keys)
\r
393 List<UUID> newNames = new List<UUID>();
\r
394 foreach (UUID key in keys)
\r
396 if (!nameCache.ContainsKey(key))
\r
401 if (newNames.Count > 0)
\r
403 client.Avatars.RequestAvatarNames(newNames);
\r
408 public bool haveAvatarName(UUID key)
\r
412 if (nameCache.ContainsKey(key))
\r
419 void Groups_OnGroupJoined(UUID groupID, bool success)
\r
421 client.Groups.RequestCurrentGroups();
\r
424 void Groups_OnGroupLeft(UUID groupID, bool success)
\r
426 client.Groups.RequestCurrentGroups();
\r
429 void Groups_OnGroupDropped(UUID groupID)
\r
431 client.Groups.RequestCurrentGroups();
\r
434 public static string SafeFileName(string fileName)
\r
436 foreach (char lDisallowed in Path.GetInvalidFileNameChars())
\r
438 fileName = fileName.Replace(lDisallowed.ToString(), "_");
\r
444 public void LogClientMessage(string fileName, string message)
\r
450 foreach (char lDisallowed in System.IO.Path.GetInvalidFileNameChars())
\r
452 fileName = fileName.Replace(lDisallowed.ToString(), "_");
\r
455 StreamWriter logfile = File.AppendText(Path.Combine(ClientDir, fileName));
\r
456 logfile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd [HH:mm:ss] ") + message);
\r
460 catch (Exception) { }
\r
464 void Groups_OnCurrentGroups(Dictionary<UUID, Group> gr)
\r
469 private void InitializeLoggingAndConfig()
\r
473 userDir = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), Properties.Resources.ProgramName);
\r
474 if (!Directory.Exists(userDir))
\r
476 Directory.CreateDirectory(userDir);
\r
481 userDir = System.Environment.CurrentDirectory;
\r
484 animCacheDir = Path.Combine(userDir, @"anim_cache");
\r
485 globalLogFile = Path.Combine(userDir, Properties.Resources.ProgramName + ".log");
\r
486 globalSettings = new Settings(Path.Combine(userDir, "settings.xml"));
\r
489 public GridClient Client
\r
491 get { return client; }
\r
494 public RadegastNetcom Netcom
\r
496 get { return netcom; }
\r
499 public StateManager State
\r
501 get { return state; }
\r
504 public frmMain MainForm
\r
506 get { return mainForm; }
\r
509 public TabsConsole TabConsole
\r
511 get { return tabsConsole; }
\r
514 public void HandleThreadException(object sender, ThreadExceptionEventArgs e)
\r
516 Logger.Log("Unhandled Thread Exception: "+e.Exception + " in " +sender,Helpers.LogLevel.Error,client);
\r