2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 //-----------------------------------------------------------------------------
26 // deal with in/out tasks, for either stdin/stdout or network/XML stream
33 #include <sys/types.h>
41 // network broadcasting
42 #include "l_net/l_net.h"
43 #include "libxml/tree.h"
46 #include <glib/gconvert.h>
47 #include <glib/gmem.h>
51 qboolean lookedForServer = qfalse;
52 UINT wm_BroadcastCommand = -1;
55 socket_t *brdcst_socket;
58 qboolean verbose = qfalse;
61 // is streamed through the network to Radiant
62 // possibly written to disk at the end of the run
63 //++timo FIXME: need to be global, required when creating nodes?
68 xmlNodePtr xml_NodeForVec( vec3_t v ){
72 sprintf( buf, "%f %f %f", v[0], v[1], v[2] );
73 ret = xmlNewNode( NULL, "point" );
74 xmlNodeSetContent( ret, buf );
78 // send a node down the stream, add it to the document
79 void xml_SendNode( xmlNodePtr node ){
81 char xmlbuf[MAX_NETMESSAGE]; // we have to copy content from the xmlBufferPtr into an aux buffer .. that sucks ..
82 // this index loops through the node buffer
86 xmlAddChild( doc->children, node );
88 if ( brdcst_socket ) {
89 xml_buf = xmlBufferCreate();
90 xmlNodeDump( xml_buf, doc, node, 0, 0 );
92 // the XML node might be too big to fit in a single network message
93 // l_net library defines an upper limit of MAX_NETMESSAGE
94 // there are some size check errors, so we use MAX_NETMESSAGE-10 to be safe
95 // if the size of the buffer exceeds MAX_NETMESSAGE-10 we'll send in several network messages
96 while ( pos < xml_buf->use )
98 // what size are we gonna send now?
99 ( xml_buf->use - pos < MAX_NETMESSAGE - 10 ) ? ( size = xml_buf->use - pos ) : ( size = MAX_NETMESSAGE - 10 );
100 //++timo just a debug thing
101 if ( size == MAX_NETMESSAGE - 10 ) {
102 Sys_FPrintf( SYS_NOXML, "Got to split the buffer\n" );
104 memcpy( xmlbuf, xml_buf->content + pos, size );
107 NMSG_WriteString( &msg, xmlbuf );
108 Net_Send( brdcst_socket, &msg );
109 // now that the thing is sent prepare to loop again
114 // NOTE: the NMSG_WriteString is limited to MAX_NETMESSAGE
115 // we will need to split into chunks
116 // (we could also go lower level, in the end it's using send and receiv which are not size limited)
117 //++timo FIXME: MAX_NETMESSAGE is not exactly the max size we can stick in the message
118 // there's some tweaking to do in l_net for that .. so let's give us a margin for now
120 //++timo we need to handle the case of a buffer too big to fit in a single message
121 // try without checks for now
122 if ( xml_buf->use > MAX_NETMESSAGE - 10 ) {
123 // if we send that we are probably gonna break the stream at the other end..
124 // and Error will call right there
125 //Error( "MAX_NETMESSAGE exceeded for XML feedback stream in FPrintf (%d)\n", xml_buf->use);
126 Sys_FPrintf( SYS_NOXML, "MAX_NETMESSAGE exceeded for XML feedback stream in FPrintf (%d)\n", xml_buf->use );
127 xml_buf->content[xml_buf->use] = '\0'; //++timo this corrupts the buffer but we don't care it's for printing
128 Sys_FPrintf( SYS_NOXML, xml_buf->content );
133 memcpy( xmlbuf, xml_buf->content, size );
136 NMSG_WriteString( &msg, xmlbuf );
137 Net_Send( brdcst_socket, &msg );
140 xmlBufferFree( xml_buf );
144 void xml_Select( char *msg, int entitynum, int brushnum, qboolean bError ){
145 xmlNodePtr node, select;
149 // now build a proper "select" XML node
150 sprintf( buf, "Entity %i, Brush %i: %s", entitynum, brushnum, msg );
151 node = xmlNewNode( NULL, "select" );
152 xmlNodeSetContent( node, buf );
153 level[0] = (int)'0' + ( bError ? SYS_ERR : SYS_WRN ) ;
155 xmlSetProp( node, "level", (char *)&level );
156 // a 'select' information
157 sprintf( buf, "%i %i", entitynum, brushnum );
158 select = xmlNewNode( NULL, "brush" );
159 xmlNodeSetContent( select, buf );
160 xmlAddChild( node, select );
161 xml_SendNode( node );
163 sprintf( buf, "Entity %i, Brush %i: %s", entitynum, brushnum, msg );
168 Sys_FPrintf( SYS_NOXML, "%s\n", buf );
173 void xml_Point( char *msg, vec3_t pt ){
174 xmlNodePtr node, point;
178 node = xmlNewNode( NULL, "pointmsg" );
179 xmlNodeSetContent( node, msg );
180 level[0] = (int)'0' + SYS_ERR;
182 xmlSetProp( node, "level", (char *)&level );
184 sprintf( buf, "%g %g %g", pt[0], pt[1], pt[2] );
185 point = xmlNewNode( NULL, "point" );
186 xmlNodeSetContent( point, buf );
187 xmlAddChild( node, point );
188 xml_SendNode( node );
190 sprintf( buf, "%s (%g %g %g)", msg, pt[0], pt[1], pt[2] );
194 #define WINDING_BUFSIZE 2048
195 void xml_Winding( char *msg, vec3_t p[], int numpoints, qboolean die ){
196 xmlNodePtr node, winding;
197 char buf[WINDING_BUFSIZE];
202 node = xmlNewNode( NULL, "windingmsg" );
203 xmlNodeSetContent( node, msg );
204 level[0] = (int)'0' + SYS_ERR;
206 xmlSetProp( node, "level", (char *)&level );
208 sprintf( buf, "%i ", numpoints );
209 for ( i = 0; i < numpoints; i++ )
211 sprintf( smlbuf, "(%g %g %g)", p[i][0], p[i][1], p[i][2] );
213 if ( strlen( buf ) + strlen( smlbuf ) > WINDING_BUFSIZE ) {
216 strcat( buf, smlbuf );
219 winding = xmlNewNode( NULL, "winding" );
220 xmlNodeSetContent( winding, buf );
221 xmlAddChild( node, winding );
222 xml_SendNode( node );
235 #include "stream_version.h"
237 void Broadcast_Setup( const char *dest ){
242 Net_StringToAddress( (char *)dest, &address );
243 brdcst_socket = Net_Connect( &address, 0 );
244 if ( brdcst_socket ) {
246 sprintf( sMsg, "<?xml version=\"1.0\"?><q3map_feedback version=\"" Q3MAP_STREAM_VERSION "\">" );
248 NMSG_WriteString( &msg, sMsg );
249 Net_Send( brdcst_socket, &msg );
253 void Broadcast_Shutdown(){
254 if ( brdcst_socket ) {
255 Sys_Printf( "Disconnecting\n" );
256 Net_Disconnect( brdcst_socket );
257 brdcst_socket = NULL;
261 // all output ends up through here
262 void FPrintf( int flag, char *buf ){
264 static qboolean bGotXML = qfalse;
269 // the following part is XML stuff only.. but maybe we don't want that message to go down the XML pipe?
270 if ( flag == SYS_NOXML ) {
274 // ouput an XML file of the run
275 // use the DOM interface to build a tree
277 <message level='flag'>
279 .. various nodes to describe corresponding geometry ..
284 doc = xmlNewDoc( "1.0" );
285 doc->children = xmlNewDocRawNode( doc, NULL, "q3map_feedback", NULL );
288 node = xmlNewNode( NULL, "message" );
290 gchar* utf8 = g_locale_to_utf8( buf, -1, NULL, NULL, NULL );
291 xmlNodeSetContent( node, utf8 );
294 level[0] = (int)'0' + flag;
296 xmlSetProp( node, "level", (char *)&level );
298 xml_SendNode( node );
303 xmlSaveFile( "XMLDump.xml", doc );
307 void Sys_FPrintf( int flag, const char *format, ... ){
308 char out_buffer[4096];
311 if ( ( flag == SYS_VRB ) && ( verbose == qfalse ) ) {
315 va_start( argptr, format );
316 vsprintf( out_buffer, format, argptr );
319 FPrintf( flag, out_buffer );
322 void Sys_Printf( const char *format, ... ){
323 char out_buffer[4096];
326 va_start( argptr, format );
327 vsprintf( out_buffer, format, argptr );
330 FPrintf( SYS_STD, out_buffer );
337 For abnormal program terminations
340 void Error( const char *error, ... ){
341 char out_buffer[4096];
345 va_start( argptr,error );
346 vsprintf( tmp, error, argptr );
349 sprintf( out_buffer, "************ ERROR ************\n%s\n", tmp );
351 FPrintf( SYS_ERR, out_buffer );
357 //++timo HACK ALERT .. if we shut down too fast the xml stream won't reach the listener.
358 // a clean solution is to send a sync request node in the stream and wait for an answer before exiting
361 Broadcast_Shutdown();