aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/ConsoleSaveFileConverter.cpp
blob: 9a3d572d34f4047c23e5c9e8b6264b9d019e8842 (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
#include "stdafx.h"
#include "net.minecraft.world.level.chunk.storage.h"
#include "net.minecraft.world.level.storage.h"
#include "ConsoleSaveFileIO.h"
#include "ConsoleSaveFileConverter.h"
#include "ProgressListener.h"

void ConsoleSaveFileConverter::ProcessSimpleFile(ConsoleSaveFile *sourceSave, FileEntry *sourceFileEntry, ConsoleSaveFile *targetSave, FileEntry *targetFileEntry)
{
	DWORD numberOfBytesRead = 0;
	DWORD numberOfBytesWritten = 0;

	byte *data = new byte[sourceFileEntry->getFileSize()];

	// Read from source
	sourceSave->readFile(sourceFileEntry, data, sourceFileEntry->getFileSize(), &numberOfBytesRead);

	// Write back to target
	targetSave->writeFile(targetFileEntry, data, numberOfBytesRead, &numberOfBytesWritten);

	delete [] data;
}

void ConsoleSaveFileConverter::ProcessStandardRegionFile(ConsoleSaveFile *sourceSave, File sourceFile, ConsoleSaveFile *targetSave, File targetFile)
{
	DWORD numberOfBytesWritten = 0;
	DWORD numberOfBytesRead = 0;

	RegionFile sourceRegionFile(sourceSave, &sourceFile);
	RegionFile targetRegionFile(targetSave, &targetFile);

	for(unsigned int x = 0; x < 32; ++x)
	{
		for(unsigned int z = 0; z < 32; ++z)
		{
			DataInputStream *dis = sourceRegionFile.getChunkDataInputStream(x,z);

			if(dis)
			{
				int read = dis->read();
				DataOutputStream *dos = targetRegionFile.getChunkDataOutputStream(x,z);
				while(read != -1)
				{

					dos->write( read & 0xff );

					read = dis->read();
				}
				dos->close();
				dos->deleteChildStream();
				delete dos;
			}

			delete dis;
		}
	}
}

void ConsoleSaveFileConverter::ConvertSave(ConsoleSaveFile *sourceSave, ConsoleSaveFile *targetSave, ProgressListener *progress)
{
	// Process level.dat
	ConsoleSavePath ldatPath( wstring(L"level.dat") );
	FileEntry *sourceLdatFe = sourceSave->createFile( ldatPath );
	FileEntry *targetLdatFe = targetSave->createFile( ldatPath );
	app.DebugPrintf("Processing level.dat\n");
	ProcessSimpleFile(sourceSave, sourceLdatFe, targetSave, targetLdatFe);

	// Process game rules
	{
		ConsoleSavePath gameRulesPath( GAME_RULE_SAVENAME );
		if(sourceSave->doesFileExist(gameRulesPath) )
		{
			FileEntry *sourceFe = sourceSave->createFile( gameRulesPath );
			FileEntry *targetFe = targetSave->createFile( gameRulesPath );
			app.DebugPrintf("Processing game rules\n");
			ProcessSimpleFile(sourceSave, sourceFe, targetSave, targetFe);
		}
	}

	// MGH added - find any player data files and copy them across
#if defined(__PS3__) || defined(__ORBIS__) || defined(__PSVITA__)
	vector<FileEntry *>* playerFiles = sourceSave->getValidPlayerDatFiles();
#else
	vector<FileEntry *> *playerFiles = sourceSave->getFilesWithPrefix( DirectoryLevelStorage::getPlayerDir() );
#endif

	if(playerFiles != NULL)
	{
		for(int fileIdx = 0; fileIdx < playerFiles->size();fileIdx++)
		{
			ConsoleSavePath sourcePlayerDatPath( playerFiles->at(fileIdx)->data.filename );
#ifdef _XBOX_ONE
			// 4J Stu - As the XUIDs on X360 and X1 are different, we don't want to transfer these over. However as the first player
			// file should be the owner of the save, we can move their data over to the current players XUID
			if(fileIdx > 0) break;
			PlayerUID xuid;
			ProfileManager.GetXUID(ProfileManager.GetPrimaryPad(), &xuid, false);
			ConsoleSavePath targetPlayerDatPath( L"players/" + xuid.toString() + L".dat" );
#else
			ConsoleSavePath targetPlayerDatPath( playerFiles->at(fileIdx)->data.filename );
#endif
			{
				FileEntry *sourceFe = sourceSave->createFile( sourcePlayerDatPath );
				FileEntry *targetFe = targetSave->createFile( targetPlayerDatPath );
				app.DebugPrintf("Processing player dat file %s\n", playerFiles->at(fileIdx)->data.filename);
				ProcessSimpleFile(sourceSave, sourceFe, targetSave, targetFe);

				targetFe->data.lastModifiedTime = sourceFe->data.lastModifiedTime;
			}
		}
		delete playerFiles;
	}


#ifdef SPLIT_SAVES
	int xzSize = LEVEL_LEGACY_WIDTH;
	int hellScale = HELL_LEVEL_LEGACY_SCALE;
	if ( sourceSave->doesFileExist( ldatPath ) ) 
	{
		ConsoleSaveFileInputStream fis = ConsoleSaveFileInputStream(sourceSave, ldatPath);
		CompoundTag *root = NbtIo::readCompressed(&fis);
		CompoundTag *tag = root->getCompound(L"Data");
		LevelData ret(tag);

		xzSize = ret.getXZSize();
		hellScale = ret.getHellScale();

		delete root;
	}

	RegionFileCache sourceCache;
	RegionFileCache targetCache;

	if(progress)
	{
		progress->progressStage(IDS_SAVETRANSFER_STAGE_CONVERTING);
	}

	// Overworld
	{
		app.DebugPrintf("Processing the overworld\n");
		int halfXZSize = xzSize / 2;

		int progressTarget = (xzSize) * (xzSize);
		int currentProgress = 0;
		if(progress) progress->progressStagePercentage((currentProgress*100)/progressTarget);

		for(int x = -halfXZSize; x < halfXZSize; ++x)
		{
			for(int z = -halfXZSize; z < halfXZSize; ++z)
			{
				//app.DebugPrintf("Processing overworld chunk %d,%d\n",x,z);
				DataInputStream *dis = sourceCache._getChunkDataInputStream(sourceSave,L"",x,z);

				if(dis)
				{
					int read = dis->read();
					DataOutputStream *dos = targetCache._getChunkDataOutputStream(targetSave,L"",x,z);
					BufferedOutputStream bos(dos, 1024 * 1024);
					while(read != -1)
					{

						bos.write( read & 0xff );

						read = dis->read();
					}
					bos.flush();
					dos->close();
					dos->deleteChildStream();
					delete dos;
				}

				delete dis;

				++currentProgress;
				if(progress) progress->progressStagePercentage( (currentProgress*100)/progressTarget);

			}
		}
	}

	// Nether
	{
		app.DebugPrintf("Processing the nether\n");
		int hellSize = xzSize / hellScale;
		int halfXZSize = hellSize / 2;

		int progressTarget = (hellSize) * (hellSize);
		int currentProgress = 0;
		if(progress) progress->progressStagePercentage((currentProgress*100)/progressTarget);

		for(int x = -halfXZSize; x < halfXZSize; ++x)
		{
			for(int z = -halfXZSize; z < halfXZSize; ++z)
			{
				//app.DebugPrintf("Processing nether chunk %d,%d\n",x,z);
				DataInputStream *dis = sourceCache._getChunkDataInputStream(sourceSave,L"DIM-1",x,z);

				if(dis)
				{
					int read = dis->read();
					DataOutputStream *dos = targetCache._getChunkDataOutputStream(targetSave,L"DIM-1",x,z);
					BufferedOutputStream bos(dos, 1024 * 1024);
					while(read != -1)
					{

						bos.write( read & 0xff );

						read = dis->read();
					}
					bos.flush();
					dos->close();
					dos->deleteChildStream();
					delete dos;
				}

				delete dis;

				++currentProgress;
				if(progress) progress->progressStagePercentage((currentProgress*100)/progressTarget);
			}
		}
	}

	// End
	{
		app.DebugPrintf("Processing the end\n");
		int halfXZSize = END_LEVEL_MAX_WIDTH / 2;

		int progressTarget = (END_LEVEL_MAX_WIDTH) * (END_LEVEL_MAX_WIDTH);
		int currentProgress = 0;
		if(progress) progress->progressStagePercentage((currentProgress*100)/progressTarget);

		for(int x = -halfXZSize; x < halfXZSize; ++x)
		{
			for(int z = -halfXZSize; z < halfXZSize; ++z)
			{
				//app.DebugPrintf("Processing end chunk %d,%d\n",x,z);
				DataInputStream *dis = sourceCache._getChunkDataInputStream(sourceSave,L"DIM1/",x,z);

				if(dis)
				{
					int read = dis->read();
					DataOutputStream *dos = targetCache._getChunkDataOutputStream(targetSave,L"DIM1/",x,z);
					BufferedOutputStream bos(dos, 1024 * 1024);
					while(read != -1)
					{

						bos.write( read & 0xff );

						read = dis->read();
					}
					bos.flush();
					dos->close();
					dos->deleteChildStream();
					delete dos;
				}

				delete dis;

				++currentProgress;
				if(progress) progress->progressStagePercentage((currentProgress*100)/progressTarget);
			}
		}
	}

#else
	// 4J Stu - Old version that just changes the compression of chunks, not usable for XboxOne style split saves or compressed tile formats
	// Process region files
	vector<FileEntry *> *allFilesInSave = sourceSave->getFilesWithPrefix(wstring(L""));
	for(AUTO_VAR(it, allFilesInSave->begin()); it < allFilesInSave->end(); ++it)
	{
		FileEntry *fe = *it;
		if( fe != sourceLdatFe )
		{
			wstring fName( fe->data.filename );
			wstring suffix(L".mcr");
			if( fName.compare(fName.length() - suffix.length(), suffix.length(), suffix) == 0 )
			{
#ifndef _CONTENT_PACKAGE
				wprintf(L"Processing a region file: %s\n", fe->data.filename);
#endif
				ProcessStandardRegionFile(sourceSave, File(fe->data.filename), targetSave, File(fe->data.filename) );
			}
			else
			{
#ifndef _CONTENT_PACKAGE
				wprintf(L"%s is not a region file, ignoring\n", fe->data.filename);
#endif
			}
		}
	}
#endif
}