Pol  Revision:4b29d2b
staticserver.cpp
Go to the documentation of this file.
1 
8 #include "staticserver.h"
9 
10 #include <string>
11 
12 #include "../clib/binaryfile.h"
13 #include "../clib/logfacility.h"
14 #include "../clib/passert.h"
15 #include "../clib/stlutil.h"
16 #include "../clib/timer.h"
17 #include "staticblock.h"
18 
19 namespace Pol
20 {
21 namespace Plib
22 {
24  : _descriptor( descriptor ), _index(), _statics()
25 {
26  Clib::BinaryFile index_file( _descriptor.path( "statidx.dat" ), std::ios::in );
27  index_file.ReadVector( _index );
28  if ( _index.empty() )
29  {
30  std::string message = "Empty file: " + _descriptor.path( "statidx.dat" );
31  throw std::runtime_error( message );
32  }
33 
34  Clib::BinaryFile statics_file( _descriptor.path( "statics.dat" ), std::ios::in );
35  statics_file.ReadVector( _statics );
36  if ( _statics.empty() )
37  {
38  std::string message = "Empty file: " + _descriptor.path( "statics.dat" );
39  throw std::runtime_error( message );
40  }
41 
42  Validate();
43 }
44 
46 
48 {
49  POLLOG_INFO << "Validating statics files: ";
50  Tools::Timer<> timer;
51  for ( unsigned short y = 0; y < _descriptor.height; y += STATICBLOCK_CHUNK )
52  {
53  for ( unsigned short x = 0; x < _descriptor.width; x += STATICBLOCK_CHUNK )
54  {
55  ValidateBlock( x, y );
56  }
57  }
58  POLLOG_INFO << "Completed in " << timer.ellapsed() << " ms.\n";
59 }
60 
61 void StaticServer::ValidateBlock( unsigned short x, unsigned short y ) const
62 {
63  unsigned short x_block = x / STATICBLOCK_CHUNK;
64  unsigned short y_block = y / STATICBLOCK_CHUNK;
65 
66  size_t block_index =
67  static_cast<size_t>( y_block ) * ( _descriptor.width >> STATICBLOCK_SHIFT ) + x_block;
68  if ( block_index + 1 >= _index.size() )
69  {
70  std::string message =
71  "statics integrity error(1): x=" + Clib::tostring( x ) + ", y=" + Clib::tostring( y );
72  throw std::runtime_error( message );
73  }
74  unsigned int first_entry_index = _index[block_index].index;
75  unsigned int num = _index[block_index + 1].index - first_entry_index;
76  if ( first_entry_index + num > _statics.size() )
77  {
78  std::string message =
79  "statics integrity error(2): x=" + Clib::tostring( x ) + ", y=" + Clib::tostring( y );
80  throw std::runtime_error( message );
81  }
82 }
83 
84 bool StaticServer::findstatic( unsigned short x, unsigned short y, unsigned short objtype ) const
85 {
87 
88  unsigned short x_block = x >> STATICBLOCK_SHIFT;
89  unsigned short y_block = y >> STATICBLOCK_SHIFT;
90  unsigned short xy = ( ( x & STATICCELL_MASK ) << 4 ) | ( y & STATICCELL_MASK );
91 
92  unsigned int block_index = x_block + y_block * ( _descriptor.width >> STATICBLOCK_SHIFT );
93  unsigned int first_entry_index = _index[block_index].index;
94  unsigned int num = _index[block_index + 1].index - first_entry_index;
95 
96  if ( num )
97  {
98  const STATIC_ENTRY* entry = &_statics[first_entry_index];
99  while ( num-- )
100  {
101  if ( entry->xy == xy && entry->objtype == objtype )
102  {
103  return true;
104  }
105  ++entry;
106  }
107  }
108  return false;
109 }
110 
111 void StaticServer::getstatics( StaticEntryList& statics, unsigned short x, unsigned short y ) const
112 {
114 
115  unsigned short x_block = x >> STATICBLOCK_SHIFT;
116  unsigned short y_block = y >> STATICBLOCK_SHIFT;
117  unsigned short xy = ( ( x & STATICCELL_MASK ) << 4 ) | ( y & STATICCELL_MASK );
118 
119  unsigned int block_index = x_block + y_block * ( _descriptor.width >> STATICBLOCK_SHIFT );
120  unsigned int first_entry_index = _index[block_index].index;
121  unsigned int num = _index[block_index + 1].index - first_entry_index;
122 
123  if ( num )
124  {
125  const STATIC_ENTRY* entry = &_statics[first_entry_index];
126  while ( num-- )
127  {
128  if ( entry->xy == xy )
129  {
130  statics.push_back( *entry );
131  }
132  ++entry;
133  }
134  }
135 }
136 
138 {
139  size_t size = sizeof( *this ) + _descriptor.sizeEstimate();
140  size += 3 * sizeof( STATIC_INDEX* ) + _index.capacity() * sizeof( STATIC_INDEX );
141  size += 3 * sizeof( STATIC_ENTRY* ) + _statics.capacity() * sizeof( STATIC_ENTRY );
142  return size;
143 }
144 }
145 }
const unsigned STATICBLOCK_SHIFT
Definition: staticblock.h:38
size_t sizeEstimate() const
#define POLLOG_INFO
Definition: logfacility.h:213
const unsigned STATICBLOCK_CHUNK
Definition: staticblock.h:37
const RealmDescriptor _descriptor
Definition: staticserver.h:43
u16 objtype
Definition: staticblock.h:27
bool findstatic(unsigned short x, unsigned short y, unsigned short objtype) const
void getstatics(StaticEntryList &statics, unsigned short x, unsigned short y) const
std::vector< STATIC_INDEX > _index
Definition: staticserver.h:45
u8 xy
Definition: staticblock.h:28
StaticServer(const RealmDescriptor &descriptor)
std::string tostring(const Bscript::BTokenType &v)
Definition: tokens.cpp:19
#define passert(exp)
Definition: passert.h:62
void ValidateBlock(unsigned short x, unsigned short y) const
Definition: staticblock.h:25
std::string path(const std::string &filename) const
long long ellapsed() const
Definition: timer.cpp:46
std::vector< STATIC_ENTRY > _statics
Definition: staticserver.h:46
void ReadVector(std::vector< T > &vec)
Definition: binaryfile.h:41
Definition: berror.cpp:12
const unsigned STATICCELL_MASK
Definition: staticblock.h:39