OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
Form1.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.IO;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.ComponentModel;
33 using System.Data;
34 using System.Diagnostics;
35 using System.Drawing;
36 using System.Text;
37 using System.Text.RegularExpressions;
38 using System.Windows.Forms;
39 using Nini.Config;
40 
41 namespace LaunchSLClient
42 {
43  public partial class Form1 : Form
44  {
45  string gridUrl = "";
46  string sandboxUrl = "";
47  string runUrl = "";
48  string runLine = "";
49  string exeFlags = "";
50  string exePath = "";
51 
52  private MachineConfig m_machineConfig;
53  private List<Grid> m_grids = new List<Grid>();
54 
55  public Form1()
56  {
57  InitializeComponent();
58 
59  m_machineConfig = getMachineConfig();
60  m_machineConfig.GetClient(ref exePath, ref runLine, ref exeFlags);
61 
62  initializeGrids();
63 
64  ArrayList menuItems = new ArrayList();
65  menuItems.Add(string.Empty);
66 
67  addLocalSims(ref menuItems);
68 
69  foreach (Grid grid in m_grids)
70  {
71  menuItems.Add(grid.Name + " - " + grid.URL);
72  }
73 
74  comboBox1.DataSource = menuItems;
75  }
76 
77  private MachineConfig getMachineConfig()
78  {
79  if (Environment.OSVersion.Platform == PlatformID.Unix)
80  {
81  if (File.Exists("/System/Library/Frameworks/Cocoa.framework/Cocoa"))
82  {
83  return new OSXConfig();
84  }
85  else
86  {
87  return new UnixConfig();
88  }
89  }
90  else
91  {
92  return new WindowsConfig();
93  }
94  }
95 
96  private void initializeGrids()
97  {
98  string iniFile = "LaunchSLClient.ini";
99 
100  if (!File.Exists(iniFile))
101  return;
102 
103  IniConfigSource configSource = new IniConfigSource(iniFile);
104 
105  foreach (IConfig config in configSource.Configs)
106  {
107  Grid grid = new Grid();
108 
109  grid.Name = config.Name;
110  grid.LoginURI = config.GetString("loginURI", "");
111  grid.URL = config.GetString("URL", "");
112 
113  m_grids.Add(grid);
114  }
115  }
116 
117  private void addLocalSandbox(ref ArrayList menuItems)
118  {
119  // build sandbox URL from Regions/default.xml
120  // this is highly dependant on a standard default.xml
121  if (File.Exists("Regions/default.xml"))
122  {
123  string sandboxHostName = "";
124  string sandboxPort = "";
125  string text;
126 
127  Regex myRegex = new Regex(".*internal_ip_port=\\\"(?<port>.*?)\\\".*external_host_name=\\\"(?<name>.*?)\\\".*");
128 
129  FileInfo defaultFile = new FileInfo("Regions/default.xml");
130  StreamReader stream = defaultFile.OpenText();
131  do
132  {
133  text = stream.ReadLine();
134  if (text == null)
135  {
136  break;
137  }
138  MatchCollection theMatches = myRegex.Matches(text);
139  foreach (Match theMatch in theMatches)
140  {
141  if (theMatch.Length != 0)
142  {
143  sandboxHostName = theMatch.Groups["name"].ToString();
144  sandboxPort = theMatch.Groups["port"].ToString();
145  }
146  }
147  } while (text != null);
148 
149  stream.Close();
150  sandboxUrl = "http:\\" + sandboxHostName + ":" + sandboxPort;
151  menuItems.Add("Local Sandbox");
152  }
153  }
154 
155  private void addLocalGrid(ref ArrayList menuItems)
156  {
157  //build local grid URL from network_servers_information.xml
158  if (File.Exists("network_servers_information.xml"))
159  {
160  string text;
161  FileInfo defaultFile = new FileInfo("network_servers_information.xml");
162  Regex myRegex = new Regex(".*UserServerURL=\\\"(?<url>.*?)\\\".*");
163  StreamReader stream = defaultFile.OpenText();
164 
165  do
166  {
167  text = stream.ReadLine();
168  if (text == null)
169  {
170  break;
171  }
172  foreach (Match theMatch in myRegex.Matches(text))
173  {
174  if (theMatch.Length != 0)
175  {
176  gridUrl = theMatch.Groups["url"].ToString();
177  }
178  }
179  } while (text != null);
180  stream.Close();
181  if (gridUrl != null)
182  {
183  menuItems.Add("Local Grid Server");
184  }
185  }
186  }
187 
188  private void addLocalSims(ref ArrayList menuItems)
189  {
190  string configDir = m_machineConfig.GetConfigDir();
191 
192  if (!string.IsNullOrEmpty(configDir))
193  {
194  Directory.SetCurrentDirectory(configDir);
195 
196  addLocalSandbox(ref menuItems);
197  addLocalGrid(ref menuItems);
198  }
199  }
200 
201  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
202  {
203  if (comboBox1.Text == string.Empty)
204  {
205  return;
206  }
207  else if (comboBox1.Text == "Local Sandbox")
208  {
209  runUrl=" -loginuri " + sandboxUrl;
210  }
211  else if (comboBox1.Text == "Local Grid Server")
212  {
213  runUrl = " -loginuri " + gridUrl;
214  }
215  else
216  {
217  foreach (Grid grid in m_grids)
218  {
219  if (comboBox1.Text == grid.Name + " - " + grid.URL)
220  {
221  if (grid.LoginURI != string.Empty)
222  runUrl = " -loginuri " + grid.LoginURI;
223  else
224  runUrl = "";
225 
226  break;
227  }
228  }
229  }
230 
231  comboBox1.DroppedDown = false;
232  Hide();
233 
234  System.Diagnostics.Process proc = new System.Diagnostics.Process();
235  proc.StartInfo.FileName = runLine;
236  proc.StartInfo.Arguments = exeFlags + " " + runUrl;
237  proc.StartInfo.UseShellExecute = false;
238  proc.StartInfo.RedirectStandardOutput = false;
239  proc.StartInfo.WorkingDirectory = exePath;
240  proc.Start();
241  proc.WaitForExit();
242 
243  Application.Exit();
244  }
245  }
246 }