OSDN Git Service

events are firing to often
[radegast/radegast.git] / Radegast / Program.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
9 //     * Redistributions of source code must retain the above copyright notice,
10 //       this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above copyright
12 //       notice, this list of conditions and the following disclaimer in the
13 //       documentation and/or other materials provided with the distribution.
14 //     * Neither the name of the application "Radegast", nor the names of its
15 //       contributors may be used to endorse or promote products derived from
16 //       this software without specific prior written permission.
17 // 
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // $Id$
30 //
31 using System;
32 using System.Windows.Forms;
33 using System.Text.RegularExpressions;
34 using System.IO;
35 using System.Reflection;
36 using CommandLine;
37 using CommandLine.Text;
38 #if (COGBOT_LIBOMV || USE_STHREADS)
39 using ThreadPoolUtil;
40 using Thread = ThreadPoolUtil.Thread;
41 using ThreadPool = ThreadPoolUtil.ThreadPool;
42 using Monitor = ThreadPoolUtil.Monitor;
43 #endif
44 using System.Threading;
45
46 namespace Radegast
47 {
48     public class CommandLine
49     {
50         [Option("u", "username", HelpText = "Username, use quotes to supply \"First Last\"")]
51         public string Username = string.Empty;
52
53         [Option("p", "password", HelpText = "Account password")]
54         public string Password = string.Empty;
55
56         [Option("a", "autologin", HelpText = "Automatially login with provided user credentials")]
57         public bool AutoLogin = false;
58
59         [Option("g", "grid", HelpText = "Grid ID to login into, try --list-grids to see IDs used for this parameter")]
60         public string Grid = string.Empty;
61
62         [Option("l", "location", HelpText = "Login location: last, home or regionname. Regioname can also be in format regionname/x/y/z")]
63         public string Location = string.Empty;
64
65         [Option(null, "list-grids", HelpText = "Lists grid IDs used for --grid option")]
66         public bool ListGrids = false;
67
68         [Option(null, "loginuri", HelpText = "Use this URI to login (don't use with --grid)")]
69         public string LoginUri = string.Empty;
70
71         [Option(null, "no-sound", HelpText = "Disable sound")]
72         public bool DisableSound = false;
73
74
75         public HelpText GetHeader()
76         {
77             HelpText header = new HelpText(Properties.Resources.RadegastTitle);
78             header.AdditionalNewLineAfterOption = true;
79             header.Copyright = new CopyrightInfo("Radegast Development Team", 2009, 2014);
80             header.AddPreOptionsLine("http://radegast.org/");
81             return header;
82         }
83
84         [HelpOption("h", "help", HelpText = "Display this help screen.")]
85         public string GetUsage()
86         {
87             HelpText usage = GetHeader();
88             usage.AddOptions(this);
89             usage.AddPostOptionsLine("Example: automatically login user called Some Resident to his last location on the Second Life main grid (agni)");
90             usage.AddPostOptionsLine("Radegast -a -g agni -u \"Some Resident\" -p \"secret\"  -l last");
91             return usage.ToString();
92         }
93     }
94
95     public static class MainProgram
96     {
97         /// <summary>
98         /// Parsed command line options
99         /// </summary>
100         public static CommandLine CommandLine;
101
102         static void RunRadegast(string[] args)
103         {
104             // Increase the number of IOCP threads available. Mono defaults to a tragically low number
105             int workerThreads, iocpThreads;
106             ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads);
107
108             if (workerThreads < 500 || iocpThreads < 1000)
109             {
110                 if (workerThreads < 500) workerThreads = 500;
111                 if (iocpThreads < 1000) iocpThreads = 1000;
112                 ThreadPool.SetMaxThreads(workerThreads, iocpThreads);
113             }
114
115             // Read command line options
116             CommandLine = new CommandLine();
117             CommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
118             if (!parser.ParseArguments(args, CommandLine))
119             {
120                 Environment.Exit(1);
121             }
122
123             // Change current working directory to Radegast install dir
124             Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
125
126             Application.EnableVisualStyles();
127             Application.SetCompatibleTextRenderingDefault(false);
128
129             // See if we only wanted to display list of grids
130             if (CommandLine.ListGrids)
131             {
132                 Console.WriteLine(CommandLine.GetHeader());
133                 Console.WriteLine();
134                 GridManager grids = new GridManager();
135                 Console.WriteLine("Use Grid ID as the parameter for --grid");
136                 Console.WriteLine("{0,-25} - {1}", "Grid ID", "Grid Name");
137                 Console.WriteLine("========================================================");
138
139                 for (int i = 0; i < grids.Count; i++)
140                 {
141                     Console.WriteLine("{0,-25} - {1}", grids[i].ID, grids[i].Name);
142                 }
143
144                 Environment.Exit(0);
145             }
146
147             // Create main Radegast instance
148             RadegastInstance instance = RadegastInstance.GlobalInstance;
149             Application.Run(instance.MainForm);
150             OpenMetaverse.WorkPool.Shutdown();
151         }
152
153         /// <summary>
154         /// The main entry point for the application.
155         /// </summary>
156         [STAThread]
157         static void Main(string[] args)
158         {
159             if (System.Diagnostics.Debugger.IsAttached)
160             {
161                 RunRadegast(args);
162             }
163             else
164             {
165                 try
166                 {
167                     RunRadegast(args);
168                 }
169                 catch (Exception e)
170                 {
171                     string errMsg = "Unhandled " + e.ToString() + ": " +
172                         e.Message + Environment.NewLine +
173                         e.StackTrace + Environment.NewLine;
174
175                     OpenMetaverse.Logger.Log(errMsg, OpenMetaverse.Helpers.LogLevel.Error);
176
177                     string dlgMsg = "Radegast has encoutered an unrecoverable errror." + Environment.NewLine +
178                         "Would you like to send the error report to help improve Radegast?";
179
180                     var res = MessageBox.Show(dlgMsg, "Unrecoverable error", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
181
182                     if (res == DialogResult.Yes)
183                     {
184                         var reporter = new ErrorReporter("http://api.radegast.org/svc/error_report");
185                         reporter.SendExceptionReport(e);
186                     }
187
188                     Environment.Exit(1);
189                 }
190             }
191         }
192     }
193 }