|
1 /* |
|
2 * Hedgewars, a worms-like game |
|
3 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 * |
|
5 * Distributed under the terms of the BSD-modified licence: |
|
6 * |
|
7 * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 * of this software and associated documentation files (the "Software"), to deal |
|
9 * with the Software without restriction, including without limitation the |
|
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 * sell copies of the Software, and to permit persons to whom the Software is |
|
12 * furnished to do so, subject to the following conditions: |
|
13 * |
|
14 * 1. Redistributions of source code must retain the above copyright notice, |
|
15 * this list of conditions and the following disclaimer. |
|
16 * 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 * this list of conditions and the following disclaimer in the documentation |
|
18 * and/or other materials provided with the distribution. |
|
19 * 3. The name of the author may not be used to endorse or promote products |
|
20 * derived from this software without specific prior written permission. |
|
21 * |
|
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 */ |
|
33 |
|
34 #include <QMessageBox> |
|
35 #include <QProcess> |
|
36 #include <QTimer> |
|
37 #include <QFile> |
|
38 #include <QString> |
|
39 #include <QByteArray> |
|
40 #include <QTextStream> |
|
41 #include "game.h" |
|
42 #include "hwconsts.h" |
|
43 |
|
44 HWGame::HWGame() |
|
45 { |
|
46 IPCServer = new QTcpServer(this); |
|
47 IPCServer->setMaxPendingConnections(1); |
|
48 if (!IPCServer->listen(QHostAddress("127.0.0.1"), IPC_PORT)) |
|
49 { |
|
50 QMessageBox::critical(this, tr("Error"), |
|
51 tr("Unable to start the server: %1.") |
|
52 .arg(IPCServer->errorString())); |
|
53 } |
|
54 connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
|
55 IPCSocket = 0; |
|
56 TeamCount = 0; |
|
57 } |
|
58 |
|
59 void HWGame::NewConnection() |
|
60 { |
|
61 QTcpSocket * client = IPCServer->nextPendingConnection(); |
|
62 if(!IPCSocket) |
|
63 { |
|
64 IPCSocket = client; |
|
65 connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
66 connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
67 msgsize = 0; |
|
68 } else |
|
69 { |
|
70 client->disconnectFromHost(); |
|
71 delete client; |
|
72 } |
|
73 } |
|
74 |
|
75 void HWGame::ClientDisconnect() |
|
76 { |
|
77 IPCSocket = 0; |
|
78 delete this; |
|
79 } |
|
80 |
|
81 void HWGame::SendTeamConfig(int index) |
|
82 { |
|
83 QFile teamcfg(teams[index]); |
|
84 if (!teamcfg.open(QIODevice::ReadOnly)) |
|
85 { |
|
86 return ; |
|
87 } |
|
88 QTextStream stream(&teamcfg); |
|
89 stream.setCodec("UTF-8"); |
|
90 QString str; |
|
91 |
|
92 while (!stream.atEnd()) |
|
93 { |
|
94 str = stream.readLine(); |
|
95 if (str.startsWith(";")) continue; |
|
96 str.prepend("e"); |
|
97 SendIPC(str.toLocal8Bit()); |
|
98 } |
|
99 teamcfg.close(); |
|
100 } |
|
101 |
|
102 void HWGame::SendConfig() |
|
103 { |
|
104 SENDIPC("TL"); |
|
105 SENDIPC("e$gmflags 0"); |
|
106 SENDIPC("eaddteam"); |
|
107 SendTeamConfig(0); |
|
108 SENDIPC("ecolor 65535"); |
|
109 SENDIPC("eadd hh0 0"); |
|
110 SENDIPC("eadd hh1 0"); |
|
111 SENDIPC("eadd hh2 0"); |
|
112 SENDIPC("eadd hh3 0"); |
|
113 SENDIPC("eaddteam"); |
|
114 SendTeamConfig(1); |
|
115 SENDIPC("ecolor 16776960"); |
|
116 SENDIPC("eadd hh0 1"); |
|
117 SENDIPC("eadd hh1 1"); |
|
118 SENDIPC("eadd hh2 1"); |
|
119 SENDIPC("eadd hh3 1"); |
|
120 } |
|
121 |
|
122 void HWGame::ParseMessage() |
|
123 { |
|
124 switch(msgsize) { |
|
125 case 1: switch(msgbuf[0]) { |
|
126 case '?': { |
|
127 SENDIPC("!"); |
|
128 break; |
|
129 } |
|
130 } |
|
131 case 5: switch(msgbuf[0]) { |
|
132 case 'C': { |
|
133 SendConfig(); |
|
134 break; |
|
135 } |
|
136 } |
|
137 } |
|
138 } |
|
139 |
|
140 void HWGame::SendIPC(const char* msg, unsigned char len) |
|
141 { |
|
142 IPCSocket->write((char *)&len, 1); |
|
143 IPCSocket->write(msg, len); |
|
144 } |
|
145 |
|
146 void HWGame::SendIPC(const QByteArray buf) |
|
147 { |
|
148 if (buf.size() > 255) return; |
|
149 unsigned char len = buf.size(); |
|
150 IPCSocket->write((char *)&len, 1); |
|
151 IPCSocket->write(buf); |
|
152 } |
|
153 |
|
154 void HWGame::ClientRead() |
|
155 { |
|
156 qint64 readbytes = 1; |
|
157 while (readbytes > 0) |
|
158 { |
|
159 if (msgsize == 0) |
|
160 { |
|
161 msgbufsize = 0; |
|
162 readbytes = IPCSocket->read((char *)&msgsize, 1); |
|
163 } else |
|
164 { |
|
165 msgbufsize += readbytes = IPCSocket->read((char *)&msgbuf[msgbufsize], msgsize - msgbufsize); |
|
166 if (msgbufsize = msgsize) |
|
167 { |
|
168 ParseMessage(); |
|
169 msgsize = 0; |
|
170 } |
|
171 } |
|
172 } |
|
173 } |
|
174 |
|
175 void HWGame::Start(int Resolution) |
|
176 { |
|
177 if (TeamCount < 2) return; |
|
178 QProcess * process; |
|
179 QStringList arguments; |
|
180 process = new QProcess; |
|
181 arguments << resolutions[0][Resolution]; |
|
182 arguments << resolutions[1][Resolution]; |
|
183 arguments << "avematan"; |
|
184 arguments << "46631"; |
|
185 arguments << "=seed="; |
|
186 arguments << "1"; |
|
187 process->start("hw", arguments); |
|
188 } |
|
189 |
|
190 void HWGame::AddTeam(const QString teamname) |
|
191 { |
|
192 if (TeamCount == 5) return; |
|
193 teams[TeamCount] = teamname; |
|
194 TeamCount++; |
|
195 } |