author | unc0rr |
Thu, 22 Nov 2012 01:19:16 +0400 | |
branch | flibqtfrontend |
changeset 8092 | 08960209db8c |
parent 8082 | 675372256a01 |
child 8363 | 0b4ac686fc44 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
486 | 3 |
* Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com> |
6952 | 4 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
184 | 5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; version 2 of the License |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
18 |
*/ |
|
19 |
||
20 |
#include "tcpBase.h" |
|
21 |
||
22 |
#include <QMessageBox> |
|
23 |
#include <QList> |
|
7794
ab7b94c03bc9
QMessageBox overhaul: unify buttons, style and appearance of info dialogs, fixing a few typos and return values along the way
koda
parents:
7673
diff
changeset
|
24 |
#include <QApplication> |
184 | 25 |
#include <QImage> |
8082
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
26 |
#include <QThread> |
184 | 27 |
|
28 |
#include "hwconsts.h" |
|
29 |
||
8082
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
30 |
#ifdef HWLIBRARY |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
31 |
extern "C" void Game(char**arguments); |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
32 |
|
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
33 |
//NOTE: most likely subclassing QThread is wrong |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
34 |
class EngineThread : public QThread |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
35 |
{ |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
36 |
protected: |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
37 |
void run(); |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
38 |
}; |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
39 |
|
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
40 |
void EngineThread::run() |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
41 |
{ |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
42 |
char *args[12]; |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
43 |
args[0] = "1"; //cShowFPS |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
44 |
args[1] = "65000"; //ipcPort |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
45 |
args[2] = "1024"; //cScreenWidth |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
46 |
args[3] = "768"; //cScreenHeight |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
47 |
args[4] = "0"; //cReducedQuality |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
48 |
args[5] = "en.txt"; //cLocaleFName |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
49 |
args[6] = "koda"; //UserNick |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
50 |
args[7] = "1"; //SetSound |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
51 |
args[8] = "1"; //SetMusic |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
52 |
args[9] = "0"; //cAltDamage |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
53 |
args[10]= "../Resources/hedgewars/Data"; //cPathPrefix |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
54 |
args[11]= NULL; //recordFileName |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
55 |
Game(args); |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
56 |
} |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
57 |
#endif |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
58 |
|
184 | 59 |
QList<TCPBase*> srvsList; |
60 |
||
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
61 |
TCPBase::~TCPBase() |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
62 |
{ |
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
63 |
// make sure this object is not in the server list anymore |
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
64 |
srvsList.removeOne(this); |
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
65 |
} |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
66 |
|
8092 | 67 |
TCPBase::TCPBase(QObject *parent) : |
8070
66bc20d089fc
Okay, remove previous request only if it has same parent
unc0rr
parents:
8069
diff
changeset
|
68 |
QObject(parent), |
8092 | 69 |
m_hasStarted(false) |
184 | 70 |
{ |
7794
ab7b94c03bc9
QMessageBox overhaul: unify buttons, style and appearance of info dialogs, fixing a few typos and return values along the way
koda
parents:
7673
diff
changeset
|
71 |
|
184 | 72 |
} |
73 |
||
74 |
void TCPBase::RealStart() |
|
75 |
{ |
|
8092 | 76 |
onEngineStart(); |
389 | 77 |
|
8082
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
78 |
#ifdef HWLIBRARY |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
79 |
EngineThread engineThread;// = new EngineThread(this); |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
80 |
engineThread.start(); |
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
81 |
#else |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
82 |
QProcess * process; |
8092 | 83 |
|
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
84 |
process = new QProcess(); |
8092 | 85 |
QStringList arguments = getArguments(); |
3697 | 86 |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
87 |
// redirect everything written on stdout/stderr |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
88 |
if(isDevBuild) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
89 |
process->setProcessChannelMode(QProcess::ForwardedChannels); |
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
90 |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
91 |
process->start(bindir->absolutePath() + "/hwengine", arguments); |
8082
675372256a01
lotsa hackery to get frontend somehow link libengine...
koda
parents:
8070
diff
changeset
|
92 |
#endif |
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
93 |
m_hasStarted = true; |
184 | 94 |
} |
95 |
||
8092 | 96 |
void TCPBase::clientDisconnected() |
184 | 97 |
{ |
8092 | 98 |
emit nextPlease(); |
7673
e06b83cbde74
fix a segfault related to tcp servers deleting themselves without removing themselves from the internal server list first.
sheepluva
parents:
7615
diff
changeset
|
99 |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
100 |
deleteLater(); |
184 | 101 |
} |
102 |
||
8092 | 103 |
void TCPBase::iStart() |
184 | 104 |
{ |
8092 | 105 |
disconnect(srvsList.first(), SIGNAL(nextPlease()), this, SLOT(iStart())); |
184 | 106 |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
107 |
RealStart(); |
184 | 108 |
} |
109 |
||
8092 | 110 |
void TCPBase::start(bool couldCancelPreviousRequest) |
184 | 111 |
{ |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
112 |
if(srvsList.isEmpty()) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
113 |
{ |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
114 |
srvsList.push_back(this); |
8092 | 115 |
|
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
116 |
RealStart(); |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
117 |
} |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
118 |
else |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
119 |
{ |
8070
66bc20d089fc
Okay, remove previous request only if it has same parent
unc0rr
parents:
8069
diff
changeset
|
120 |
TCPBase * last = srvsList.last(); |
66bc20d089fc
Okay, remove previous request only if it has same parent
unc0rr
parents:
8069
diff
changeset
|
121 |
if(couldCancelPreviousRequest |
66bc20d089fc
Okay, remove previous request only if it has same parent
unc0rr
parents:
8069
diff
changeset
|
122 |
&& last->couldBeRemoved() |
66bc20d089fc
Okay, remove previous request only if it has same parent
unc0rr
parents:
8069
diff
changeset
|
123 |
&& (last->parent() == parent())) |
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
124 |
{ |
8070
66bc20d089fc
Okay, remove previous request only if it has same parent
unc0rr
parents:
8069
diff
changeset
|
125 |
srvsList.removeLast(); |
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
126 |
last->deleteLater(); |
8092 | 127 |
start(couldCancelPreviousRequest); |
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
128 |
} else |
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
129 |
{ |
8092 | 130 |
connect(srvsList.last(), SIGNAL(nextPlease()), this, SLOT(iStart())); |
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
131 |
srvsList.push_back(this); |
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
132 |
} |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
133 |
} |
184 | 134 |
} |
135 |
||
8069
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
136 |
bool TCPBase::couldBeRemoved() |
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
137 |
{ |
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
138 |
return false; |
bb7671829935
- Only allow one engine instance running at the moment
unc0rr
parents:
7794
diff
changeset
|
139 |
} |