Pol  Revision:cb584c9
uofile02.cpp
Go to the documentation of this file.
1 
10 #include <cstdio>
11 
12 #include "../clib/logfacility.h"
13 #include "../clib/passert.h"
14 #include "../clib/rawtypes.h"
15 #include "../clib/stlutil.h"
16 #include "uofile.h"
17 #include "uofilei.h"
18 #include "ustruct.h"
19 
20 
21 namespace Pol
22 {
23 namespace Plib
24 {
25 unsigned int num_static_patches = 0;
26 }
27 namespace Core
28 {
29 bool static_debug_on = false;
30 
31 // I'd put these in an anonymous namespace, but the debugger can't see 'em...at least not easily.
32 
33 unsigned int last_block = ~0u;
37 
41 
42 typedef std::map<unsigned int, unsigned int> StaticDifBlockIndex;
43 StaticDifBlockIndex stadifl;
44 
46 {
47  std::vector<USTRUCT_STATIC> statics;
48  int count;
49 };
50 static std::vector<USTRUCT_STATIC_BUFFER> rawstatic_buffer_vec;
51 static bool rawstatic_init = false;
52 
54 {
55  unsigned index = 0;
56  if ( stadifl_file != NULL )
57  {
58  u32 blockid;
59  while ( fread( &blockid, sizeof blockid, 1, stadifl_file ) == 1 )
60  {
61  stadifl[blockid] = index;
62  ++index;
63  }
64  }
66 }
67 
68 void readstaticblock( std::vector<USTRUCT_STATIC>* ppst, int* pnum, unsigned short x,
69  unsigned short y )
70 {
71  if ( !rawstatic_init ) // FIXME just for safety cause I'm lazy
73  if ( x >= uo_map_width || y >= uo_map_height )
74  {
75  ERROR_PRINT << "readstaticblock: x=" << x << ",y=" << y << "\n";
76  }
77  unsigned int x_block, y_block;
78  x_block = x / 8;
79  y_block = y / 8;
80  unsigned int block = ( x_block * ( uo_map_height / 8 ) + y_block );
81  *ppst = rawstatic_buffer_vec.at( block ).statics;
82  *pnum = rawstatic_buffer_vec.at( block ).count;
83 }
84 
86 {
87  unsigned int block = 0;
88  USTRUCT_IDX idx;
89  while ( fread( &idx, sizeof idx, 1, sidxfile ) == 1 )
90  {
92  StaticDifBlockIndex::const_iterator citr = stadifl.find( block );
93  if ( citr == stadifl.end() )
94  {
95  if ( idx.length != 0xFFffFFffLu && idx.offset != 0xFFffFFffLu )
96  {
97  if ( fseek( statfile, idx.offset, SEEK_SET ) != 0 )
98  {
99  throw std::runtime_error( "readstaticblock: fseek(statfile) to " +
100  Clib::tostring( idx.offset ) + " failed." );
101  }
102  srec_count = idx.length / sizeof srecs[0];
103  int x = block / uo_map_width; // FIXME is this correct?
104  int y = ( block % uo_map_width ) / 8;
105  passert_always_r( srec_count <= cfg_max_statics_per_block,
106  "to many static items in area " + Clib::tostring( x ) + " " +
107  Clib::tostring( y ) + " " + Clib::tostring( x + 7 ) + " " +
108  Clib::tostring( y + 7 ) +
109  " - maybe double items... you've to reduce amount of " +
110  Clib::tostring( srec_count ) + " items below " +
111  Clib::tostring( cfg_max_statics_per_block ) + " items" );
112 
113  // dave 9/8/3, Austin's statics had a normal offset but a length of 0. badly written tool?
114  if ( idx.length != 0 && fread( srecs, idx.length, 1, statfile ) != 1 )
115  {
116  throw std::runtime_error( "readstaticblock: fread(statfile) failed." +
117  Clib::tostring( block ) );
118  }
119 
120 
121  if ( srec_count > cfg_warning_statics_per_block )
122  INFO_PRINT << " Warning: " << srec_count << " items found in area " << x << " " << y
123  << " " << ( x + 7 ) << " " << ( y + 7 ) << "\n";
124 
125  buf.count = srec_count;
126  }
127  else
128  {
129  buf.count = 0;
130  }
131  }
132  else
133  {
134  // it's in the dif file.. get it from there.
135  unsigned dif_index = ( *citr ).second;
136  int offset = dif_index * sizeof idx;
137  if ( fseek( stadifi_file, offset, SEEK_SET ) != 0 )
138  {
139  throw std::runtime_error( "readstaticblock: fseek(stadifi) to " + Clib::tostring( offset ) +
140  " failed." );
141  }
142 
143  if ( fread( &idx, sizeof idx, 1, stadifi_file ) != 1 )
144  {
145  throw std::runtime_error( "readstaticblock: fread(stadifi) failed." );
146  }
147 
148  if ( idx.length != 0xFFffFFffLu )
149  {
150  if ( fseek( stadif_file, idx.offset, SEEK_SET ) != 0 )
151  {
152  throw std::runtime_error( "readstaticblock: fseek(stadif) to " +
153  Clib::tostring( idx.offset ) + " failed." );
154  }
155 
156  srec_count = idx.length / sizeof srecs[0];
157  int x = block / uo_map_width;
158  int y = ( block % uo_map_width ) / ( uo_map_height / 8 );
159  passert_always_r( srec_count <= cfg_max_statics_per_block,
160  "to many static items in area " + Clib::tostring( x ) + " " +
161  Clib::tostring( y ) + " " + Clib::tostring( x + 7 ) + " " +
162  Clib::tostring( y + 7 ) +
163  " - maybe double items... you've to reduce amount of " +
164  Clib::tostring( srec_count ) + " items below " +
165  Clib::tostring( cfg_max_statics_per_block ) + " items" );
166 
167  if ( fread( srecs, idx.length, 1, stadif_file ) != 1 )
168  {
169  throw std::runtime_error( "readstaticblock: fread(stadif) failed." );
170  }
171 
172  if ( srec_count > cfg_warning_statics_per_block )
173  INFO_PRINT << " Warning: " << srec_count << " items found in dif-area " << x << " " << y
174  << " " << ( x + 7 ) << " " << ( y + 7 ) << "\n";
175 
176  buf.count = srec_count;
177  }
178  else
179  {
180  buf.count = 0;
181  }
182  }
183  if ( buf.count > 0 )
184  {
185  buf.statics.assign( &srecs[0], &srecs[buf.count] );
186  }
187  rawstatic_buffer_vec.push_back( buf );
188  ++block;
189  }
190  rawstatic_init = true;
191 }
192 }
193 }
std::map< unsigned int, unsigned int > StaticDifBlockIndex
Definition: uofile02.cpp:42
FILE * stadifl_file
Definition: uofile00.cpp:26
FILE * stadifi_file
Definition: uofile00.cpp:27
unsigned int num_static_patches
Definition: uofile02.cpp:25
static bool rawstatic_init
Definition: uofile02.cpp:51
bool cfg_use_new_hsa_format
Definition: uofile02.cpp:40
void rawstaticfullread()
Definition: uofile02.cpp:85
unsigned int u32
Definition: rawtypes.h:27
void read_static_diffs()
Definition: uofile02.cpp:53
FILE * sidxfile
Definition: uofile00.cpp:22
USTRUCT_IDX idxrec
Definition: uofile02.cpp:34
int cfg_max_statics_per_block
Definition: uofile02.cpp:38
bool static_debug_on
Definition: uofile02.cpp:29
std::string tostring(const Bscript::BTokenType &v)
Definition: tokens.cpp:19
void readstaticblock(std::vector< USTRUCT_STATIC > *ppst, int *pnum, unsigned short x, unsigned short y)
Definition: uofile02.cpp:68
#define MAX_STATICS_PER_BLOCK
Definition: uofile.h:22
static std::vector< USTRUCT_STATIC_BUFFER > rawstatic_buffer_vec
Definition: uofile02.cpp:50
int srec_count
Definition: uofile02.cpp:36
#define passert_always_r(exp, reason)
Definition: passert.h:84
unsigned short uo_map_height
Definition: uofile00.cpp:90
unsigned int last_block
Definition: uofile02.cpp:33
StaticDifBlockIndex stadifl
Definition: uofile02.cpp:43
unsigned short uo_map_width
Definition: uofile00.cpp:89
#define ERROR_PRINT
Definition: logfacility.h:230
std::vector< USTRUCT_STATIC > statics
Definition: uofile02.cpp:47
#define INFO_PRINT
Definition: logfacility.h:223
USTRUCT_STATIC srecs[MAX_STATICS_PER_BLOCK]
Definition: uofile02.cpp:35
FILE * statfile
Definition: uofile00.cpp:23
Definition: berror.cpp:12
FILE * stadif_file
Definition: uofile00.cpp:28
int cfg_warning_statics_per_block
Definition: uofile02.cpp:39