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
48 private frmMain mainForm;
\r
49 private TabsConsole tabsConsole;
\r
51 // Singleton, there can be only one instance
\r
52 private static RadegastInstance globalInstance = null;
\r
53 public static RadegastInstance GlobalInstance
\r
57 if (globalInstance == null)
\r
59 globalInstance = new RadegastInstance(new GridClient());
\r
61 return globalInstance;
\r
65 private string userDir;
\r
67 /// System (not grid!) user's dir
\r
69 public string UserDir { get { return userDir; } }
\r
72 /// Grid client's user dir for settings and logs
\r
74 public string ClientDir
\r
78 if (client != null && client.Self != null && !string.IsNullOrEmpty(client.Self.Name))
\r
80 return Path.Combine(userDir, client.Self.Name);
\r
84 return Environment.CurrentDirectory;
\r
89 public string InventoryCacheFileName { get { return Path.Combine(ClientDir, "inventory.cache"); } }
\r
91 private string animCacheDir;
\r
92 public string AnimCacheDir { get { return animCacheDir; } }
\r
94 private string globalLogFile;
\r
95 public string GlobalLogFile { get { return globalLogFile; } }
\r
97 private bool monoRuntime;
\r
98 public bool MonoRuntime { get { return monoRuntime; } }
\r
100 private Dictionary<UUID, Group> groups;
\r
101 public Dictionary<UUID, Group> Groups { get { return groups; } }
\r
103 private Settings globalSettings;
\r
105 /// Global settings for the entire application
\r
107 public Settings GlobalSettings { get { return globalSettings; } }
\r
109 private Settings clientSettings;
\r
111 /// Per client settings
\r
113 public Settings ClientSettings { get { return clientSettings; } }
\r
115 public Dictionary<UUID, string> nameCache = new Dictionary<UUID, string>();
\r
117 public const string INCOMPLETE_NAME = "Loading...";
\r
119 public readonly bool advancedDebugging = false;
\r
121 public readonly List<IRadegastPlugin> PluginsLoaded = new List<IRadegastPlugin>();
\r
123 public RadegastInstance(GridClient client0)
\r
125 // incase something else calls GlobalInstance while we are loading
\r
126 globalInstance = this;
\r
130 netcom = new RadegastNetcom(client);
\r
131 state = new StateManager(this);
\r
132 InitializeLoggingAndConfig();
\r
134 client.Settings.MULTIPLE_SIMS = true;
\r
136 client.Settings.USE_INTERPOLATION_TIMER = false;
\r
137 client.Settings.ALWAYS_REQUEST_OBJECTS = true;
\r
138 client.Settings.ALWAYS_DECODE_OBJECTS = true;
\r
139 client.Settings.OBJECT_TRACKING = true;
\r
140 client.Settings.ENABLE_SIMSTATS = true;
\r
141 client.Settings.FETCH_MISSING_INVENTORY = true;
\r
142 client.Settings.SEND_AGENT_THROTTLE = true;
\r
143 client.Settings.SEND_AGENT_UPDATES = true;
\r
145 client.Settings.USE_ASSET_CACHE = true;
\r
146 client.Settings.ASSET_CACHE_DIR = Path.Combine(userDir, "cache");
\r
147 client.Assets.Cache.AutoPruneEnabled = false;
\r
149 client.Throttle.Texture = 2446000.0f;
\r
150 client.Throttle.Asset = 2446000.0f;
\r
151 client.Settings.THROTTLE_OUTGOING_PACKETS = true;
\r
152 client.Settings.LOGIN_TIMEOUT = 120 * 1000;
\r
153 client.Settings.SIMULATOR_TIMEOUT = 120 * 1000;
\r
154 client.Settings.MAX_CONCURRENT_TEXTURE_DOWNLOADS = 20;
\r
156 mainForm = new frmMain(this);
\r
157 mainForm.InitializeControls();
\r
158 tabsConsole = mainForm.TabConsole;
\r
160 groups = new Dictionary<UUID, Group>();
\r
162 client.Groups.OnCurrentGroups += new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
\r
163 client.Groups.OnGroupLeft += new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);
\r
164 client.Groups.OnGroupDropped += new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);
\r
165 client.Groups.OnGroupJoined += new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);
\r
166 client.Avatars.OnAvatarNames += new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
167 client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected);
\r
168 ScanAndLoadPlugins();
\r
171 public void CleanUp()
\r
173 if (client != null)
\r
175 client.Groups.OnCurrentGroups -= new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
\r
176 client.Groups.OnGroupLeft -= new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);
\r
177 client.Groups.OnGroupDropped -= new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);
\r
178 client.Groups.OnGroupJoined -= new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);
\r
179 client.Avatars.OnAvatarNames -= new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
180 client.Network.OnConnected -= new NetworkManager.ConnectedCallback(Network_OnConnected);
\r
183 lock (PluginsLoaded)
\r
185 PluginsLoaded.ForEach(plug =>
\r
189 plug.StopPlugin(this);
\r
191 catch (Exception) { }
\r
199 Logger.Log("RadegastInstance finished cleaning up.", Helpers.LogLevel.Debug);
\r
202 private void ScanAndLoadPlugins()
\r
204 string dirName = Application.StartupPath;
\r
206 if (!Directory.Exists(dirName)) return;
\r
208 foreach (string loadfilename in Directory.GetFiles(dirName))
\r
210 if (loadfilename.ToLower().EndsWith(".dll") || loadfilename.ToLower().EndsWith(".exe"))
\r
214 Assembly assembly = Assembly.LoadFile(loadfilename);
\r
215 foreach (Type type in assembly.GetTypes())
\r
217 if (typeof(IRadegastPlugin).IsAssignableFrom(type))
\r
219 foreach (var ci in type.GetConstructors())
\r
221 if (ci.GetParameters().Length > 0) continue;
\r
224 IRadegastPlugin plug = (IRadegastPlugin)ci.Invoke(new object[0]);
\r
225 plug.StartPlugin(this);
\r
226 lock (PluginsLoaded) PluginsLoaded.Add(plug);
\r
229 catch (Exception ex)
\r
231 Logger.Log("ERROR in Radegast Plugin: " + ex.Message, Helpers.LogLevel.Debug);
\r
237 catch (BadImageFormatException)
\r
241 catch (ReflectionTypeLoadException)
\r
243 // Out of date or dlls missing sub dependencies
\r
249 void Network_OnConnected(object sender)
\r
253 if (!Directory.Exists(ClientDir))
\r
254 Directory.CreateDirectory(ClientDir);
\r
256 catch (Exception ex)
\r
258 Logger.Log("Failed to create client directory", Helpers.LogLevel.Warning, ex);
\r
261 clientSettings = new Settings(Path.Combine(ClientDir, "client_settings.xml"));
\r
264 void Avatars_OnAvatarNames(Dictionary<UUID, string> names)
\r
268 foreach (KeyValuePair<UUID, string> av in names)
\r
270 if (!nameCache.ContainsKey(av.Key))
\r
272 nameCache.Add(av.Key, av.Value);
\r
278 public string getAvatarName(UUID key)
\r
282 if (key == UUID.Zero)
\r
284 return "(???) (???)";
\r
286 if (nameCache.ContainsKey(key))
\r
288 return nameCache[key];
\r
292 client.Avatars.RequestAvatarName(key);
\r
293 return INCOMPLETE_NAME;
\r
298 public void getAvatarNames(List<UUID> keys)
\r
302 List<UUID> newNames = new List<UUID>();
\r
303 foreach (UUID key in keys)
\r
305 if (!nameCache.ContainsKey(key))
\r
310 if (newNames.Count > 0)
\r
312 client.Avatars.RequestAvatarNames(newNames);
\r
317 public bool haveAvatarName(UUID key)
\r
321 if (nameCache.ContainsKey(key))
\r
328 void Groups_OnGroupJoined(UUID groupID, bool success)
\r
330 client.Groups.RequestCurrentGroups();
\r
333 void Groups_OnGroupLeft(UUID groupID, bool success)
\r
335 client.Groups.RequestCurrentGroups();
\r
338 void Groups_OnGroupDropped(UUID groupID)
\r
340 client.Groups.RequestCurrentGroups();
\r
343 public static string SafeFileName(string fileName)
\r
345 foreach (char lDisallowed in Path.GetInvalidFileNameChars())
\r
347 fileName = fileName.Replace(lDisallowed.ToString(), "_");
\r
353 public void LogClientMessage(string fileName, string message)
\r
359 foreach (char lDisallowed in System.IO.Path.GetInvalidFileNameChars())
\r
361 fileName = fileName.Replace(lDisallowed.ToString(), "_");
\r
364 StreamWriter logfile = File.AppendText(Path.Combine(ClientDir, fileName));
\r
365 logfile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd [HH:mm:ss] ") + message);
\r
369 catch (Exception) { }
\r
373 void Groups_OnCurrentGroups(Dictionary<UUID, Group> gr)
\r
378 private void InitializeLoggingAndConfig()
\r
380 // Are we running mono?
\r
381 monoRuntime = Type.GetType("Mono.Runtime") != null;
\r
385 userDir = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), Properties.Resources.ProgramName);
\r
386 if (!Directory.Exists(userDir))
\r
388 Directory.CreateDirectory(userDir);
\r
393 userDir = System.Environment.CurrentDirectory;
\r
396 animCacheDir = Path.Combine(userDir, @"anim_cache");
\r
397 globalLogFile = Path.Combine(userDir, Properties.Resources.ProgramName + ".log");
\r
398 globalSettings = new Settings(Path.Combine(userDir, "settings.xml"));
\r
401 public GridClient Client
\r
403 get { return client; }
\r
406 public RadegastNetcom Netcom
\r
408 get { return netcom; }
\r
411 public StateManager State
\r
413 get { return state; }
\r
416 public frmMain MainForm
\r
418 get { return mainForm; }
\r
421 public TabsConsole TabConsole
\r
423 get { return tabsConsole; }
\r