OSDN Git Service

Renamed netcom namespace
[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 Radegast.Netcom;\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.USE_INTERPOLATION_TIMER = false;\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.MAX_CONCURRENT_TEXTURE_DOWNLOADS = 20;\r
138 \r
139             netcom = new RadegastNetcom(client);\r
140             imageCache = new ImageCache();\r
141             state = new StateManager(this);\r
142 \r
143             InitializeConfigLegacy();\r
144 \r
145             mainForm = new frmMain(this);\r
146             mainForm.InitializeControls();\r
147             tabsConsole = mainForm.TabConsole;\r
148 \r
149             Application.ApplicationExit += new EventHandler(Application_ApplicationExit);\r
150             groups = new Dictionary<UUID, Group>();\r
151          \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         }\r
158 \r
159         public void CleanUp()\r
160         {\r
161             if (client != null)\r
162             {\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             }\r
169 \r
170             if (MonoRuntime)\r
171             {\r
172                 Environment.Exit(0);\r
173             }\r
174 \r
175         }\r
176 \r
177         void Avatars_OnAvatarNames(Dictionary<UUID, string> names)\r
178         {\r
179             lock (nameCache)\r
180             {\r
181                 foreach (KeyValuePair<UUID, string> av in names)\r
182                 {\r
183                     if (OnAvatarName != null) try { OnAvatarName(av.Key, av.Value); }\r
184                         catch (Exception) { };\r
185 \r
186                     if (!nameCache.ContainsKey(av.Key))\r
187                     {\r
188                         nameCache.Add(av.Key, av.Value);\r
189                     }\r
190                 }\r
191             }\r
192         }\r
193 \r
194         public string getAvatarName(UUID key)\r
195         {\r
196             lock (nameCache)\r
197             {\r
198                 if (nameCache.ContainsKey(key))\r
199                 {\r
200                     return nameCache[key];\r
201                 }\r
202                 else\r
203                 {\r
204                     client.Avatars.RequestAvatarName(key);\r
205                     return "Loading...";\r
206                 }\r
207             }\r
208         }\r
209 \r
210         public void getAvatarNames(List<UUID> keys)\r
211         {\r
212             lock (nameCache)\r
213             {\r
214                 List<UUID> newNames = new List<UUID>();\r
215                 foreach (UUID key in keys)\r
216                 {\r
217                     if (!nameCache.ContainsKey(key))\r
218                     {\r
219                         newNames.Add(key);\r
220                     }\r
221                 }\r
222                 if (newNames.Count > 0)\r
223                 {\r
224                     client.Avatars.RequestAvatarNames(newNames);\r
225                 }\r
226             }\r
227         }\r
228 \r
229         public bool haveAvatarName(UUID key)\r
230         {\r
231             lock (nameCache)\r
232             {\r
233                 if (nameCache.ContainsKey(key))\r
234                     return true;\r
235                 else\r
236                     return false;\r
237             }\r
238         }\r
239 \r
240         void Groups_OnGroupJoined(UUID groupID, bool success)\r
241         {\r
242             client.Groups.RequestCurrentGroups();\r
243         }\r
244 \r
245         void Groups_OnGroupLeft(UUID groupID, bool success)\r
246         {\r
247             client.Groups.RequestCurrentGroups();\r
248         }\r
249 \r
250         void Groups_OnGroupDropped(UUID groupID)\r
251         {\r
252             client.Groups.RequestCurrentGroups();\r
253         }\r
254 \r
255         public void LogClientMessage(string fileName, string message)\r
256         {\r
257             lock (this)\r
258             {\r
259                 try\r
260                 {\r
261                     foreach (char lDisallowed in System.IO.Path.GetInvalidFileNameChars())\r
262                     {\r
263                         fileName = fileName.Replace(lDisallowed.ToString(), "_");\r
264                     }\r
265 \r
266                     StreamWriter logfile = File.AppendText(Path.Combine(ClientDir, fileName));\r
267                     logfile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd [HH:mm:ss] ") + message);\r
268                     logfile.Close();\r
269                     logfile.Dispose();\r
270                 }\r
271                 catch (Exception) { }\r
272             }\r
273         }\r
274 \r
275         void Groups_OnCurrentGroups(Dictionary<UUID, Group> gr)\r
276         {\r
277             this.groups = gr;\r
278         }\r
279 \r
280         private void Application_ApplicationExit(object sender, EventArgs e)\r
281         {\r
282             config.SaveCurrentConfig();\r
283         }\r
284 \r
285         private void InitializeLoggingAndConfig()\r
286         {\r
287             // Are we running mono?\r
288             if (null == Type.GetType("Mono.Runtime"))\r
289             {\r
290                 monoRuntime = false;\r
291             }\r
292             else\r
293             {\r
294                 monoRuntime = true;\r
295             }\r
296 \r
297             try\r
298             {\r
299                 userDir = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), Properties.Resources.ProgramName);\r
300                 if (!Directory.Exists(userDir))\r
301                 {\r
302                     Directory.CreateDirectory(userDir);\r
303                 }\r
304             }\r
305             catch (Exception)\r
306             {\r
307                 userDir = System.Environment.CurrentDirectory;\r
308             };\r
309 \r
310             animCacheDir = Path.Combine(userDir, @"anim_cache");\r
311             globalLogFile = Path.Combine(userDir, Properties.Resources.ProgramName + ".log");\r
312         }\r
313 \r
314         private void InitializeConfigLegacy()\r
315         {\r
316             config = new ConfigManager(this);\r
317             config.ApplyDefault();\r
318 \r
319             netcom.LoginOptions.FirstName = config.CurrentConfig.FirstName;\r
320             netcom.LoginOptions.LastName = config.CurrentConfig.LastName;\r
321             netcom.LoginOptions.Password = config.CurrentConfig.PasswordMD5;\r
322             netcom.LoginOptions.IsPasswordMD5 = true;\r
323         }\r
324 \r
325         public GridClient Client\r
326         {\r
327             get { return client; }\r
328         }\r
329 \r
330         public RadegastNetcom Netcom\r
331         {\r
332             get { return netcom; }\r
333         }\r
334 \r
335         public ImageCache ImageCache\r
336         {\r
337             get { return imageCache; }\r
338         }\r
339 \r
340         public StateManager State\r
341         {\r
342             get { return state; }\r
343         }\r
344 \r
345         public ConfigManager Config\r
346         {\r
347             get { return config; }\r
348         }\r
349 \r
350         public frmMain MainForm\r
351         {\r
352             get { return mainForm; }\r
353         }\r
354 \r
355         public TabsConsole TabConsole\r
356         {\r
357             get { return tabsConsole; }\r
358         }\r
359     }\r
360 }\r