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 StateManager state;
\r
47 private ConfigManager config;
\r
49 private frmMain mainForm;
\r
50 private TabsConsole tabsConsole;
\r
52 // Singleton, there can be only one instance
\r
53 private static RadegastInstance globalInstance = null;
\r
54 public static RadegastInstance GlobalInstance
\r
58 if (globalInstance == null)
\r
60 globalInstance = new RadegastInstance();
\r
62 return globalInstance;
\r
66 private string userDir;
\r
68 /// System (not grid!) user's dir
\r
70 public string UserDir { get { return userDir; } }
\r
73 /// Grid client's user dir for settings and logs
\r
75 public string ClientDir
\r
79 if (client != null && client.Self != null && !string.IsNullOrEmpty(client.Self.Name))
\r
81 return Path.Combine(userDir, client.Self.Name);
\r
85 return Environment.CurrentDirectory;
\r
90 public string InventoryCacheFileName { get { return Path.Combine(ClientDir, "inventory.cache"); } }
\r
92 private string animCacheDir;
\r
93 public string AnimCacheDir { get { return animCacheDir; } }
\r
95 private string globalLogFile;
\r
96 public string GlobalLogFile { get { return globalLogFile; } }
\r
98 private bool monoRuntime;
\r
99 public bool MonoRuntime { get { return monoRuntime; } }
\r
101 private Dictionary<UUID, Group> groups;
\r
102 public Dictionary<UUID, Group> Groups { get { return groups; } }
\r
104 public Dictionary<UUID, string> nameCache = new Dictionary<UUID, string>();
\r
106 public const string INCOMPLETE_NAME = "Loading...";
\r
108 public readonly bool advancedDebugging = false;
\r
110 public readonly List<IRadegastPlugin> PluginsLoaded = new List<IRadegastPlugin>();
\r
112 private RadegastInstance()
\r
114 InitializeLoggingAndConfig();
\r
116 client = new GridClient();
\r
117 client.Settings.USE_INTERPOLATION_TIMER = false;
\r
118 client.Settings.ALWAYS_REQUEST_OBJECTS = true;
\r
119 client.Settings.ALWAYS_DECODE_OBJECTS = true;
\r
120 client.Settings.OBJECT_TRACKING = true;
\r
121 client.Settings.ENABLE_SIMSTATS = true;
\r
122 client.Settings.FETCH_MISSING_INVENTORY = true;
\r
123 client.Settings.MULTIPLE_SIMS = false;
\r
124 client.Settings.SEND_AGENT_THROTTLE = true;
\r
125 client.Settings.SEND_AGENT_UPDATES = true;
\r
127 client.Settings.USE_ASSET_CACHE = true;
\r
128 client.Settings.ASSET_CACHE_DIR = Path.Combine(userDir, "cache");
\r
129 client.Assets.Cache.AutoPruneEnabled = false;
\r
131 client.Throttle.Texture = 2446000.0f;
\r
132 client.Throttle.Asset = 2446000.0f;
\r
133 client.Settings.THROTTLE_OUTGOING_PACKETS = true;
\r
134 client.Settings.LOGIN_TIMEOUT = 120 * 1000;
\r
135 client.Settings.SIMULATOR_TIMEOUT = 120 * 1000;
\r
136 client.Settings.MAX_CONCURRENT_TEXTURE_DOWNLOADS = 20;
\r
138 netcom = new RadegastNetcom(client);
\r
139 state = new StateManager(this);
\r
141 InitializeConfigLegacy();
\r
143 mainForm = new frmMain(this);
\r
144 mainForm.InitializeControls();
\r
145 tabsConsole = mainForm.TabConsole;
\r
147 Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
\r
148 groups = new Dictionary<UUID, Group>();
\r
150 client.Groups.OnCurrentGroups += new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
\r
151 client.Groups.OnGroupLeft += new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);
\r
152 client.Groups.OnGroupDropped += new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);
\r
153 client.Groups.OnGroupJoined += new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);
\r
154 client.Avatars.OnAvatarNames += new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
155 client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected);
\r
156 ScanAndLoadPlugins();
\r
159 public void CleanUp()
\r
161 if (client != null)
\r
163 client.Groups.OnCurrentGroups -= new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
\r
164 client.Groups.OnGroupLeft -= new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);
\r
165 client.Groups.OnGroupDropped -= new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);
\r
166 client.Groups.OnGroupJoined -= new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);
\r
167 client.Avatars.OnAvatarNames -= new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
168 client.Network.OnConnected -= new NetworkManager.ConnectedCallback(Network_OnConnected);
\r
171 lock (PluginsLoaded)
\r
173 PluginsLoaded.ForEach(plug =>
\r
177 plug.StopPlugin(this);
\r
179 catch (Exception) { }
\r
187 Logger.Log("RadegastInstance finished cleaning up.", Helpers.LogLevel.Debug);
\r
190 private void ScanAndLoadPlugins()
\r
192 string dirName = Application.StartupPath;
\r
194 if (!Directory.Exists(dirName)) return;
\r
196 foreach (string loadfilename in Directory.GetFiles(dirName))
\r
198 if (loadfilename.ToLower().EndsWith(".dll") || loadfilename.ToLower().EndsWith(".exe"))
\r
202 Assembly assembly = Assembly.LoadFile(loadfilename);
\r
203 foreach (Type type in assembly.GetTypes())
\r
205 if (typeof(IRadegastPlugin).IsAssignableFrom(type))
\r
207 foreach (var ci in type.GetConstructors())
\r
209 if (ci.GetParameters().Length > 0) continue;
\r
212 IRadegastPlugin plug = (IRadegastPlugin)ci.Invoke(new object[0]);
\r
213 plug.StartPlugin(this);
\r
214 lock (PluginsLoaded) PluginsLoaded.Add(plug);
\r
217 catch (Exception ex)
\r
219 Logger.Log("ERROR in Radegast Plugin: " + ex.Message, Helpers.LogLevel.Debug);
\r
225 catch (BadImageFormatException)
\r
229 catch (ReflectionTypeLoadException)
\r
231 // Out of date or dlls missing sub dependencies
\r
237 void Network_OnConnected(object sender)
\r
241 if (!Directory.Exists(ClientDir))
\r
242 Directory.CreateDirectory(ClientDir);
\r
244 catch (Exception ex)
\r
246 Logger.Log("Failed to create client directory", Helpers.LogLevel.Warning, ex);
\r
250 void Avatars_OnAvatarNames(Dictionary<UUID, string> names)
\r
254 foreach (KeyValuePair<UUID, string> av in names)
\r
256 if (!nameCache.ContainsKey(av.Key))
\r
258 nameCache.Add(av.Key, av.Value);
\r
264 public string getAvatarName(UUID key)
\r
268 if (key == UUID.Zero)
\r
270 return "(???) (???)";
\r
272 if (nameCache.ContainsKey(key))
\r
274 return nameCache[key];
\r
278 client.Avatars.RequestAvatarName(key);
\r
279 return INCOMPLETE_NAME;
\r
284 public void getAvatarNames(List<UUID> keys)
\r
288 List<UUID> newNames = new List<UUID>();
\r
289 foreach (UUID key in keys)
\r
291 if (!nameCache.ContainsKey(key))
\r
296 if (newNames.Count > 0)
\r
298 client.Avatars.RequestAvatarNames(newNames);
\r
303 public bool haveAvatarName(UUID key)
\r
307 if (nameCache.ContainsKey(key))
\r
314 void Groups_OnGroupJoined(UUID groupID, bool success)
\r
316 client.Groups.RequestCurrentGroups();
\r
319 void Groups_OnGroupLeft(UUID groupID, bool success)
\r
321 client.Groups.RequestCurrentGroups();
\r
324 void Groups_OnGroupDropped(UUID groupID)
\r
326 client.Groups.RequestCurrentGroups();
\r
329 public void LogClientMessage(string fileName, string message)
\r
335 foreach (char lDisallowed in System.IO.Path.GetInvalidFileNameChars())
\r
337 fileName = fileName.Replace(lDisallowed.ToString(), "_");
\r
340 StreamWriter logfile = File.AppendText(Path.Combine(ClientDir, fileName));
\r
341 logfile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd [HH:mm:ss] ") + message);
\r
345 catch (Exception) { }
\r
349 void Groups_OnCurrentGroups(Dictionary<UUID, Group> gr)
\r
354 private void Application_ApplicationExit(object sender, EventArgs e)
\r
356 config.SaveCurrentConfig();
\r
359 private void InitializeLoggingAndConfig()
\r
361 // Are we running mono?
\r
362 monoRuntime = Type.GetType("Mono.Runtime") != null;
\r
366 userDir = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), Properties.Resources.ProgramName);
\r
367 if (!Directory.Exists(userDir))
\r
369 Directory.CreateDirectory(userDir);
\r
374 userDir = System.Environment.CurrentDirectory;
\r
377 animCacheDir = Path.Combine(userDir, @"anim_cache");
\r
378 globalLogFile = Path.Combine(userDir, Properties.Resources.ProgramName + ".log");
\r
381 private void InitializeConfigLegacy()
\r
383 config = new ConfigManager(this);
\r
384 config.ApplyDefault();
\r
386 netcom.LoginOptions.FirstName = config.CurrentConfig.FirstName;
\r
387 netcom.LoginOptions.LastName = config.CurrentConfig.LastName;
\r
388 netcom.LoginOptions.Password = config.CurrentConfig.PasswordMD5;
\r
389 netcom.LoginOptions.IsPasswordMD5 = true;
\r
392 public GridClient Client
\r
394 get { return client; }
\r
397 public RadegastNetcom Netcom
\r
399 get { return netcom; }
\r
402 public StateManager State
\r
404 get { return state; }
\r
407 public ConfigManager Config
\r
409 get { return config; }
\r
412 public frmMain MainForm
\r
414 get { return mainForm; }
\r
417 public TabsConsole TabConsole
\r
419 get { return tabsConsole; }
\r