Subversion Repositories spk

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#include "bob_dom_cut.h"
2
#include "../common/indian.h"
3
 
4
#define max(a, b) (a > b ? a : b)
5
//---------------------------------------------------------------------------------
114 cycrow 6
bool bob_name::toFile(otextfilestream& os)
1 cycrow 7
{
8
	if(m_text && *m_text!=0) {
9
		int oldf=os.flags();
10
		os << noSemicolons << "N " << autoSemicolons << m_text;
11
		os.flags(oldf);
12
	}
13
	return os.good();
14
}
15
//---------------------------------------------------------------------------------
16
// PATH
114 cycrow 17
bool bob_path::load(ibinaryfilestream& is, int version)
1 cycrow 18
{
19
	int hdr;
20
	is >> hdr;
114 cycrow 21
	if(hdr!=HDR_BEGIN){
1 cycrow 22
		error(e_badHeader);
23
		return false;
24
	}
25
	is >> partIdx;
26
	if(version < 6) {
27
		int bid;
28
		is >> bid;
29
		m_bodyId=new char[20];
30
		_itoa(bid, m_bodyId, 10);
31
	}
114 cycrow 32
	else
1 cycrow 33
		is >> m_bodyId;
114 cycrow 34
 
1 cycrow 35
	is >> cockpitIdx >> parentIdx >> bodyFlags;
114 cycrow 36
 
37
	if(peek(is)==bob_name::HDR_BEGIN){
1 cycrow 38
		if(name.load(is)==false){
39
			for(ErrorIterator &it=name.errors.begin(); it!=name.errors.end(); ++it){
40
				error(it->code, "name: %s", it->text);
41
			}
42
			return false;
43
		}
44
	}
45
 
114 cycrow 46
	if(peek(is)==bob_constants::HDR_BEGIN){
1 cycrow 47
		if(constants.load(is)==false){
48
			for(ErrorIterator &it=name.errors.begin(); it!=name.errors.end(); ++it){
49
				error(it->code, "constants: %s", it->text);
50
			}
51
			return false;
52
		}
53
	}
114 cycrow 54
 
55
	if(peek(is)==bob_dom_bob::HDR_BEGIN){
1 cycrow 56
		bob=new bob_dom_bob(m_settings);
57
		if(bob->load(is)==false){
58
			for(ErrorIterator &it=bob->errors.begin(); it!=bob->errors.end(); ++it){
59
				error(it->code, "bob->%s", it->text);
60
			}
61
			return false;
62
		}
63
	}
64
	int noteLineCount;
65
	is >> noteLineCount;
66
	if(noteLineCount > 0){
67
		if(m_notes.load(is, noteLineCount)==false){
68
			for(ErrorIterator &it=m_notes.errors.begin(); it!=m_notes.errors.end(); ++it){
69
				error(it->code, "notes->%s", it->text);
70
			}
71
			return false;
72
		}
73
	}
114 cycrow 74
 
1 cycrow 75
	int statCount;
76
	is >> statCount;
77
	if(loadStatValues(is, statCount)==false) return false;
114 cycrow 78
 
1 cycrow 79
	is >> hdr;
80
	if(is.fail())
81
		error(e_notEnoughData);
114 cycrow 82
	else if(hdr!=HDR_END)
1 cycrow 83
		error(e_badEndHeader);
114 cycrow 84
 
1 cycrow 85
	/////////////////
114 cycrow 86
	//if(hdr==HDR_END) formatStats();
1 cycrow 87
	////////////////
114 cycrow 88
	return hdr==HDR_END && !is.fail();
1 cycrow 89
}
90
//---------------------------------------------------------------------------------
114 cycrow 91
bool bob_path::loadStatValues(ibinaryfilestream& is, int count)
1 cycrow 92
{
93
	int hdr;
94
	is >> hdr;
114 cycrow 95
	if(hdr!=HDR_STAT_BEGIN){
1 cycrow 96
		error(e_badHeader, "Expected STAT header.");
97
		return false;
98
	}
114 cycrow 99
 
1 cycrow 100
	child_type *ch;
101
	bool bRes;
114 cycrow 102
 
1 cycrow 103
	for(int i=0; i < count; i++){
104
		ch=createChild();
105
		bRes=ch->load(is);
114 cycrow 106
 
1 cycrow 107
		for(ErrorIterator &it=ch->errors.begin(); it!=ch->errors.end(); ++it){
108
			error(it->severity, it->code, "stat->frame[%d]: %s", i, it->text);
109
		}
110
		if(bRes==false) break;
111
	}
114 cycrow 112
 
1 cycrow 113
	is >> hdr;
114 cycrow 114
	if(hdr!=HDR_STAT_END){
1 cycrow 115
		error(s_error, e_moreData, "More data in STAT section than expected - skipping");
116
		do{
117
			is >> hdr;
118
		}
114 cycrow 119
		while(hdr!=HDR_STAT_END && is.good());
1 cycrow 120
	}
114 cycrow 121
 
122
	if(hdr!=HDR_STAT_END)
1 cycrow 123
		error(e_notEnoughData);
114 cycrow 124
	return hdr==HDR_STAT_END;
1 cycrow 125
}
126
//---------------------------------------------------------------------------------
114 cycrow 127
bool bob_path::toFile(obinaryfilestream& os, int cutVersion)
1 cycrow 128
{
114 cycrow 129
	os << HDR_BEGIN;
1 cycrow 130
	os << partIdx;
131
	if(cutVersion >= 6)
132
		os << bodyId();
133
	else
134
		os << atoi(bodyId());
114 cycrow 135
 
1 cycrow 136
	os << cockpitIdx;
137
	os << parentIdx;
138
	os << bodyFlags;
139
	os << name;
140
	constants.toFile(os);
141
	if(bob) bob->toFile(os);
142
	os << (int)m_notes.size();
143
	m_notes.toFile(os);
144
	os << (int)size();
114 cycrow 145
 
146
	os << HDR_STAT_BEGIN;
1 cycrow 147
	for(iterator &it=begin(); it!=end(); ++it){
148
		it->toFile(os);
149
	}
114 cycrow 150
	os << HDR_STAT_END << HDR_END;
1 cycrow 151
	return true;
152
}
153
//---------------------------------------------------------------------------------
114 cycrow 154
bool bob_path::toFile(otextfilestream& os, int idx)
1 cycrow 155
{
156
	os << noSemicolons << "P " << partIdx << "; B " << bodyId() << "; ";
157
	if(parentIdx!=-1)
158
		os << "F " << parentIdx << "; ";
114 cycrow 159
 
1 cycrow 160
	if(cockpitIdx > 0)
161
		os << "C " << cockpitIdx << "; ";
114 cycrow 162
 
1 cycrow 163
	os << name;
114 cycrow 164
 
1 cycrow 165
	constants.toFile(os);
166
 
167
	/*
168
		we've got old x2 flags and new x3 flags
169
		these 2 can be mixed together
114 cycrow 170
 
1 cycrow 171
		for example body || light
114 cycrow 172
 
1 cycrow 173
		only the body (64) and scene (128) can be mixed with anything else imho
174
	*/
175
	if(bodyFlags!=0) {
114 cycrow 176
		if(bodyFlags & fBody)
1 cycrow 177
			os << "b ";
114 cycrow 178
		if(bodyFlags & fScene)
1 cycrow 179
			os << "j ";
180
		int i=bodyFlags & 0x3F;
114 cycrow 181
		if(i)
1 cycrow 182
			os << (char) ('b' + i) << ' ';
183
	}
114 cycrow 184
 
1 cycrow 185
	os << "// idx " << idx << ", flags: " << bodyFlags << endl;
114 cycrow 186
 
1 cycrow 187
	if(m_notes.size())
188
		m_notes.toFile(os);
114 cycrow 189
 
1 cycrow 190
	if(bob){
191
		os << noSemicolons << "L { // beginning of embedded BOB" << endl;
192
		os << *bob << "} // end of embedded BOB" << endl << endl;
193
	}
114 cycrow 194
 
1 cycrow 195
	int i=0;
196
	for(iterator &it=begin(); it!=end(); ++it, ++i){
197
		it->toFile(os);
114 cycrow 198
 
1 cycrow 199
		os << " // " << i << endl;
200
	}
201
	os << endl;
114 cycrow 202
 
1 cycrow 203
	return os.good();
204
}
205
//---------------------------------------------------------------------------------
206
// CUT file
114 cycrow 207
bool bob_dom_cut::load(ibinaryfilestream& is)
1 cycrow 208
{
209
	int hdr;
210
	is >> hdr;
114 cycrow 211
	if(hdr!=HDR_BEGIN){
1 cycrow 212
		error(e_badHeader);
213
		return false;
214
	}
114 cycrow 215
 
216
	if(peek(is)==bob_info::HDR_BEGIN){
1 cycrow 217
		if(!info.load(is)){
218
			for(ErrorIterator &it=info.errors.begin(); it!=info.errors.end(); ++it){
219
				error(it->code, "info: %s", it->text);
220
			}
221
			return false;
222
		}
223
	}
114 cycrow 224
 
1 cycrow 225
	is >> version;
114 cycrow 226
 
1 cycrow 227
	if(version!=supported_version)
228
		error(s_warning, e_badVersion, "Unsupported CUT1 version, loading might fail");
114 cycrow 229
 
1 cycrow 230
	is >> m_storedPathCount;
114 cycrow 231
 
1 cycrow 232
	child_type *ch;
233
	for(int i=0; i < m_storedPathCount; i++){
234
		ch=createChild();
235
		bool bRes=ch->load(is, version);
236
		for(ErrorIterator &it=ch->errors.begin(); it!=ch->errors.end(); ++it){
237
			error(it->code, "path[%d]->%s", i, it->text);
238
		}
239
		if(bRes==false) return false;
240
	}
114 cycrow 241
 
1 cycrow 242
	is >> hdr;
114 cycrow 243
	if(hdr!=HDR_END)
1 cycrow 244
		error(e_badEndHeader);
114 cycrow 245
	return hdr==HDR_END;
1 cycrow 246
}
247
//---------------------------------------------------------------------------------
114 cycrow 248
bool bob_dom_cut::convert(ibinaryfilestream& is, otextfilestream& os)
1 cycrow 249
{
250
	int hdr;
251
	is >> hdr;
114 cycrow 252
	if(hdr!=HDR_BEGIN){
1 cycrow 253
		error(e_badHeader);
254
		return false;
255
	}
114 cycrow 256
 
257
	if(peek(is)==bob_info::HDR_BEGIN){
1 cycrow 258
		if(!info.load(is)){
259
			for(ErrorIterator &it=info.errors.begin(); it!=info.errors.end(); ++it){
260
				error(it->code, "info: %s", it->text);
261
			}
262
			return false;
263
		}
264
	}
114 cycrow 265
 
1 cycrow 266
	is >> version;
114 cycrow 267
 
1 cycrow 268
	os << info << endl << "VER: " << autoSemicolons << version << endl << endl;
114 cycrow 269
 
1 cycrow 270
	is >> m_storedPathCount;
114 cycrow 271
 
1 cycrow 272
	child_type *ch;
273
	for(int i=0; i < m_storedPathCount; i++){
114 cycrow 274
		ch=new bob_path(m_settings);
1 cycrow 275
		bool bRes=ch->load(is, version);
114 cycrow 276
 
1 cycrow 277
		for(ErrorIterator &it=ch->errors.begin(); it!=ch->errors.end(); ++it){
278
			error(it->code, "path[%d]->%s", i, it->text);
279
		}
114 cycrow 280
 
1 cycrow 281
		if(bRes)
282
			ch->toFile(os, i);
114 cycrow 283
 
1 cycrow 284
		delete ch;
285
		if(bRes==false) return false;
286
	}
114 cycrow 287
 
1 cycrow 288
	is >> hdr;
114 cycrow 289
	if(hdr!=HDR_END)
1 cycrow 290
		error(e_badEndHeader);
114 cycrow 291
	return hdr==HDR_END;
1 cycrow 292
}
293
//---------------------------------------------------------------------------------
114 cycrow 294
bool bob_dom_cut::toFile(obinaryfilestream& os)
1 cycrow 295
{
114 cycrow 296
	os << HDR_BEGIN << info << version << (int)size();
1 cycrow 297
 
298
	for(iterator &it=begin(); it!=end(); ++it){
299
		it->toFile(os, version);
300
	}
114 cycrow 301
	os << HDR_END;
302
 
1 cycrow 303
	return os.good();
304
}
305
//---------------------------------------------------------------------------------
114 cycrow 306
bool bob_dom_cut::toFile(otextfilestream& os)
1 cycrow 307
{
308
	os << noSemicolons << info << endl;
309
	os << "VER: " << autoSemicolons << version << endl << endl;
114 cycrow 310
 
1 cycrow 311
	int idx=0;
312
	for(iterator &it=begin(); it!=end(); ++it){
313
		it->toFile(os, idx++);
314
		os << endl;
315
	}
114 cycrow 316
 
1 cycrow 317
	return os.good();
318
}
319
//---------------------------------------------------------------------------------
320
// NOTES - section NOTE
114 cycrow 321
bool bob_notes::load(ibinaryfilestream& is, int lineCount)
1 cycrow 322
{
323
	int hdr;
114 cycrow 324
 
1 cycrow 325
	is >> hdr;
114 cycrow 326
	if(hdr!=HDR_BEGIN){
1 cycrow 327
		error(e_badHeader);
328
		return false;
329
	}
330
	child_type *ch;
331
	for(int i=0; i < lineCount; i++){
332
		ch=createChild();
333
		if(ch->load(is)==false){
334
			error(ch->errorCode, "line[%d]: %s", i, bob_traslate_error(ch->errorCode));
335
			return false;
336
		}
337
	}
338
	is >> hdr;
114 cycrow 339
	if(hdr!=HDR_END)
1 cycrow 340
		error(e_badEndHeader);
114 cycrow 341
 
342
	return hdr==HDR_END && !is.fail();
1 cycrow 343
}
344
//---------------------------------------------------------------------------------
114 cycrow 345
bool bob_notes::toFile(obinaryfilestream& os)
1 cycrow 346
{
347
	if(size()==0) return true;
114 cycrow 348
 
349
	os << HDR_BEGIN;
1 cycrow 350
	for(iterator &it=begin(); it!=end(); ++it){
351
		it->toFile(os);
352
	}
114 cycrow 353
	os << HDR_END;
1 cycrow 354
	return os.good();
355
}
356
//---------------------------------------------------------------------------------
114 cycrow 357
bool bob_notes::toFile(otextfilestream& os)
1 cycrow 358
{
359
 os << noSemicolons << "T // Notes" << endl;
360
	for(iterator &it=begin(); it!=end(); ++it){
361
		it->toFile(os);
362
		os << endl;
363
	}
364
	os << -1 << " // end of Notes" << endl;
365
	return os.good();
366
}
367
//---------------------------------------------------------------------------------
368
// NOTE - object
114 cycrow 369
bool bob_note::load(ibinaryfilestream& is)
1 cycrow 370
{
371
	is >> value;
372
	is >> text;
373
	if(is.fail())
374
		errorCode=e_notEnoughData;
375
	return !is.fail();
376
}
377
//---------------------------------------------------------------------------------
114 cycrow 378
bool bob_note::toFile(obinaryfilestream& os)
1 cycrow 379
{
380
	os << value << text;
381
	return os.good();
382
}
383
//---------------------------------------------------------------------------------
114 cycrow 384
bool bob_note::toFile(otextfilestream& os)
1 cycrow 385
{
386
	os << autoSemicolons << value << text;
387
	return os.good();
388
}
389
//---------------------------------------------------------------------------------
114 cycrow 390
bool bob_constants::load(ibinaryfilestream& is)
1 cycrow 391
{
392
	int hdr, count;
393
	is >> hdr;
114 cycrow 394
	if(hdr!=HDR_BEGIN){
1 cycrow 395
		error(e_badHeader);
396
		return false;
397
	}
114 cycrow 398
 
1 cycrow 399
	is >> count;
114 cycrow 400
 
1 cycrow 401
	constant c;
402
	for(int i=0; i < count; i++){
403
		c.load(is);
404
		values.push_back(c);
405
	}
406
	is >> hdr;
114 cycrow 407
	if(hdr!=HDR_END){
1 cycrow 408
		error(e_badEndHeader);
409
		return false;
410
	}
411
	return is.good();
412
}
413
//---------------------------------------------------------------------------------
114 cycrow 414
bool bob_constants::toFile(obinaryfilestream& os)
1 cycrow 415
{
416
	if(values.size()==0) return true;
114 cycrow 417
 
418
	os << HDR_BEGIN << (int)values.size();
419
 
1 cycrow 420
	for(iterator &it=values.begin(); it!=values.end(); ++it){
421
		(*it).toFile(os);
422
	}
114 cycrow 423
	os << HDR_END;
1 cycrow 424
	return os.good();
425
}
426
//---------------------------------------------------------------------------------
114 cycrow 427
bool bob_constants::toFile(otextfilestream& os)
1 cycrow 428
{
429
	if(values.size()==0) return true;
114 cycrow 430
 
1 cycrow 431
	os << noSemicolons << "K " << autoSemicolons << (int)values.size();
432
	for(iterator &it=values.begin(); it!=values.end(); ++it){
433
		(*it).toFile(os);
434
	}
435
	os << noSemicolons;
436
	return os.good();
437
}
438
//---------------------------------------------------------------------------------
114 cycrow 439
bool bob_constants::constant::load(ibinaryfilestream& is)
1 cycrow 440
{
441
	is >> a >> b;
442
	return is.good();
443
}
444
//---------------------------------------------------------------------------------
114 cycrow 445
bool bob_constants::constant::toFile(obinaryfilestream& os)
1 cycrow 446
{
447
	os << a << b;
448
	return os.good();
449
}
450
//---------------------------------------------------------------------------------
114 cycrow 451
bool bob_constants::constant::toFile(otextfilestream& os)
1 cycrow 452
{
453
	os << a << b;
454
	return os.good();
455
}
456
//---------------------------------------------------------------------------------