]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/common/inout.c
Merge remote-tracking branch 'github/master'
[xonotic/netradiant.git] / tools / quake3 / common / inout.c
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 //-----------------------------------------------------------------------------
23 //
24 //
25 // DESCRIPTION:
26 // deal with in/out tasks, for either stdin/stdout or network/XML stream
27 //
28
29 #include "cmdlib.h"
30 #include "mathlib.h"
31 #include "polylib.h"
32 #include "inout.h"
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #ifdef WIN32
37 #include <direct.h>
38 #include <windows.h>
39 #endif
40
41 // network broadcasting
42 #include "l_net/l_net.h"
43 #include "libxml/tree.h"
44
45 // utf8 conversion
46 #include <glib.h>
47 #include <glib/gconvert.h>
48 #include <glib/gmem.h>
49
50 #ifdef WIN32
51 HWND hwndOut = NULL;
52 qboolean lookedForServer = qfalse;
53 UINT wm_BroadcastCommand = -1;
54 #endif
55
56 socket_t *brdcst_socket;
57 netmessage_t msg;
58
59 qboolean verbose = qfalse;
60
61 // our main document
62 // is streamed through the network to Radiant
63 // possibly written to disk at the end of the run
64 //++timo FIXME: need to be global, required when creating nodes?
65 xmlDocPtr doc;
66 xmlNodePtr tree;
67
68 // some useful stuff
69 xmlNodePtr xml_NodeForVec( vec3_t v ){
70         xmlNodePtr ret;
71         char buf[1024];
72
73         sprintf( buf, "%f %f %f", v[0], v[1], v[2] );
74         ret = xmlNewNode( NULL, (xmlChar*)"point" );
75         xmlNodeSetContent( ret, (xmlChar*)buf );
76         return ret;
77 }
78
79 // send a node down the stream, add it to the document
80 void xml_SendNode( xmlNodePtr node ){
81         xmlBufferPtr xml_buf;
82         char xmlbuf[MAX_NETMESSAGE]; // we have to copy content from the xmlBufferPtr into an aux buffer .. that sucks ..
83         // this index loops through the node buffer
84         int pos = 0;
85         int size;
86
87         xmlAddChild( doc->children, node );
88
89         if ( brdcst_socket ) {
90                 xml_buf = xmlBufferCreate();
91                 xmlNodeDump( xml_buf, doc, node, 0, 0 );
92
93                 // the XML node might be too big to fit in a single network message
94                 // l_net library defines an upper limit of MAX_NETMESSAGE
95                 // there are some size check errors, so we use MAX_NETMESSAGE-10 to be safe
96                 // if the size of the buffer exceeds MAX_NETMESSAGE-10 we'll send in several network messages
97                 while ( pos < (int)xml_buf->use )
98                 {
99                         // what size are we gonna send now?
100                         ( xml_buf->use - pos < MAX_NETMESSAGE - 10 ) ? ( size = xml_buf->use - pos ) : ( size = MAX_NETMESSAGE - 10 );
101                         //++timo just a debug thing
102                         if ( size == MAX_NETMESSAGE - 10 ) {
103                                 Sys_FPrintf( SYS_NOXML, "Got to split the buffer\n" );
104                         }
105                         memcpy( xmlbuf, xml_buf->content + pos, size );
106                         xmlbuf[size] = '\0';
107                         NMSG_Clear( &msg );
108                         NMSG_WriteString( &msg, xmlbuf );
109                         Net_Send( brdcst_socket, &msg );
110                         // now that the thing is sent prepare to loop again
111                         pos += size;
112                 }
113
114 #if 0
115                 // NOTE: the NMSG_WriteString is limited to MAX_NETMESSAGE
116                 // we will need to split into chunks
117                 // (we could also go lower level, in the end it's using send and receiv which are not size limited)
118                 //++timo FIXME: MAX_NETMESSAGE is not exactly the max size we can stick in the message
119                 //  there's some tweaking to do in l_net for that .. so let's give us a margin for now
120
121                 //++timo we need to handle the case of a buffer too big to fit in a single message
122                 // try without checks for now
123                 if ( xml_buf->use > MAX_NETMESSAGE - 10 ) {
124                         // if we send that we are probably gonna break the stream at the other end..
125                         // and Error will call right there
126                         //Error( "MAX_NETMESSAGE exceeded for XML feedback stream in FPrintf (%d)\n", xml_buf->use);
127                         Sys_FPrintf( SYS_NOXML, "MAX_NETMESSAGE exceeded for XML feedback stream in FPrintf (%d)\n", xml_buf->use );
128                         xml_buf->content[xml_buf->use] = '\0'; //++timo this corrupts the buffer but we don't care it's for printing
129                         Sys_FPrintf( SYS_NOXML, xml_buf->content );
130
131                 }
132
133                 size = xml_buf->use;
134                 memcpy( xmlbuf, xml_buf->content, size );
135                 xmlbuf[size] = '\0';
136                 NMSG_Clear( &msg );
137                 NMSG_WriteString( &msg, xmlbuf );
138                 Net_Send( brdcst_socket, &msg );
139 #endif
140
141                 xmlBufferFree( xml_buf );
142         }
143 }
144
145 void xml_Select( char *msg, int entitynum, int brushnum, qboolean bError ){
146         xmlNodePtr node, select;
147         char buf[1024];
148         char level[2];
149
150         // now build a proper "select" XML node
151         sprintf( buf, "Entity %i, Brush %i: %s", entitynum, brushnum, msg );
152         node = xmlNewNode( NULL, (xmlChar*)"select" );
153         xmlNodeSetContent( node, (xmlChar*)buf );
154         level[0] = (int)'0' + ( bError ? SYS_ERR : SYS_WRN )  ;
155         level[1] = 0;
156         xmlSetProp( node, (xmlChar*)"level", (xmlChar *)&level );
157         // a 'select' information
158         sprintf( buf, "%i %i", entitynum, brushnum );
159         select = xmlNewNode( NULL, (xmlChar*)"brush" );
160         xmlNodeSetContent( select, (xmlChar*)buf );
161         xmlAddChild( node, select );
162         xml_SendNode( node );
163
164         sprintf( buf, "Entity %i, Brush %i: %s", entitynum, brushnum, msg );
165         if ( bError ) {
166                 Error( buf );
167         }
168         else{
169                 Sys_FPrintf( SYS_NOXML, "%s\n", buf );
170         }
171
172 }
173
174 void xml_Point( char *msg, vec3_t pt ){
175         xmlNodePtr node, point;
176         char buf[1024];
177         char level[2];
178
179         node = xmlNewNode( NULL, (xmlChar*)"pointmsg" );
180         xmlNodeSetContent( node, (xmlChar*)msg );
181         level[0] = (int)'0' + SYS_ERR;
182         level[1] = 0;
183         xmlSetProp( node, (xmlChar*)"level", (xmlChar *)&level );
184         // a 'point' node
185         sprintf( buf, "%g %g %g", pt[0], pt[1], pt[2] );
186         point = xmlNewNode( NULL, (xmlChar*)"point" );
187         xmlNodeSetContent( point, (xmlChar*)buf );
188         xmlAddChild( node, point );
189         xml_SendNode( node );
190
191         sprintf( buf, "%s (%g %g %g)", msg, pt[0], pt[1], pt[2] );
192         Error( buf );
193 }
194
195 #define WINDING_BUFSIZE 2048
196 void xml_Winding( char *msg, vec3_t p[], int numpoints, qboolean die ){
197         xmlNodePtr node, winding;
198         char buf[WINDING_BUFSIZE];
199         char smlbuf[128];
200         char level[2];
201         int i;
202
203         node = xmlNewNode( NULL, (xmlChar*)"windingmsg" );
204         xmlNodeSetContent( node, (xmlChar*)msg );
205         level[0] = (int)'0' + SYS_ERR;
206         level[1] = 0;
207         xmlSetProp( node, (xmlChar*)"level", (xmlChar *)&level );
208         // a 'winding' node
209         sprintf( buf, "%i ", numpoints );
210         for ( i = 0; i < numpoints; i++ )
211         {
212                 sprintf( smlbuf, "(%g %g %g)", p[i][0], p[i][1], p[i][2] );
213                 // don't overflow
214                 if ( strlen( buf ) + strlen( smlbuf ) > WINDING_BUFSIZE ) {
215                         break;
216                 }
217                 strcat( buf, smlbuf );
218         }
219
220         winding = xmlNewNode( NULL, (xmlChar*)"winding" );
221         xmlNodeSetContent( winding, (xmlChar*)buf );
222         xmlAddChild( node, winding );
223         xml_SendNode( node );
224
225         if ( die ) {
226                 Error( msg );
227         }
228         else
229         {
230                 Sys_Printf( msg );
231                 Sys_Printf( "\n" );
232         }
233 }
234
235 // in include
236 #include "stream_version.h"
237
238 void Broadcast_Setup( const char *dest ){
239         address_t address;
240         char sMsg[1024];
241
242         Net_Setup();
243         Net_StringToAddress( dest, &address );
244         brdcst_socket = Net_Connect( &address, 0 );
245         if ( brdcst_socket ) {
246                 // send in a header
247                 sprintf( sMsg, "<?xml version=\"1.0\"?><q3map_feedback version=\"" Q3MAP_STREAM_VERSION "\">" );
248                 NMSG_Clear( &msg );
249                 NMSG_WriteString( &msg, sMsg );
250                 Net_Send( brdcst_socket, &msg );
251         }
252 }
253
254 void Broadcast_Shutdown(){
255         if ( brdcst_socket ) {
256                 Sys_Printf( "Disconnecting\n" );
257                 Net_Disconnect( brdcst_socket );
258                 brdcst_socket = NULL;
259         }
260 }
261
262 // all output ends up through here
263 void FPrintf( int flag, char *buf ){
264         xmlNodePtr node;
265         static qboolean bGotXML = qfalse;
266         char level[2];
267
268         printf( "%s", buf );
269
270         // the following part is XML stuff only.. but maybe we don't want that message to go down the XML pipe?
271         if ( flag == SYS_NOXML ) {
272                 return;
273         }
274
275         // ouput an XML file of the run
276         // use the DOM interface to build a tree
277         /*
278            <message level='flag'>
279            message string
280            .. various nodes to describe corresponding geometry ..
281            </message>
282          */
283         if ( !bGotXML ) {
284                 // initialize
285                 doc = xmlNewDoc( (xmlChar*)"1.0" );
286                 doc->children = xmlNewDocRawNode( doc, NULL, (xmlChar*)"q3map_feedback", NULL );
287                 bGotXML = qtrue;
288         }
289         node = xmlNewNode( NULL, (xmlChar*)"message" );
290         {
291                 gchar* utf8 = g_locale_to_utf8( buf, -1, NULL, NULL, NULL );
292                 xmlNodeSetContent( node, (xmlChar*)utf8 );
293                 g_free( utf8 );
294         }
295         level[0] = (int)'0' + flag;
296         level[1] = 0;
297         xmlSetProp( node, (xmlChar*)"level", (xmlChar *)&level );
298
299         xml_SendNode( node );
300 }
301
302 #ifdef DBG_XML
303 void DumpXML(){
304         xmlSaveFile( "XMLDump.xml", doc );
305 }
306 #endif
307
308 void Sys_FPrintf( int flag, const char *format, ... ){
309         char out_buffer[4096];
310         va_list argptr;
311
312         if ( ( flag == SYS_VRB ) && ( verbose == qfalse ) ) {
313                 return;
314         }
315
316         va_start( argptr, format );
317         vsprintf( out_buffer, format, argptr );
318         va_end( argptr );
319
320         FPrintf( flag, out_buffer );
321 }
322
323 void Sys_Printf( const char *format, ... ){
324         char out_buffer[4096];
325         va_list argptr;
326
327         va_start( argptr, format );
328         vsprintf( out_buffer, format, argptr );
329         va_end( argptr );
330
331         FPrintf( SYS_STD, out_buffer );
332 }
333
334 /*
335    =================
336    Error
337
338    For abnormal program terminations
339    =================
340  */
341 void Error( const char *error, ... ){
342         char out_buffer[4096];
343         char tmp[4096];
344         va_list argptr;
345
346         va_start( argptr,error );
347         vsprintf( tmp, error, argptr );
348         va_end( argptr );
349
350         sprintf( out_buffer, "************ ERROR ************\n%s\n", tmp );
351
352         FPrintf( SYS_ERR, out_buffer );
353
354 #ifdef DBG_XML
355         DumpXML();
356 #endif
357
358         //++timo HACK ALERT .. if we shut down too fast the xml stream won't reach the listener.
359         // a clean solution is to send a sync request node in the stream and wait for an answer before exiting
360         Sys_Sleep( 1000 );
361
362         Broadcast_Shutdown();
363
364         exit( 1 );
365 }