|
|
|
@ -42,153 +42,19 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
|
|
|
|
|
|
|
|
|
#include "plResPatcher.h" |
|
|
|
|
#include "hsResMgr.h" |
|
|
|
|
#include "plgDispatch.h" |
|
|
|
|
|
|
|
|
|
#include "plAgeLoader/plAgeLoader.h" |
|
|
|
|
#include "plCompression/plZlibStream.h" |
|
|
|
|
#include "pnEncryption/plChecksum.h" |
|
|
|
|
#include "plMessage/plResPatcherMsg.h" |
|
|
|
|
#include "pnNetBase/pnNbError.h" |
|
|
|
|
#include "plNetGameLib/plNetGameLib.h" |
|
|
|
|
#include "pfPatcher/pfPatcher.h" |
|
|
|
|
#include "plProgressMgr/plProgressMgr.h" |
|
|
|
|
#include "plResMgr/plResManager.h" |
|
|
|
|
#include "plStatusLog/plStatusLog.h" |
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
class plResDownloadStream : public plZlibStream |
|
|
|
|
{ |
|
|
|
|
plOperationProgress* fProgress; |
|
|
|
|
plFileName fFilename; |
|
|
|
|
bool fIsZipped; |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
plResDownloadStream(plOperationProgress* prog, const plFileName& reqFile) |
|
|
|
|
: fProgress(prog) |
|
|
|
|
{
|
|
|
|
|
fIsZipped = reqFile.GetFileExt().CompareI("gz") == 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
virtual bool Open(const plFileName& filename, const char* mode) |
|
|
|
|
{ |
|
|
|
|
fFilename = filename; |
|
|
|
|
return plZlibStream::Open(filename, mode); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
virtual uint32_t Write(uint32_t count, const void* buf) |
|
|
|
|
{ |
|
|
|
|
fProgress->Increment((float)count); |
|
|
|
|
if (fIsZipped) |
|
|
|
|
return plZlibStream::Write(count, buf); |
|
|
|
|
else |
|
|
|
|
return fOutput->Write(count, buf); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
plFileName GetFileName() const { return fFilename; } |
|
|
|
|
bool IsZipped() const { return fIsZipped; } |
|
|
|
|
void Unlink() const { plFileSystem::Unlink(fFilename); } |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static void FileDownloaded( |
|
|
|
|
ENetError result, |
|
|
|
|
void* param, |
|
|
|
|
const plFileName & filename, |
|
|
|
|
hsStream* writer) |
|
|
|
|
{ |
|
|
|
|
plResPatcher* patcher = (plResPatcher*)param; |
|
|
|
|
plFileName file = filename; |
|
|
|
|
if (((plResDownloadStream*)writer)->IsZipped()) |
|
|
|
|
file = file.StripFileExt(); // Kill off .gz
|
|
|
|
|
writer->Close(); |
|
|
|
|
|
|
|
|
|
switch (result) |
|
|
|
|
{ |
|
|
|
|
case kNetSuccess: |
|
|
|
|
{ |
|
|
|
|
PatcherLog(kStatus, " Download Complete: %s", file.AsString().c_str()); |
|
|
|
|
|
|
|
|
|
// If this is a PRP, then we need to add it to the ResManager
|
|
|
|
|
plFileName clientPath = static_cast<plResDownloadStream*>(writer)->GetFileName(); |
|
|
|
|
if (clientPath.GetFileExt().CompareI("prp") == 0) |
|
|
|
|
{ |
|
|
|
|
plResManager* clientResMgr = static_cast<plResManager*>(hsgResMgr::ResMgr()); |
|
|
|
|
clientResMgr->AddSinglePage(clientPath); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Continue down the warpath
|
|
|
|
|
patcher->IssueRequest(); |
|
|
|
|
delete writer; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
case kNetErrFileNotFound: |
|
|
|
|
PatcherLog(kError, " Download Failed: %s not found", file.AsString().c_str()); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
char* error = hsWStringToString(NetErrorToString(result)); |
|
|
|
|
PatcherLog(kError, " Download Failed: %s", error); |
|
|
|
|
delete[] error; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Failure case
|
|
|
|
|
static_cast<plResDownloadStream*>(writer)->Unlink(); |
|
|
|
|
patcher->Finish(false); |
|
|
|
|
delete writer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void ManifestDownloaded( |
|
|
|
|
ENetError result, |
|
|
|
|
void* param, |
|
|
|
|
const wchar_t group[], |
|
|
|
|
const NetCliFileManifestEntry manifest[], |
|
|
|
|
uint32_t entryCount) |
|
|
|
|
{ |
|
|
|
|
plResPatcher* patcher = (plResPatcher*)param; |
|
|
|
|
plString name = plString::FromWchar(group); |
|
|
|
|
if (IS_NET_SUCCESS(result)) |
|
|
|
|
PatcherLog(kInfo, " Downloaded manifest %s", name.c_str()); |
|
|
|
|
else { |
|
|
|
|
PatcherLog(kError, " Failed to download manifest %s", name.c_str()); |
|
|
|
|
patcher->Finish(false); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < entryCount; ++i) |
|
|
|
|
{ |
|
|
|
|
const NetCliFileManifestEntry mfs = manifest[i]; |
|
|
|
|
plFileName fileName = plString::FromWchar(mfs.clientName); |
|
|
|
|
plFileName downloadName = plString::FromWchar(mfs.downloadName); |
|
|
|
|
|
|
|
|
|
// See if the files are the same
|
|
|
|
|
// 1. Check file size before we do time consuming md5 operations
|
|
|
|
|
// 2. Do wasteful md5. We should consider implementing a CRC instead.
|
|
|
|
|
if (plFileInfo(fileName).FileSize() == mfs.fileSize) |
|
|
|
|
{ |
|
|
|
|
plMD5Checksum cliMD5(fileName); |
|
|
|
|
plMD5Checksum srvMD5; |
|
|
|
|
srvMD5.SetFromHexString(plString::FromWchar(mfs.md5, 32).c_str()); |
|
|
|
|
|
|
|
|
|
if (cliMD5 == srvMD5) |
|
|
|
|
continue; |
|
|
|
|
else |
|
|
|
|
PatcherLog(kInfo, " Enqueueing %s: MD5 Checksums Differ", fileName.AsString().c_str()); |
|
|
|
|
} else |
|
|
|
|
PatcherLog(kInfo, " Enqueueing %s: File Sizes Differ", fileName.AsString().c_str()); |
|
|
|
|
|
|
|
|
|
// If we're still here, then we need to update the file.
|
|
|
|
|
float size = mfs.zipSize ? (float)mfs.zipSize : (float)mfs.fileSize; |
|
|
|
|
patcher->GetProgress()->SetLength(size + patcher->GetProgress()->GetMax()); |
|
|
|
|
patcher->RequestFile(downloadName, fileName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
patcher->IssueRequest(); |
|
|
|
|
} |
|
|
|
|
extern bool gDataServerLocal; |
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static char* sLastError = nil; |
|
|
|
|
plResPatcher* plResPatcher::fInstance = nil; |
|
|
|
|
plResPatcher* plResPatcher::fInstance = nullptr; |
|
|
|
|
|
|
|
|
|
plResPatcher* plResPatcher::GetInstance() |
|
|
|
|
{ |
|
|
|
@ -199,134 +65,102 @@ plResPatcher* plResPatcher::GetInstance()
|
|
|
|
|
|
|
|
|
|
void plResPatcher::Shutdown() |
|
|
|
|
{ |
|
|
|
|
// Better not call this while we're patching
|
|
|
|
|
delete fInstance; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
plResPatcher::plResPatcher() |
|
|
|
|
: fPatching(false), fProgress(nil) { } |
|
|
|
|
|
|
|
|
|
plResPatcher::~plResPatcher() |
|
|
|
|
void plResPatcher::OnCompletion(ENetError result, const plString& status) |
|
|
|
|
{ |
|
|
|
|
if (fProgress) |
|
|
|
|
delete fProgress; |
|
|
|
|
plString error = plString::Null; |
|
|
|
|
if (IS_NET_ERROR(result)) |
|
|
|
|
error = plString::Format("Update Failed: %S\n%s", NetErrorAsString(result), status.c_str()); |
|
|
|
|
plgDispatch::Dispatch()->MsgQueue(new plResPatcherMsg(IS_NET_SUCCESS(result), error)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void plResPatcher::IssueRequest() |
|
|
|
|
void plResPatcher::OnFileDownloadBegin(const plFileName& file) |
|
|
|
|
{ |
|
|
|
|
if (!fPatching) return; |
|
|
|
|
if (fRequests.empty()) |
|
|
|
|
// Wheee!
|
|
|
|
|
Finish(); |
|
|
|
|
else { |
|
|
|
|
Request req = fRequests.front(); |
|
|
|
|
fRequests.pop(); |
|
|
|
|
|
|
|
|
|
plString title; |
|
|
|
|
if (req.fType == kManifest) |
|
|
|
|
{ |
|
|
|
|
PatcherLog(kMajorStatus, " Downloading manifest... %s", req.fFile.AsString().c_str()); |
|
|
|
|
title = plString::Format("Checking %s for updates...", req.fFile.AsString().c_str()); |
|
|
|
|
NetCliFileManifestRequest(ManifestDownloaded, this, req.fFile.AsString().ToWchar()); |
|
|
|
|
} else if (req.fType == kFile) { |
|
|
|
|
PatcherLog(kMajorStatus, " Downloading file... %s", req.fFriendlyName.AsString().c_str()); |
|
|
|
|
title = plString::Format("Downloading... %s", req.fFriendlyName.GetFileName().c_str()); |
|
|
|
|
fProgress->SetTitle(plString::Format("Downloading %s...", file.GetFileName().c_str())); |
|
|
|
|
|
|
|
|
|
// If this is a PRP, we need to unload it from the ResManager
|
|
|
|
|
|
|
|
|
|
if (req.fFriendlyName.GetFileExt().CompareI("prp") == 0) |
|
|
|
|
((plResManager*)hsgResMgr::ResMgr())->RemoveSinglePage(req.fFriendlyName); |
|
|
|
|
|
|
|
|
|
plFileSystem::CreateDir(req.fFriendlyName.StripFileName(), true); |
|
|
|
|
plResDownloadStream* stream = new plResDownloadStream(fProgress, req.fFile); |
|
|
|
|
if (stream->Open(req.fFriendlyName, "wb")) |
|
|
|
|
NetCliFileDownloadRequest(req.fFile, stream, FileDownloaded, this); |
|
|
|
|
else { |
|
|
|
|
PatcherLog(kError, " Unable to create file %s", req.fFriendlyName.AsString().c_str()); |
|
|
|
|
Finish(false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fProgress->SetTitle(title); |
|
|
|
|
if (file.GetFileExt().CompareI("prp") == 0) { |
|
|
|
|
plResManager* mgr = static_cast<plResManager*>(hsgResMgr::ResMgr()); |
|
|
|
|
if (mgr) |
|
|
|
|
mgr->RemoveSinglePage(file); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void plResPatcher::Finish(bool success) |
|
|
|
|
void plResPatcher::OnFileDownloaded(const plFileName& file) |
|
|
|
|
{ |
|
|
|
|
while (fRequests.size()) |
|
|
|
|
fRequests.pop(); |
|
|
|
|
|
|
|
|
|
fPatching = false; |
|
|
|
|
if (success) |
|
|
|
|
PatcherLog(kHeader, "--- Patch Completed Successfully ---"); |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
PatcherLog(kHeader, "--- Patch Killed by Error ---"); |
|
|
|
|
if (fProgress) |
|
|
|
|
fProgress->SetAborting(); |
|
|
|
|
if (file.GetFileExt().CompareI("prp") == 0) { |
|
|
|
|
plResManager* mgr = static_cast<plResManager*>(hsgResMgr::ResMgr()); |
|
|
|
|
if (mgr) |
|
|
|
|
mgr->AddSinglePage(file); |
|
|
|
|
} |
|
|
|
|
delete fProgress; fProgress = nil; |
|
|
|
|
|
|
|
|
|
plResPatcherMsg* pMsg = new plResPatcherMsg(success, sLastError); |
|
|
|
|
delete[] sLastError; sLastError = nil; |
|
|
|
|
pMsg->Send(); // whoosh... off it goes
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void plResPatcher::RequestFile(const plFileName& srvName, const plFileName& cliName) |
|
|
|
|
void plResPatcher::OnProgressTick(uint64_t dl, uint64_t total, const plString& msg) |
|
|
|
|
{ |
|
|
|
|
fRequests.push(Request(srvName, kFile, cliName)); |
|
|
|
|
if (dl && total) { |
|
|
|
|
fProgress->SetLength(total); |
|
|
|
|
fProgress->SetHowMuch(dl); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
plString status = plString::Format("%s / %s", |
|
|
|
|
plFileSystem::ConvertFileSize(dl).c_str(), |
|
|
|
|
plFileSystem::ConvertFileSize(total).c_str() |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
fProgress->SetStatusText(status); |
|
|
|
|
fProgress->SetInfoText(msg); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void plResPatcher::RequestManifest(const plString& age) |
|
|
|
|
pfPatcher* plResPatcher::CreatePatcher() |
|
|
|
|
{ |
|
|
|
|
fRequests.push(Request(age, kManifest)); |
|
|
|
|
pfPatcher* patcher = new pfPatcher; |
|
|
|
|
patcher->OnCompletion(std::bind(&plResPatcher::OnCompletion, this, std::placeholders::_1, std::placeholders::_2)); |
|
|
|
|
patcher->OnFileDownloadBegin(std::bind(&plResPatcher::OnFileDownloadBegin, this, std::placeholders::_1)); |
|
|
|
|
patcher->OnFileDownloaded(std::bind(&plResPatcher::OnFileDownloaded, this, std::placeholders::_1)); |
|
|
|
|
patcher->OnProgressTick(std::bind(&plResPatcher::OnProgressTick, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); |
|
|
|
|
return patcher; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void plResPatcher::Start() |
|
|
|
|
void plResPatcher::InitProgress() |
|
|
|
|
{ |
|
|
|
|
hsAssert(!fPatching, "Too many calls to plResPatcher::Start"); |
|
|
|
|
fPatching = true; |
|
|
|
|
PatcherLog(kHeader, "--- Patch Started (%i requests) ---", fRequests.size()); |
|
|
|
|
fProgress = plProgressMgr::GetInstance()->RegisterOperation(0.0, "Checking for updates...", |
|
|
|
|
plProgressMgr::kUpdateText, false, true); |
|
|
|
|
IssueRequest(); |
|
|
|
|
// this is deleted in plAgeLoader::MsgReceive for thread safety
|
|
|
|
|
fProgress = plProgressMgr::GetInstance()->RegisterOperation(0.f, nullptr, plProgressMgr::kUpdateText); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void PatcherLog(PatcherLogType type, const char* format, ...) |
|
|
|
|
plResPatcher::plResPatcher() |
|
|
|
|
: fProgress(nullptr) { } |
|
|
|
|
|
|
|
|
|
plResPatcher::~plResPatcher() |
|
|
|
|
{ |
|
|
|
|
uint32_t color = 0; |
|
|
|
|
switch (type) |
|
|
|
|
{ |
|
|
|
|
case kHeader: color = plStatusLog::kWhite; break; |
|
|
|
|
case kInfo: color = plStatusLog::kBlue; break; |
|
|
|
|
case kMajorStatus: color = plStatusLog::kYellow; break; |
|
|
|
|
case kStatus: color = plStatusLog::kGreen; break; |
|
|
|
|
case kError: color = plStatusLog::kRed; break; |
|
|
|
|
} |
|
|
|
|
delete fProgress; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static plStatusLog* gStatusLog = nil; |
|
|
|
|
if (!gStatusLog) |
|
|
|
|
{ |
|
|
|
|
gStatusLog = plStatusLogMgr::GetInstance().CreateStatusLog( |
|
|
|
|
20, |
|
|
|
|
"patcher.log", |
|
|
|
|
plStatusLog::kFilledBackground | plStatusLog::kAlignToTop | plStatusLog::kDeleteForMe); |
|
|
|
|
void plResPatcher::Update(const std::vector<plString>& manifests) |
|
|
|
|
{ |
|
|
|
|
if (gDataServerLocal) |
|
|
|
|
plgDispatch::Dispatch()->MsgSend(new plResPatcherMsg()); |
|
|
|
|
else { |
|
|
|
|
InitProgress(); |
|
|
|
|
pfPatcher* patcher = CreatePatcher(); |
|
|
|
|
patcher->RequestManifest(manifests); |
|
|
|
|
patcher->Start(); // whoosh... off it goes
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
va_list args; |
|
|
|
|
va_start(args, format); |
|
|
|
|
|
|
|
|
|
if (type == kError) |
|
|
|
|
{ |
|
|
|
|
sLastError = new char[1024]; // Deleted by Finish(false)
|
|
|
|
|
vsnprintf(sLastError, 1024, format, args); |
|
|
|
|
gStatusLog->AddLine(sLastError, color); |
|
|
|
|
} else |
|
|
|
|
gStatusLog->AddLineV(color, format, args); |
|
|
|
|
|
|
|
|
|
va_end(args); |
|
|
|
|
void plResPatcher::Update(const plString& manifest) |
|
|
|
|
{ |
|
|
|
|
if (gDataServerLocal) |
|
|
|
|
plgDispatch::Dispatch()->MsgSend(new plResPatcherMsg()); |
|
|
|
|
else { |
|
|
|
|
InitProgress(); |
|
|
|
|
pfPatcher* patcher = CreatePatcher(); |
|
|
|
|
patcher->RequestManifest(manifest); |
|
|
|
|
patcher->Start(); // whoosh... off it goes
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|