EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PyDumpVisitor.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #include "eve-common.h"
27 
29 #include "python/PyVisitor.h"
30 #include "python/PyRep.h"
31 #include "python/PyDumpVisitor.h"
32 #include "utils/EVEUtils.h"
33 
34 /************************************************************************/
35 /* PyDumpVisitor */
36 /************************************************************************/
37 PyDumpVisitor::PyDumpVisitor( const char* pfx, bool full_nested ) : PyPfxVisitor( pfx ), mFullNested( full_nested ) {}
38 
40 {
41  _print( "%s Integer: %i", _pfx(), rep->value() );
42  return true;
43 }
44 
45 bool PyDumpVisitor::VisitLong( const PyLong* rep )
46 {
47  _print( "%s Long: %" PRIi64, _pfx(), rep->value() );
48  return true;
49 }
50 
52 {
53  _print( "%s Real: %f", _pfx(), rep->value() );
54  return true;
55 }
56 
58 {
59  _print( "%s Boolean: %s", _pfx(), rep->value() ? "true" : "false" );
60  return true;
61 }
62 
63 bool PyDumpVisitor::VisitNone( const PyNone* rep )
64 {
65  _print( "%s None", _pfx() );
66  return true;
67 }
68 
70 {
71  _print( "%s Buffer (%lu):", _pfx(), rep->content().size() );
72 
73  _pfxExtend( " " );
74  _dump( _pfx(), &rep->content()[ 0 ], rep->content().size() );
75  _pfxWithdraw();
76 
77  return true;
78 }
79 
81 {
82  if (IsPrintable( rep ) )
83  _print( "%s String: '%s'", _pfx(), rep->content().c_str() );
84  else
85  _print( "%sBinary String: (len=%lu)", _pfx(), rep->content().length() );
86 
87  return true;
88 }
89 
91 {
92  // how to do it correctly?
93  if (IsPrintable( rep ) )
94  _print( "%s WString: '%s'", _pfx(), rep->content().c_str() );
95  else
96  _print( "%ssBinary WString: (len=%lu)'", _pfx(), rep->content().length() );
97 
98  return true;
99 }
100 
102 {
103  _print( "%s Token: '%s'", _pfx(), rep->content().c_str() );
104 
105  return true;
106 }
107 
109 {
110  bool res(true);
111  if (rep->empty())
112  _print( "%s Tuple: Empty", _pfx() );
113  else {
114  _print( "%s Tuple: %lu elements", _pfx(), rep->size() );
115 
116  PyTuple::const_iterator cur = rep->begin(), end = rep->end();
117  for ( uint32 i = 0; cur != end; ++cur, ++i ) {
118  if (*cur == nullptr)
119  continue;
120  if (i > 100 && !fullNested()) {
121  _print( "%s ... truncated ...", _pfx() );
122  break;
123  }
124 
125  _pfxExtend( " [%2u] ", i );
126  res = (*cur)->visit( *this );
127  _pfxWithdraw();
128  }
129  }
130  return res;
131 }
132 
134 {
135  bool res(true);
136  if (rep->empty())
137  _print( "%s List: Empty", _pfx() );
138  else {
139  _print( "%s List: %lu elements", _pfx(), rep->size() );
140 
141  PyList::const_iterator cur = rep->begin(), end = rep->end();
142  for ( uint32 i = 0; cur != end; ++cur, ++i ) {
143  if (*cur == nullptr)
144  continue;
145  if (i > 100 && !fullNested()) {
146  _print( "%s ... truncated ...", _pfx() );
147  break;
148  }
149 
150  _pfxExtend( " [%2u] ", i );
151  res = (*cur)->visit( *this );
152  _pfxWithdraw();
153  }
154  }
155  return res;
156 }
157 
159 {
160  if (rep->empty())
161  _print( "%s Dictionary: Empty", _pfx() );
162  else {
163  _print( "%s Dictionary: %lu entries", _pfx(), rep->size() );
164 
165  PyDict::const_iterator cur = rep->begin(), end = rep->end();
166  for ( uint32 i = 0; cur != end; ++cur, ++i ) {
167  if (i > 100 && !fullNested() ) {
168  _print( "%s ... truncated ...", _pfx() );
169  break;
170  }
171 
172  _pfxExtend( " [%2u] Key: ", i );
173  bool res = cur->first->visit( *this );
174  _pfxWithdraw();
175 
176  if (!res )
177  return false;
178 
179  _pfxExtend( " [%2u] Value: ", i );
180  res = cur->second->visit( *this );
181  _pfxWithdraw();
182 
183  if (!res )
184  return false;
185  }
186  }
187  return true;
188 }
189 
191 {
192  _print( "%sObject:", _pfx() );
193 
194  _pfxExtend( " Type: " );
195  bool res = rep->type()->visit( *this );
196  _pfxWithdraw();
197 
198  if (!res )
199  return false;
200 
201  _pfxExtend( " Args: " );
202  res = rep->arguments()->visit( *this );
203  _pfxWithdraw();
204 
205  return res;
206 }
207 
209 {
210  _print( "%s ObjectEx%s:", _pfx(), rep->isType2() ? "2" : "1" );
211 
212  _print( "%s Header:", _pfx() );
213  if (rep->header() == nullptr ) {
214  _print( "%s (None)", _pfx() );
215  } else {
216  _pfxExtend( " " );
217  bool res = rep->header()->visit( *this );
218  _pfxWithdraw();
219 
220  if (!res )
221  return false;
222  }
223 
224  _print( "%s List:", _pfx() );
225  if (rep->list().empty()) {
226  _print( "%s Empty", _pfx() );
227  } else {
228  PyObjectEx::const_list_iterator cur = rep->list().begin(), end = rep->list().end();
229  for( uint32 i = 0; cur != end; ++cur, ++i ) {
230  if (*cur == nullptr)
231  continue;
232  if (i > 100 && !fullNested() ) {
233  _print( "%s ... truncated ...", _pfx() );
234  break;
235  }
236 
237  _pfxExtend( " [%2u] ", i );
238  bool res = (*cur)->visit( *this );
239  _pfxWithdraw();
240 
241  if (!res )
242  return false;
243  }
244  }
245 
246  _print( "%s Dict:", _pfx() );
247  if (rep->dict().empty()) {
248  _print( "%s Empty", _pfx() );
249  } else {
250  PyObjectEx::const_dict_iterator cur = rep->dict().begin(), end = rep->dict().end();
251  for( uint32 i = 0; cur != end; ++cur, ++i ) {
252  if (i > 100 && !fullNested() ) {
253  _print( "%s ... truncated ...", _pfx() );
254  break;
255  }
256 
257  _pfxExtend( " [%2u] Key: ", i );
258  bool res = cur->first->visit( *this );
259  _pfxWithdraw();
260 
261  if (!res)
262  return false;
263 
264  _pfxExtend( " [%2u] Value: ", i );
265  res = cur->second->visit( *this );
266  _pfxWithdraw();
267 
268  if (!res)
269  return false;
270  }
271  }
272 
273  return true;
274 }
275 
277 {
278  _print( "%sPacked Row:", _pfx() );
279  _print( "%scolumn_count=%u", _pfx(), rep->header()->ColumnCount() );
280 
281  PyPackedRow::const_iterator cur = rep->begin(), end = rep->end();
282  for (uint32 i = 0; cur != end; ++cur, ++i) {
283  _pfxExtend( " [%2u] %s: ", i, rep->header()->GetColumnName( i )->content().c_str() );
284 
285  bool res(true);
286  if ((*cur) == nullptr )
287  _print( "%s (None)", _pfx() );
288  else
289  res = (*cur)->visit( *this );
290 
291  _pfxWithdraw();
292 
293  if (!res)
294  return false;
295  }
296 
297  return true;
298 }
299 
301 {
302  _print( "%s Substruct:", _pfx() );
303 
304  _pfxExtend( " " );
305  bool res = PyVisitor::VisitSubStruct( rep );
306  _pfxWithdraw();
307 
308  return res;
309 }
310 
312 {
313  _print( "%s Substream: %s", _pfx(), ( rep->decoded() == nullptr ) ? "from data" : "from rep" );
314 
315  _pfxExtend( " " );
316  bool res = PyVisitor::VisitSubStream( rep );
317  _pfxWithdraw();
318 
319  return res;
320 }
321 
323 {
324  _print( "%sChecksumStream: 0x%08x", _pfx(), rep->checksum() );
325 
326  _pfxExtend( " " );
327  bool res = PyVisitor::VisitChecksumedStream( rep );
328  _pfxWithdraw();
329 
330  return res;
331 }
332 
333 PyLogDumpVisitor::PyLogDumpVisitor( LogType log_type, LogType log_hex_type, const char* pfx, bool full_nested, bool full_hex )
334 : PyDumpVisitor( pfx, full_nested ),
335  mFullHex( full_hex ),
336  mLogType( log_type ),
337  mLogHexType( log_hex_type )
338 {
339 }
340 
341 void PyLogDumpVisitor::_print( const char* fmt, ... )
342 {
343  if (!is_log_enabled( logType() ) )
344  return;
345 
346  va_list ap;
347  va_start( ap, fmt );
348 
349  log_messageVA( logType(), fmt, ap );
350 
351  va_end( ap );
352 }
353 
354 void PyLogDumpVisitor::_dump( const char* pfx, const uint8* data, size_t len )
355 {
356  if (!is_log_enabled( logHexType() ) )
357  return;
358 
359  if (fullHex() )
360  pfxHexDump( pfx, logHexType(), data, len );
361  else
362  pfxHexDumpPreview( pfx, logHexType(), data, len );
363 }
364 
365 PyFileDumpVisitor::PyFileDumpVisitor( FILE* _file, const char* pfx, bool full_nested/*false*/, bool full_hex/*false*/ )
366 : PyDumpVisitor( pfx, full_nested ),
367  mFullHex( full_hex ),
368  mFile( _file )
369 {
370 }
371 
372 void PyFileDumpVisitor::_print( const char* fmt, ... )
373 {
374  va_list ap;
375  va_start( ap, fmt );
376 
377  vfprintf( file(), fmt, ap );
378  fprintf( file(), "\n" );
379 
380  va_end( ap );
381 }
382 
383 void PyFileDumpVisitor::_dump( const char* pfx, const uint8* data, size_t len )
384 {
385  if (fullHex())
386  pfxHexDump( pfx, file(), data, len );
387  else
388  pfxHexDumpPreview( pfx, file(), data, len );
389 }
list_type & list()
Definition: PyRep.h:889
list_type::const_iterator const_list_iterator
Definition: PyRep.h:866
unsigned __int8 uint8
Definition: eve-compat.h:46
PyLogDumpVisitor(LogType log_type, LogType log_hex_type, const char *pfx="", bool full_nested=false, bool full_hex=false)
double value() const
Definition: PyRep.h:309
bool VisitLong(const PyLong *rep)
bool empty() const
Definition: PyRep.h:770
bool VisitString(const PyString *rep)
void _pfxExtend(const char *fmt,...)
Definition: PyVisitor.cpp:148
Python string.
Definition: PyRep.h:430
int32 value() const
Definition: PyRep.h:247
virtual bool VisitSubStream(const PySubStream *rep)
Definition: PyVisitor.cpp:119
Python's dictionary.
Definition: PyRep.h:719
const Buffer & content() const
Get the const PyBuffer content.
Definition: PyRep.h:407
size_t size() const
Definition: PyRep.h:591
DBRowDescriptor * header() const
Definition: PyRep.h:983
const std::string & content() const
Get the PyWString content.
Definition: PyRep.h:499
bool fullHex() const
Definition: PyDumpVisitor.h:99
bool fullHex() const
Definition: PyDumpVisitor.h:78
bool empty() const
Definition: PyRep.h:592
bool VisitTuple(const PyTuple *rep)
the nested types Visitor
storage_type::const_iterator const_iterator
Definition: PyRep.h:572
void _dump(const char *pfx, const uint8 *data, size_t len)
Python floating point number.
Definition: PyRep.h:292
Python wide string.
Definition: PyRep.h:475
bool VisitWString(const PyWString *rep)
bool value() const
Definition: PyRep.h:340
const_iterator begin() const
Definition: PyRep.h:660
storage_type::const_iterator const_iterator
Definition: PyRep.h:644
void _pfxWithdraw()
Definition: PyVisitor.h:91
bool VisitInteger(const PyInt *rep)
primitive data visitors
bool VisitList(const PyList *rep)
bool visit(PyVisitor &v) const
Visits object.
Definition: PyRep.cpp:455
void _print(const char *fmt,...)
Python tuple.
Definition: PyRep.h:567
PyRep * arguments() const
Definition: PyRep.h:845
void pfxHexDump(const char *pfx, FILE *into, const uint8 *data, size_t length)
Definition: utils_hex.cpp:63
bool IsPrintable(const PyString *str)
Checks whether string is printable.
Definition: EVEUtils.cpp:31
PyString * GetColumnName(uint32 index) const
Definition: PyDatabase.cpp:65
PyDumpVisitor(const char *pfx="", bool full_nested=false)
Python boolean.
Definition: PyRep.h:323
PyFileDumpVisitor(FILE *_file, const char *pfx="", bool full_nested=false, bool full_hex=false)
PyRep * decoded() const
Definition: PyRep.h:1048
#define is_log_enabled(type)
Definition: logsys.h:78
Python extended object.
Definition: PyRep.h:861
bool VisitBoolean(const PyBool *rep)
Python object.
Definition: PyRep.h:826
uint32 ColumnCount() const
Definition: PyDatabase.cpp:60
Python's "none".
Definition: PyRep.h:352
storage_type::const_iterator const_iterator
Definition: PyRep.h:966
size_t size() const
Definition: PyRep.h:769
dict_type::const_iterator const_dict_iterator
Definition: PyRep.h:870
virtual void _dump(const char *pfx, const uint8 *data, size_t len)=0
bool VisitDict(const PyDict *rep)
virtual bool VisitSubStruct(const PySubStruct *rep)
wrapper types Visitor
Definition: PyVisitor.cpp:112
Python integer.
Definition: PyRep.h:231
bool VisitToken(const PyToken *rep)
bool VisitPackedRow(const PyPackedRow *rep)
PackedRow type visitor.
dict_type & dict()
Definition: PyRep.h:892
bool VisitNone(const PyNone *rep)
const_iterator begin() const
Definition: PyRep.h:986
bool fullNested() const
Definition: PyDumpVisitor.h:36
void _print(const char *fmt,...)
bool VisitReal(const PyFloat *rep)
unsigned __int32 uint32
Definition: eve-compat.h:50
int64 value() const
Definition: PyRep.h:278
PyString * type() const
Definition: PyRep.h:844
bool VisitObjectEx(const PyObjectEx *rep)
const std::string & content() const
Obtain token.
Definition: PyRep.h:555
const_iterator begin() const
Definition: PyRep.h:766
Python token (eg. class name).
Definition: PyRep.h:522
LogType logType() const
Definition: PyDumpVisitor.h:80
uint32 checksum() const
Definition: PyRep.h:1082
LogType
Definition: logsys.h:59
const_iterator end() const
Definition: PyRep.h:661
storage_type::const_iterator const_iterator
Definition: PyRep.h:750
#define PRIi64
Definition: eve-compat.h:83
const_iterator end() const
Definition: PyRep.h:987
bool isType2() const
Definition: PyRep.h:887
PyRep * header() const
Definition: PyRep.h:886
size_type size() const
Definition: Buffer.h:610
FILE * file() const
const std::string & content() const
Get the PyString content.
Definition: PyRep.h:458
void pfxHexDumpPreview(const char *pfx, FILE *into, const uint8 *data, size_t length)
Definition: utils_hex.cpp:87
const_iterator end() const
Definition: PyRep.h:767
size_t size() const
Definition: PyRep.h:663
bool VisitSubStream(const PySubStream *rep)
Python buffer.
Definition: PyRep.h:382
Packed row.
Definition: PyRep.h:961
bool VisitChecksumedStream(const PyChecksumedStream *rep)
void log_messageVA(LogType type, const char *fmt, va_list args)
Definition: logsys.cpp:79
LogType logHexType() const
Definition: PyDumpVisitor.h:81
const_iterator end() const
Definition: PyRep.h:589
const_iterator begin() const
Definition: PyRep.h:588
bool VisitObject(const PyObject *rep)
Object type visitor.
const char * _pfx() const
Definition: PyVisitor.h:89
virtual void _print(const char *fmt,...)=0
bool VisitBuffer(const PyBuffer *rep)
void _dump(const char *pfx, const uint8 *data, size_t len)
bool empty() const
Definition: PyRep.h:664
Python list.
Definition: PyRep.h:639
virtual bool visit(PyVisitor &v) const =0
Visits object.
bool VisitSubStruct(const PySubStruct *rep)
wrapper types Visitor
Python long integer.
Definition: PyRep.h:261
virtual bool VisitChecksumedStream(const PyChecksumedStream *rep)
Definition: PyVisitor.cpp:133