| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Diagnostics; |
| 4 |
using System.Linq; |
| 5 |
using System.Text; |
| 6 |
using System.Windows.Forms; |
| 7 |
|
| 8 |
namespace UT3_Server_Launcher |
| 9 |
{ |
| 10 |
class Server |
| 11 |
{ |
| 12 |
|
| 13 |
////////// |
| 14 |
// 定義 // |
| 15 |
////////// |
| 16 |
|
| 17 |
const string colon = ":"; |
| 18 |
const string comma = ","; |
| 19 |
const string space = " "; |
| 20 |
const string sepalater = space + colon + space; |
| 21 |
|
| 22 |
|
| 23 |
////////////////// |
| 24 |
// メンバー変数 // |
| 25 |
////////////////// |
| 26 |
|
| 27 |
Mode mode; |
| 28 |
Map map; |
| 29 |
Bot bot; |
| 30 |
Mut mut; |
| 31 |
|
| 32 |
string UT3BIN = Properties.Settings.Default.UT3BIN; |
| 33 |
string OPTION = Properties.Settings.Default.OPTION; |
| 34 |
|
| 35 |
Process process1 = null; |
| 36 |
|
| 37 |
bool bDebug = Properties.Settings.Default.Debug; |
| 38 |
|
| 39 |
// tab1 Server |
| 40 |
string ServerName; |
| 41 |
decimal GamePort; |
| 42 |
decimal QueryPort; |
| 43 |
string GameSpyName; |
| 44 |
string GameSpyPass; |
| 45 |
string AdminName; |
| 46 |
string AdminPass; |
| 47 |
string LogName; |
| 48 |
string JoinPass; |
| 49 |
|
| 50 |
// tab2 Options |
| 51 |
bool bReportStats; |
| 52 |
bool bAdvertiseServer; |
| 53 |
bool bPureServer; |
| 54 |
bool bLANonly; |
| 55 |
bool bDedicatedServer; |
| 56 |
bool bJoinInProgress; |
| 57 |
bool bAllowInvites; |
| 58 |
bool bUsesPresence; |
| 59 |
bool bJoinViaPresence; |
| 60 |
bool bUnattended; |
| 61 |
bool bNoHomeDir; |
| 62 |
string DirectoryPath; |
| 63 |
|
| 64 |
// tab3 Game |
| 65 |
int ModeIndex; |
| 66 |
int MapIndex; |
| 67 |
decimal MinPlayers; |
| 68 |
decimal MaxPlayers; |
| 69 |
decimal NumOfBots; |
| 70 |
decimal Spectators; |
| 71 |
int BotSkill; |
| 72 |
int BotRaito; |
| 73 |
decimal TimeLimit; |
| 74 |
decimal GoalScore; |
| 75 |
bool bReady; |
| 76 |
bool bForceRespwan; |
| 77 |
|
| 78 |
// tab4 Mutator |
| 79 |
string[] EnableMUT; |
| 80 |
string[] ActiveMUT; |
| 81 |
string OtherMUT; |
| 82 |
|
| 83 |
public Server(FormMain fm) |
| 84 |
{ |
| 85 |
// コンストラクタ |
| 86 |
// FormMainのコントロールから値をコピーする |
| 87 |
|
| 88 |
mode = fm.mode; |
| 89 |
map = fm.map; |
| 90 |
bot = fm.bot; |
| 91 |
mut = fm.mut; |
| 92 |
|
| 93 |
// tab1 Server |
| 94 |
ServerName = fm.txtBxServerName.Text; |
| 95 |
GamePort = fm.numGamePort.Value; |
| 96 |
QueryPort = fm.numQueryPort.Value; |
| 97 |
GameSpyName = fm.txtBxGsName.Text; |
| 98 |
GameSpyPass = fm.txtBxGsPass.Text; |
| 99 |
AdminName = fm.txtBxAdmnName.Text; |
| 100 |
AdminPass = fm.txtBxAdmnPass.Text; |
| 101 |
LogName = fm.txtBxLogName.Text; |
| 102 |
JoinPass = fm.txtBxJoinPass.Text; |
| 103 |
|
| 104 |
// tab2 Options |
| 105 |
bReportStats = fm.chkBxReport.Checked; |
| 106 |
bAdvertiseServer= fm.chkBxAdvertise.Checked; |
| 107 |
bPureServer = fm.chkBxPure.Checked; |
| 108 |
bLANonly = fm.chkBxLan.Checked; |
| 109 |
bDedicatedServer= fm.chkBxDedicated.Checked; |
| 110 |
bJoinInProgress = fm.chkBxJoinIn.Checked; |
| 111 |
bAllowInvites = fm.chkBxAllow.Checked; |
| 112 |
bUsesPresence = fm.chkBxUserPresence.Checked; |
| 113 |
bJoinViaPresence= fm.chkBxJoinVia.Checked; |
| 114 |
bUnattended = fm.chkBxUnattend.Checked; |
| 115 |
bNoHomeDir = fm.chkBxNoHomDir.Checked; |
| 116 |
DirectoryPath = fm.txtBxConfigDir.Text; |
| 117 |
|
| 118 |
// tab3 Game; |
| 119 |
ModeIndex = fm.cmbBxGameType.SelectedIndex; |
| 120 |
MapIndex = fm.cmbBxMapName.SelectedIndex; |
| 121 |
MinPlayers = fm.numMinPlayers.Value; |
| 122 |
MaxPlayers = fm.numMaxPlayers.Value; |
| 123 |
NumOfBots = fm.numNumOfBots.Value; |
| 124 |
BotSkill = fm.cmbBxBotSkill.SelectedIndex; |
| 125 |
TimeLimit = fm.numTimeLimit.Value; |
| 126 |
Spectators = fm.numSpectators.Value; |
| 127 |
BotRaito = fm.cmbBxVsBot.SelectedIndex; |
| 128 |
GoalScore = fm.numGoalScore.Value; |
| 129 |
bReady = fm.chkBxReady.Checked; |
| 130 |
bForceRespwan = fm.chkBxForceRespawn.Checked; |
| 131 |
|
| 132 |
|
| 133 |
// tab4 Mutator |
| 134 |
int count = 0; |
| 135 |
|
| 136 |
count = fm.lstBxEnableMut.Items.Count; |
| 137 |
EnableMUT = new string[count]; |
| 138 |
for (int i = 0; i < count; i++) |
| 139 |
{ |
| 140 |
EnableMUT[i] = fm.lstBxEnableMut.Items[i].ToString(); |
| 141 |
} |
| 142 |
|
| 143 |
count = fm.lstBxActiveMut.Items.Count; |
| 144 |
ActiveMUT = new string[count]; |
| 145 |
for (int i = 0; i < count; i++) |
| 146 |
{ |
| 147 |
ActiveMUT[i] = fm.lstBxActiveMut.Items[i].ToString(); |
| 148 |
} |
| 149 |
|
| 150 |
OtherMUT = fm.txtBxOtherMut.Text; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
////////////// |
| 155 |
// 独自関数 // |
| 156 |
////////////// |
| 157 |
|
| 158 |
public bool start() |
| 159 |
{ |
| 160 |
ProcessStartInfo psInfo = new ProcessStartInfo(); |
| 161 |
|
| 162 |
psInfo.FileName = @UT3BIN; // 実行ファイル(UT3.exe) |
| 163 |
string Arguments = getArgument() + OPTION; // パラメータ |
| 164 |
|
| 165 |
psInfo.Arguments = Arguments; |
| 166 |
psInfo.UseShellExecute = false; // シェル機能を使用しない |
| 167 |
psInfo.CreateNoWindow = false; // コンソール・ウィンドウを開かない |
| 168 |
psInfo.RedirectStandardOutput = false; // 標準出力をリダイレクト |
| 169 |
|
| 170 |
|
| 171 |
bool success = true; |
| 172 |
|
| 173 |
if (bDebug) |
| 174 |
{ |
| 175 |
// デバッグ用 |
| 176 |
MessageBox.Show(UT3BIN + space + Arguments + sepalater + getArgument().Length + space + "length"); |
| 177 |
success = false; |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
try |
| 182 |
{ |
| 183 |
process1 = Process.Start(psInfo); // 実行開始 |
| 184 |
} |
| 185 |
catch |
| 186 |
{ |
| 187 |
success = false; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return success; |
| 192 |
} |
| 193 |
|
| 194 |
public bool stop() |
| 195 |
{ |
| 196 |
if (process1 == null) |
| 197 |
{ |
| 198 |
// サーバーが動いていない |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
process1.Kill(); |
| 203 |
process1 = null; |
| 204 |
|
| 205 |
return true; |
| 206 |
} |
| 207 |
|
| 208 |
public bool checkAlive() |
| 209 |
{ |
| 210 |
// サーバーが生きているかチェックする |
| 211 |
if (process1 == null) |
| 212 |
{ |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
if (process1.HasExited) |
| 217 |
{ |
| 218 |
// 不正に終了していた |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
// 問題無かった |
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
public string makeBatchArgs() |
| 227 |
{ |
| 228 |
// バッチファイルの中身を作って返す |
| 229 |
|
| 230 |
string Directory = string.Format("cd {0}\r\n", UT3BIN.Substring(0, UT3BIN.IndexOf(@"\UT3.exe"))); |
| 231 |
string Arguments = getArgument() + OPTION; |
| 232 |
|
| 233 |
return Directory + "UT3.exe" + space + Arguments; |
| 234 |
} |
| 235 |
|
| 236 |
private string getArgument() |
| 237 |
{ |
| 238 |
// UT3.exeのパラメータを作成 |
| 239 |
|
| 240 |
string ARG = string.Empty; |
| 241 |
|
| 242 |
ARG += "Server" + space + map.getArg(MapIndex); |
| 243 |
ARG += "?Game=" + mode.getArg(ModeIndex); |
| 244 |
ARG += "?ServerDescription=" + toASCII(ServerName); |
| 245 |
|
| 246 |
ARG += "?Numplay=" + NumOfBots; |
| 247 |
ARG += "?MinNetPlayers=" + MinPlayers; |
| 248 |
ARG += "?MaxPlayers=" + MaxPlayers; |
| 249 |
ARG += "?MaxSpectators=" + Spectators; |
| 250 |
ARG += "?TimeLimit=" + TimeLimit; |
| 251 |
ARG += "?GoalScore=" + GoalScore; |
| 252 |
|
| 253 |
ARG += "?Difficulty=" + bot.skill.getArg(BotSkill); |
| 254 |
ARG += "?VsBots=" + bot.raito.getArg(BotRaito); |
| 255 |
|
| 256 |
ARG += "?AdminName=" + AdminName; |
| 257 |
ARG += "?AdminPassword=" + AdminPass; |
| 258 |
ARG += "?GamePassword=" + JoinPass; |
| 259 |
|
| 260 |
ARG += "?bUseStats=" + bReportStats; |
| 261 |
ARG += "?bShouldAdvertise=" + bAdvertiseServer; |
| 262 |
ARG += "?PureServer=" + bPureServer; |
| 263 |
ARG += "?bIsLanMatch=" + bLANonly; |
| 264 |
ARG += "?bIsDedicated=" + bDedicatedServer; |
| 265 |
ARG += "?bAllowJoinInProgress=" + bJoinInProgress; |
| 266 |
ARG += "?bAllowInvites=" + bAllowInvites; |
| 267 |
ARG += "?bUsesPresence=" + bUsesPresence; |
| 268 |
ARG += "?bAllowJoinViaPresence="+ bJoinViaPresence; |
| 269 |
ARG += "?bPlayersMustBeReady=" + bReady; |
| 270 |
ARG += "?ForceRespawn=" + bForceRespwan; |
| 271 |
|
| 272 |
// mutator |
| 273 |
if (ActiveMUT.Length > 0 || OtherMUT != "") |
| 274 |
{ |
| 275 |
ARG += "?mutator="; |
| 276 |
foreach (string mutator in ActiveMUT) |
| 277 |
{ |
| 278 |
ARG += mut.getArg(mutator) + comma; |
| 279 |
} |
| 280 |
ARG += OtherMUT; |
| 281 |
} |
| 282 |
|
| 283 |
ARG += space + "-login=" + GameSpyName; |
| 284 |
ARG += space + "-password=" + GameSpyPass; |
| 285 |
ARG += space + "-Port=" + GamePort; |
| 286 |
ARG += space + "-QueryPort=" + QueryPort; |
| 287 |
ARG += space + "-log=" + LogName; |
| 288 |
|
| 289 |
// no home directory |
| 290 |
if (bNoHomeDir) |
| 291 |
{ |
| 292 |
ARG += space + "-NoHomeDir=" + bNoHomeDir; |
| 293 |
ARG += space + "-ConfigSubDir=" + DirectoryPath; |
| 294 |
} |
| 295 |
|
| 296 |
return ARG; |
| 297 |
} |
| 298 |
|
| 299 |
private string toASCII(string target) |
| 300 |
{ |
| 301 |
// 一文字ずつASCII(3桁) + "000"(3桁) へ変換 |
| 302 |
byte[] b = Encoding.GetEncoding("ASCII").GetBytes(target); |
| 303 |
|
| 304 |
string tmp = string.Empty; |
| 305 |
foreach (byte data in b) |
| 306 |
{ |
| 307 |
tmp += string.Format("{0:000}", data) + "000"; |
| 308 |
} |
| 309 |
|
| 310 |
return tmp; |
| 311 |
} |
| 312 |
} |
| 313 |
} |