OSDN Git Service

Try to prevent getting nullref immedately after login because clientDir is null
[radegast/radegast.git] / Radegast / Core / RadegastInstance.cs
1 // \r
2 // Radegast Metaverse Client\r
3 // Copyright (c) 2009, Radegast Development Team\r
4 // All rights reserved.\r
5 // \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
8 // \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
17 // \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
28 //\r
29 // $Id$\r
30 //\r
31 using System;\r
32 using System.Collections.Generic;\r
33 using System.IO;\r
34 using System.Text;\r
35 using System.Windows.Forms;\r
36 using RadegastNc;\r
37 using OpenMetaverse;\r
38 \r
39 namespace Radegast\r
40 {\r
41     public class RadegastInstance\r
42     {\r
43         private GridClient client;\r
44         private RadegastNetcom netcom;\r
45 \r
46         private ImageCache imageCache;\r
47         private StateManager state;\r
48         private ConfigManager config;\r
49 \r
50         private frmMain mainForm;\r
51         private TabsConsole tabsConsole;\r
52 \r
53         // Singleton, there can be only one instance\r
54         private static RadegastInstance globalInstance = null;\r
55         public static RadegastInstance GlobalInstance\r
56         {\r
57             get\r
58             {\r
59                 if (globalInstance == null)\r
60                 {\r
61                     globalInstance = new RadegastInstance();\r
62                 }\r
63                 return globalInstance;\r
64             }\r
65         }\r
66 \r
67         private string userDir;\r
68         /// <summary>\r
69         /// System (not grid!) user's dir\r
70         /// </summary>\r
71         public string UserDir { get { return userDir; } }\r
72 \r
73         /// <summary>\r
74         /// Grid client's user dir for settings and logs\r
75         /// </summary>\r
76         public string ClientDir\r
77         {\r
78             get\r
79             {\r
80                 if (client != null && client.Self != null && !string.IsNullOrEmpty(client.Self.Name))\r
81                 {\r
82                     return Path.Combine(userDir, client.Self.Name);\r
83                 }\r
84                 else\r
85                 {\r
86                     return Environment.CurrentDirectory;\r
87                 }\r
88             }\r
89         }\r
90 \r
91         public string InventoryCacheFileName { get { return Path.Combine(ClientDir, "inventory.cache"); } }\r
92 \r
93         private string animCacheDir;\r
94         public string AnimCacheDir { get { return animCacheDir; } }\r
95 \r
96         private string globalLogFile;\r
97         public string GlobalLogFile { get { return globalLogFile; } }\r
98 \r
99         private bool monoRuntime;\r
100         public bool MonoRuntime { get { return monoRuntime; } }\r
101 \r
102         private Dictionary<UUID, Group> groups;\r
103         public Dictionary<UUID, Group> Groups { get { return groups; } }\r
104 \r
105         public delegate void AvatarNameCallback(UUID agentID, string agentName);\r
106         public event AvatarNameCallback OnAvatarName;\r
107 \r
108         public Dictionary<UUID, string> nameCache = new Dictionary<UUID,string>();\r
109 \r
110         public readonly bool advancedDebugging = false;\r
111 \r
112         private RadegastInstance()\r
113         {\r
114             InitializeLoggingAndConfig();\r
115 \r
116             // Settings.PIPELINE_REFRESH_INTERVAL = 2000.0f;\r
117 \r
118             client = new GridClient();\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 = false;\r
125             client.Settings.SEND_AGENT_THROTTLE = true;\r
126             client.Settings.SEND_AGENT_UPDATES = true;\r
127 \r
128             client.Settings.USE_TEXTURE_CACHE = true;\r
129             client.Settings.TEXTURE_CACHE_DIR = Path.Combine(userDir,  "cache");\r
130             client.Assets.Cache.AutoPruneEnabled = false;\r
131     \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.USE_INTERPOLATION_TIMER = false;\r
138             client.Settings.MAX_CONCURRENT_TEXTURE_DOWNLOADS = 20;\r
139 \r
140             netcom = new RadegastNetcom(client);\r
141             imageCache = new ImageCache();\r
142             state = new StateManager(this);\r
143 \r
144             InitializeConfigLegacy();\r
145 \r
146             mainForm = new frmMain(this);\r
147             mainForm.InitializeControls();\r
148             tabsConsole = mainForm.TabConsole;\r
149 \r
150             Application.ApplicationExit += new EventHandler(Application_ApplicationExit);\r
151             groups = new Dictionary<UUID, Group>();\r
152          \r
153             client.Groups.OnCurrentGroups += new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);\r
154             client.Groups.OnGroupLeft += new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);\r
155             client.Groups.OnGroupDropped += new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);\r
156             client.Groups.OnGroupJoined += new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);\r
157             client.Avatars.OnAvatarNames += new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);\r
158         }\r
159 \r
160         public void CleanUp()\r
161         {\r
162             if (client != null)\r
163             {\r
164                 client.Groups.OnCurrentGroups -= new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);\r
165                 client.Groups.OnGroupLeft -= new GroupManager.GroupLeftCallback(Groups_OnGroupLeft);\r
166                 client.Groups.OnGroupDropped -= new GroupManager.GroupDroppedCallback(Groups_OnGroupDropped);\r
167                 client.Groups.OnGroupJoined -= new GroupManager.GroupJoinedCallback(Groups_OnGroupJoined);\r
168                 client.Avatars.OnAvatarNames -= new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);\r
169             }\r
170 \r
171             if (MonoRuntime)\r
172             {\r
173                 Environment.Exit(0);\r
174             }\r
175 \r
176         }\r
177 \r
178         void Avatars_OnAvatarNames(Dictionary<UUID, string> names)\r
179         {\r
180             lock (nameCache)\r
181             {\r
182                 foreach (KeyValuePair<UUID, string> av in names)\r
183                 {\r
184                     if (OnAvatarName != null) try { OnAvatarName(av.Key, av.Value); }\r
185                         catch (Exception) { };\r
186 \r
187                     if (!nameCache.ContainsKey(av.Key))\r
188                     {\r
189                         nameCache.Add(av.Key, av.Value);\r
190                     }\r
191                 }\r
192             }\r
193         }\r
194 \r
195         public string getAvatarName(UUID key)\r
196         {\r
197             lock (nameCache)\r
198             {\r
199                 if (nameCache.ContainsKey(key))\r
200                 {\r
201                     return nameCache[key];\r
202                 }\r
203                 else\r
204                 {\r
205                     client.Avatars.RequestAvatarName(key);\r
206                     return "Loading...";\r
207                 }\r
208             }\r
209         }\r
210 \r
211         public void getAvatarNames(List<UUID> keys)\r
212         {\r
213             lock (nameCache)\r
214             {\r
215                 List<UUID> newNames = new List<UUID>();\r
216                 foreach (UUID key in keys)\r
217                 {\r
218                     if (!nameCache.ContainsKey(key))\r
219                     {\r
220                         newNames.Add(key);\r
221                     }\r
222                 }\r
223                 if (newNames.Count > 0)\r
224                 {\r
225                     client.Avatars.RequestAvatarNames(newNames);\r
226                 }\r
227             }\r
228         }\r
229 \r
230         public bool haveAvatarName(UUID key)\r
231         {\r
232             lock (nameCache)\r
233             {\r
234                 if (nameCache.ContainsKey(key))\r
235                     return true;\r
236                 else\r
237                     return false;\r
238             }\r
239         }\r
240 \r
241         void Groups_OnGroupJoined(UUID groupID, bool success)\r
242         {\r
243             client.Groups.RequestCurrentGroups();\r
244         }\r
245 \r
246         void Groups_OnGroupLeft(UUID groupID, bool success)\r
247         {\r
248             client.Groups.RequestCurrentGroups();\r
249         }\r
250 \r
251         void Groups_OnGroupDropped(UUID groupID)\r
252         {\r
253             client.Groups.RequestCurrentGroups();\r
254         }\r
255 \r
256         public void LogClientMessage(string fileName, string message)\r
257         {\r
258             lock (this)\r
259             {\r
260                 try\r
261                 {\r
262                     foreach (char lDisallowed in System.IO.Path.GetInvalidFileNameChars())\r
263                     {\r
264                         fileName = fileName.Replace(lDisallowed.ToString(), "_");\r
265                     }\r
266 \r
267                     StreamWriter logfile = File.AppendText(Path.Combine(ClientDir, fileName));\r
268                     logfile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd [HH:mm:ss] ") + message);\r
269                     logfile.Close();\r
270                     logfile.Dispose();\r
271                 }\r
272                 catch (Exception) { }\r
273             }\r
274         }\r
275 \r
276         void Groups_OnCurrentGroups(Dictionary<UUID, Group> gr)\r
277         {\r
278             this.groups = gr;\r
279         }\r
280 \r
281         private void Application_ApplicationExit(object sender, EventArgs e)\r
282         {\r
283             config.SaveCurrentConfig();\r
284         }\r
285 \r
286         private void InitializeLoggingAndConfig()\r
287         {\r
288             // Are we running mono?\r
289             if (null == Type.GetType("Mono.Runtime"))\r
290             {\r
291                 monoRuntime = false;\r
292             }\r
293             else\r
294             {\r
295                 monoRuntime = true;\r
296             }\r
297 \r
298             try\r
299             {\r
300                 userDir = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), Properties.Resources.ProgramName);\r
301                 if (!Directory.Exists(userDir))\r
302                 {\r
303                     Directory.CreateDirectory(userDir);\r
304                 }\r
305             }\r
306             catch (Exception)\r
307             {\r
308                 userDir = System.Environment.CurrentDirectory;\r
309             };\r
310 \r
311             animCacheDir = Path.Combine(userDir, @"anim_cache");\r
312             globalLogFile = Path.Combine(userDir, Properties.Resources.ProgramName + ".log");\r
313         }\r
314 \r
315         private void InitializeConfigLegacy()\r
316         {\r
317             config = new ConfigManager(this);\r
318             config.ApplyDefault();\r
319 \r
320             netcom.LoginOptions.FirstName = config.CurrentConfig.FirstName;\r
321             netcom.LoginOptions.LastName = config.CurrentConfig.LastName;\r
322             netcom.LoginOptions.Password = config.CurrentConfig.PasswordMD5;\r
323             netcom.LoginOptions.IsPasswordMD5 = true;\r
324         }\r
325 \r
326         public GridClient Client\r
327         {\r
328             get { return client; }\r
329         }\r
330 \r
331         public RadegastNetcom Netcom\r
332         {\r
333             get { return netcom; }\r
334         }\r
335 \r
336         public ImageCache ImageCache\r
337         {\r
338             get { return imageCache; }\r
339         }\r
340 \r
341         public StateManager State\r
342         {\r
343             get { return state; }\r
344         }\r
345 \r
346         public ConfigManager Config\r
347         {\r
348             get { return config; }\r
349         }\r
350 \r
351         public frmMain MainForm\r
352         {\r
353             get { return mainForm; }\r
354         }\r
355 \r
356         public TabsConsole TabConsole\r
357         {\r
358             get { return tabsConsole; }\r
359         }\r
360     }\r
361 }\r