aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/ArchiveFile.cpp
blob: 608428b6eb7e3ff82581a827aaf4518cf6348631 (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
#include "stdafx.h"

#include "..\Minecraft.World\StringHelpers.h"
#include "..\Minecraft.World\compression.h"

#include "ArchiveFile.h"

void ArchiveFile::_readHeader(DataInputStream *dis)
{
	int numberOfFiles = dis->readInt();

	for (int i = 0; i < numberOfFiles; i++)
	{
		MetaData *meta = new MetaData();
		meta->filename = dis->readUTF();
		meta->ptr = dis->readInt();
		meta->filesize = dis->readInt();

		// Filenames preceeded by an asterisk have been compressed.
		if (meta->filename[0] == '*')
		{
			meta->filename = meta->filename.substr(1);
			meta->isCompressed = true;
		} 
		else meta->isCompressed = false;

		m_index.insert( pair<wstring,PMetaData>(meta->filename,meta) );
	}
}

ArchiveFile::ArchiveFile(File file)
{
	m_cachedData = nullptr;
	m_sourcefile = file;
	app.DebugPrintf("Loading archive file...\n");
#ifndef _CONTENT_PACKAGE
	char buf[256];
	wcstombs(buf, file.getPath().c_str(), 256);
	app.DebugPrintf("archive file - %s\n",buf);
#endif

	if(!file.exists())
	{
		app.DebugPrintf("Failed to load archive file!\n");//,file.getPath());
		app.FatalLoadError();
	}

	FileInputStream fis(file);

#if defined _XBOX_ONE || defined __ORBIS__ || defined _WINDOWS64
	byteArray readArray(static_cast<unsigned int>(file.length()));
	fis.read(readArray,0,file.length());

	ByteArrayInputStream bais(readArray);
	DataInputStream dis(&bais);

	m_cachedData = readArray.data;
#else
	DataInputStream dis(&fis);
#endif

	_readHeader(&dis);

	dis.close();
	fis.close();
#if defined _XBOX_ONE || defined __ORBIS__ || defined _WINDOWS64
	bais.reset();
#endif
	app.DebugPrintf("Finished loading archive file\n");
}

ArchiveFile::~ArchiveFile()
{
	delete m_cachedData;
}

vector<wstring> *ArchiveFile::getFileList()
{
	vector<wstring> *out = new vector<wstring>();
	
	for ( const auto& it : m_index )
		out->push_back( it.first );

	return out;
}

bool ArchiveFile::hasFile(const wstring &filename)
{
	return m_index.find(filename) != m_index.end();
}

int ArchiveFile::getFileSize(const wstring &filename)
{
	return hasFile(filename) ? m_index.at(filename)->filesize : -1;
}

byteArray ArchiveFile::getFile(const wstring &filename)
{
	byteArray out;
	auto it = m_index.find(filename);

	if(it == m_index.end())
	{
		app.DebugPrintf("Couldn't find file in archive\n");
		app.DebugPrintf("Failed to find file '%ls' in archive\n", filename.c_str());
#ifndef _CONTENT_PACKAGE
		__debugbreak();
#endif
		app.FatalLoadError();
	}
	else
	{
		PMetaData data = it->second;

#if defined _XBOX_ONE || defined __ORBIS__ || defined _WINDOWS64
		out = byteArray(data->filesize );

		memcpy( out.data, m_cachedData + data->ptr, data->filesize );
#else

#ifdef _UNICODE
		HANDLE hfile = CreateFile(	m_sourcefile.getPath().c_str(), 
			GENERIC_READ,
			0,
			nullptr,
			OPEN_EXISTING,
			FILE_ATTRIBUTE_NORMAL,
			nullptr
			);
#else
		app.DebugPrintf("Createfile archive\n");
		HANDLE hfile = CreateFile(	wstringtofilename(m_sourcefile.getPath()), 
			GENERIC_READ,
			0,
			nullptr,
			OPEN_EXISTING,
			FILE_ATTRIBUTE_NORMAL,
			nullptr
			);
#endif

		if (hfile != INVALID_HANDLE_VALUE)
		{
			app.DebugPrintf("hfile ok\n");
			DWORD ok = SetFilePointer(	hfile,
				data->ptr,
				nullptr,
				FILE_BEGIN
				);

			if (ok != INVALID_SET_FILE_POINTER)
			{
				PBYTE pbData = new BYTE[ data->filesize ];

				DWORD bytesRead = -1;
				BOOL bSuccess = ReadFile(	hfile,
					(LPVOID) pbData,
					data->filesize,
					&bytesRead,
					nullptr
					);

				if(bSuccess==FALSE)
				{
					app.FatalLoadError();
				}
				assert(bytesRead == data->filesize);
				out = byteArray(pbData, data->filesize);
			}
			else
			{
				app.FatalLoadError();
			}

			CloseHandle(hfile);
		}
		else
		{
			app.DebugPrintf("bad hfile\n");
			app.FatalLoadError();
		}
#endif

		// Compressed filenames are preceeded with an asterisk.
		if ( data->isCompressed && out.data != nullptr )
		{
			/* 4J-JEV:
			* If a compressed file is accessed before compression object is 
			* initialized it will crash here (Compression::getCompression).
			*/
			///4 279 553 556

			ByteArrayInputStream bais(out);
			DataInputStream dis(&bais);
			unsigned int decompressedSize = dis.readInt();
			dis.close();

			PBYTE uncompressedBuffer = new BYTE[decompressedSize];
			Compression::getCompression()->Decompress(uncompressedBuffer, &decompressedSize, out.data+4, out.length-4);

			delete [] out.data;

			out.data = uncompressedBuffer;
			out.length = decompressedSize;
		}

		assert(out.data != nullptr); // THERE IS NO FILE WITH THIS NAME!

	}

	return out;
}