2013-02-22 11:04:09 +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
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 11:04:09 +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
|
|
|
|
2013-02-22 11:04:09 +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
|
|
|
|
2013-02-22 11:04:09 +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 __AssetsManager__
|
|
|
|
#define __AssetsManager__
|
|
|
|
|
|
|
|
#include <string>
|
2013-08-07 13:07:01 +08:00
|
|
|
|
2013-06-24 13:59:41 +08:00
|
|
|
#include <mutex>
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "2d/CCNode.h"
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "extensions/ExtensionMacros.h"
|
2014-07-24 12:57:39 +08:00
|
|
|
#include "extensions/ExtensionExport.h"
|
2013-02-26 16:43:46 +08:00
|
|
|
|
2021-06-18 11:31:07 +08:00
|
|
|
struct zlib_filefunc_def_s;
|
2015-05-29 04:32:08 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace network
|
|
|
|
{
|
|
|
|
class Downloader;
|
|
|
|
}
|
2015-09-17 16:52:31 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
NS_AX_END // namespace axys
|
2022-07-11 17:50:21 +08:00
|
|
|
|
|
|
|
NS_AX_EXT_BEGIN
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2013-05-15 17:09:28 +08:00
|
|
|
class AssetsManagerDelegateProtocol;
|
|
|
|
|
|
|
|
/*
|
2013-02-22 11:04:09 +08:00
|
|
|
* This class is used to auto update resources, such as pictures or scripts.
|
|
|
|
* The updated package should be a zip file. And there should be a file named
|
|
|
|
* version in the server, which contains version code.
|
|
|
|
*/
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_EX_DLL AssetsManager : public Node
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-26 12:08:18 +08:00
|
|
|
enum class ErrorCode
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
2013-05-15 17:09:28 +08:00
|
|
|
// Error caused by creating a file to store downloaded data
|
2013-07-26 12:08:18 +08:00
|
|
|
CREATE_FILE,
|
2013-05-15 17:09:28 +08:00
|
|
|
/** Error caused by network
|
2015-12-11 23:37:58 +08:00
|
|
|
-- network unavailable
|
2013-05-15 17:09:28 +08:00
|
|
|
-- timeout
|
|
|
|
-- ...
|
|
|
|
*/
|
2013-07-26 12:08:18 +08:00
|
|
|
NETWORK,
|
2013-05-15 17:09:28 +08:00
|
|
|
/** There is not a new version
|
|
|
|
*/
|
2013-07-26 12:08:18 +08:00
|
|
|
NO_NEW_VERSION,
|
2013-05-15 17:09:28 +08:00
|
|
|
/** Error caused in uncompressing stage
|
|
|
|
-- can not open zip file
|
|
|
|
-- can not read file global information
|
|
|
|
-- can not read file information
|
|
|
|
-- can not create a directory
|
|
|
|
-- ...
|
|
|
|
*/
|
2013-07-26 12:08:18 +08:00
|
|
|
UNCOMPRESS,
|
2013-02-22 11:04:09 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
/* @brief Creates a AssetsManager with new package url, version code url and storage path.
|
|
|
|
*
|
|
|
|
* @param packageUrl URL of new package, the package should be a zip file.
|
|
|
|
* @param versionFileUrl URL of version file. It should contain version code of new package.
|
|
|
|
* @param storagePath The path to store downloaded resources.
|
2013-09-13 16:46:31 +08:00
|
|
|
* @js NA
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua new
|
2013-02-22 11:04:09 +08:00
|
|
|
*/
|
2013-05-15 17:09:28 +08:00
|
|
|
AssetsManager(const char* packageUrl = NULL, const char* versionFileUrl = NULL, const char* storagePath = NULL);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-05-15 17:09:28 +08:00
|
|
|
virtual ~AssetsManager();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-08-07 13:07:01 +08:00
|
|
|
typedef std::function<void(int)> ErrorCallback;
|
|
|
|
typedef std::function<void(int)> ProgressCallback;
|
2019-11-24 15:43:28 +08:00
|
|
|
typedef std::function<void()> SuccessCallback;
|
2013-08-07 13:07:01 +08:00
|
|
|
|
|
|
|
/* @brief To access within scripting environment
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
static AssetsManager* create(const char* packageUrl,
|
|
|
|
const char* versionFileUrl,
|
|
|
|
const char* storagePath,
|
|
|
|
ErrorCallback errorCallback,
|
|
|
|
ProgressCallback progressCallback,
|
|
|
|
SuccessCallback successCallback);
|
2013-08-07 13:07:01 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
/* @brief Check out if there is a new version resource.
|
|
|
|
* You may use this method before updating, then let user determine whether
|
|
|
|
* he wants to update resources.
|
|
|
|
*/
|
|
|
|
virtual bool checkUpdate();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-14 14:09:12 +08:00
|
|
|
using Node::update;
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Download new package if there is a new version, and uncompress downloaded zip file.
|
|
|
|
* Ofcourse it will set search path that stores downloaded files.
|
|
|
|
*/
|
2013-11-11 17:06:32 +08:00
|
|
|
virtual void update();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
/* @brief Gets url of package.
|
|
|
|
*/
|
|
|
|
const char* getPackageUrl() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Sets package url.
|
2013-02-22 11:04:09 +08:00
|
|
|
*/
|
|
|
|
void setPackageUrl(const char* packageUrl);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Gets version file url.
|
|
|
|
*/
|
2013-02-22 11:04:09 +08:00
|
|
|
const char* getVersionFileUrl() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Gets version file url.
|
|
|
|
*/
|
2013-02-22 11:04:09 +08:00
|
|
|
void setVersionFileUrl(const char* versionFileUrl);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Gets current version code.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view getVersion();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Deletes recorded version code.
|
|
|
|
*/
|
|
|
|
void deleteVersion();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
/* @brief Gets storage path.
|
|
|
|
*/
|
|
|
|
const char* getStoragePath() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
/* @brief Sets storage path.
|
|
|
|
*
|
|
|
|
* @param storagePath The path to store downloaded resources.
|
2016-07-03 23:42:10 +08:00
|
|
|
* @warning The path should be a valid path.
|
2013-02-22 11:04:09 +08:00
|
|
|
*/
|
|
|
|
void setStoragePath(const char* storagePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-05-15 17:09:28 +08:00
|
|
|
/** @brief Sets delegate, the delegate will receive messages
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2013-05-15 17:09:28 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setDelegate(AssetsManagerDelegateProtocol* delegate);
|
|
|
|
|
2013-10-22 16:14:49 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
AssetsManagerDelegateProtocol* getDelegate() const { return _delegate; }
|
|
|
|
|
2013-05-15 17:09:28 +08:00
|
|
|
/** @brief Sets connection time out in seconds
|
|
|
|
*/
|
|
|
|
void setConnectionTimeout(unsigned int timeout);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-02-18 15:16:36 +08:00
|
|
|
/** @brief Gets connection time out in seconds
|
2013-05-15 17:09:28 +08:00
|
|
|
*/
|
|
|
|
unsigned int getConnectionTimeout();
|
2013-08-07 13:36:26 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
protected:
|
|
|
|
void checkStoragePath();
|
|
|
|
bool uncompress();
|
|
|
|
void setSearchPath();
|
2013-06-24 13:59:41 +08:00
|
|
|
void downloadAndUncompress();
|
2021-06-18 11:31:07 +08:00
|
|
|
void fillZipFunctionOverrides(zlib_filefunc_def_s& zipFunctionOverrides);
|
2013-08-07 13:07:01 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
private:
|
|
|
|
//! The path to store downloaded resources.
|
|
|
|
std::string _storagePath;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
//! The version of downloaded resources.
|
|
|
|
std::string _version;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
std::string _packageUrl;
|
|
|
|
std::string _versionFileUrl;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-05-15 17:09:28 +08:00
|
|
|
std::string _downloadedVersion;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::network::Downloader* _downloader;
|
2013-08-07 13:07:01 +08:00
|
|
|
|
2013-05-15 17:09:28 +08:00
|
|
|
unsigned int _connectionTimeout;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
AssetsManagerDelegateProtocol* _delegate;
|
|
|
|
|
2013-06-24 13:59:41 +08:00
|
|
|
bool _isDownloading;
|
2013-08-08 17:36:56 +08:00
|
|
|
bool _shouldDeleteDelegateWhenExit;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-08-09 13:39:40 +08:00
|
|
|
std::string keyOfVersion() const;
|
|
|
|
std::string keyOfDownloadedVersion() const;
|
2013-05-15 17:09:28 +08:00
|
|
|
};
|
|
|
|
|
2013-08-08 17:36:56 +08:00
|
|
|
class AssetsManagerDelegateProtocol
|
2013-05-15 17:09:28 +08:00
|
|
|
{
|
2013-11-13 11:22:34 +08:00
|
|
|
public:
|
|
|
|
virtual ~AssetsManagerDelegateProtocol(){};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-05-15 17:09:28 +08:00
|
|
|
public:
|
|
|
|
/* @brief Call back function for error
|
|
|
|
@param errorCode Type of error
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2013-05-15 17:09:28 +08:00
|
|
|
*/
|
2019-11-24 15:43:28 +08:00
|
|
|
virtual void onError(AssetsManager::ErrorCode errorCode) {}
|
2013-05-15 17:09:28 +08:00
|
|
|
/** @brief Call back function for recording downloading percent
|
|
|
|
@param percent How much percent downloaded
|
2013-08-01 17:47:37 +08:00
|
|
|
@warning This call back function just for recording downloading percent.
|
2013-05-15 17:09:28 +08:00
|
|
|
AssetsManager will do some other thing after downloading, you should
|
2021-12-25 10:04:45 +08:00
|
|
|
write code in onSuccess() after downloading.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2013-05-15 17:09:28 +08:00
|
|
|
*/
|
2019-11-24 15:43:28 +08:00
|
|
|
virtual void onProgress(int percent) {}
|
2013-05-15 17:09:28 +08:00
|
|
|
/** @brief Call back function for success
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2013-05-15 17:09:28 +08:00
|
|
|
*/
|
2019-11-24 15:43:28 +08:00
|
|
|
virtual void onSuccess() {}
|
2013-02-22 11:04:09 +08:00
|
|
|
};
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_END
|
2013-02-26 16:43:46 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
#endif /* defined(__AssetsManager__) */
|