Develop and Download Open Source Software

Browse Subversion Repository

Contents of /UT3 Server Launcher/FormMain.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (show annotations) (download)
Tue Sep 8 11:06:03 2009 UTC (14 years, 7 months ago) by piglet
File size: 19292 byte(s)


1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Collections.Specialized;
5 using System.ComponentModel;
6 using System.Data;
7 using System.Drawing;
8 using System.IO;
9 using System.Linq;
10 using System.Text;
11 using System.Windows.Forms;
12
13 namespace UT3_Server_Launcher
14 {
15 public partial class FormMain : Form
16 {
17
18 //////////
19 // 定義 //
20 //////////
21
22 const string colon = ":";
23 const string comma = ",";
24 const string space = " ";
25 const string sepalater = space + colon + space;
26
27
28 //////////////////
29 // メンバー変数 //
30 //////////////////
31
32 public string profileName = "Profile01";
33 public string title = string.Empty;
34
35 public bool bAlive;
36
37 public Mode mode = new Mode();
38 public Map map = new Map();
39 public Bot bot = new Bot();
40 public Mut mut = new Mut();
41
42 Server server = null;
43
44
45 // 各モードに最適な設定値
46 // 順番に MinPlayer, MaxPlayer, NumOfBot, TimeLimit, GoalScore
47
48 int[] DeathMatch = new int[5] { 1, 6, 3, 13, 25 };
49 int[] TeamDeathMatch = new int[5] { 1, 10, 4, 15, 60 };
50 int[] Duel = new int[5] { 1, 3, 1, 10, 15 };
51 int[] Betrayal = new int[5] { 1, 6, 3, 13, 25 };
52 int[] Greed = new int[5] { 1, 10, 6, 15, 50 };
53 int[] CaptureTheFlag = new int[5] { 1, 12, 6, 15, 5 };
54 int[] VehicleCTF = new int[5] { 1, 14, 6, 20, 5 };
55 int[] Warfare = new int[5] { 1, 18, 8, 20, 3 };
56
57
58 public FormMain()
59 {
60 InitializeComponent();
61 }
62
63 private void FormMain_Load(object sender, EventArgs e)
64 {
65 // 一番最初の処理
66
67 // フォーム全体を初期化
68 initializeForm();
69
70 // タイトル名を保存
71 title = this.Text;
72
73 // 標準のプロフィールを読む
74 loadProfile(profileName);
75
76 // toolstripの位置を復元
77 ToolStripManager.LoadSettings(this);
78
79 }
80
81 private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
82 {
83 // 終了前の処理
84
85 // サーバーが動いていたら警告
86 if (server != null)
87 {
88 if (MessageBox.Show(Properties.Resources.msgFinish,
89 Properties.Resources.wordConfirmation,
90 MessageBoxButtons.YesNo, MessageBoxIcon.Question)
91 == DialogResult.No)
92 {
93 e.Cancel = true;
94 }
95 }
96
97 // toolstripの位置を保存
98 ToolStripManager.SaveSettings(this);
99 }
100
101 private void btnAllRight_Click(object sender, EventArgs e)
102 {
103 // 全てのMUTを有効にする
104
105 // 再描画を止めてから操作
106 lstBxEnableMut.BeginUpdate();
107 foreach (object item in lstBxEnableMut.Items)
108 {
109 lstBxActiveMut.Items.Add(item);
110 }
111 lstBxEnableMut.Items.Clear();
112 lstBxEnableMut.EndUpdate();
113
114 }
115
116 private void btnAllLeft_Click(object sender, EventArgs e)
117 {
118 // 全てのMUTを無効にする
119
120 // 再描画を止めてから操作
121 lstBxActiveMut.BeginUpdate();
122 foreach (object item in lstBxActiveMut.Items)
123 {
124 lstBxEnableMut.Items.Add(item);
125 }
126 lstBxActiveMut.Items.Clear();
127 lstBxActiveMut.EndUpdate();
128 }
129
130 private void btnRight_Click(object sender, EventArgs e)
131 {
132 // ひとつのMUTを有効にする
133
134 // 何も選択されていない
135 if (lstBxEnableMut.SelectedIndex < 0)
136 return;
137
138 object item = lstBxEnableMut.SelectedItem;
139 lstBxActiveMut.Items.Add(item);
140 lstBxEnableMut.Items.Remove(item);
141 }
142
143 private void btnLeft_Click(object sender, EventArgs e)
144 {
145 // ひとつのMUTを無効にする
146
147 // 何も選択されていない
148 if (lstBxActiveMut.SelectedIndex < 0)
149 return;
150
151 object item = lstBxActiveMut.SelectedItem;
152 lstBxEnableMut.Items.Add(item);
153 lstBxActiveMut.Items.Remove(item);
154 }
155
156 private void btnOpenDir_Click(object sender, EventArgs e)
157 {
158 // 設定ファイルの入ったフォルダを指定する
159
160 string UT3BIN = Properties.Settings.Default.UT3BIN;
161
162 // Configフォルダのパスを求める
163 int index = UT3BIN.IndexOf(@"\Binaries\UT3.exe");
164 string UT3CFG = UT3BIN.Substring(0, index) + @"\UTGame\Config";
165
166 // インストールフォルダが設定されているか確認する
167 if (File.Exists(UT3BIN))
168 {
169 // 正常に設定されていた(UT3.exe)が存在している
170
171 // フォルダを指定する
172 folderBrowserDialog1.Description = Properties.Resources.msgConfigDir;
173 folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
174 folderBrowserDialog1.SelectedPath = UT3CFG;
175 folderBrowserDialog1.ShowNewFolderButton = true;
176
177 if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
178 {
179 // \UTGame\Config以下を指定せねばならない
180
181 // 指定されたフォルダ名を求める
182 index = folderBrowserDialog1.SelectedPath.IndexOf(@"\UTGame\Config\");
183 if (index < 0)
184 {
185 // \UTGame\Config 以下が指定されていなかった!
186 MessageBox.Show(Properties.Resources.msgErrConfigDir);
187 }
188 else
189 {
190 txtBxConfigDir.Text = folderBrowserDialog1.SelectedPath.Substring(index + @"\UTGame\Config\".Length);
191 }
192 }
193 }
194 else
195 {
196 MessageBox.Show(Properties.Resources.msgExistsUT3BIN);
197 return;
198 }
199 }
200
201 private void cmbBxGameType_SelectedIndexChanged(object sender, EventArgs e)
202 {
203 // マップリストを入れ替える
204
205 int[] value = new int[5];
206
207 // この値を入れ替えることで得られるマップリストが変わる
208 map.SelectedMode = cmbBxGameType.SelectedIndex;
209
210
211 // 各モードごとに最適な値を設定する
212 value = getSuitableValue(cmbBxGameType.SelectedIndex);
213 numMinPlayers.Value = value[0];
214 numMaxPlayers.Value = value[1];
215 numNumOfBots.Value = value[2];
216 numTimeLimit.Value = value[3];
217 numGoalScore.Value = value[4];
218
219
220 cmbBxMapName.Items.Clear();
221 cmbBxMapName.Items.AddRange(map.getList());
222 cmbBxMapName.SelectedIndex = 0;
223 }
224
225 private void chkBxNoHomDir_CheckedChanged_1(object sender, EventArgs e)
226 {
227 if (chkBxNoHomDir.Checked)
228 {
229 txtBxConfigDir.Visible = true;
230 btnOpenDir.Visible = true;
231 }
232 else
233 {
234 txtBxConfigDir.Visible = false;
235 btnOpenDir.Visible = false;
236 }
237 }
238
239 private void timer1_Tick(object sender, EventArgs e)
240 {
241 // 定期的な処理を書く(1分間隔)
242
243 // サーバーが生きているかチェックする
244 if (bAlive)
245 {
246 if (server.checkAlive() == false)
247 {
248 restartServer();
249 }
250 }
251
252 }
253
254 //////////////////
255 // メニュー関連 //
256 //////////////////
257
258 private void aboutAToolStripMenuItem_Click(object sender, EventArgs e)
259 {
260 // バージョン情報を出す
261 AboutBox1 about = new AboutBox1();
262 about.ShowDialog();
263 }
264
265 private void batchFileBToolStripMenuItem_Click(object sender, EventArgs e)
266 {
267 // サーバーを立ち上げるバッチファイルを作る
268
269 // saveFileDialogの準備
270 saveFileDialog1.FileName = txtBxServerName.Text;
271 saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
272 saveFileDialog1.Filter =
273 "batch file(*.bat)|*.bat|all files(*.*)|*.*";
274 saveFileDialog1.FilterIndex = 1;
275 saveFileDialog1.Title = "Select As";
276 saveFileDialog1.RestoreDirectory = true;
277
278 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
279 {
280 // ストリームを開く
281 StreamWriter sw =
282 new StreamWriter(saveFileDialog1.FileName,
283 false,
284 System.Text.Encoding.GetEncoding(932));
285
286 Server srv = new Server(this);
287
288 sw.WriteLine(srv.makeBatchArgs());
289
290 // ストリームを閉じる
291 sw.Close();
292 }
293
294 }
295
296 private void exitXToolStripMenuItem_Click(object sender, EventArgs e)
297 {
298 this.Close();
299 }
300
301 private void preferencesPToolStripMenuItem_Click(object sender, EventArgs e)
302 {
303 FormPreferences preferences = new FormPreferences();
304 preferences.ShowDialog();
305 }
306
307 private void loadProfileLToolStripMenuItem_Click(object sender, EventArgs e)
308 {
309 string profile = profileName;
310
311 // プロファイル選択画面を出す
312 FormProfile fprofile = new FormProfile(this);
313 fprofile.ShowDialog();
314
315 // プロファイル名が変わっていたら
316 if (profile != profileName)
317 {
318 loadProfile(profileName);
319 }
320 }
321
322 private void saveProfileSToolStripMenuItem_Click(object sender, EventArgs e)
323 {
324 saveProfile(profileName);
325 }
326
327 private void startSToolStripMenuItem_Click(object sender, EventArgs e)
328 {
329 startServer();
330 }
331
332 private void stopToolStripMenuItem_Click(object sender, EventArgs e)
333 {
334 stopServer();
335 }
336
337 private void restartToolStripMenuItem_Click(object sender, EventArgs e)
338 {
339 restartServer();
340 }
341
342
343 //////////////////////////
344 // ツールストリップ関連 //
345 //////////////////////////
346
347 private void toolStripButtonStart_Click(object sender, EventArgs e)
348 {
349 startServer();
350 }
351
352 private void toolStripButtonStop_Click(object sender, EventArgs e)
353 {
354 stopServer();
355 }
356
357 private void toolStripButtonRestart_Click(object sender, EventArgs e)
358 {
359 restartServer();
360 }
361
362 private void toolStripButtonNew_Click(object sender, EventArgs e)
363 {
364 // フォームをクリアするか確認する
365 if (MessageBox.Show(Properties.Resources.msgNewProfile,
366 Properties.Resources.wordConfirmation,
367 MessageBoxButtons.YesNo, MessageBoxIcon.Question)
368 == DialogResult.Yes)
369 {
370 initializeForm();
371 }
372 }
373
374 private void toolStripButtonLoad_Click(object sender, EventArgs e)
375 {
376 string profile = profileName;
377
378 // プロファイル選択画面を出す
379 FormProfile fprofile = new FormProfile(this);
380 fprofile.ShowDialog();
381
382 // 他のプロファイルが選ばれていたら読み込む
383 if (profile != profileName)
384 {
385 loadProfile(profileName);
386 }
387 }
388
389 private void toolStripButtonSave_Click(object sender, EventArgs e)
390 {
391 saveProfile(profileName);
392 }
393
394
395 //////////////
396 // 独自関数 //
397 //////////////
398
399 private int[] getSuitableValue(int SelectedIndex)
400 {
401 switch (SelectedIndex)
402 {
403 case 0:
404 return DeathMatch;
405 case 1:
406 return TeamDeathMatch;
407 case 2:
408 return Duel;
409 case 3:
410 return Betrayal;
411 case 4:
412 return Greed;
413 case 5:
414 return CaptureTheFlag;
415 case 6:
416 return VehicleCTF;
417 case 7:
418 return Warfare;
419 default:
420 return DeathMatch;
421 }
422 }
423
424 private void initializeForm()
425 {
426 // フォームデータの初期化
427
428 Properties.Settings.Default.Reset();
429
430 // 各種コンボボックスのクリア
431 cmbBxGameType.Items.Clear();
432 cmbBxMapName.Items.Clear();
433 cmbBxBotSkill.Items.Clear();
434 cmbBxVsBot.Items.Clear();
435 lstBxEnableMut.Items.Clear();
436 lstBxActiveMut.Items.Clear();
437
438
439 // データの追加
440
441 // Game type
442 cmbBxGameType.Items.AddRange(mode.getList());
443 cmbBxGameType.SelectedIndex = Properties.Settings.Default.GameType;
444
445 // Map name
446 cmbBxMapName.Items.AddRange(map.getList());
447 cmbBxMapName.SelectedIndex = Properties.Settings.Default.MapName;
448
449 // Bot Skill
450 cmbBxBotSkill.Items.AddRange(bot.skill.getList());
451 cmbBxBotSkill.SelectedIndex = Properties.Settings.Default.BotSkill;
452
453 // vs Bots
454 cmbBxVsBot.Items.AddRange(bot.raito.getList());
455 cmbBxVsBot.SelectedIndex = Properties.Settings.Default.VsBots;
456
457 // Mutator
458 lstBxEnableMut.Items.AddRange(mut.getList());
459 lstBxEnableMut.SelectedIndex = 0;
460 }
461
462 private void loadProfile(string name)
463 {
464 // 設定ファイルに保存された情報を元に復元する
465
466 Properties.Settings.Default.SettingsKey = name;
467 Properties.Settings.Default.Reload();
468
469 // プロファイル名をタイトルに表示
470 this.Text = title + sepalater + name;
471
472 // EnableMUTとActiveMUTがnullなら設定が存在しない
473 if (Properties.Settings.Default.ActiveMUT == null &&
474 Properties.Settings.Default.ActiveMUT == null)
475 {
476 initializeForm();
477 return;
478 }
479
480 // tab3 Game
481 cmbBxGameType.SelectedIndex = Properties.Settings.Default.GameType;
482 cmbBxMapName.SelectedIndex = Properties.Settings.Default.MapName;
483 cmbBxVsBot.SelectedIndex = Properties.Settings.Default.VsBots;
484 cmbBxBotSkill.SelectedIndex = Properties.Settings.Default.BotSkill;
485
486 // tab4 Mutators
487 lstBxEnableMut.Items.Clear();
488 for (int i = 0; i < Properties.Settings.Default.EnableMUT.Count; i++)
489 {
490 lstBxEnableMut.Items.Add(Properties.Settings.Default.EnableMUT[i]);
491 }
492
493 lstBxActiveMut.Items.Clear();
494 for (int i = 0; i < Properties.Settings.Default.ActiveMUT.Count; i++)
495 {
496 lstBxActiveMut.Items.Add(Properties.Settings.Default.ActiveMUT[i]);
497 }
498 }
499
500 private void saveProfile(string name)
501 {
502 // 通常手順では保存されない情報を保存する
503
504 // プロファイル名
505 Properties.Settings.Default.SettingsKey = name;
506
507 // プロファイル名をタイトルに表示
508 this.Text = title + sepalater + name;
509
510 // tab3 Game
511 Properties.Settings.Default.GameType = cmbBxGameType.SelectedIndex;
512 Properties.Settings.Default.MapName = cmbBxMapName.SelectedIndex;
513 Properties.Settings.Default.VsBots = cmbBxVsBot.SelectedIndex;
514 Properties.Settings.Default.BotSkill = cmbBxBotSkill.SelectedIndex;
515
516 // tab4 Mutators
517
518 // lstBxEnableMUTの一覧を保存
519 StringCollection EnableMUT = new StringCollection();
520 foreach (object item in lstBxEnableMut.Items)
521 {
522 EnableMUT.Add(item.ToString());
523 }
524 Properties.Settings.Default.EnableMUT = EnableMUT;
525
526 // lstBxActiveMUTの一覧を保存
527 StringCollection ActiveMUT = new StringCollection();
528 foreach (object item in lstBxActiveMut.Items)
529 {
530 ActiveMUT.Add(item.ToString());
531 }
532 Properties.Settings.Default.ActiveMUT = ActiveMUT;
533
534 // 設定ファイルに保存
535 Properties.Settings.Default.Save();
536 }
537
538 private void startServer()
539 {
540 // サーバーを開始する
541
542 if (server != null)
543 {
544 // 既にサーバーが動いていた
545 return;
546 }
547
548 server = new Server(this);
549 bool success = server.start();
550 if (!success)
551 {
552 // start()に失敗した
553 server = null;
554 return;
555 }
556
557 this.Text = title + sepalater + Properties.Resources.wordWorking + sepalater + "[" + txtBxServerName.Text + "]";
558 }
559
560 private void stopServer()
561 {
562 // サーバーを停止する
563
564 if (server == null)
565 {
566 // サーバーが動いてない
567 return;
568 }
569
570 server.stop();
571 server = null;
572
573 this.Text = title + sepalater + Properties.Resources.wordStoped + sepalater + "[" + txtBxServerName.Text + "]";
574 }
575
576 private void restartServer()
577 {
578 // サーバーを再起動する
579
580 if (server == null)
581 {
582 // サーバーが動いていない
583 return;
584 }
585
586 stopServer();
587 startServer();
588 }
589
590 }
591 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26