the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "..\..\..\Minecraft.World\compression.h"
3#include "..\..\..\Minecraft.World\StringHelpers.h"
4#include "..\..\..\Minecraft.World\File.h"
5#include "..\..\..\Minecraft.World\compression.h"
6#include "..\DLC\DLCPack.h"
7#include "..\DLC\DLCLocalisationFile.h"
8#include "..\DLC\DLCGameRulesFile.h"
9#include "..\DLC\DLCGameRules.h"
10#include "..\DLC\DLCGameRulesHeader.h"
11#include "..\..\StringTable.h"
12#include "ConsoleGameRules.h"
13#include "GameRuleManager.h"
14
15WCHAR *GameRuleManager::wchTagNameA[] =
16{
17 L"", // eGameRuleType_Root
18 L"MapOptions", // eGameRuleType_LevelGenerationOptions
19 L"ApplySchematic", // eGameRuleType_ApplySchematic
20 L"GenerateStructure", // eGameRuleType_GenerateStructure
21 L"GenerateBox", // eGameRuleType_GenerateBox
22 L"PlaceBlock", // eGameRuleType_PlaceBlock
23 L"PlaceContainer", // eGameRuleType_PlaceContainer
24 L"PlaceSpawner", // eGameRuleType_PlaceSpawner
25 L"BiomeOverride", // eGameRuleType_BiomeOverride
26 L"StartFeature", // eGameRuleType_StartFeature
27 L"AddItem", // eGameRuleType_AddItem
28 L"AddEnchantment", // eGameRuleType_AddEnchantment
29 L"LevelRules", // eGameRuleType_LevelRules
30 L"NamedArea", // eGameRuleType_NamedArea
31 L"UseTile", // eGameRuleType_UseTileRule
32 L"CollectItem", // eGameRuleType_CollectItemRule
33 L"CompleteAll", // eGameRuleType_CompleteAllRule
34 L"UpdatePlayer", // eGameRuleType_UpdatePlayerRule
35};
36
37WCHAR *GameRuleManager::wchAttrNameA[] =
38{
39 L"descriptionName", // eGameRuleAttr_descriptionName
40 L"promptName", // eGameRuleAttr_promptName
41 L"dataTag", // eGameRuleAttr_dataTag
42 L"enchantmentId", // eGameRuleAttr_enchantmentId
43 L"enchantmentLevel", // eGameRuleAttr_enchantmentLevel
44 L"itemId", // eGameRuleAttr_itemId
45 L"quantity", // eGameRuleAttr_quantity
46 L"auxValue", // eGameRuleAttr_auxValue
47 L"slot", // eGameRuleAttr_slot
48 L"name", // eGameRuleAttr_name
49 L"food", // eGameRuleAttr_food
50 L"health", // eGameRuleAttr_health
51 L"tileId", // eGameRuleAttr_tileId
52 L"useCoords", // eGameRuleAttr_useCoords
53 L"seed", // eGameRuleAttr_seed
54 L"flatworld", // eGameRuleAttr_flatworld
55 L"filename", // eGameRuleAttr_filename
56 L"rot", // eGameRuleAttr_rot
57 L"data", // eGameRuleAttr_data
58 L"block", // eGameRuleAttr_block
59 L"entity", // eGameRuleAttr_entity
60 L"facing", // eGameRuleAttr_facing
61 L"edgeTile", // eGameRuleAttr_edgeTile
62 L"fillTile", // eGameRuleAttr_fillTile
63 L"skipAir", // eGameRuleAttr_skipAir
64 L"x", // eGameRuleAttr_x
65 L"x0", // eGameRuleAttr_x0
66 L"x1", // eGameRuleAttr_x1
67 L"y", // eGameRuleAttr_y
68 L"y0", // eGameRuleAttr_y0
69 L"y1", // eGameRuleAttr_y1
70 L"z", // eGameRuleAttr_z
71 L"z0", // eGameRuleAttr_z0
72 L"z1", // eGameRuleAttr_z1
73 L"chunkX", // eGameRuleAttr_chunkX
74 L"chunkZ", // eGameRuleAttr_chunkZ
75 L"yRot", // eGameRuleAttr_yRot
76 L"spawnX", // eGameRuleAttr_spawnX
77 L"spawnY", // eGameRuleAttr_spawnY
78 L"spawnZ", // eGameRuleAttr_spawnZ
79 L"orientation",
80 L"dimension",
81 L"topTileId", // eGameRuleAttr_topTileId
82 L"biomeId", // eGameRuleAttr_biomeId
83 L"feature", // eGameRuleAttr_feature
84};
85
86GameRuleManager::GameRuleManager()
87{
88 m_currentGameRuleDefinitions = NULL;
89 m_currentLevelGenerationOptions = NULL;
90}
91
92void GameRuleManager::loadGameRules(DLCPack *pack)
93{
94 StringTable *strings = NULL;
95
96 if(pack->doesPackContainFile(DLCManager::e_DLCType_LocalisationData,L"languages.loc"))
97 {
98 DLCLocalisationFile *localisationFile = (DLCLocalisationFile *)pack->getFile(DLCManager::e_DLCType_LocalisationData, L"languages.loc");
99 strings = localisationFile->getStringTable();
100 }
101
102 int gameRulesCount = pack->getDLCItemsCount(DLCManager::e_DLCType_GameRulesHeader);
103 for(int i = 0; i < gameRulesCount; ++i)
104 {
105 DLCGameRulesHeader *dlcHeader = (DLCGameRulesHeader *)pack->getFile(DLCManager::e_DLCType_GameRulesHeader, i);
106 DWORD dSize;
107 byte *dData = dlcHeader->getData(dSize);
108
109 LevelGenerationOptions *createdLevelGenerationOptions = new LevelGenerationOptions(pack);
110 // = loadGameRules(dData, dSize); //, strings);
111
112 createdLevelGenerationOptions->setGrSource( dlcHeader );
113 createdLevelGenerationOptions->setSrc( LevelGenerationOptions::eSrc_fromDLC );
114
115 readRuleFile(createdLevelGenerationOptions, dData, dSize, strings);
116
117 dlcHeader->lgo = createdLevelGenerationOptions;
118 }
119
120 gameRulesCount = pack->getDLCItemsCount(DLCManager::e_DLCType_GameRules);
121 for (int i = 0; i < gameRulesCount; ++i)
122 {
123 DLCGameRulesFile *dlcFile = (DLCGameRulesFile *)pack->getFile(DLCManager::e_DLCType_GameRules, i);
124
125 DWORD dSize;
126 byte *dData = dlcFile->getData(dSize);
127
128 LevelGenerationOptions *createdLevelGenerationOptions = new LevelGenerationOptions(pack);
129 // = loadGameRules(dData, dSize); //, strings);
130
131 createdLevelGenerationOptions->setGrSource( new JustGrSource() );
132 createdLevelGenerationOptions->setSrc( LevelGenerationOptions::eSrc_tutorial );
133
134 readRuleFile(createdLevelGenerationOptions, dData, dSize, strings);
135
136 createdLevelGenerationOptions->setLoadedData();
137 }
138}
139
140LevelGenerationOptions *GameRuleManager::loadGameRules(byte *dIn, UINT dSize)
141{
142 LevelGenerationOptions *lgo = new LevelGenerationOptions();
143 lgo->setGrSource( new JustGrSource() );
144 lgo->setSrc( LevelGenerationOptions::eSrc_fromSave );
145 loadGameRules(lgo, dIn, dSize);
146 lgo->setLoadedData();
147 return lgo;
148}
149
150// 4J-JEV: Reverse of saveGameRules.
151void GameRuleManager::loadGameRules(LevelGenerationOptions *lgo, byte *dIn, UINT dSize)
152{
153 app.DebugPrintf("GameRuleManager::LoadingGameRules:\n");
154
155 ByteArrayInputStream bais( byteArray(dIn,dSize) );
156 DataInputStream dis(&bais);
157
158 // Read file header.
159
160 //dis.readInt(); // File Size
161
162 short version = dis.readShort();
163 assert( 0x1 == version );
164 app.DebugPrintf("\tversion=%d.\n", version);
165
166 for (int i = 0; i < 8; i++) dis.readByte();
167
168 BYTE compression_type = dis.readByte();
169
170 app.DebugPrintf("\tcompressionType=%d.\n", compression_type);
171
172 UINT compr_len, decomp_len;
173 compr_len = dis.readInt();
174 decomp_len = dis.readInt();
175
176 app.DebugPrintf("\tcompr_len=%d.\n\tdecomp_len=%d.\n", compr_len, decomp_len);
177
178
179 // Decompress File Body
180
181 byteArray content(new BYTE[decomp_len], decomp_len),
182 compr_content(new BYTE[compr_len], compr_len);
183 dis.read(compr_content);
184
185 Compression::getCompression()->SetDecompressionType( (Compression::ECompressionTypes)compression_type );
186 Compression::getCompression()->DecompressLZXRLE( content.data, &content.length,
187 compr_content.data, compr_content.length);
188 Compression::getCompression()->SetDecompressionType( SAVE_FILE_PLATFORM_LOCAL );
189
190 dis.close();
191 bais.close();
192
193 delete [] compr_content.data;
194
195 ByteArrayInputStream bais2( content );
196 DataInputStream dis2( &bais2 );
197
198 // Read StringTable.
199 byteArray bStringTable;
200 bStringTable.length = dis2.readInt();
201 bStringTable.data = new BYTE[ bStringTable.length ];
202 dis2.read(bStringTable);
203 StringTable *strings = new StringTable(bStringTable.data, bStringTable.length);
204
205 // Read RuleFile.
206 byteArray bRuleFile;
207 bRuleFile.length = content.length - bStringTable.length;
208 bRuleFile.data = new BYTE[ bRuleFile.length ];
209 dis2.read(bRuleFile);
210
211 // 4J-JEV: I don't believe that the path-name is ever used.
212 //DLCGameRulesFile *dlcgr = new DLCGameRulesFile(L"__PLACEHOLDER__");
213 //dlcgr->addData(bRuleFile.data,bRuleFile.length);
214
215 if (readRuleFile(lgo, bRuleFile.data, bRuleFile.length, strings))
216 {
217 // Set current gen options and ruleset.
218 //createdLevelGenerationOptions->setFromSaveGame(true);
219 lgo->setSrc(LevelGenerationOptions::eSrc_fromSave);
220 setLevelGenerationOptions( lgo );
221 //m_currentGameRuleDefinitions = lgo->getRequiredGameRules();
222 }
223 else
224 {
225 delete lgo;
226 }
227
228 //delete [] content.data;
229
230 // Close and return.
231 dis2.close();
232 bais2.close();
233
234 return ;
235}
236
237// 4J-JEV: Reverse of loadGameRules.
238void GameRuleManager::saveGameRules(byte **dOut, UINT *dSize)
239{
240 if (m_currentGameRuleDefinitions == NULL &&
241 m_currentLevelGenerationOptions == NULL)
242 {
243 app.DebugPrintf("GameRuleManager:: Nothing here to save.");
244 *dOut = NULL;
245 *dSize = 0;
246 return;
247 }
248
249 app.DebugPrintf("GameRuleManager::saveGameRules:\n");
250
251 // Initialise output stream.
252 ByteArrayOutputStream baos;
253 DataOutputStream dos(&baos);
254
255 // Write header.
256
257 // VERSION NUMBER
258 dos.writeShort( 0x1 ); // version_number
259
260 // Write 8 bytes of empty space in case we need them later.
261 // Mainly useful for the ones we save embedded in game saves.
262 for (UINT i = 0; i < 8; i++)
263 dos.writeByte(0x0);
264
265 dos.writeByte(APPROPRIATE_COMPRESSION_TYPE); // m_compressionType
266
267 // -- START COMPRESSED -- //
268 ByteArrayOutputStream compr_baos;
269 DataOutputStream compr_dos(&compr_baos);
270
271 if (m_currentGameRuleDefinitions == NULL)
272 {
273 compr_dos.writeInt( 0 ); // numStrings for StringTable
274 compr_dos.writeInt( version_number );
275 compr_dos.writeByte(Compression::eCompressionType_None); // compression type
276 for (int i=0; i<2; i++) compr_dos.writeByte(0x0); // Padding.
277 compr_dos.writeInt( 0 ); // StringLookup.length
278 compr_dos.writeInt( 0 ); // SchematicFiles.length
279 compr_dos.writeInt( 0 ); // XmlObjects.length
280 }
281 else
282 {
283 StringTable *st = m_currentGameRuleDefinitions->getStringTable();
284
285 if (st == NULL)
286 {
287 app.DebugPrintf("GameRuleManager::saveGameRules: StringTable == NULL!");
288 }
289 else
290 {
291 // Write string table.
292 byteArray stba;
293 m_currentGameRuleDefinitions->getStringTable()->getData(&stba.data, &stba.length);
294 compr_dos.writeInt( stba.length );
295 compr_dos.write( stba );
296
297 // Write game rule file to second
298 // buffer and generate string lookup.
299 writeRuleFile(&compr_dos);
300 }
301 }
302
303 // Compress compr_dos and write to dos.
304 byteArray compr_ba(new BYTE[ compr_baos.buf.length ], compr_baos.buf.length);
305 Compression::getCompression()->CompressLZXRLE( compr_ba.data, &compr_ba.length,
306 compr_baos.buf.data, compr_baos.buf.length );
307
308 app.DebugPrintf("\tcompr_ba.length=%d.\n\tcompr_baos.buf.length=%d.\n",
309 compr_ba.length, compr_baos.buf.length );
310
311 dos.writeInt( compr_ba.length ); // Write length
312 dos.writeInt( compr_baos.buf.length );
313 dos.write(compr_ba);
314
315 delete [] compr_ba.data;
316
317 compr_dos.close();
318 compr_baos.close();
319 // -- END COMPRESSED -- //
320
321 // return
322 *dSize = baos.buf.length;
323 *dOut = baos.buf.data;
324
325 baos.buf.data = NULL;
326
327 dos.close(); baos.close();
328}
329
330// 4J-JEV: Reverse of readRuleFile.
331void GameRuleManager::writeRuleFile(DataOutputStream *dos)
332{
333 // Write Header
334 dos->writeShort(version_number); // Version number.
335 dos->writeByte(Compression::eCompressionType_None); // compression type
336 for (int i=0; i<8; i++) dos->writeBoolean(false); // Padding.
337
338 // Write string lookup.
339 int numStrings = ConsoleGameRules::eGameRuleType_Count + ConsoleGameRules::eGameRuleAttr_Count;
340 dos->writeInt(numStrings);
341 for (int i = 0; i < ConsoleGameRules::eGameRuleType_Count; i++) dos->writeUTF( wchTagNameA[i] );
342 for (int i = 0; i < ConsoleGameRules::eGameRuleAttr_Count; i++) dos->writeUTF( wchAttrNameA[i] );
343
344 // Write schematic files.
345 unordered_map<wstring, ConsoleSchematicFile *> *files;
346 files = getLevelGenerationOptions()->getUnfinishedSchematicFiles();
347 dos->writeInt( files->size() );
348 for (AUTO_VAR(it, files->begin()); it != files->end(); it++)
349 {
350 wstring filename = it->first;
351 ConsoleSchematicFile *file = it->second;
352
353 ByteArrayOutputStream fileBaos;
354 DataOutputStream fileDos(&fileBaos);
355 file->save(&fileDos);
356
357 dos->writeUTF(filename);
358 //dos->writeInt(file->m_data.length);
359 dos->writeInt(fileBaos.buf.length);
360 dos->write((byteArray)fileBaos.buf);
361
362 fileDos.close(); fileBaos.close();
363 }
364
365 // Write xml objects.
366 dos->writeInt( 2 ); // numChildren
367 m_currentLevelGenerationOptions->write(dos);
368 m_currentGameRuleDefinitions->write(dos);
369}
370
371bool GameRuleManager::readRuleFile(LevelGenerationOptions *lgo, byte *dIn, UINT dSize, StringTable *strings) //(DLCGameRulesFile *dlcFile, StringTable *strings)
372{
373 bool levelGenAdded = false;
374 bool gameRulesAdded = false;
375 LevelGenerationOptions *levelGenerator = lgo;//new LevelGenerationOptions();
376 LevelRuleset *gameRules = new LevelRuleset();
377
378 //DWORD dwLen = 0;
379 //PBYTE pbData = dlcFile->getData(dwLen);
380 //byteArray data(pbData,dwLen);
381
382 byteArray data(dIn, dSize);
383 ByteArrayInputStream bais(data);
384 DataInputStream dis(&bais);
385
386 // Read File.
387
388 // version_number
389 __int64 version = dis.readShort();
390 unsigned char compressionType = 0;
391 if(version == 0)
392 {
393 for (int i = 0; i < 14; i++) dis.readByte(); // Read padding.
394 }
395 else
396 {
397 compressionType = dis.readByte();
398
399 // Read the spare bytes we inserted for future use
400 for(int i = 0; i < 8; ++i) dis.readBoolean();
401 }
402
403 ByteArrayInputStream *contentBais = NULL;
404 DataInputStream *contentDis = NULL;
405
406 if(compressionType == Compression::eCompressionType_None)
407 {
408 // No compression
409 // No need to read buffer size, as we can read the stream as it is;
410 app.DebugPrintf("De-compressing game rules with: None\n");
411 contentDis = &dis;
412 }
413 else
414 {
415 unsigned int uncompressedSize = dis.readInt();
416 unsigned int compressedSize = dis.readInt();
417 byteArray compressedBuffer(compressedSize);
418 dis.read(compressedBuffer);
419
420 byteArray decompressedBuffer = byteArray(uncompressedSize);
421
422 switch(compressionType)
423 {
424 case Compression::eCompressionType_None:
425 memcpy(decompressedBuffer.data, compressedBuffer.data, uncompressedSize);
426 break;
427
428 case Compression::eCompressionType_RLE:
429 app.DebugPrintf("De-compressing game rules with: RLE\n");
430 Compression::getCompression()->Decompress( decompressedBuffer.data, &decompressedBuffer.length, compressedBuffer.data, compressedSize);
431 break;
432
433 default:
434 app.DebugPrintf("De-compressing game rules.");
435#ifndef _CONTENT_PACKAGE
436 assert( compressionType == APPROPRIATE_COMPRESSION_TYPE );
437#endif
438 // 4J-JEV: DecompressLZXRLE uses the correct platform specific compression type. (need to assert that the data is compressed with it though).
439 Compression::getCompression()->DecompressLZXRLE(decompressedBuffer.data, &decompressedBuffer.length, compressedBuffer.data, compressedSize);
440 break;
441/* 4J-JEV:
442 Each platform has only 1 method of compression, 'compression.h' file deals with it.
443
444 case Compression::eCompressionType_LZXRLE:
445 app.DebugPrintf("De-compressing game rules with: LZX+RLE\n");
446 Compression::getCompression()->DecompressLZXRLE( decompressedBuffer.data, &uncompressedSize, compressedBuffer.data, compressedSize);
447 break;
448 default:
449 app.DebugPrintf("Invalid compression type %d found\n", compressionType);
450 __debugbreak();
451
452 delete [] compressedBuffer.data; delete [] decompressedBuffer.data;
453 dis.close(); bais.reset();
454
455 if(!gameRulesAdded) delete gameRules;
456 return false;
457 */
458 };
459
460 delete [] compressedBuffer.data;
461
462 contentBais = new ByteArrayInputStream(decompressedBuffer);
463 contentDis = new DataInputStream(contentBais);
464 }
465
466 // string lookup.
467 UINT numStrings = contentDis->readInt();
468 vector<wstring> tagsAndAtts;
469 for (UINT i = 0; i < numStrings; i++)
470 tagsAndAtts.push_back( contentDis->readUTF() );
471
472 unordered_map<int, ConsoleGameRules::EGameRuleType> tagIdMap;
473 for(int type = (int)ConsoleGameRules::eGameRuleType_Root; type < (int)ConsoleGameRules::eGameRuleType_Count; ++type)
474 {
475 for(UINT i = 0; i < numStrings; ++i)
476 {
477 if(tagsAndAtts[i].compare(wchTagNameA[type]) == 0)
478 {
479 tagIdMap.insert( unordered_map<int, ConsoleGameRules::EGameRuleType>::value_type(i, (ConsoleGameRules::EGameRuleType)type) );
480 break;
481 }
482 }
483 }
484
485 // 4J-JEV: TODO: As yet unused.
486 /*
487 unordered_map<int, ConsoleGameRules::EGameRuleAttr> attrIdMap;
488 for(int attr = (int)ConsoleGameRules::eGameRuleAttr_descriptionName; attr < (int)ConsoleGameRules::eGameRuleAttr_Count; ++attr)
489 {
490 for (UINT i = 0; i < numStrings; i++)
491 {
492 if (tagsAndAtts[i].compare(wchAttrNameA[attr]) == 0)
493 {
494 tagIdMap.insert( unordered_map<int, ConsoleGameRules::EGameRuleAttr>::value_type(i , (ConsoleGameRules::EGameRuleAttr)attr) );
495 break;
496 }
497 }
498 }*/
499
500 // subfile
501 UINT numFiles = contentDis->readInt();
502 for (UINT i = 0; i < numFiles; i++)
503 {
504 wstring sFilename = contentDis->readUTF();
505 int length = contentDis->readInt();
506 byteArray ba( length );
507
508 contentDis->read(ba);
509
510 levelGenerator->loadSchematicFile(sFilename, ba.data, ba.length);
511
512 }
513
514 LEVEL_GEN_ID lgoID = LEVEL_GEN_ID_NULL;
515
516 // xml objects
517 UINT numObjects = contentDis->readInt();
518 for(UINT i = 0; i < numObjects; ++i)
519 {
520 int tagId = contentDis->readInt();
521 ConsoleGameRules::EGameRuleType tagVal = ConsoleGameRules::eGameRuleType_Invalid;
522 AUTO_VAR(it,tagIdMap.find(tagId));
523 if(it != tagIdMap.end()) tagVal = it->second;
524
525 GameRuleDefinition *rule = NULL;
526
527 if(tagVal == ConsoleGameRules::eGameRuleType_LevelGenerationOptions)
528 {
529 rule = levelGenerator;
530 levelGenAdded = true;
531 //m_levelGenerators.addLevelGenerator(L"",levelGenerator);
532 lgoID = addLevelGenerationOptions(levelGenerator);
533 levelGenerator->loadStringTable(strings);
534 }
535 else if(tagVal == ConsoleGameRules::eGameRuleType_LevelRules)
536 {
537 rule = gameRules;
538 gameRulesAdded = true;
539 m_levelRules.addLevelRule(L"",gameRules);
540 levelGenerator->setRequiredGameRules(gameRules);
541 gameRules->loadStringTable(strings);
542 }
543
544 readAttributes(contentDis, &tagsAndAtts, rule);
545 readChildren(contentDis, &tagsAndAtts, &tagIdMap, rule);
546 }
547
548 if(compressionType != 0)
549 {
550 // Not default
551 contentDis->close();
552 if(contentBais != NULL) delete contentBais;
553 delete contentDis;
554 }
555
556 dis.close();
557 bais.reset();
558
559 //if(!levelGenAdded) { delete levelGenerator; levelGenerator = NULL; }
560 if(!gameRulesAdded) delete gameRules;
561
562 return true;
563 //return levelGenerator;
564}
565
566LevelGenerationOptions *GameRuleManager::readHeader(DLCGameRulesHeader *grh)
567{
568 LevelGenerationOptions *out =
569 new LevelGenerationOptions();
570
571
572 out->setSrc(LevelGenerationOptions::eSrc_fromDLC);
573 out->setGrSource(grh);
574 addLevelGenerationOptions(out);
575
576 return out;
577}
578
579void GameRuleManager::readAttributes(DataInputStream *dis, vector<wstring> *tagsAndAtts, GameRuleDefinition *rule)
580{
581 int numAttrs = dis->readInt();
582 for (UINT att = 0; att < numAttrs; ++att)
583 {
584 int attID = dis->readInt();
585 wstring value = dis->readUTF();
586
587 if(rule != NULL) rule->addAttribute(tagsAndAtts->at(attID),value);
588 }
589}
590
591void GameRuleManager::readChildren(DataInputStream *dis, vector<wstring> *tagsAndAtts, unordered_map<int, ConsoleGameRules::EGameRuleType> *tagIdMap, GameRuleDefinition *rule)
592{
593 int numChildren = dis->readInt();
594 for(UINT child = 0; child < numChildren; ++child)
595 {
596 int tagId = dis->readInt();
597 ConsoleGameRules::EGameRuleType tagVal = ConsoleGameRules::eGameRuleType_Invalid;
598 AUTO_VAR(it,tagIdMap->find(tagId));
599 if(it != tagIdMap->end()) tagVal = it->second;
600
601 GameRuleDefinition *childRule = NULL;
602 if(rule != NULL) childRule = rule->addChild(tagVal);
603
604 readAttributes(dis,tagsAndAtts,childRule);
605 readChildren(dis,tagsAndAtts,tagIdMap,childRule);
606 }
607}
608
609void GameRuleManager::processSchematics(LevelChunk *levelChunk)
610{
611 if(getLevelGenerationOptions() != NULL)
612 {
613 LevelGenerationOptions *levelGenOptions = getLevelGenerationOptions();
614 levelGenOptions->processSchematics(levelChunk);
615 }
616}
617
618void GameRuleManager::processSchematicsLighting(LevelChunk *levelChunk)
619{
620 if(getLevelGenerationOptions() != NULL)
621 {
622 LevelGenerationOptions *levelGenOptions = getLevelGenerationOptions();
623 levelGenOptions->processSchematicsLighting(levelChunk);
624 }
625}
626
627void GameRuleManager::loadDefaultGameRules()
628{
629#ifdef _XBOX
630#ifdef _TU_BUILD
631 wstring fileRoot = L"UPDATE:\\res\\GameRules\\Tutorial.pck";
632#else
633 wstring fileRoot = L"GAME:\\res\\TitleUpdate\\GameRules\\Tutorial.pck";
634#endif
635 File packedTutorialFile(fileRoot);
636 if(loadGameRulesPack(&packedTutorialFile))
637 {
638 m_levelGenerators.getLevelGenerators()->at(0)->setWorldName(app.GetString(IDS_PLAY_TUTORIAL));
639 //m_levelGenerators.getLevelGenerators()->at(0)->setDefaultSaveName(L"Tutorial");
640 m_levelGenerators.getLevelGenerators()->at(0)->setDefaultSaveName(app.GetString(IDS_TUTORIALSAVENAME));
641 }
642
643#ifndef _CONTENT_PACKAGE
644 // 4J Stu - Remove these just now
645 //File testRulesPath(L"GAME:\\GameRules");
646 //vector<File *> *packFiles = testRulesPath.listFiles();
647
648 //for(AUTO_VAR(it,packFiles->begin()); it != packFiles->end(); ++it)
649 //{
650 // loadGameRulesPack(*it);
651 //}
652 //delete packFiles;
653#endif
654
655#else // _XBOX
656
657#ifdef _WINDOWS64
658 File packedTutorialFile(L"Windows64Media\\Tutorial\\Tutorial.pck");
659 if(!packedTutorialFile.exists()) packedTutorialFile = File(L"Windows64\\Tutorial\\Tutorial.pck");
660#elif defined(__ORBIS__)
661 File packedTutorialFile(L"/app0/orbis/Tutorial/Tutorial.pck");
662#elif defined(__PSVITA__)
663 File packedTutorialFile(L"PSVita/Tutorial/Tutorial.pck");
664#elif defined(__PS3__)
665 File packedTutorialFile(L"PS3/Tutorial/Tutorial.pck");
666#else
667 File packedTutorialFile(L"Tutorial\\Tutorial.pck");
668#endif
669 if(loadGameRulesPack(&packedTutorialFile))
670 {
671 m_levelGenerators.getLevelGenerators()->at(0)->setWorldName(app.GetString(IDS_PLAY_TUTORIAL));
672 //m_levelGenerators.getLevelGenerators()->at(0)->setDefaultSaveName(L"Tutorial");
673 m_levelGenerators.getLevelGenerators()->at(0)->setDefaultSaveName(app.GetString(IDS_TUTORIALSAVENAME));
674 }
675#if 0
676 wstring fpTutorial = L"Tutorial.pck";
677 if(app.getArchiveFileSize(fpTutorial) >= 0)
678 {
679 DLCPack *pack = new DLCPack(L"",0xffffffff);
680 DWORD dwFilesProcessed = 0;
681 if ( app.m_dlcManager.readDLCDataFile(dwFilesProcessed,fpTutorial,pack,true) )
682 {
683 app.m_dlcManager.addPack(pack);
684 //m_levelGenerators.getLevelGenerators()->at(0)->setWorldName(app.GetString(IDS_PLAY_TUTORIAL));
685 //m_levelGenerators.getLevelGenerators()->at(0)->setDefaultSaveName(app.GetString(IDS_TUTORIALSAVENAME));
686 }
687 else delete pack;
688 }
689#endif
690#endif
691}
692
693bool GameRuleManager::loadGameRulesPack(File *path)
694{
695 bool success = false;
696 if(path->exists())
697 {
698 DLCPack *pack = new DLCPack(L"",0xffffffff);
699 DWORD dwFilesProcessed = 0;
700 if( app.m_dlcManager.readDLCDataFile(dwFilesProcessed, path->getPath(),pack))
701 {
702 app.m_dlcManager.addPack(pack);
703 success = true;
704 }
705 else
706 {
707 delete pack;
708 }
709 }
710 return success;
711}
712
713void GameRuleManager::setLevelGenerationOptions(LevelGenerationOptions *levelGen)
714{
715 unloadCurrentGameRules();
716
717 m_currentGameRuleDefinitions = NULL;
718 m_currentLevelGenerationOptions = levelGen;
719
720 if(m_currentLevelGenerationOptions != NULL && m_currentLevelGenerationOptions->requiresGameRules() )
721 {
722 m_currentGameRuleDefinitions = m_currentLevelGenerationOptions->getRequiredGameRules();
723 }
724
725 if(m_currentLevelGenerationOptions != NULL)
726 m_currentLevelGenerationOptions->reset_start();
727}
728
729LPCWSTR GameRuleManager::GetGameRulesString(const wstring &key)
730{
731 if(m_currentGameRuleDefinitions != NULL && !key.empty() )
732 {
733 return m_currentGameRuleDefinitions->getString(key);
734 }
735 else
736 {
737 return L"";
738 }
739}
740
741LEVEL_GEN_ID GameRuleManager::addLevelGenerationOptions(LevelGenerationOptions *lgo)
742{
743 vector<LevelGenerationOptions *> *lgs = m_levelGenerators.getLevelGenerators();
744
745 for (int i = 0; i<lgs->size(); i++)
746 if (lgs->at(i) == lgo)
747 return i;
748
749 lgs->push_back(lgo);
750 return lgs->size() - 1;
751}
752
753void GameRuleManager::unloadCurrentGameRules()
754{
755 if (m_currentLevelGenerationOptions != NULL)
756 {
757 if (m_currentGameRuleDefinitions != NULL
758 && m_currentLevelGenerationOptions->isFromSave())
759 m_levelRules.removeLevelRule( m_currentGameRuleDefinitions );
760
761 if (m_currentLevelGenerationOptions->isFromSave())
762 {
763 m_levelGenerators.removeLevelGenerator( m_currentLevelGenerationOptions );
764
765 delete m_currentLevelGenerationOptions;
766 }
767 else if (m_currentLevelGenerationOptions->isFromDLC())
768 {
769 m_currentLevelGenerationOptions->reset_finish();
770 }
771 }
772
773 m_currentGameRuleDefinitions = NULL;
774 m_currentLevelGenerationOptions = NULL;
775}