2014-10-09 18:30:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30: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
|
|
|
|
2014-10-09 18:30: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
|
|
|
|
2014-10-09 18:30: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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __AssetsManagerEx__
|
|
|
|
#define __AssetsManagerEx__
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2015-08-13 15:14:10 +08:00
|
|
|
#include "network/CCDownloader.h"
|
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
#include "CCEventAssetsManagerEx.h"
|
2015-08-13 15:14:10 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
#include "Manifest.h"
|
|
|
|
#include "extensions/ExtensionMacros.h"
|
|
|
|
#include "extensions/ExtensionExport.h"
|
2020-05-06 15:42:25 +08:00
|
|
|
#include "rapidjson/document-wrapper.h"
|
2014-10-09 18:30:39 +08:00
|
|
|
|
2021-06-18 11:31:07 +08:00
|
|
|
struct zlib_filefunc_def_s;
|
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This class is used to auto update resources, such as pictures or scripts.
|
|
|
|
*/
|
|
|
|
class CC_EX_DLL AssetsManagerEx : public Ref
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//! Update states
|
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
UNCHECKED,
|
|
|
|
PREDOWNLOAD_VERSION,
|
|
|
|
DOWNLOADING_VERSION,
|
|
|
|
VERSION_LOADED,
|
|
|
|
PREDOWNLOAD_MANIFEST,
|
|
|
|
DOWNLOADING_MANIFEST,
|
|
|
|
MANIFEST_LOADED,
|
|
|
|
NEED_UPDATE,
|
|
|
|
UPDATING,
|
2015-09-18 04:43:56 +08:00
|
|
|
UNZIPPING,
|
2014-10-09 18:30:39 +08:00
|
|
|
UP_TO_DATE,
|
|
|
|
FAIL_TO_UPDATE
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
const static std::string VERSION_ID;
|
|
|
|
const static std::string MANIFEST_ID;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Create function for creating a new AssetsManagerEx
|
|
|
|
@param manifestUrl The url for the local manifest file
|
2016-02-18 15:16:36 +08:00
|
|
|
@param storagePath The storage path for downloaded assets
|
2014-10-09 18:30:39 +08:00
|
|
|
@warning The cached manifest in your storage path have higher priority and will be searched first,
|
|
|
|
only if it doesn't exist, AssetsManagerEx will use the given manifestUrl.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
static AssetsManagerEx* create(std::string_view manifestUrl, std::string_view storagePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Check out if there is a new version of manifest.
|
|
|
|
* You may use this method before updating, then let user determine whether
|
|
|
|
* he wants to update resources.
|
|
|
|
*/
|
|
|
|
void checkUpdate();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Update with the current local manifest.
|
|
|
|
*/
|
|
|
|
void update();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Reupdate all failed assets under the current AssetsManagerEx context
|
|
|
|
*/
|
|
|
|
void downloadFailedAssets();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Gets the current update state.
|
|
|
|
*/
|
|
|
|
State getState() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Gets storage path.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view getStoragePath() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
/** @brief Function for retrieving the local manifest object
|
2014-10-09 18:30:39 +08:00
|
|
|
*/
|
|
|
|
const Manifest* getLocalManifest() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
/** @brief Function for retrieving the remote manifest object
|
2014-10-09 18:30:39 +08:00
|
|
|
*/
|
|
|
|
const Manifest* getRemoteManifest() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
/** @brief Function for retrieving the max concurrent task count
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const int getMaxConcurrentTask() const { return _maxConcurrentTask; };
|
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
/** @brief Function for setting the max concurrent task count
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setMaxConcurrentTask(const int max) { _maxConcurrentTask = max; };
|
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
/** @brief Set the handle function for comparing manifests versions
|
|
|
|
* @param handle The compare function
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setVersionCompareHandle(const std::function<int(std::string_view versionA, std::string_view versionB)>& handle)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
_versionCompareHandle = handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Set the verification function for checking whether downloaded asset is correct, e.g. using md5
|
|
|
|
* verification
|
2017-03-15 16:09:02 +08:00
|
|
|
* @param callback The verify callback function
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setVerifyCallback(const std::function<bool(std::string_view path, Manifest::Asset asset)>& callback)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
_verifyCallback = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
CC_CONSTRUCTOR_ACCESS :
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
AssetsManagerEx(std::string_view manifestUrl, std::string_view storagePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
virtual ~AssetsManagerEx();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
protected:
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view basename(std::string_view path) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string get(std::string_view key) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void initManifests(std::string_view manifestUrl);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void loadLocalManifest(std::string_view manifestUrl);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
void prepareLocalManifest();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void setStoragePath(std::string_view storagePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void adjustPath(std::string& path);
|
|
|
|
|
|
|
|
void dispatchUpdateEvent(EventAssetsManagerEx::EventCode code,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view message = "",
|
|
|
|
std::string_view assetId = "",
|
|
|
|
int curle_code = 0,
|
|
|
|
int curlm_code = 0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
void downloadVersion();
|
|
|
|
void parseVersion();
|
|
|
|
void downloadManifest();
|
|
|
|
void parseManifest();
|
|
|
|
void startUpdate();
|
|
|
|
void updateSucceed();
|
2021-12-26 23:26:34 +08:00
|
|
|
bool decompress(std::string_view filename);
|
|
|
|
void decompressDownloadedZip(std::string_view customId, std::string_view storagePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Update a list of assets under the current AssetsManagerEx context
|
|
|
|
*/
|
2015-09-15 11:23:46 +08:00
|
|
|
void updateAssets(const DownloadUnits& assets);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Retrieve all failed assets during the last update
|
|
|
|
*/
|
2015-09-15 11:23:46 +08:00
|
|
|
const DownloadUnits& getFailedAssets() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-12-11 23:37:58 +08:00
|
|
|
/** @brief Function for destroying the downloaded version file and manifest file
|
2014-10-09 18:30:39 +08:00
|
|
|
*/
|
|
|
|
void destroyDownloadedVersion();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
/** @brief Download items in queue with max concurrency setting
|
|
|
|
*/
|
|
|
|
void queueDowload();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void fileError(std::string_view identifier,
|
|
|
|
std::string_view errorStr,
|
2021-12-25 10:04:45 +08:00
|
|
|
int errorCode = 0,
|
|
|
|
int errorCodeInternal = 0);
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void fileSuccess(std::string_view customId, std::string_view storagePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Call back function for error handling,
|
|
|
|
the error will then be reported to user's listener registed in addUpdateEventListener
|
|
|
|
@param error The error object contains ErrorCode, message, asset url, asset key
|
|
|
|
@warning AssetsManagerEx internal use only
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2015-09-15 11:23:46 +08:00
|
|
|
virtual void onError(const network::DownloadTask& task,
|
|
|
|
int errorCode,
|
|
|
|
int errorCodeInternal,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view errorStr);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Call back function for recording downloading percent of the current asset,
|
|
|
|
the progression will then be reported to user's listener registed in addUpdateProgressEventListener
|
|
|
|
@param total Total size to download for this asset
|
|
|
|
@param downloaded Total size already downloaded for this asset
|
|
|
|
@param url The url of this asset
|
|
|
|
@param customId The key of this asset
|
|
|
|
@warning AssetsManagerEx internal use only
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual void onProgress(double total, double downloaded, std::string_view url, std::string_view customId);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
/** @brief Call back function for success of the current asset
|
|
|
|
the success event will then be send to user's listener registed in addUpdateEventListener
|
|
|
|
@param srcUrl The url of this asset
|
|
|
|
@param customId The key of this asset
|
|
|
|
@warning AssetsManagerEx internal use only
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual void onSuccess(std::string_view srcUrl, std::string_view storagePath, std::string_view customId);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
private:
|
2015-09-15 11:23:46 +08:00
|
|
|
void batchDownload();
|
2016-04-20 11:53:38 +08:00
|
|
|
|
|
|
|
// Called when one DownloadUnits finished
|
|
|
|
void onDownloadUnitsFinished();
|
2021-12-25 10:04:45 +08:00
|
|
|
void fillZipFunctionOverrides(zlib_filefunc_def_s& zipFunctionOverrides);
|
2021-04-25 15:22:43 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! The event of the current AssetsManagerEx in event dispatcher
|
|
|
|
std::string _eventName;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Reference to the global event dispatcher
|
2021-12-25 10:04:45 +08:00
|
|
|
EventDispatcher* _eventDispatcher;
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Reference to the global file utils
|
2021-12-25 10:04:45 +08:00
|
|
|
FileUtils* _fileUtils;
|
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! State of update
|
2019-08-13 18:19:32 +08:00
|
|
|
State _updateState = State::UNCHECKED;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Downloader
|
2015-08-13 15:14:10 +08:00
|
|
|
std::shared_ptr<network::Downloader> _downloader;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! The reference to the local assets
|
2021-12-26 23:26:34 +08:00
|
|
|
const hlookup::string_map<Manifest::Asset>* _assets = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
//! The path to store successfully downloaded version.
|
2014-10-09 18:30:39 +08:00
|
|
|
std::string _storagePath;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
//! The path to store downloading version.
|
|
|
|
std::string _tempStoragePath;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
//! The local path of cached temporary version file
|
|
|
|
std::string _tempVersionPath;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! The local path of cached manifest file
|
|
|
|
std::string _cacheManifestPath;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! The local path of cached temporary manifest file
|
|
|
|
std::string _tempManifestPath;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! The path of local manifest file
|
|
|
|
std::string _manifestUrl;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Local manifest
|
2021-12-25 10:04:45 +08:00
|
|
|
Manifest* _localManifest = nullptr;
|
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Local temporary manifest for download resuming
|
2021-12-25 10:04:45 +08:00
|
|
|
Manifest* _tempManifest = nullptr;
|
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Remote manifest
|
2021-12-25 10:04:45 +08:00
|
|
|
Manifest* _remoteManifest = nullptr;
|
2021-04-25 15:22:43 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Whether user have requested to update
|
2016-11-04 15:03:32 +08:00
|
|
|
enum class UpdateEntry : char
|
|
|
|
{
|
|
|
|
NONE,
|
|
|
|
CHECK_UPDATE,
|
|
|
|
DO_UPDATE
|
|
|
|
};
|
|
|
|
|
2019-08-13 18:19:32 +08:00
|
|
|
UpdateEntry _updateEntry = UpdateEntry::NONE;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! All assets unit to download
|
2015-09-15 11:23:46 +08:00
|
|
|
DownloadUnits _downloadUnits;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! All failed units
|
2015-09-15 11:23:46 +08:00
|
|
|
DownloadUnits _failedUnits;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
//! Download queue
|
|
|
|
std::vector<std::string> _queue;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
//! Max concurrent task count for downloading
|
2019-08-13 18:19:32 +08:00
|
|
|
int _maxConcurrentTask = 32;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
//! Current concurrent task count
|
2019-08-13 18:19:32 +08:00
|
|
|
int _currConcurrentTask = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Download percent
|
2019-08-13 18:19:32 +08:00
|
|
|
float _percent = 0.f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Download percent by file
|
2019-08-13 18:19:32 +08:00
|
|
|
float _percentByFile = 0.f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Indicate whether the total size should be enabled
|
|
|
|
int _totalEnabled;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Indicate the number of file whose total size have been collected
|
|
|
|
int _sizeCollected;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Total file size need to be downloaded (sum of all file)
|
|
|
|
double _totalSize;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Downloaded size for each file
|
2021-12-26 23:26:34 +08:00
|
|
|
hlookup::string_map<double> _downloadedSize;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Total number of assets to download
|
2019-08-13 18:19:32 +08:00
|
|
|
int _totalToDownload = 0;
|
2014-10-09 18:30:39 +08:00
|
|
|
//! Total number of assets still waiting to be downloaded
|
2019-08-13 18:19:32 +08:00
|
|
|
int _totalWaitToDownload = 0;
|
2017-03-15 16:09:02 +08:00
|
|
|
//! Next target percent for saving the manifest file
|
2019-08-13 18:19:32 +08:00
|
|
|
float _nextSavePoint = 0.f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
//! Handle function to compare versions between different manifests
|
2021-12-26 23:26:34 +08:00
|
|
|
std::function<int(std::string_view versionA, std::string_view versionB)> _versionCompareHandle = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-03-15 16:09:02 +08:00
|
|
|
//! Callback function to verify the downloaded assets
|
2021-12-26 23:26:34 +08:00
|
|
|
std::function<bool(std::string_view path, Manifest::Asset asset)> _verifyCallback = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-11-26 14:39:56 +08:00
|
|
|
//! Marker for whether the assets manager is inited
|
2019-08-13 18:19:32 +08:00
|
|
|
bool _inited = false;
|
2014-10-09 18:30:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|
|
|
|
|
|
|
|
#endif /* defined(__AssetsManagerEx__) */
|