22#include <boost/asio/streambuf.hpp>
23#include <boost/asio/read.hpp>
24#include <boost/asio/read_until.hpp>
25#include <boost/asio/write.hpp>
26#include <boost/asio/ssl/stream.hpp>
27#include <boost/filesystem/operations.hpp>
34 case ERROR_SUCCESS:
return "SUCCESS";
35 case ERROR_FILE_CORRUPT:
return "FILE_CORRUPT";
36 case ERROR_CAN_NOT_COMPLETE:
return "CAN_NOT_COMPLETE";
37 case ERROR_HANDLE_EOF:
return "HANDLE_EOF";
38 case ERROR_NO_MORE_FILES:
return "NO_MORE_FILES";
39 case ERROR_BAD_FORMAT:
return "BAD_FORMAT";
40 case ERROR_INSUFFICIENT_BUFFER:
return "INSUFFICIENT_BUFFER";
41 case ERROR_ALREADY_EXISTS:
return "ALREADY_EXISTS";
42 case ERROR_DISK_FULL:
return "DISK_FULL";
43 case ERROR_INVALID_PARAMETER:
return "INVALID_PARAMETER";
44 case ERROR_NOT_SUPPORTED:
return "NOT_SUPPORTED";
45 case ERROR_NOT_ENOUGH_MEMORY:
return "NOT_ENOUGH_MEMORY";
46 case ERROR_INVALID_HANDLE:
return "INVALID_HANDLE";
47 case ERROR_ACCESS_DENIED:
return "ACCESS_DENIED";
48 case ERROR_FILE_NOT_FOUND:
return "FILE_NOT_FOUND";
49 case ERROR_FILE_ENCRYPTED:
return "FILE_ENCRYPTED";
50 case ERROR_FILE_OFFLINE:
return "FILE_OFFLINE";
51 default:
return "UNKNOWN";
59 boost::system::error_code error;
61 boost::asio::ssl::context sslContext(boost::asio::ssl::context::sslv23);
62 sslContext.set_options(boost::asio::ssl::context::no_sslv2, error);
63 sslContext.set_options(boost::asio::ssl::context::no_sslv3, error);
64 sslContext.set_options(boost::asio::ssl::context::no_tlsv1, error);
65 sslContext.set_options(boost::asio::ssl::context::no_tlsv1_1, error);
66 sslContext.set_default_verify_paths(error);
74 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(ioContext, sslContext);
75 socket.set_verify_mode(boost::asio::ssl::verify_none, error);
79 socket.lowest_layer().connect(*endpoint, error);
83 if (!SSL_set_tlsext_host_name(socket.native_handle(), serverName.c_str()))
86 socket.handshake(boost::asio::ssl::stream_base::client, error);
90 boost::asio::streambuf request;
91 std::ostream request_stream(&request);
93 request_stream <<
"GET " << getCommand <<
" HTTP/1.0\r\n";
94 request_stream <<
"Host: " << serverName <<
"\r\n";
95 request_stream <<
"Connection: close\r\n\r\n";
98 boost::asio::write(socket, request);
101 boost::asio::streambuf response;
102 boost::asio::read_until(socket, response,
"\r\n", error);
105 printf(
"Downloading tact key list failed to read HTTP response status %s", error.message().c_str());
110 std::string http_version;
112 std::string status_message;
113 std::istream response_stream(&response);
115 response_stream >> http_version;
116 response_stream >> status_code;
117 std::getline(response_stream, status_message);
119 if (status_code != 200)
121 printf(
"Downloading tact key list failed with server response %u %s", status_code, status_message.c_str());
126 boost::asio::read_until(socket, response,
"\r\n\r\n");
129 printf(
"Downloading tact key list failed to read HTTP response headers %s", error.message().c_str());
135 while (std::getline(response_stream, header) && header !=
"\r")
139 std::stringstream rawBody;
142 if (response.size() > 0)
143 rawBody << &response;
146 while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
147 rawBody << &response;
149 return rawBody.str();
153 bool GetStorageInfo(HANDLE storage, CASC_STORAGE_INFO_CLASS storageInfoClass, T* value)
155 size_t infoDataSizeNeeded = 0;
156 return ::CascGetStorageInfo(storage, storageInfoClass, value,
sizeof(T), &infoDataSizeNeeded);
162using CASCCharType = std::remove_const_t<std::remove_pointer_t<
decltype(CASC_OPEN_STORAGE_ARGS::szLocalPath)>>;
172 static Optional<std::string> const tactKeys = DownloadFile(
"raw.githubusercontent.com", 443,
"/wowdev/TACTKeys/master/WoW.txt");
174 return tactKeys && CascImportKeysFromString(
_handle, tactKeys->c_str());
186 CASC_OPEN_STORAGE_ARGS args = {};
187 args.Size =
sizeof(CASC_OPEN_STORAGE_ARGS);
188 args.szLocalPath = strPath.c_str();
189 args.szCodeName = strProduct.c_str();
190 args.dwLocaleMask = localeMask;
191 HANDLE handle =
nullptr;
192 if (!CascOpenStorageEx(
nullptr, &args,
false, &handle))
194 DWORD lastError = GetCascError();
195 printf(
"Error opening casc storage '%s': %s\n", path.string().c_str(),
HumanReadableCASCError(lastError));
196 CascCloseStorage(handle);
197 SetCascError(lastError);
201 printf(
"Opened casc storage '%s'\n", path.string().c_str());
205 printf(
"Failed to load additional online encryption keys, some files might not be extracted.\n");
215 CASC_OPEN_STORAGE_ARGS args = {};
216 args.Size =
sizeof(CASC_OPEN_STORAGE_ARGS);
217 args.szLocalPath = strPath.c_str();
218 args.szCodeName = strProduct.c_str();
219 args.szRegion = strRegion.c_str();
220 args.dwLocaleMask = localeMask;
222 HANDLE handle =
nullptr;
223 if (!::CascOpenStorageEx(
nullptr, &args,
true, &handle))
225 DWORD lastError = GetCascError();
227 CascCloseStorage(handle);
228 SetCascError(lastError);
233 if (!GetStorageInfo(handle, CascStorageFeatures, &features) || !(features & CASC_FEATURE_ONLINE))
235 printf(
"Local casc storage detected in cache path \"%s\" (or its parent directory). Remote storage not opened!\n", path.string().c_str());
236 CascCloseStorage(handle);
237 SetCascError(ERROR_FILE_OFFLINE);
241 printf(
"Opened remote casc storage '%s'\n", path.string().c_str());
245 printf(
"Failed to load additional online encryption keys, some files might not be extracted.\n");
252 CASC_STORAGE_PRODUCT product;
253 if (GetStorageInfo(
_handle, CascStorageProduct, &product))
254 return product.BuildNumber;
262 if (GetStorageInfo(
_handle, CascStorageInstalledLocales, &locales))
270 return CascFindEncryptionKey(
_handle, keyLookup) !=
nullptr;
275 DWORD openFlags = CASC_OPEN_BY_NAME;
276 if (zerofillEncryptedParts)
277 openFlags |= CASC_OVERCOME_ENCRYPTED;
279 HANDLE handle =
nullptr;
280 if (!::CascOpenFile(
_handle, fileName, localeMask, openFlags, &handle))
282 DWORD lastError = GetCascError();
284 fprintf(stderr,
"Failed to open '%s' in CASC storage: %s\n", fileName,
HumanReadableCASCError(lastError));
286 CascCloseFile(handle);
287 SetCascError(lastError);
291 return new File(handle);
296 DWORD openFlags = CASC_OPEN_BY_FILEID;
297 if (zerofillEncryptedParts)
298 openFlags |= CASC_OVERCOME_ENCRYPTED;
300 HANDLE handle =
nullptr;
301 if (!::CascOpenFile(
_handle, CASC_FILE_DATA_ID(fileDataId), localeMask, openFlags, &handle))
303 DWORD lastError = GetCascError();
305 fprintf(stderr,
"Failed to open 'FileDataId %u' in CASC storage: %s\n", fileDataId,
HumanReadableCASCError(lastError));
307 CascCloseFile(handle);
308 SetCascError(lastError);
312 return new File(handle);
326 CASC_FILE_FULL_INFO info;
327 if (!::CascGetFileInfo(
_handle, CascFileFullInfo, &info,
sizeof(info),
nullptr))
328 return CASC_INVALID_ID;
330 return info.FileDataId;
336 if (!::CascGetFileSize64(
_handle, &size))
345 if (!::CascSetFilePointer64(
_handle, 0, &position, FILE_CURRENT))
348 return int64(position);
354 memcpy(parts, &position,
sizeof(parts));
355 return ::CascSetFilePointer64(
_handle, position,
nullptr, FILE_BEGIN);
360 DWORD bytesReadDWORD;
361 if (!::CascReadFile(
_handle, buffer, bytes, &bytesReadDWORD))
365 *bytesRead = bytesReadDWORD;
std::optional< T > Optional
Optional helper class to wrap optional values within.
bool SetPointer(int64 position)
bool ReadFile(void *buffer, uint32 bytes, uint32 *bytesRead)
static Storage * Open(boost::filesystem::path const &path, uint32 localeMask, char const *product)
static Storage * OpenRemote(boost::filesystem::path const &path, uint32 localeMask, char const *product, char const *region)
File * OpenFile(char const *fileName, uint32 localeMask, bool printErrors=false, bool zerofillEncryptedParts=false) const
bool HasTactKey(uint64 keyLookup) const
uint32 GetBuildNumber() const
uint32 GetInstalledLocalesMask() const
bool LoadOnlineTactKeys()
std::remove_const_t< std::remove_pointer_t< decltype(CASC_OPEN_STORAGE_ARGS::szLocalPath)> > CASCCharType
char const * HumanReadableCASCError(uint32 error)
std::basic_string< CASCCharType > CASCStringType