QTfrontend/game.cpp
changeset 32 78bff13b11c0
parent 31 99888245a4e8
child 36 a803a00a3272
equal deleted inserted replaced
31:99888245a4e8 32:78bff13b11c0
    68 	{
    68 	{
    69 		IPCSocket = client;
    69 		IPCSocket = client;
    70 		connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    70 		connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    71 		connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    71 		connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    72 		msgsize = 0;
    72 		msgsize = 0;
       
    73 		if (toSendBuf.size() > 0)
       
    74 			SENDIPC("?");
    73 	} else
    75 	} else
    74 	{
    76 	{
    75 		client->disconnectFromHost();
    77 		client->disconnectFromHost();
    76 		delete client;
    78 		delete client;
    77 	}
    79 	}
    89 	LocalCFG(teams[index]);
    91 	LocalCFG(teams[index]);
    90 }
    92 }
    91 
    93 
    92 void HWGame::SendConfig()
    94 void HWGame::SendConfig()
    93 {
    95 {
    94 	if (gameType == gtDemo)
       
    95 	{
       
    96 		SENDIPC("TD");
       
    97 		RawSendIPC(*toSendBuf);
       
    98 		delete toSendBuf;
       
    99 		return ;
       
   100 	}
       
   101 	SENDIPC("TL");
    96 	SENDIPC("TL");
   102 //	SENDIPC("e$gmflags 0");
    97 //	SENDIPC("e$gmflags 0");
   103 	SENDIPC("eaddteam");
    98 	SENDIPC("eaddteam");
   104 	SendTeamConfig(0);
    99 	SendTeamConfig(0);
   105 //	if () SENDIPC("rdriven");
   100 //	if () SENDIPC("rdriven");
   137 			{
   132 			{
   138 				SENDIPC("TN");
   133 				SENDIPC("TN");
   139 				emit SendNet(QByteArray("\x01""C"));
   134 				emit SendNet(QByteArray("\x01""C"));
   140 			}
   135 			}
   141 			else
   136 			else
   142 				SendConfig();
   137 			{
       
   138 				if (gameType == gtLocal)
       
   139 				 	SendConfig();
       
   140 			}
   143 			break;
   141 			break;
   144 		}
   142 		}
   145 		case '+':
   143 		case '+':
   146 		{
   144 		{
   147 			break;
   145 			break;
   148 		}
   146 		}
   149 		default:
   147 		default:
   150 		{
   148 		{
       
   149 			QByteArray tmpbuf = QByteArray::fromRawData((char *)&msgsize, 1) + QByteArray::fromRawData(msgbuf, msgsize);
   151 			if (gameType == gtNet)
   150 			if (gameType == gtNet)
   152 			{
   151 			{
   153 				emit SendNet(QByteArray::fromRawData(msgbuf, msgsize));
   152 				emit SendNet(tmpbuf);
   154 			}
   153 			}
   155 			demo->append(msgsize);
   154 			demo->append(tmpbuf);
   156 			demo->append(QByteArray::fromRawData(msgbuf, msgsize));
       
   157 		}
   155 		}
   158 	}
   156 	}
   159 }
   157 }
   160 
   158 
   161 void HWGame::SendIPC(const char * msg, quint8 len)
   159 void HWGame::SendIPC(const char * msg, quint8 len)
   162 {
   160 {
   163 	IPCSocket->write((char *)&len, 1);
   161 	SendIPC(QByteArray::fromRawData(msg, len));
   164 	IPCSocket->write(msg, len);
       
   165 	if ((len > 5) && !((msg[0] == 'e') && (msg[1] == 'b')))
       
   166 	{
       
   167 		demo->append(len);
       
   168 		demo->append(QByteArray::fromRawData(msg, len));
       
   169 	}
       
   170 }
   162 }
   171 
   163 
   172 void HWGame::SendIPC(const QByteArray & buf)
   164 void HWGame::SendIPC(const QByteArray & buf)
   173 {
   165 {
   174 	if (buf.size() > MAXMSGCHARS) return;
   166 	if (buf.size() > MAXMSGCHARS) return;
   175 	quint8 len = buf.size();
   167 	if (!IPCSocket)
   176 	IPCSocket->write((char *)&len, 1);
   168 	{
   177 	IPCSocket->write(buf);
   169 		toSendBuf += buf;
   178 	demo->append(len);
   170 	} else
   179 	demo->append(buf);
   171 	{
       
   172 		quint8 len = buf.size();
       
   173 		RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
       
   174 	}
   180 }
   175 }
   181 
   176 
   182 void HWGame::RawSendIPC(const QByteArray & buf)
   177 void HWGame::RawSendIPC(const QByteArray & buf)
   183 {
   178 {
   184 	IPCSocket->write(buf);
   179 	if (!IPCSocket)
   185 	demo->append(buf);
   180 	{
       
   181 		toSendBuf += buf;
       
   182 	} else
       
   183 	{
       
   184 		if (toSendBuf.size() > 0)
       
   185 		{
       
   186 			IPCSocket->write(toSendBuf);
       
   187 			demo->append(toSendBuf);
       
   188 		}
       
   189 		IPCSocket->write(buf);
       
   190 		demo->append(buf);
       
   191 	}
   186 }
   192 }
   187 
   193 
   188 void HWGame::FromNet(const QByteArray & msg)
   194 void HWGame::FromNet(const QByteArray & msg)
   189 {
   195 {
   190 	// ?local config?
       
   191 	RawSendIPC(msg);
   196 	RawSendIPC(msg);
   192 }
   197 }
   193 
   198 
   194 void HWGame::ClientRead()
   199 void HWGame::ClientRead()
   195 {
   200 {
   297 		return ;
   302 		return ;
   298 	}
   303 	}
   299 
   304 
   300 	// read demo
   305 	// read demo
   301 	QDataStream stream(&demofile);
   306 	QDataStream stream(&demofile);
   302 	toSendBuf = new QByteArray();
       
   303 	char buf[512];
   307 	char buf[512];
   304 	quint32 readbytes;
   308 	quint32 readbytes;
   305 	do
   309 	do
   306 	{
   310 	{
   307 		readbytes = stream.readRawData((char *)&buf, 512);
   311 		readbytes = stream.readRawData((char *)&buf, 512);
   308 		toSendBuf -> append(QByteArray((char *)&buf, readbytes));
   312 		toSendBuf.append(QByteArray((char *)&buf, readbytes));
   309 
   313 
   310 	} while (readbytes > 0);
   314 	} while (readbytes > 0);
   311 	demofile.close();
   315 	demofile.close();
   312 
   316 
   313 	// cut seed
   317 	// cut seed
   314 	quint32 index = toSendBuf->indexOf(10);
   318 	quint32 index = toSendBuf.indexOf(10);
   315 	if (!index)
   319 	if ((index < 3) || (index > 20))
   316 	{
   320 	{
   317 		QMessageBox::critical(0,
   321 		QMessageBox::critical(0,
   318 				tr("Error"),
   322 				tr("Error"),
   319 				tr("Corrupted demo file %s").arg(demofilename),
   323 				tr("Corrupted demo file %1").arg(demofilename),
   320 				tr("Quit"));
   324 				tr("Quit"));
   321 		return ;
   325 		return ;
   322 	}
   326 	}
   323 	seed = QString(toSendBuf->left(index++));
   327 	seed = QString(toSendBuf.left(index++));
   324 	toSendBuf->remove(0, index);
   328 	toSendBuf.remove(0, index);
   325 
   329 
       
   330 	toSendBuf = QByteArray::fromRawData("\x02TD", 3) + toSendBuf;
   326 	// run engine
   331 	// run engine
   327 	QProcess * process;
       
   328 	QStringList arguments;
       
   329 	process = new QProcess;
       
   330 	arguments << resolutions[0][vid_Resolution];
       
   331 	arguments << resolutions[1][vid_Resolution];
       
   332 	arguments << GetThemeBySeed();
       
   333 	arguments << "46631";
       
   334 	arguments << seed;
       
   335 	arguments << (vid_Fullscreen ? "1" : "0");
       
   336 	process->start("hw", arguments);
       
   337 	demo = new QByteArray;
   332 	demo = new QByteArray;
       
   333 	Start();
   338 }
   334 }
   339 
   335 
   340 void HWGame::StartNet(const QString & netseed)
   336 void HWGame::StartNet(const QString & netseed)
   341 {
   337 {
   342 	gameType = gtNet;
   338 	gameType = gtNet;
   343 	seed = netseed;
   339 	seed = netseed;
   344 	Start();
       
   345 	demo = new QByteArray;
   340 	demo = new QByteArray;
   346 	demo->append(seed.toLocal8Bit());
   341 	demo->append(seed.toLocal8Bit());
   347 	demo->append(10);
   342 	demo->append(10);
       
   343 	Start();
   348 }
   344 }
   349 
   345 
   350 void HWGame::StartLocal()
   346 void HWGame::StartLocal()
   351 {
   347 {
   352 	gameType = gtLocal;
   348 	gameType = gtLocal;
   353 	if (TeamCount < 2) return;
   349 	if (TeamCount < 2) return;
   354 	seedgen.GenRNDStr(seed, 10);
   350 	seedgen.GenRNDStr(seed, 10);
   355 	Start();
       
   356 	demo = new QByteArray;
   351 	demo = new QByteArray;
   357 	demo->append(seed.toLocal8Bit());
   352 	demo->append(seed.toLocal8Bit());
   358 	demo->append(10);
   353 	demo->append(10);
       
   354 	Start();
   359 }
   355 }
   360 
   356 
   361 void HWGame::LocalCFG(const QString & teamname)
   357 void HWGame::LocalCFG(const QString & teamname)
   362 {
   358 {
   363 	QFile teamcfg(cfgdir.absolutePath() + "/" + teamname + ".cfg");
   359 	QFile teamcfg(cfgdir.absolutePath() + "/" + teamname + ".cfg");