aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/DirectoryLevelStorage.cpp
blob: c557e37af1225f724d4e1a90c9068ae40f65ddd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#include "stdafx.h"
#include "System.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.chunk.storage.h"
#include "net.minecraft.world.level.dimension.h"
#include "com.mojang.nbt.h"
#include "File.h"
#include "DataInputStream.h"
#include "FileInputStream.h"
#include "LevelData.h"
#include "DirectoryLevelStorage.h"
#include "ConsoleSaveFileIO.h"

const wstring DirectoryLevelStorage::sc_szPlayerDir(L"players/");

_MapDataMappings::_MapDataMappings()
{
#ifndef _DURANGO
	ZeroMemory(xuids,sizeof(PlayerUID)*MAXIMUM_MAP_SAVE_DATA);
#endif
	ZeroMemory(dimensions,sizeof(byte)*(MAXIMUM_MAP_SAVE_DATA/4));
}

int _MapDataMappings::getDimension(int id)
{
	const int offset = (2*(id%4));
	const int val = (dimensions[id>>2] & (3 << offset))>>offset;

	int returnVal=0;

	switch(val)
	{
	case 0:
		returnVal = 0; // Overworld
		break;
	case 1:
		returnVal = -1; // Nether
		break;
	case 2:
		returnVal = 1; // End
		break;
	default:
#ifndef _CONTENT_PACKAGE
		printf("Read invalid dimension from MapDataMapping\n");
		__debugbreak();
#endif
		break;
	}
	return returnVal;
}

void _MapDataMappings::setMapping(int id, PlayerUID xuid, int dimension)
{
	xuids[id] = xuid;

	const int offset = (2*(id%4));

	// Reset it first
	dimensions[id>>2] &= ~( 2 << offset );
	switch(dimension)
	{
	case 0: // Overworld
		//dimensions[id>>2] &= ~( 2 << offset );
		break;
	case -1: // Nether
		dimensions[id>>2] |= ( 1 << offset );
		break;
	case 1: // End
		dimensions[id>>2] |= ( 2 << offset );
		break;
	default:
#ifndef _CONTENT_PACKAGE
		printf("Trinyg to set a MapDataMapping for an invalid dimension.\n");
		__debugbreak();
#endif
		break;
	}
}

// Old version the only used 1 bit for dimension indexing
_MapDataMappings_old::_MapDataMappings_old()
{
#ifndef _DURANGO
	ZeroMemory(xuids,sizeof(PlayerUID)*MAXIMUM_MAP_SAVE_DATA);
#endif
	ZeroMemory(dimensions,sizeof(byte)*(MAXIMUM_MAP_SAVE_DATA/8));
}

int _MapDataMappings_old::getDimension(int id)
{
	return dimensions[id>>3] & (128 >> (id%8) ) ? -1 : 0;
}

void _MapDataMappings_old::setMapping(int id, PlayerUID xuid, int dimension)
{
	xuids[id] = xuid;
	if( dimension == 0 )
	{
		dimensions[id>>3] &= ~( 128 >> (id%8) );
	}
	else
	{
		dimensions[id>>3] |= ( 128 >> (id%8) );
	}
}

#ifdef _LARGE_WORLDS
void DirectoryLevelStorage::PlayerMappings::addMapping(int id, int centreX, int centreZ, int dimension, int scale)
{
	const int64_t index = ( static_cast<int64_t>(centreZ & 0x1FFFFFFF) << 34) | ( static_cast<int64_t>(centreX & 0x1FFFFFFF) << 5) | ( (scale & 0x7) << 2) | (dimension & 0x3);
	m_mappings[index] = id;
	//app.DebugPrintf("Adding mapping: %d - (%d,%d)/%d/%d [%I64d - 0x%016llx]\n", id, centreX, centreZ, dimension, scale, index, index);
}

bool DirectoryLevelStorage::PlayerMappings::getMapping(int &id, int centreX, int centreZ, int dimension, int scale)
{
	//int64_t zMasked = centreZ & 0x1FFFFFFF;
	//int64_t xMasked = centreX & 0x1FFFFFFF;
	//int64_t zShifted = zMasked << 34;
	//int64_t xShifted = xMasked << 5;
	//app.DebugPrintf("xShifted = %d (0x%016x), zShifted = %I64d (0x%016llx)\n", xShifted, xShifted, zShifted, zShifted);
	const int64_t index = ( static_cast<int64_t>(centreZ & 0x1FFFFFFF) << 34) | ( static_cast<int64_t>(centreX & 0x1FFFFFFF) << 5) | ( (scale & 0x7) << 2) | (dimension & 0x3);
	const auto it = m_mappings.find(index);
	if(it != m_mappings.end())
	{
		id = it->second;
		//app.DebugPrintf("Found mapping: %d - (%d,%d)/%d/%d [%I64d - 0x%016llx]\n", id, centreX, centreZ, dimension, scale, index, index);
		return true;
	}
	else
	{
		//app.DebugPrintf("Failed to find mapping: (%d,%d)/%d/%d [%I64d - 0x%016llx]\n", centreX, centreZ, dimension, scale, index, index);
		return false;
	}
}

void DirectoryLevelStorage::PlayerMappings::writeMappings(DataOutputStream *dos)
{
	dos->writeInt(m_mappings.size());
	for (const auto& it : m_mappings )
	{
		app.DebugPrintf("    -- %lld (0x%016llx) = %d\n", it.first, it.first, it.second);
		dos->writeLong(it.first);
		dos->writeInt(it.second);
	}
}

void DirectoryLevelStorage::PlayerMappings::readMappings(DataInputStream *dis)
{
	const int count = dis->readInt();
	for(unsigned int i = 0; i < count; ++i)
	{
		int64_t index = dis->readLong();
		const int id = dis->readInt();
		m_mappings[index] = id;
		app.DebugPrintf("    -- %lld (0x%016llx) = %d\n", index, index, id);
	}
}
#endif

DirectoryLevelStorage::DirectoryLevelStorage(ConsoleSaveFile *saveFile, const File dir, const wstring& levelId, bool createPlayerDir) : sessionId( System::currentTimeMillis() ),
	dir( L"" ), playerDir( sc_szPlayerDir ), dataDir( wstring(L"data/") ), levelId(levelId)
{
	m_saveFile = saveFile;
	m_bHasLoadedMapDataMappings = false;

#ifdef _LARGE_WORLDS
	m_usedMappings = byteArray(MAXIMUM_MAP_SAVE_DATA/8);
#endif
}

DirectoryLevelStorage::~DirectoryLevelStorage()
{
	delete m_saveFile;

	for(const auto& it : m_cachedSaveData )
	{
		delete it.second;
	}

#ifdef _LARGE_WORLDS
	delete m_usedMappings.data;
#endif
}

void DirectoryLevelStorage::initiateSession()
{
	// 4J Jev, removed try/catch.

	const File dataFile = File( dir, wstring(L"session.lock") );
	FileOutputStream fos = FileOutputStream(dataFile);
	DataOutputStream dos = DataOutputStream(&fos);
	dos.writeLong(sessionId);
	dos.close();

}

File DirectoryLevelStorage::getFolder()
{
	return dir;
}

void DirectoryLevelStorage::checkSession()
{
	// 4J-PB - Not in the Xbox game

	/*
	File dataFile = File( dir, wstring(L"session.lock"));
	FileInputStream fis = FileInputStream(dataFile);
	DataInputStream dis = DataInputStream(&fis);
	dis.close();
	*/
}

ChunkStorage *DirectoryLevelStorage::createChunkStorage(Dimension *dimension)
{
	// 4J Jev, removed try/catch.

	if (dynamic_cast<HellDimension *>(dimension) != nullptr)
	{
		const File dir2 = File(dir, LevelStorage::NETHER_FOLDER);
		//dir2.mkdirs(); // 4J Removed
		return new OldChunkStorage(dir2, true);
	}
	if (dynamic_cast<TheEndDimension *>(dimension) != nullptr)
	{
		const File dir2 = File(dir, LevelStorage::ENDER_FOLDER);
		//dir2.mkdirs(); // 4J Removed
		return new OldChunkStorage(dir2, true);
	}

	return new OldChunkStorage(dir, true);
}

LevelData *DirectoryLevelStorage::prepareLevel()
{
	// 4J Stu Added
#ifdef _LARGE_WORLDS
	const ConsoleSavePath mapFile = getDataFile(L"largeMapDataMappings");
#else
	ConsoleSavePath mapFile = getDataFile(L"mapDataMappings");
#endif
	if (!m_bHasLoadedMapDataMappings && !mapFile.getName().empty() && getSaveFile()->doesFileExist( mapFile ))
	{
		DWORD NumberOfBytesRead;
		FileEntry *fileEntry = getSaveFile()->createFile(mapFile);

#ifdef __PS3__
		// 4J Stu - This version changed happened before initial release
		if(getSaveFile()->getSaveVersion() < SAVE_FILE_VERSION_CHANGE_MAP_DATA_MAPPING_SIZE)
		{
			// Delete the old file
			if(fileEntry) getSaveFile()->deleteFile( fileEntry );

			// Save a new, blank version
			saveMapIdLookup();
		}
		else
#elif defined(_DURANGO)
		// 4J Stu - This version changed happened before initial release
		if(getSaveFile()->getSaveVersion() < SAVE_FILE_VERSION_DURANGO_CHANGE_MAP_DATA_MAPPING_SIZE)
		{
			// Delete the old file
			if(fileEntry) getSaveFile()->deleteFile( fileEntry );

			// Save a new, blank version
			saveMapIdLookup();
		}
		else
#endif
		{
			getSaveFile()->setFilePointer(fileEntry,0,nullptr, FILE_BEGIN);

#ifdef _LARGE_WORLDS
			const byteArray data(fileEntry->getFileSize());
			getSaveFile()->readFile( fileEntry, data.data, fileEntry->getFileSize(), &NumberOfBytesRead);
			assert( NumberOfBytesRead == fileEntry->getFileSize() );

			ByteArrayInputStream bais(data);
			DataInputStream dis(&bais);
			const int count = dis.readInt();
			app.DebugPrintf("Loading %d mappings\n", count);
			for(unsigned int i = 0; i < count; ++i)
			{
				PlayerUID playerUid = dis.readPlayerUID();
#ifdef _WINDOWS64
				app.DebugPrintf("  -- %d\n", playerUid);
#else
				app.DebugPrintf("  -- %ls\n", playerUid.toString().c_str());
#endif
				m_playerMappings[playerUid].readMappings(&dis);
			}
			dis.readFully(m_usedMappings);
#else

			if(getSaveFile()->getSaveVersion() < END_DIMENSION_MAP_MAPPINGS_SAVE_VERSION)
			{
				MapDataMappings_old oldMapDataMappings;
				getSaveFile()->readFile(	fileEntry,
					&oldMapDataMappings, // data buffer
					sizeof(MapDataMappings_old), // number of bytes to read
					&NumberOfBytesRead // number of bytes read
					);
				assert( NumberOfBytesRead == sizeof(MapDataMappings_old) );

				for(unsigned int i = 0; i < MAXIMUM_MAP_SAVE_DATA; ++i)
				{
					m_saveableMapDataMappings.setMapping(i,oldMapDataMappings.xuids[i],oldMapDataMappings.getDimension(i));
				}
			}
			else
			{
				getSaveFile()->readFile(	fileEntry,
					&m_saveableMapDataMappings, // data buffer
					sizeof(MapDataMappings), // number of bytes to read
					&NumberOfBytesRead // number of bytes read
					);
				assert( NumberOfBytesRead == sizeof(MapDataMappings) );
			}

			memcpy(&m_mapDataMappings,&m_saveableMapDataMappings,sizeof(MapDataMappings));
#endif


			// Write out our changes now
			if(getSaveFile()->getSaveVersion() < END_DIMENSION_MAP_MAPPINGS_SAVE_VERSION) saveMapIdLookup();
		}

		m_bHasLoadedMapDataMappings = true;
	}

	// 4J Jev, removed try/catch

	const ConsoleSavePath dataFile = ConsoleSavePath( wstring( L"level.dat" ) );

	if ( m_saveFile->doesFileExist( dataFile ) )
	{
		ConsoleSaveFileInputStream fis = ConsoleSaveFileInputStream(m_saveFile, dataFile);
		CompoundTag *root = NbtIo::readCompressed(&fis);
		CompoundTag *tag = root->getCompound(L"Data");
		LevelData *ret = new LevelData(tag);
		delete root;
		return ret;
	}

	return nullptr;
}

void DirectoryLevelStorage::saveLevelData(LevelData *levelData, vector<shared_ptr<Player> > *players)
{
	// 4J Jev, removed try/catch

	CompoundTag *dataTag = levelData->createTag(players);

	CompoundTag *root = new CompoundTag();
	root->put(L"Data", dataTag);

	const ConsoleSavePath currentFile = ConsoleSavePath( wstring( L"level.dat" ) );

	ConsoleSaveFileOutputStream fos = ConsoleSaveFileOutputStream( m_saveFile, currentFile );
	NbtIo::writeCompressed(root, &fos);

	delete root;
}

void DirectoryLevelStorage::saveLevelData(LevelData *levelData)
{
	// 4J Jev, removed try/catch

	CompoundTag *dataTag = levelData->createTag();

	CompoundTag *root = new CompoundTag();
	root->put(L"Data", dataTag);

	const ConsoleSavePath currentFile = ConsoleSavePath( wstring( L"level.dat" ) );

	ConsoleSaveFileOutputStream fos = ConsoleSaveFileOutputStream( m_saveFile, currentFile );
	NbtIo::writeCompressed(root, &fos);

	delete root;
}

void DirectoryLevelStorage::save(shared_ptr<Player> player)
{
	// 4J Jev, removed try/catch.
	PlayerUID playerXuid = player->getXuid();
#if defined(__PS3__) || defined(__ORBIS__)
	if( playerXuid != INVALID_XUID )
#else
	if( playerXuid != INVALID_XUID && !player->isGuest() )
#endif
	{
		CompoundTag *tag = new CompoundTag();
		player->saveWithoutId(tag);
#if defined(__PS3__) || defined(__ORBIS__) || defined(__PSVITA__)
		ConsoleSavePath realFile = ConsoleSavePath( m_saveFile->getPlayerDataFilenameForSave(playerXuid).c_str() );
#elif defined(_DURANGO)
		ConsoleSavePath realFile = ConsoleSavePath( playerDir.getName() + player->getXuid().toString() + L".dat" );
#else
		const ConsoleSavePath realFile = ConsoleSavePath( playerDir.getName() + std::to_wstring( player->getXuid() ) + L".dat" );
#endif
		// If saves are disabled (e.g. because we are writing the save buffer to disk) then cache this player data
		if(StorageManager.GetSaveDisabled())
		{
			ByteArrayOutputStream *bos = new ByteArrayOutputStream();
			NbtIo::writeCompressed(tag,bos);

			const auto it = m_cachedSaveData.find(realFile.getName());
			if(it != m_cachedSaveData.end() )
			{
				delete it->second;
			}
			m_cachedSaveData[realFile.getName()] = bos;
			app.DebugPrintf("Cached saving of file %ls due to saves being disabled\n", realFile.getName().c_str() );
		}
		else
		{
			ConsoleSaveFileOutputStream fos = ConsoleSaveFileOutputStream( m_saveFile, realFile );
			NbtIo::writeCompressed(tag, &fos);
		}
		delete tag;
	}
	else if( playerXuid != INVALID_XUID )
	{
		app.DebugPrintf("Not saving player as their XUID is a guest\n");
		dontSaveMapMappingForPlayer(playerXuid);
	}
}

// 4J Changed return val to bool to check if new player or loaded player
CompoundTag *DirectoryLevelStorage::load(shared_ptr<Player> player)
{
	CompoundTag *tag = loadPlayerDataTag( player->getXuid() );
	if (tag != nullptr)
	{
		player->load(tag);
	}
	return tag;
}

CompoundTag *DirectoryLevelStorage::loadPlayerDataTag(PlayerUID xuid)
{
	// 4J Jev, removed try/catch.
#if defined(__PS3__) || defined(__ORBIS__) || defined(__PSVITA__)
	ConsoleSavePath realFile = ConsoleSavePath( m_saveFile->getPlayerDataFilenameForLoad(xuid).c_str() );
#elif defined(_DURANGO)
	ConsoleSavePath realFile = ConsoleSavePath( playerDir.getName() + xuid.toString() + L".dat" );
#else
	const ConsoleSavePath realFile = ConsoleSavePath( playerDir.getName() + std::to_wstring( xuid ) + L".dat" );
#endif
	const auto it = m_cachedSaveData.find(realFile.getName());
	if(it != m_cachedSaveData.end() )
	{
		ByteArrayOutputStream *bos = it->second;
		ByteArrayInputStream bis(bos->buf, 0, bos->size());
		CompoundTag *tag = NbtIo::readCompressed(&bis);
		bis.reset();
		app.DebugPrintf("Loaded player data from cached file %ls\n", realFile.getName().c_str() );
		return tag;
	}
	else if ( m_saveFile->doesFileExist( realFile ) )
	{
		ConsoleSaveFileInputStream fis = ConsoleSaveFileInputStream(m_saveFile, realFile);
		return NbtIo::readCompressed(&fis);
	}
	return nullptr;
}

// 4J Added function
void DirectoryLevelStorage::clearOldPlayerFiles()
{
	if(StorageManager.GetSaveDisabled() ) return;

#if defined(__PS3__) || defined(__ORBIS__) || defined(__PSVITA__)
	vector<FileEntry *> *playerFiles = m_saveFile->getValidPlayerDatFiles();
#else
	vector<FileEntry *> *playerFiles = m_saveFile->getFilesWithPrefix( playerDir.getName() );
#endif

	if( playerFiles != nullptr )
	{
#ifndef _FINAL_BUILD
		if(app.DebugSettingsOn() && app.GetGameSettingsDebugMask(ProfileManager.GetPrimaryPad())&(1L<<eDebugSetting_DistributableSave))
		{
			for(unsigned int i = 0; i < playerFiles->size(); ++i )
			{
				const FileEntry *file = playerFiles->at(i);
				wstring xuidStr = replaceAll( replaceAll(file->data.filename,playerDir.getName(),L""),L".dat",L"");
#if defined(__PS3__) || defined(__ORBIS__) || defined(_DURANGO)
				PlayerUID xuid(xuidStr);
#else
				const PlayerUID xuid = _fromString<PlayerUID>(xuidStr);
#endif
				deleteMapFilesForPlayer(xuid);
				m_saveFile->deleteFile( playerFiles->at(i) );
			}
		}
		else
#endif
			if( playerFiles->size() > MAX_PLAYER_DATA_SAVES )
			{
				sort(playerFiles->begin(), playerFiles->end(), FileEntry::newestFirst );

				for(unsigned int i = MAX_PLAYER_DATA_SAVES; i < playerFiles->size(); ++i )
				{
					const FileEntry *file = playerFiles->at(i);
					wstring xuidStr = replaceAll( replaceAll(file->data.filename,playerDir.getName(),L""),L".dat",L"");
#if defined(__PS3__) || defined(__ORBIS__) || defined(_DURANGO)
					PlayerUID xuid(xuidStr);
#else
					const PlayerUID xuid = _fromString<PlayerUID>(xuidStr);
#endif
					deleteMapFilesForPlayer(xuid);
					m_saveFile->deleteFile( playerFiles->at(i) );
				}
			}

			delete playerFiles;
	}
}

PlayerIO *DirectoryLevelStorage::getPlayerIO()
{
	return this;
}

void DirectoryLevelStorage::closeAll()
{
}

ConsoleSavePath DirectoryLevelStorage::getDataFile(const wstring& id)
{
	return ConsoleSavePath( dataDir.getName() + id + L".dat" );
}

wstring DirectoryLevelStorage::getLevelId()
{
	return levelId;
}

void DirectoryLevelStorage::flushSaveFile(bool autosave)
{
#ifndef _CONTENT_PACKAGE
	if(app.DebugSettingsOn() && app.GetGameSettingsDebugMask(ProfileManager.GetPrimaryPad())&(1L<<eDebugSetting_DistributableSave))
	{
		// Delete gamerules files if it exists
		const ConsoleSavePath gameRulesFiles(GAME_RULE_SAVENAME);
		if(m_saveFile->doesFileExist(gameRulesFiles))
		{
			FileEntry *fe = m_saveFile->createFile(gameRulesFiles);
			m_saveFile->deleteFile( fe );
		}
	}
#endif
	m_saveFile->Flush(autosave);
}

// 4J Added
void DirectoryLevelStorage::resetNetherPlayerPositions()
{
	if(app.GetResetNether())
	{
#if defined(__PS3__) || defined(__ORBIS__) || defined(__PSVITA__)
		vector<FileEntry *> *playerFiles = m_saveFile->getValidPlayerDatFiles();
#else
		const vector<FileEntry *> *playerFiles = m_saveFile->getFilesWithPrefix( playerDir.getName() );
#endif

		if ( playerFiles )
		{
			for ( FileEntry * realFile : *playerFiles )
			{
				ConsoleSaveFileInputStream fis = ConsoleSaveFileInputStream(m_saveFile, realFile);
				CompoundTag *tag = NbtIo::readCompressed(&fis);
				if (tag != nullptr)
				{
					// If the player is in the nether, set their y position above the top of the nether
					// This will force the player to be spawned in a valid position in the overworld when they are loaded
					if(tag->contains(L"Dimension") && tag->getInt(L"Dimension") == LevelData::DIMENSION_NETHER && tag->contains(L"Pos"))
					{
						ListTag<DoubleTag> *pos = (ListTag<DoubleTag> *) tag->getList(L"Pos");
						pos->get(1)->data = DBL_MAX;

						ConsoleSaveFileOutputStream fos = ConsoleSaveFileOutputStream( m_saveFile, realFile );
						NbtIo::writeCompressed(tag, &fos);
					}
					delete tag;
				}
			}
			delete playerFiles;
		}
	}
}

int DirectoryLevelStorage::getAuxValueForMap(PlayerUID xuid, int dimension, int centreXC, int centreZC, int scale)
{
	int mapId = -1;
	bool foundMapping = false;

#ifdef _LARGE_WORLDS
	const auto it = m_playerMappings.find(xuid);
	if(it != m_playerMappings.end())
	{
		foundMapping = it->second.getMapping(mapId, centreXC, centreZC, dimension, scale);
	}

	if(!foundMapping)
	{
		for(unsigned int i = 0; i < m_usedMappings.length; ++i)
		{
			if(m_usedMappings[i] < 0xFF)
			{
				unsigned int offset = 0;
				for(; offset < 8; ++offset)
				{
					if( !(m_usedMappings[i] & (1<<offset)) )
					{
						break;
					}
				}
				mapId = (i*8) + offset;
				m_playerMappings[xuid].addMapping(mapId, centreXC, centreZC, dimension, scale);
				m_usedMappings[i] |= (1<<offset);
				break;
			}
		}
	}
#else
	for(unsigned int i = 0; i < MAXIMUM_MAP_SAVE_DATA; ++i)
	{
		if(m_mapDataMappings.xuids[i] == xuid && m_mapDataMappings.getDimension(i) == dimension)
		{
			foundMapping = true;
			mapId = i;
			break;
		}
		if( mapId < 0 && m_mapDataMappings.xuids[i] == INVALID_XUID )
		{
			mapId = i;
		}
	}
	if( !foundMapping && mapId >= 0 && mapId < MAXIMUM_MAP_SAVE_DATA )
	{
		m_mapDataMappings.setMapping(mapId, xuid, dimension);
		m_saveableMapDataMappings.setMapping(mapId, xuid, dimension);

		// If we had an old map file for a mapping that is no longer valid, delete it
		std::wstring id = wstring( L"map_" ) + std::to_wstring(mapId);
		ConsoleSavePath file = getDataFile(id);

		if(m_saveFile->doesFileExist(file) )
		{
			auto it = find(m_mapFilesToDelete.begin(), m_mapFilesToDelete.end(), mapId);
			if(it != m_mapFilesToDelete.end()) m_mapFilesToDelete.erase(it);

			m_saveFile->deleteFile( m_saveFile->createFile(file) );
		}
	}
#endif
	return mapId;
}

void DirectoryLevelStorage::saveMapIdLookup()
{
	if(StorageManager.GetSaveDisabled() ) return;

#ifdef _LARGE_WORLDS
	const ConsoleSavePath file = getDataFile(L"largeMapDataMappings");
#else
	ConsoleSavePath file = getDataFile(L"mapDataMappings");
#endif

	if (!file.getName().empty())
	{
		DWORD NumberOfBytesWritten;
		FileEntry *fileEntry = m_saveFile->createFile(file);
		m_saveFile->setFilePointer(fileEntry,0,nullptr, FILE_BEGIN);

#ifdef _LARGE_WORLDS
		ByteArrayOutputStream baos;
		DataOutputStream dos(&baos);
		dos.writeInt(m_playerMappings.size());
		app.DebugPrintf("Saving %d mappings\n", m_playerMappings.size());
		for ( auto& it : m_playerMappings )
		{
#ifdef _WINDOWS64
			app.DebugPrintf("  -- %d\n", it.first);
#else
			app.DebugPrintf("  -- %ls\n", it.first.toString().c_str());
#endif
			dos.writePlayerUID(it.first);
			it.second.writeMappings(&dos);
		}
		dos.write(m_usedMappings);
		m_saveFile->writeFile(	fileEntry,
			baos.buf.data, // data buffer
			baos.size(), // number of bytes to write
			&NumberOfBytesWritten // number of bytes written
			);
#else
		m_saveFile->writeFile(	fileEntry,
			&m_saveableMapDataMappings, // data buffer
			sizeof(MapDataMappings), // number of bytes to write
			&NumberOfBytesWritten // number of bytes written
			);
		assert( NumberOfBytesWritten == sizeof(MapDataMappings) );
#endif
	}
}

void DirectoryLevelStorage::dontSaveMapMappingForPlayer(PlayerUID xuid)
{
#ifdef _LARGE_WORLDS
	const auto it = m_playerMappings.find(xuid);
	if(it != m_playerMappings.end())
	{
		for (auto itMap = it->second.m_mappings.begin(); itMap != it->second.m_mappings.end(); ++itMap)
		{
			const int index = itMap->second / 8;
			const int offset = itMap->second % 8;
			m_usedMappings[index] &= ~(1<<offset);
		}
		m_playerMappings.erase(it);
	}
#else
	for(unsigned int i = 0; i < MAXIMUM_MAP_SAVE_DATA; ++i)
	{
		if(m_saveableMapDataMappings.xuids[i] == xuid)
		{
			m_saveableMapDataMappings.setMapping(i,INVALID_XUID,0);
		}
	}
#endif
}

void DirectoryLevelStorage::deleteMapFilesForPlayer(shared_ptr<Player> player)
{
	const PlayerUID playerXuid = player->getXuid();
	if(playerXuid != INVALID_XUID) deleteMapFilesForPlayer(playerXuid);
}

void DirectoryLevelStorage::deleteMapFilesForPlayer(PlayerUID xuid)
{
#ifdef _LARGE_WORLDS
	const auto it = m_playerMappings.find(xuid);
	if(it != m_playerMappings.end())
	{
		for (auto itMap = it->second.m_mappings.begin(); itMap != it->second.m_mappings.end(); ++itMap)
		{
			std::wstring id = wstring( L"map_" ) + std::to_wstring(itMap->second);
			ConsoleSavePath file = getDataFile(id);

			if(m_saveFile->doesFileExist(file) )
			{
				// If we can't actually delete this file, store the name so we can delete it later
				if(StorageManager.GetSaveDisabled()) m_mapFilesToDelete.push_back(itMap->second);
				else m_saveFile->deleteFile( m_saveFile->createFile(file) );
			}

			const int index = itMap->second / 8;
			const int offset = itMap->second % 8;
			m_usedMappings[index] &= ~(1<<offset);
		}
		m_playerMappings.erase(it);
	}
#else
	bool changed = false;
	for(unsigned int i = 0; i < MAXIMUM_MAP_SAVE_DATA; ++i)
	{
		if(m_mapDataMappings.xuids[i] == xuid)
		{
			changed = true;

			std::wstring id = wstring( L"map_" ) + std::to_wstring(i);
			ConsoleSavePath file = getDataFile(id);

			if(m_saveFile->doesFileExist(file) )
			{
				// If we can't actually delete this file, store the name so we can delete it later
				if(StorageManager.GetSaveDisabled()) m_mapFilesToDelete.push_back(i);
				else m_saveFile->deleteFile( m_saveFile->createFile(file) );
			}
			m_mapDataMappings.setMapping(i,INVALID_XUID,0);
			m_saveableMapDataMappings.setMapping(i,INVALID_XUID,0);
			break;
		}
	}
#endif
}

void DirectoryLevelStorage::saveAllCachedData()
{
	if(StorageManager.GetSaveDisabled() ) return;

	// Save any files that were saved while saving was disabled
	for ( auto& it : m_cachedSaveData )
	{
		ByteArrayOutputStream *bos = it.second;

		ConsoleSavePath realFile = ConsoleSavePath( it.first );
		ConsoleSaveFileOutputStream fos = ConsoleSaveFileOutputStream( m_saveFile, realFile );

		app.DebugPrintf("Actually writing cached file %ls\n",it.first.c_str() );
		fos.write(bos->buf, 0, bos->size() );
		delete bos;
	}
	m_cachedSaveData.clear();

	for (const auto& it : m_mapFilesToDelete )
	{
		std::wstring id = wstring( L"map_" ) + std::to_wstring(it);
		ConsoleSavePath file = getDataFile(id);
		if(m_saveFile->doesFileExist(file) )
		{
			m_saveFile->deleteFile( m_saveFile->createFile(file) );
		}
	}
	m_mapFilesToDelete.clear();
}