mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-05 02:52:39 +08:00
Merge pull request #630 from pandamicro/develop
Fix AssetsManager issues and improvements
This commit is contained in:
@@ -113,7 +113,7 @@ JSFunctionWrapper::JSFunctionWrapper(JSContext* cx, JS::HandleObject jsthis, JS:
|
||||
{
|
||||
_jsthis = jsthis;
|
||||
_fval = fval;
|
||||
setOwner(cx, JS::RootedValue(cx, owner));
|
||||
setOwner(cx, owner);
|
||||
}
|
||||
|
||||
JSFunctionWrapper::~JSFunctionWrapper()
|
||||
@@ -153,7 +153,6 @@ JSFunctionWrapper::~JSFunctionWrapper()
|
||||
void JSFunctionWrapper::setOwner(JSContext* cx, JS::HandleValue owner)
|
||||
{
|
||||
JSAutoCompartment(cx, ScriptingCore::getInstance()->getGlobalObject());
|
||||
JS::RootedValue ownerVal(cx, owner);
|
||||
if (!owner.isNullOrUndefined())
|
||||
{
|
||||
_owner = owner;
|
||||
@@ -165,14 +164,14 @@ void JSFunctionWrapper::setOwner(JSContext* cx, JS::HandleValue owner)
|
||||
}
|
||||
|
||||
JS::RootedValue thisVal(cx, OBJECT_TO_JSVAL(_jsthis));
|
||||
if (!thisVal.isNullOrUndefined())
|
||||
if (thisVal.isObject() && ownerObj.get() != _jsthis.get())
|
||||
{
|
||||
js_add_object_reference(ownerVal, thisVal);
|
||||
js_add_object_reference(owner, thisVal);
|
||||
}
|
||||
JS::RootedValue funcVal(cx, _fval);
|
||||
if (!funcVal.isNullOrUndefined())
|
||||
if (funcVal.isObject())
|
||||
{
|
||||
js_add_object_reference(ownerVal, funcVal);
|
||||
js_add_object_reference(owner, funcVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ const std::string AssetsManagerEx::MANIFEST_ID = "@manifest";
|
||||
|
||||
// Implementation of AssetsManagerEx
|
||||
|
||||
AssetsManagerEx::AssetsManagerEx(const std::string& manifestUrl, const std::string& storagePath, const VersionCompareHandle& handle/* = nullptr*/)
|
||||
AssetsManagerEx::AssetsManagerEx(const std::string& manifestUrl, const std::string& storagePath)
|
||||
: _updateState(State::UNINITED)
|
||||
, _assets(nullptr)
|
||||
, _storagePath("")
|
||||
@@ -68,6 +68,35 @@ AssetsManagerEx::AssetsManagerEx(const std::string& manifestUrl, const std::stri
|
||||
, _percent(0)
|
||||
, _percentByFile(0)
|
||||
, _totalSize(0)
|
||||
, _sizeCollected(0)
|
||||
, _totalDownloaded(0)
|
||||
, _totalToDownload(0)
|
||||
, _totalWaitToDownload(0)
|
||||
, _nextSavePoint(0.0)
|
||||
, _downloadResumed(false)
|
||||
, _maxConcurrentTask(32)
|
||||
, _currConcurrentTask(0)
|
||||
, _verifyCallback(nullptr)
|
||||
, _inited(false)
|
||||
{
|
||||
init(manifestUrl, storagePath);
|
||||
}
|
||||
|
||||
AssetsManagerEx::AssetsManagerEx(const std::string& manifestUrl, const std::string& storagePath, const VersionCompareHandle& handle)
|
||||
: _updateState(State::UNINITED)
|
||||
, _assets(nullptr)
|
||||
, _storagePath("")
|
||||
, _tempVersionPath("")
|
||||
, _cacheManifestPath("")
|
||||
, _tempManifestPath("")
|
||||
, _localManifest(nullptr)
|
||||
, _tempManifest(nullptr)
|
||||
, _remoteManifest(nullptr)
|
||||
, _updateEntry(UpdateEntry::NONE)
|
||||
, _percent(0)
|
||||
, _percentByFile(0)
|
||||
, _totalSize(0)
|
||||
, _sizeCollected(0)
|
||||
, _totalDownloaded(0)
|
||||
, _totalToDownload(0)
|
||||
, _totalWaitToDownload(0)
|
||||
@@ -78,6 +107,11 @@ AssetsManagerEx::AssetsManagerEx(const std::string& manifestUrl, const std::stri
|
||||
, _versionCompareHandle(handle)
|
||||
, _verifyCallback(nullptr)
|
||||
, _inited(false)
|
||||
{
|
||||
init(manifestUrl, storagePath);
|
||||
}
|
||||
|
||||
void AssetsManagerEx::init(const std::string& manifestUrl, const std::string& storagePath)
|
||||
{
|
||||
// Init variables
|
||||
_eventDispatcher = Director::getInstance()->getEventDispatcher();
|
||||
@@ -731,12 +765,7 @@ void AssetsManagerEx::downloadManifest()
|
||||
if (_updateState != State::PREDOWNLOAD_MANIFEST)
|
||||
return;
|
||||
|
||||
std::string manifestUrl;
|
||||
if (_remoteManifest->isVersionLoaded()) {
|
||||
manifestUrl = _remoteManifest->getManifestFileUrl();
|
||||
} else {
|
||||
manifestUrl = _localManifest->getManifestFileUrl();
|
||||
}
|
||||
std::string manifestUrl = _localManifest->getManifestFileUrl();
|
||||
|
||||
if (manifestUrl.size() > 0)
|
||||
{
|
||||
@@ -802,8 +831,8 @@ void AssetsManagerEx::prepareUpdate()
|
||||
_downloadedSize.clear();
|
||||
_totalEnabled = false;
|
||||
|
||||
// Temporary manifest exists, resuming previous download
|
||||
if (_tempManifest && _tempManifest->isLoaded() && _tempManifest->versionEquals(_remoteManifest))
|
||||
// Temporary manifest exists, previously updating and equals to the remote version, resuming previous download
|
||||
if (_tempManifest && _tempManifest->isLoaded() && _tempManifest->isUpdating() && _tempManifest->versionEquals(_remoteManifest))
|
||||
{
|
||||
_tempManifest->saveToFile(_tempManifestPath);
|
||||
_tempManifest->genResumeAssetsList(&_downloadUnits);
|
||||
@@ -838,8 +867,6 @@ void AssetsManagerEx::prepareUpdate()
|
||||
{
|
||||
// Generate download units for all assets that need to be updated or added
|
||||
std::string packageUrl = _remoteManifest->getPackageUrl();
|
||||
// Save current download manifest information for resuming
|
||||
_tempManifest->saveToFile(_tempManifestPath);
|
||||
// Preprocessing local files in previous version and creating download folders
|
||||
for (auto it = diff_map.begin(); it != diff_map.end(); ++it)
|
||||
{
|
||||
@@ -856,6 +883,10 @@ void AssetsManagerEx::prepareUpdate()
|
||||
_tempManifest->setAssetDownloadState(it->first, Manifest::DownloadState::UNSTARTED);
|
||||
}
|
||||
}
|
||||
// Start updating the temp manifest
|
||||
_tempManifest->setUpdating(true);
|
||||
// Save current download manifest information for resuming
|
||||
_tempManifest->saveToFile(_tempManifestPath);
|
||||
|
||||
_totalWaitToDownload = _totalToDownload = (int)_downloadUnits.size();
|
||||
}
|
||||
@@ -888,6 +919,9 @@ void AssetsManagerEx::startUpdate()
|
||||
|
||||
void AssetsManagerEx::updateSucceed()
|
||||
{
|
||||
// Set temp manifest's updating
|
||||
_tempManifest->setUpdating(false);
|
||||
|
||||
// Every thing is correctly downloaded, do the following
|
||||
// 1. rename temporary manifest to valid manifest
|
||||
_fileUtils->renameFile(_tempStoragePath, TEMP_MANIFEST_FILENAME, MANIFEST_FILENAME);
|
||||
@@ -957,6 +991,8 @@ void AssetsManagerEx::checkUpdate()
|
||||
_updateEntry = UpdateEntry::CHECK_UPDATE;
|
||||
|
||||
switch (_updateState) {
|
||||
case State::FAIL_TO_UPDATE:
|
||||
_updateState = State::UNCHECKED;
|
||||
case State::UNCHECKED:
|
||||
case State::PREDOWNLOAD_VERSION:
|
||||
{
|
||||
@@ -968,7 +1004,6 @@ void AssetsManagerEx::checkUpdate()
|
||||
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ALREADY_UP_TO_DATE);
|
||||
}
|
||||
break;
|
||||
case State::FAIL_TO_UPDATE:
|
||||
case State::NEED_UPDATE:
|
||||
{
|
||||
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND);
|
||||
|
||||
@@ -188,12 +188,16 @@ public:
|
||||
|
||||
CC_CONSTRUCTOR_ACCESS:
|
||||
|
||||
AssetsManagerEx(const std::string& manifestUrl, const std::string& storagePath, const VersionCompareHandle& handle = nullptr);
|
||||
AssetsManagerEx(const std::string& manifestUrl, const std::string& storagePath);
|
||||
|
||||
AssetsManagerEx(const std::string& manifestUrl, const std::string& storagePath, const VersionCompareHandle& handle);
|
||||
|
||||
virtual ~AssetsManagerEx();
|
||||
|
||||
protected:
|
||||
|
||||
void init(const std::string& manifestUrl, const std::string& storagePath);
|
||||
|
||||
std::string basename(const std::string& path) const;
|
||||
|
||||
std::string get(const std::string& key) const;
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#define KEY_VERSION_URL "remoteVersionUrl"
|
||||
#define KEY_GROUP_VERSIONS "groupVersions"
|
||||
#define KEY_ENGINE_VERSION "engineVersion"
|
||||
#define KEY_UPDATING "updating"
|
||||
#define KEY_ASSETS "assets"
|
||||
#define KEY_COMPRESSED_FILES "compressedFiles"
|
||||
#define KEY_SEARCH_PATHS "searchPaths"
|
||||
@@ -73,6 +74,7 @@ static int cmpVersion(const std::string& v1, const std::string& v2)
|
||||
Manifest::Manifest(const std::string& manifestUrl/* = ""*/)
|
||||
: _versionLoaded(false)
|
||||
, _loaded(false)
|
||||
, _updating(false)
|
||||
, _manifestRoot("")
|
||||
, _remoteManifestUrl("")
|
||||
, _remoteVersionUrl("")
|
||||
@@ -88,6 +90,7 @@ Manifest::Manifest(const std::string& manifestUrl/* = ""*/)
|
||||
Manifest::Manifest(const std::string& content, const std::string& manifestRoot)
|
||||
: _versionLoaded(false)
|
||||
, _loaded(false)
|
||||
, _updating(false)
|
||||
, _manifestRoot("")
|
||||
, _remoteManifestUrl("")
|
||||
, _remoteVersionUrl("")
|
||||
@@ -188,6 +191,22 @@ bool Manifest::isLoaded() const
|
||||
return _loaded;
|
||||
}
|
||||
|
||||
void Manifest::setUpdating(bool updating)
|
||||
{
|
||||
if (_loaded && _json.IsObject())
|
||||
{
|
||||
if (_json.HasMember(KEY_UPDATING) && _json[KEY_UPDATING].IsBool())
|
||||
{
|
||||
_json[KEY_UPDATING].SetBool(updating);
|
||||
}
|
||||
else
|
||||
{
|
||||
_json.AddMember<bool>(KEY_UPDATING, updating, _json.GetAllocator());
|
||||
}
|
||||
_updating = updating;
|
||||
}
|
||||
}
|
||||
|
||||
bool Manifest::versionEquals(const Manifest *b) const
|
||||
{
|
||||
// Check manifest version
|
||||
@@ -528,6 +547,12 @@ void Manifest::loadVersion(const rapidjson::Document &json)
|
||||
_engineVer = json[KEY_ENGINE_VERSION].GetString();
|
||||
}
|
||||
|
||||
// Retrieve updating flag
|
||||
if ( json.HasMember(KEY_UPDATING) && json[KEY_UPDATING].IsBool() )
|
||||
{
|
||||
_updating = json[KEY_UPDATING].GetBool();
|
||||
}
|
||||
|
||||
_versionLoaded = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -139,6 +139,16 @@ public:
|
||||
*/
|
||||
void parseJSONString(const std::string& content, const std::string& manifestRoot);
|
||||
|
||||
/** @brief Get whether the manifest is being updating
|
||||
* @return Updating or not
|
||||
*/
|
||||
bool isUpdating() const { return _updating; };
|
||||
|
||||
/** @brief Set whether the manifest is being updating
|
||||
* @param updating Updating or not
|
||||
*/
|
||||
void setUpdating(bool updating);
|
||||
|
||||
protected:
|
||||
|
||||
/** @brief Load the json file into local json object
|
||||
@@ -228,6 +238,9 @@ private:
|
||||
//! Indicate whether the manifest have been fully loaded
|
||||
bool _loaded;
|
||||
|
||||
//! Indicate whether the manifest is updating and can be resumed in the future
|
||||
bool _updating;
|
||||
|
||||
//! Reference to the global file utils
|
||||
FileUtils *_fileUtils;
|
||||
|
||||
|
||||
Submodule native/tools/bindings-generator updated: 1112ee219d...f62bdefb18
Reference in New Issue
Block a user