axmol/extensions/assets-manager/AssetsManager.cpp

641 lines
20 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "AssetsManager.h"
#include <thread>
#include "base/Director.h"
#include "base/Scheduler.h"
#include "base/UserDefault.h"
#include "network/Downloader.h"
#include "platform/FileUtils.h"
2019-11-23 20:27:39 +08:00
#ifdef MINIZIP_FROM_SYSTEM
2021-12-25 10:04:45 +08:00
# include <minizip/unzip.h>
#else // from our embedded sources
# include "unzip.h"
2019-11-23 20:27:39 +08:00
#endif
#include <ioapi.h>
2019-11-23 20:27:39 +08:00
NS_AX_EXT_BEGIN
2019-11-23 20:27:39 +08:00
using namespace std;
USING_NS_AX;
2022-08-08 18:02:17 +08:00
using namespace ax::network;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
#define KEY_OF_VERSION "current-version-code"
#define KEY_OF_DOWNLOADED_VERSION "downloaded-version-code"
#define TEMP_PACKAGE_FILE_NAME "cocos2dx-update-temp-package.zip"
#define BUFFER_SIZE 8192
#define MAX_FILENAME 512
2019-11-23 20:27:39 +08:00
class AssetManagerZipFileInfo
{
public:
std::string zipFileName{};
};
// unzip overrides to support FileStream
long AssetManager_tell_file_func(voidpf opaque, voidpf stream)
{
if (stream == nullptr)
return -1;
auto* fs = (FileStream*)stream;
return fs->tell();
}
long AssetManager_seek_file_func(voidpf opaque, voidpf stream, uint32_t offset, int origin)
{
if (stream == nullptr)
return -1;
auto* fs = (FileStream*)stream;
2023-08-25 16:46:32 +08:00
return fs->seek((long)offset, origin); // must return 0 for success or -1 for error
}
voidpf AssetManager_open_file_func(voidpf opaque, const char* filename, int mode)
{
FileStream::Mode fsMode;
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
fsMode = FileStream::Mode::READ;
else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
fsMode = FileStream::Mode::APPEND;
else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
fsMode = FileStream::Mode::WRITE;
else
return nullptr;
return FileUtils::getInstance()->openFileStream(filename, fsMode).release();
}
voidpf AssetManager_opendisk_file_func(voidpf opaque, voidpf stream, uint32_t number_disk, int mode)
{
if (stream == nullptr)
return nullptr;
2021-12-25 10:04:45 +08:00
const auto zipFileInfo = static_cast<AssetManagerZipFileInfo*>(opaque);
std::string diskFilename = zipFileInfo->zipFileName;
const auto pos = diskFilename.rfind('.', std::string::npos);
if (pos != std::string::npos && pos != 0)
{
const size_t bufferSize = 5;
char extensionBuffer[bufferSize];
snprintf(&extensionBuffer[0], bufferSize, ".z%02u", number_disk + 1);
diskFilename.replace(pos, std::min((size_t)4, zipFileInfo->zipFileName.size() - pos), extensionBuffer);
return AssetManager_open_file_func(opaque, diskFilename.c_str(), mode);
}
return nullptr;
}
uint32_t AssetManager_read_file_func(voidpf opaque, voidpf stream, void* buf, uint32_t size)
{
if (stream == nullptr)
return (uint32_t)-1;
auto* fs = (FileStream*)stream;
return fs->read(buf, size);
}
uint32_t AssetManager_write_file_func(voidpf opaque, voidpf stream, const void* buf, uint32_t size)
{
if (stream == nullptr)
return (uint32_t)-1;
auto* fs = (FileStream*)stream;
return fs->write(buf, size);
}
int AssetManager_close_file_func(voidpf opaque, voidpf stream)
{
if (stream == nullptr)
return -1;
2021-12-25 10:04:45 +08:00
auto* fs = (FileStream*)stream;
const auto result = fs->close(); // 0 for success, -1 for error
delete fs;
return result;
}
// THis isn't supported by FileStream, so just check if the stream is null and open
int AssetManager_error_file_func(voidpf opaque, voidpf stream)
{
if (stream == nullptr)
{
return -1;
}
auto* fs = (FileStream*)stream;
if (fs->isOpen())
{
return 0;
}
return -1;
}
// End of Overrides
2019-11-23 20:27:39 +08:00
// Implementation of AssetsManager
2021-12-25 10:04:45 +08:00
AssetsManager::AssetsManager(const char* packageUrl /* =nullptr */,
const char* versionFileUrl /* =nullptr */,
const char* storagePath /* =nullptr */)
: _storagePath(storagePath ? storagePath : "")
, _version("")
, _packageUrl(packageUrl ? packageUrl : "")
, _versionFileUrl(versionFileUrl ? versionFileUrl : "")
, _downloadedVersion("")
, _downloader(new Downloader())
, _connectionTimeout(0)
, _delegate(nullptr)
, _isDownloading(false)
, _shouldDeleteDelegateWhenExit(false)
2019-11-23 20:27:39 +08:00
{
checkStoragePath();
// convert downloader error code to AssetsManager::ErrorCode
2021-12-25 10:04:45 +08:00
_downloader->onTaskError = [this](const DownloadTask& /*task*/, int errorCode, int /*errorCodeInternal*/,
std::string_view /*errorStr*/) {
2019-11-23 20:27:39 +08:00
_isDownloading = false;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (nullptr == _delegate)
{
return;
}
auto err = (DownloadTask::ERROR_OPEN_FILE_FAILED == errorCode) ? ErrorCode::CREATE_FILE : ErrorCode::NETWORK;
2019-11-23 20:27:39 +08:00
_delegate->onError(err);
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// progress callback
2021-12-25 10:04:45 +08:00
_downloader->onTaskProgress = [this](const DownloadTask& task) {
if (FileUtils::getInstance()->getFileExtension(task.requestURL) != ".zip")
2019-11-23 20:27:39 +08:00
{
// get version progress don't report
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (nullptr == _delegate)
{
return;
}
2021-12-25 10:04:45 +08:00
int percent = task.progressInfo.totalBytesExpected
? int(task.progressInfo.totalBytesReceived * 100 / task.progressInfo.totalBytesExpected)
: 0;
2019-11-23 20:27:39 +08:00
_delegate->onProgress(percent);
2022-07-16 10:43:05 +08:00
AXLOG("downloading... %d%%", percent);
2019-11-23 20:27:39 +08:00
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// get version from version file when get data success
2021-12-25 10:04:45 +08:00
_downloader->onDataTaskSuccess = [this](const DownloadTask& /*task*/, std::vector<unsigned char>& data) {
2019-11-23 20:27:39 +08:00
// store version info to member _version
2021-12-25 10:04:45 +08:00
const char* p = (char*)data.data();
2019-11-23 20:27:39 +08:00
_version.insert(_version.end(), p, p + data.size());
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (getVersion() == _version)
{
if (_delegate)
{
_delegate->onError(ErrorCode::NO_NEW_VERSION);
}
2022-07-16 10:43:05 +08:00
AXLOG("there is not new version");
2019-11-23 20:27:39 +08:00
// Set resource search path.
setSearchPath();
_isDownloading = false;
return;
}
// start download new version assets
// 1. Urls of package and version should be valid;
// 2. Package should be a zip file.
2021-12-25 10:04:45 +08:00
if (_versionFileUrl.empty() || _packageUrl.empty() ||
FileUtils::getInstance()->getFileExtension(_packageUrl) != ".zip")
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("no version file url, or no package url, or the package is not a zip file");
2019-11-23 20:27:39 +08:00
_isDownloading = false;
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Is package already downloaded?
_downloadedVersion = UserDefault::getInstance()->getStringForKey(keyOfDownloadedVersion().c_str());
if (_downloadedVersion == _version)
{
downloadAndUncompress();
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// start download;
const string outFileName = _storagePath + TEMP_PACKAGE_FILE_NAME;
_downloader->createDownloadFileTask(_packageUrl, outFileName);
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// after download package, do uncompress operation
2021-12-25 10:04:45 +08:00
_downloader->onFileTaskSuccess = [this](const DownloadTask& /*task*/) { downloadAndUncompress(); };
2019-11-23 20:27:39 +08:00
}
AssetsManager::~AssetsManager()
{
if (_shouldDeleteDelegateWhenExit)
{
delete _delegate;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_downloader);
2019-11-23 20:27:39 +08:00
}
void AssetsManager::checkStoragePath()
{
if (!_storagePath.empty() && _storagePath[_storagePath.size() - 1] != '/')
2019-11-23 20:27:39 +08:00
{
_storagePath.push_back('/');
2019-11-23 20:27:39 +08:00
}
}
// Multiple key names
static std::string keyWithHash(const char* prefix, std::string_view url)
2019-11-23 20:27:39 +08:00
{
char buf[256];
snprintf(buf, sizeof(buf), "%s%zd", prefix, std::hash<std::string_view>()(url));
2019-11-23 20:27:39 +08:00
return buf;
}
// hashed version
std::string AssetsManager::keyOfVersion() const
{
2021-12-25 10:04:45 +08:00
return keyWithHash(KEY_OF_VERSION, _packageUrl);
2019-11-23 20:27:39 +08:00
}
// hashed version
std::string AssetsManager::keyOfDownloadedVersion() const
{
2021-12-25 10:04:45 +08:00
return keyWithHash(KEY_OF_DOWNLOADED_VERSION, _packageUrl);
2019-11-23 20:27:39 +08:00
}
bool AssetsManager::checkUpdate()
{
2021-12-25 10:04:45 +08:00
if (_versionFileUrl.empty() || _isDownloading)
return false;
2019-11-23 20:27:39 +08:00
// Clear _version before assign new value.
_version.clear();
_isDownloading = true;
_downloader->createDownloadDataTask(_versionFileUrl);
return true;
}
void AssetsManager::downloadAndUncompress()
{
2021-12-25 10:04:45 +08:00
std::thread([this]() {
2019-11-23 20:27:39 +08:00
do
{
// Uncompress zip file.
2021-12-25 10:04:45 +08:00
if (!uncompress())
2019-11-23 20:27:39 +08:00
{
2023-03-08 08:34:17 +08:00
Director::getInstance()->getScheduler()->runOnAxmolThread([&, this] {
2021-12-25 10:04:45 +08:00
UserDefault::getInstance()->setStringForKey(this->keyOfDownloadedVersion().c_str(), "");
2019-11-23 20:27:39 +08:00
UserDefault::getInstance()->flush();
if (this->_delegate)
this->_delegate->onError(ErrorCode::UNCOMPRESS);
});
break;
}
2021-12-25 10:04:45 +08:00
2023-03-08 08:34:17 +08:00
Director::getInstance()->getScheduler()->runOnAxmolThread([&, this] {
2019-11-23 20:27:39 +08:00
// Record new version code.
UserDefault::getInstance()->setStringForKey(this->keyOfVersion().c_str(), this->_version);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Unrecord downloaded version code.
UserDefault::getInstance()->setStringForKey(this->keyOfDownloadedVersion().c_str(), "");
UserDefault::getInstance()->flush();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Set resource search path.
this->setSearchPath();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Delete unloaded zip file.
string zipfileName = this->_storagePath + TEMP_PACKAGE_FILE_NAME;
if (remove(zipfileName.c_str()) != 0)
{
2022-07-16 10:43:05 +08:00
AXLOG("can not remove downloaded zip file %s", zipfileName.c_str());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
if (this->_delegate)
this->_delegate->onSuccess();
2019-11-23 20:27:39 +08:00
});
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
} while (0);
2021-12-25 10:04:45 +08:00
_isDownloading = false;
2019-11-23 20:27:39 +08:00
}).detach();
}
void AssetsManager::update()
{
// all operation in checkUpdate, nothing need to do
// keep this function for compatibility
}
bool AssetsManager::uncompress()
{
// Open the zip file
string outFileName = _storagePath + TEMP_PACKAGE_FILE_NAME;
zlib_filefunc_def_s zipFunctionOverrides;
fillZipFunctionOverrides(zipFunctionOverrides);
AssetManagerZipFileInfo zipFileInfo;
zipFileInfo.zipFileName = outFileName;
zipFunctionOverrides.opaque = &zipFileInfo;
// Open the zip file
unzFile zipfile = unzOpen2(outFileName.c_str(), &zipFunctionOverrides);
2021-12-25 10:04:45 +08:00
if (!zipfile)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("can not open downloaded zip file %s", outFileName.c_str());
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Get info about the zip file
unz_global_info global_info;
if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)
{
2022-07-16 10:43:05 +08:00
AXLOG("can not read file global info of %s", outFileName.c_str());
2019-11-23 20:27:39 +08:00
unzClose(zipfile);
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Buffer to hold data read from the zip file
char readBuffer[BUFFER_SIZE];
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AXLOG("start uncompressing");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Loop to extract all files.
uLong i;
for (i = 0; i < global_info.number_entry; ++i)
{
// Get info about current file.
unz_file_info fileInfo;
char fileName[MAX_FILENAME];
2021-12-25 10:04:45 +08:00
if (unzGetCurrentFileInfo(zipfile, &fileInfo, fileName, MAX_FILENAME, nullptr, 0, nullptr, 0) != UNZ_OK)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("can not read file info");
2019-11-23 20:27:39 +08:00
unzClose(zipfile);
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
const string fullPath = _storagePath + fileName;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Check if this entry is a directory or a file.
const size_t filenameLength = strlen(fileName);
2021-12-25 10:04:45 +08:00
if (fileName[filenameLength - 1] == '/')
2019-11-23 20:27:39 +08:00
{
// Entry is a directory, so create it.
// If the directory exists, it will failed silently.
if (!FileUtils::getInstance()->createDirectory(fullPath))
{
2022-07-16 10:43:05 +08:00
AXLOG("can not create directory %s", fullPath.c_str());
2019-11-23 20:27:39 +08:00
unzClose(zipfile);
return false;
}
}
else
{
2021-12-25 10:04:45 +08:00
// There are not directory entry in some case.
// So we need to test whether the file directory exists when uncompressing file entry
2019-11-23 20:27:39 +08:00
//, if does not exist then create directory
const string fileNameStr(fileName);
2021-12-25 10:04:45 +08:00
size_t startIndex = 0;
size_t index = fileNameStr.find('/', startIndex);
while (index != std::string::npos)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
const string dir = _storagePath + fileNameStr.substr(0, index);
auto fsOut = FileUtils::getInstance()->openFileStream(dir, FileStream::Mode::READ);
if (!fsOut)
2019-11-23 20:27:39 +08:00
{
if (!FileUtils::getInstance()->createDirectory(dir))
{
2022-07-16 10:43:05 +08:00
AXLOG("can not create directory %s", dir.c_str());
2019-11-23 20:27:39 +08:00
unzClose(zipfile);
return false;
}
else
{
2022-07-16 10:43:05 +08:00
AXLOG("create directory %s", dir.c_str());
2019-11-23 20:27:39 +08:00
}
}
else
{
fsOut.reset();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
startIndex = index + 1;
index = fileNameStr.find('/', startIndex);
2019-11-23 20:27:39 +08:00
}
// Entry is a file, so extract it.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Open current file.
if (unzOpenCurrentFile(zipfile) != UNZ_OK)
{
2022-07-16 10:43:05 +08:00
AXLOG("can not open file %s", fileName);
2019-11-23 20:27:39 +08:00
unzClose(zipfile);
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Create a file to store current file.
auto fsOut = FileUtils::getInstance()->openFileStream(fullPath, FileStream::Mode::WRITE);
if (!fsOut)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("can not open destination file %s", fullPath.c_str());
2019-11-23 20:27:39 +08:00
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Write current file content to destinate file.
int error = UNZ_OK;
do
{
error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE);
if (error < 0)
{
2022-07-16 10:43:05 +08:00
AXLOG("can not read zip file %s, error code is %d", fileName, error);
2019-11-23 20:27:39 +08:00
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
fsOut.reset();
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (error > 0)
{
fsOut->write(readBuffer, error);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
} while (error > 0);
fsOut.reset();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
unzCloseCurrentFile(zipfile);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Goto next entry listed in the zip file.
2021-12-25 10:04:45 +08:00
if ((i + 1) < global_info.number_entry)
2019-11-23 20:27:39 +08:00
{
if (unzGoToNextFile(zipfile) != UNZ_OK)
{
2022-07-16 10:43:05 +08:00
AXLOG("can not read next file");
2019-11-23 20:27:39 +08:00
unzClose(zipfile);
return false;
}
}
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AXLOG("end uncompressing");
2019-11-23 20:27:39 +08:00
unzClose(zipfile);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
}
void AssetsManager::setSearchPath()
{
2021-12-25 10:04:45 +08:00
vector<string> searchPaths = FileUtils::getInstance()->getSearchPaths();
2019-11-23 20:27:39 +08:00
vector<string>::iterator iter = searchPaths.begin();
searchPaths.insert(iter, _storagePath);
FileUtils::getInstance()->setSearchPaths(searchPaths);
}
const char* AssetsManager::getPackageUrl() const
{
return _packageUrl.c_str();
}
2021-12-25 10:04:45 +08:00
void AssetsManager::setPackageUrl(const char* packageUrl)
2019-11-23 20:27:39 +08:00
{
_packageUrl = packageUrl;
}
const char* AssetsManager::getStoragePath() const
{
return _storagePath.c_str();
}
2021-12-25 10:04:45 +08:00
void AssetsManager::setStoragePath(const char* storagePath)
2019-11-23 20:27:39 +08:00
{
_storagePath = storagePath;
checkStoragePath();
}
const char* AssetsManager::getVersionFileUrl() const
{
return _versionFileUrl.c_str();
}
2021-12-25 10:04:45 +08:00
void AssetsManager::setVersionFileUrl(const char* versionFileUrl)
2019-11-23 20:27:39 +08:00
{
_versionFileUrl = versionFileUrl;
}
std::string_view AssetsManager::getVersion()
2019-11-23 20:27:39 +08:00
{
return UserDefault::getInstance()->getStringForKey(keyOfVersion().data());
2019-11-23 20:27:39 +08:00
}
void AssetsManager::deleteVersion()
{
UserDefault::getInstance()->setStringForKey(keyOfVersion().c_str(), "");
}
2021-12-25 10:04:45 +08:00
void AssetsManager::setDelegate(AssetsManagerDelegateProtocol* delegate)
2019-11-23 20:27:39 +08:00
{
_delegate = delegate;
}
void AssetsManager::setConnectionTimeout(unsigned int timeout)
{
_connectionTimeout = timeout;
}
unsigned int AssetsManager::getConnectionTimeout()
{
return _connectionTimeout;
}
2021-12-25 10:04:45 +08:00
AssetsManager* AssetsManager::create(const char* packageUrl,
const char* versionFileUrl,
const char* storagePath,
ErrorCallback errorCallback,
ProgressCallback progressCallback,
SuccessCallback successCallback)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
class DelegateProtocolImpl : public AssetsManagerDelegateProtocol
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
public:
DelegateProtocolImpl(ErrorCallback& aErrorCallback,
ProgressCallback& aProgressCallback,
SuccessCallback& aSuccessCallback)
: errorCallback(aErrorCallback), progressCallback(aProgressCallback), successCallback(aSuccessCallback)
2019-11-23 20:27:39 +08:00
{}
virtual void onError(AssetsManager::ErrorCode errorCode) { errorCallback(int(errorCode)); }
virtual void onProgress(int percent) { progressCallback(percent); }
virtual void onSuccess() { successCallback(); }
2021-12-25 10:04:45 +08:00
private:
2019-11-23 20:27:39 +08:00
ErrorCallback errorCallback;
ProgressCallback progressCallback;
SuccessCallback successCallback;
};
2021-12-25 10:04:45 +08:00
auto* manager = new AssetsManager(packageUrl, versionFileUrl, storagePath);
auto* delegate = new DelegateProtocolImpl(errorCallback, progressCallback, successCallback);
2019-11-23 20:27:39 +08:00
manager->setDelegate(delegate);
manager->_shouldDeleteDelegateWhenExit = true;
manager->autorelease();
return manager;
}
2021-12-25 10:04:45 +08:00
void AssetsManager::fillZipFunctionOverrides(zlib_filefunc_def_s& zipFunctionOverrides)
{
zipFunctionOverrides.zopen_file = AssetManager_open_file_func;
zipFunctionOverrides.zopendisk_file = AssetManager_opendisk_file_func;
2021-12-25 10:04:45 +08:00
zipFunctionOverrides.zread_file = AssetManager_read_file_func;
zipFunctionOverrides.zwrite_file = AssetManager_write_file_func;
zipFunctionOverrides.ztell_file = AssetManager_tell_file_func;
zipFunctionOverrides.zseek_file = AssetManager_seek_file_func;
zipFunctionOverrides.zclose_file = AssetManager_close_file_func;
zipFunctionOverrides.zerror_file = AssetManager_error_file_func;
zipFunctionOverrides.opaque = nullptr;
}
NS_AX_EXT_END