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.Windows.Forms;
\r
36 using Radegast.Netcom;
\r
37 using OpenMetaverse;
\r
41 public class RadegastInstance
\r
43 private GridClient client;
\r
44 private RadegastNetcom netcom;
\r
46 private ImageCache imageCache;
\r
47 private StateManager state;
\r
48 private ConfigManager config;
\r
50 private frmMain mainForm;
\r
51 private TabsConsole tabsConsole;
\r
53 // Singleton, there can be only one instance
\r
54 private static RadegastInstance globalInstance = null;
\r
55 public static RadegastInstance GlobalInstance
\r
59 if (globalInstance == null)
\r
61 globalInstance = new RadegastInstance();
\r
63 return globalInstance;
\r
67 private string userDir;
\r
69 /// System (not grid!) user's dir
\r
71 public string UserDir { get { return userDir; } }
\r
74 /// Grid client's user dir for settings and logs
\r
76 public string ClientDir
\r
80 if (client != null && client.Self != null && !string.IsNullOrEmpty(client.Self.Name))
\r
82 return Path.Combine(userDir, client.Self.Name);
\r
86 return Environment.CurrentDirectory;
\r
91 public string InventoryCacheFileName { get { return Path.Combine(ClientDir, "inventory.cache"); } }
\r
93 private string animCacheDir;
\r
94 public string AnimCacheDir { get { return animCacheDir; } }
\r
96 private string globalLogFile;
\r
97 public string GlobalLogFile { get { return globalLogFile; } }
\r
99 private bool monoRuntime;
\r
100 public bool MonoRuntime { get { return monoRuntime; } }
\r
102 private Dictionary<UUID, Group> groups;
\r
103 public Dictionary<UUID, Group> Groups { get { return groups; } }
\r
105 public Dictionary<UUID, string> nameCache = new Dictionary<UUID,string>();
\r
107 public const string INCOMPLETE_NAME = "Loading...";
\r
109 public readonly bool advancedDebugging = false;
\r
111 public readonly List<IRadegastPlugin> PluginsLoaded = new List<IRadegastPlugin>();
\r
113 private RadegastInstance()
\r
115 InitializeLoggingAndConfig();
\r
117 client = new GridClient();
\r
118 client.Settings.USE_INTERPOLATION_TIMER = false;
\r
119 client.Settings.ALWAYS_REQUEST_OBJECTS = true;
\r
120 client.Settings.ALWAYS_DECODE_OBJECTS = true;
\r
121 client.Settings.OBJECT_TRACKING = true;
\r
122 client.Settings.ENABLE_SIMSTATS = true;
\r
123 client.Settings.FETCH_MISSING_INVENTORY = true;
\r
124 client.Settings.MULTIPLE_SIMS = true;
\r
125 client.Settings.SEND_AGENT_THROTTLE = true;
\r
126 client.Settings.SEND_AGENT_UPDATES = true;
\r
128 client.Settings.USE_ASSET_CACHE = true;
\r
129 client.Settings.ASSET_CACHE_DIR = Path.Combine(userDir, "cache");
\r
130 client.Assets.Cache.AutoPruneEnabled = false;
\r
132 client.Throttle.Texture = 2446000.0f;
\r
133 client.Throttle.Asset = 2446000.0f;
\r
134 client.Settings.THROTTLE_OUTGOING_PACKETS = true;
\r
135 client.Settings.LOGIN_TIMEOUT = 120 * 1000;
\r
136 client.Settings.SIMULATOR_TIMEOUT = 120 * 1000;
\r
137 client.Settings.MAX_CONCURRENT_TEXTURE_DOWNLOADS = 20;
\r
139 netcom = new RadegastNetcom(client);
\r
140 imageCache = new ImageCache();
\r
141 state = new StateManager(this);
\r
143 InitializeConfigLegacy();
\r
145 mainForm = new frmMain(this);
\r
146 mainForm.InitializeControls();
\r
147 tabsConsole = mainForm.TabConsole;
\r
149 Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
\r
150 groups = new Dictionary<UUID, Group>();
\r
152 client.Groups.OnCurrentGroups += new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
\r
153 client.Groups.OnGroupLeft += new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);
\r
154 client.Groups.OnGroupDropped += new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);
\r
155 client.Groups.OnGroupJoined += new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);
\r
156 client.Avatars.OnAvatarNames += new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
157 ScanAndLoadPlugins();
\r
160 private void ScanAndLoadPlugins()
\r
162 string dirName = Application.StartupPath;
\r
164 if (!Directory.Exists(dirName)) return;
\r
166 foreach (string loadfilename in Directory.GetFiles(dirName))
\r
168 if (loadfilename.ToLower().EndsWith(".dll") || loadfilename.ToLower().EndsWith(".exe"))
\r
172 Assembly assembly = Assembly.LoadFile(loadfilename);
\r
173 foreach (Type type in assembly.GetTypes())
\r
175 if (typeof(IRadegastPlugin).IsAssignableFrom(type))
\r
177 foreach (var ci in type.GetConstructors())
\r
179 if (ci.GetParameters().Length > 0) continue;
\r
182 IRadegastPlugin plug = (IRadegastPlugin) ci.Invoke(new object[0]);
\r
183 plug.StartPlugin(this);
\r
184 lock (PluginsLoaded) PluginsLoaded.Add(plug);
\r
187 catch (Exception ex)
\r
189 Logger.Log("ERROR in Radegast Plugin: " + ex.Message, Helpers.LogLevel.Debug);
\r
194 } catch (BadImageFormatException) {
\r
196 } catch (ReflectionTypeLoadException) {
\r
197 // Out of date or dlls missing sub dependencies
\r
203 public void CleanUp()
\r
205 if (client != null)
\r
207 client.Groups.OnCurrentGroups -= new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
\r
208 client.Groups.OnGroupLeft -= new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);
\r
209 client.Groups.OnGroupDropped -= new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);
\r
210 client.Groups.OnGroupJoined -= new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);
\r
211 client.Avatars.OnAvatarNames -= new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
213 lock (PluginsLoaded)
\r
215 PluginsLoaded.ForEach(plug =>
\r
219 plug.StopPlugin(this);
\r
221 catch (Exception){}
\r
226 Environment.Exit(0);
\r
232 void Avatars_OnAvatarNames(Dictionary<UUID, string> names)
\r
236 foreach (KeyValuePair<UUID, string> av in names)
\r
238 //if (OnAvatarName != null) try { OnAvatarName(av.Key, av.Value); }
\r
239 // catch (Exception) { };
\r
241 if (!nameCache.ContainsKey(av.Key))
\r
243 nameCache.Add(av.Key, av.Value);
\r
249 public string getAvatarName(UUID key)
\r
253 if (key == UUID.Zero)
\r
255 return "(???) (???)";
\r
257 if (nameCache.ContainsKey(key))
\r
259 return nameCache[key];
\r
263 client.Avatars.RequestAvatarName(key);
\r
264 return INCOMPLETE_NAME;
\r
269 public void getAvatarNames(List<UUID> keys)
\r
273 List<UUID> newNames = new List<UUID>();
\r
274 foreach (UUID key in keys)
\r
276 if (!nameCache.ContainsKey(key))
\r
281 if (newNames.Count > 0)
\r
283 client.Avatars.RequestAvatarNames(newNames);
\r
288 public bool haveAvatarName(UUID key)
\r
292 if (nameCache.ContainsKey(key))
\r
299 void Groups_OnGroupJoined(UUID groupID, bool success)
\r
301 client.Groups.RequestCurrentGroups();
\r
304 void Groups_OnGroupLeft(UUID groupID, bool success)
\r
306 client.Groups.RequestCurrentGroups();
\r
309 void Groups_OnGroupDropped(UUID groupID)
\r
311 client.Groups.RequestCurrentGroups();
\r
314 public void LogClientMessage(string fileName, string message)
\r
320 foreach (char lDisallowed in System.IO.Path.GetInvalidFileNameChars())
\r
322 fileName = fileName.Replace(lDisallowed.ToString(), "_");
\r
325 StreamWriter logfile = File.AppendText(Path.Combine(ClientDir, fileName));
\r
326 logfile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd [HH:mm:ss] ") + message);
\r
330 catch (Exception) { }
\r
334 void Groups_OnCurrentGroups(Dictionary<UUID, Group> gr)
\r
339 private void Application_ApplicationExit(object sender, EventArgs e)
\r
341 config.SaveCurrentConfig();
\r
344 private void InitializeLoggingAndConfig()
\r
346 // Are we running mono?
\r
347 if (null == Type.GetType("Mono.Runtime"))
\r
349 monoRuntime = false;
\r
353 monoRuntime = true;
\r
358 userDir = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), Properties.Resources.ProgramName);
\r
359 if (!Directory.Exists(userDir))
\r
361 Directory.CreateDirectory(userDir);
\r
366 userDir = System.Environment.CurrentDirectory;
\r
369 animCacheDir = Path.Combine(userDir, @"anim_cache");
\r
370 globalLogFile = Path.Combine(userDir, Properties.Resources.ProgramName + ".log");
\r
373 private void InitializeConfigLegacy()
\r
375 config = new ConfigManager(this);
\r
376 config.ApplyDefault();
\r
378 netcom.LoginOptions.FirstName = config.CurrentConfig.FirstName;
\r
379 netcom.LoginOptions.LastName = config.CurrentConfig.LastName;
\r
380 netcom.LoginOptions.Password = config.CurrentConfig.PasswordMD5;
\r
381 netcom.LoginOptions.IsPasswordMD5 = true;
\r
384 public GridClient Client
\r
386 get { return client; }
\r
389 public RadegastNetcom Netcom
\r
391 get { return netcom; }
\r
394 public ImageCache ImageCache
\r
396 get { return imageCache; }
\r
399 public StateManager State
\r
401 get { return state; }
\r
404 public ConfigManager Config
\r
406 get { return config; }
\r
409 public frmMain MainForm
\r
411 get { return mainForm; }
\r
414 public TabsConsole TabConsole
\r
416 get { return tabsConsole; }
\r