]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/qdata_heretic2/common/inout.c
Remove pointless static guards
[xonotic/netradiant.git] / tools / quake2 / qdata_heretic2 / 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 #ifdef WIN32
46 HWND hwndOut = NULL;
47 qboolean lookedForServer = false;
48 UINT wm_BroadcastCommand = -1;
49 #endif
50
51 socket_t *brdcst_socket;
52 netmessage_t msg;
53
54 qboolean verbose = false;
55
56 // our main document
57 // is streamed through the network to Radiant
58 // possibly written to disk at the end of the run
59 //++timo FIXME: need to be global, required when creating nodes?
60 xmlDocPtr doc;
61 xmlNodePtr tree;
62
63 // some useful stuff
64 xmlNodePtr xml_NodeForVec( vec3_t v ){
65         xmlNodePtr ret;
66         char buf[1024];
67
68         sprintf( buf, "%f %f %f", v[0], v[1], v[2] );
69         ret = xmlNewNode( NULL, "point" );
70         xmlNodeSetContent( ret, buf );
71         return ret;
72 }
73
74 // send a node down the stream, add it to the document
75 void xml_SendNode( xmlNodePtr node ){
76         xmlBufferPtr xml_buf;
77         char xmlbuf[MAX_NETMESSAGE]; // we have to copy content from the xmlBufferPtr into an aux buffer .. that sucks ..
78         // this index loops through the node buffer
79         int pos = 0;
80         int size;
81
82         xmlAddChild( doc->children, node );
83
84         if ( brdcst_socket ) {
85                 xml_buf = xmlBufferCreate();
86                 xmlNodeDump( xml_buf, doc, node, 0, 0 );
87
88                 // the XML node might be too big to fit in a single network message
89                 // l_net library defines an upper limit of MAX_NETMESSAGE
90                 // there are some size check errors, so we use MAX_NETMESSAGE-10 to be safe
91                 // if the size of the buffer exceeds MAX_NETMESSAGE-10 we'll send in several network messages
92                 while ( pos < xml_buf->use )
93                 {
94                         // what size are we gonna send now?
95                         ( xml_buf->use - pos < MAX_NETMESSAGE - 10 ) ? ( size = xml_buf->use - pos ) : ( size = MAX_NETMESSAGE - 10 );
96                         //++timo just a debug thing
97                         if ( size == MAX_NETMESSAGE - 10 ) {
98                                 Sys_FPrintf( SYS_NOXML, "Got to split the buffer\n" );
99                         }
100                         memcpy( xmlbuf, xml_buf->content + pos, size );
101                         xmlbuf[size] = '\0';
102                         NMSG_Clear( &msg );
103                         NMSG_WriteString( &msg, xmlbuf );
104                         Net_Send( brdcst_socket, &msg );
105                         // now that the thing is sent prepare to loop again
106                         pos += size;
107                 }
108
109                 xmlBufferFree( xml_buf );
110         }
111 }
112
113 void xml_Select( char *msg, int entitynum, int brushnum, qboolean bError ){
114         xmlNodePtr node, select;
115         char buf[1024];
116         char level[2];
117
118         // now build a proper "select" XML node
119         sprintf( buf, "Entity %i, Brush %i: %s", entitynum, brushnum, msg );
120         node = xmlNewNode( NULL, "select" );
121         xmlNodeSetContent( node, buf );
122         level[0] = (int)'0' + ( bError ? SYS_ERR : SYS_WRN )  ;
123         level[1] = 0;
124         xmlSetProp( node, "level", (char *)&level );
125         // a 'select' information
126         sprintf( buf, "%i %i", entitynum, brushnum );
127         select = xmlNewNode( NULL, "brush" );
128         xmlNodeSetContent( select, buf );
129         xmlAddChild( node, select );
130         xml_SendNode( node );
131
132         sprintf( buf, "Entity %i, Brush %i: %s", entitynum, brushnum, msg );
133         if ( bError ) {
134                 Error( buf );
135         }
136         else{
137                 Sys_FPrintf( SYS_NOXML, "%s\n", buf );
138         }
139
140 }
141
142 void xml_Point( char *msg, vec3_t pt ){
143         xmlNodePtr node, point;
144         char buf[1024];
145         char level[2];
146
147         node = xmlNewNode( NULL, "pointmsg" );
148         xmlNodeSetContent( node, msg );
149         level[0] = (int)'0' + SYS_ERR;
150         level[1] = 0;
151         xmlSetProp( node, "level", (char *)&level );
152         // a 'point' node
153         sprintf( buf, "%g %g %g", pt[0], pt[1], pt[2] );
154         point = xmlNewNode( NULL, "point" );
155         xmlNodeSetContent( point, buf );
156         xmlAddChild( node, point );
157         xml_SendNode( node );
158
159         sprintf( buf, "%s (%g %g %g)", msg, pt[0], pt[1], pt[2] );
160         Error( buf );
161 }
162
163 #define WINDING_BUFSIZE 2048
164 void xml_Winding( char *msg, vec3_t p[], int numpoints, qboolean die ){
165         xmlNodePtr node, winding;
166         char buf[WINDING_BUFSIZE];
167         char smlbuf[128];
168         char level[2];
169         int i;
170
171         node = xmlNewNode( NULL, "windingmsg" );
172         xmlNodeSetContent( node, msg );
173         level[0] = (int)'0' + SYS_ERR;
174         level[1] = 0;
175         xmlSetProp( node, "level", (char *)&level );
176         // a 'winding' node
177         sprintf( buf, "%i ", numpoints );
178         for ( i = 0; i < numpoints; i++ )
179         {
180                 sprintf( smlbuf, "(%g %g %g)", p[i][0], p[i][1], p[i][2] );
181                 // don't overflow
182                 if ( strlen( buf ) + strlen( smlbuf ) > WINDING_BUFSIZE ) {
183                         break;
184                 }
185                 strcat( buf, smlbuf );
186         }
187
188         winding = xmlNewNode( NULL, "winding" );
189         xmlNodeSetContent( winding, buf );
190         xmlAddChild( node, winding );
191         xml_SendNode( node );
192
193         if ( die ) {
194                 Error( msg );
195         }
196         else
197         {
198                 Sys_Printf( msg );
199                 Sys_Printf( "\n" );
200         }
201 }
202
203 // in include
204 #include "stream_version.h"
205
206 void Broadcast_Setup( const char *dest ){
207         address_t address;
208         char sMsg[1024];
209
210         Net_Setup();
211         Net_StringToAddress( (char *)dest, &address );
212         brdcst_socket = Net_Connect( &address, 0 );
213         if ( brdcst_socket ) {
214                 // send in a header
215                 sprintf( sMsg, "<?xml version=\"1.0\"?><q3map_feedback version=\"" Q3MAP_STREAM_VERSION "\">" );
216                 NMSG_Clear( &msg );
217                 NMSG_WriteString( &msg, sMsg );
218                 Net_Send( brdcst_socket, &msg );
219         }
220 }
221
222 void Broadcast_Shutdown(){
223         if ( brdcst_socket ) {
224                 Sys_Printf( "Disconnecting\n" );
225                 Net_Disconnect( brdcst_socket );
226                 brdcst_socket = NULL;
227         }
228 }
229
230 // all output ends up through here
231 void FPrintf( int flag, char *buf ){
232         xmlNodePtr node;
233         static qboolean bGotXML = false;
234         char level[2];
235
236         printf( buf );
237
238         // the following part is XML stuff only.. but maybe we don't want that message to go down the XML pipe?
239         if ( flag == SYS_NOXML ) {
240                 return;
241         }
242
243         // ouput an XML file of the run
244         // use the DOM interface to build a tree
245         /*
246            <message level='flag'>
247            message string
248            .. various nodes to describe corresponding geometry ..
249            </message>
250          */
251         if ( !bGotXML ) {
252                 // initialize
253                 doc = xmlNewDoc( "1.0" );
254                 doc->children = xmlNewDocRawNode( doc, NULL, "q3map_feedback", NULL );
255                 bGotXML = true;
256         }
257         node = xmlNewNode( NULL, "message" );
258         xmlNodeSetContent( node, buf );
259         level[0] = (int)'0' + flag;
260         level[1] = 0;
261         xmlSetProp( node, "level", (char *)&level );
262
263         xml_SendNode( node );
264 }
265
266 #ifdef DBG_XML
267 void DumpXML(){
268         xmlSaveFile( "XMLDump.xml", doc );
269 }
270 #endif
271
272 void Sys_FPrintf( int flag, const char *format, ... ){
273         char out_buffer[4096];
274         va_list argptr;
275
276         if ( ( flag == SYS_VRB ) && ( verbose == false ) ) {
277                 return;
278         }
279
280         va_start( argptr, format );
281         vsprintf( out_buffer, format, argptr );
282         va_end( argptr );
283
284         FPrintf( flag, out_buffer );
285 }
286
287 void Sys_Printf( const char *format, ... ){
288         char out_buffer[4096];
289         va_list argptr;
290
291         va_start( argptr, format );
292         vsprintf( out_buffer, format, argptr );
293         va_end( argptr );
294
295         FPrintf( SYS_STD, out_buffer );
296 }
297
298 /*
299    =================
300    Error
301
302    For abnormal program terminations
303    =================
304  */
305 void Error( const char *error, ... ){
306         char out_buffer[4096];
307         char tmp[4096];
308         va_list argptr;
309
310         va_start( argptr,error );
311         vsprintf( tmp, error, argptr );
312         va_end( argptr );
313
314         sprintf( out_buffer, "************ ERROR ************\n%s\n", tmp );
315
316         FPrintf( SYS_ERR, out_buffer );
317
318 #ifdef DBG_XML
319         DumpXML();
320 #endif
321
322         //++timo HACK ALERT .. if we shut down too fast the xml stream won't reach the listener.
323         // a clean solution is to send a sync request node in the stream and wait for an answer before exiting
324         Sys_Sleep( 1000 );
325
326         Broadcast_Shutdown();
327
328         exit( 1 );
329 }