From 714ca23e4784102d2eaa3a8754f636d23ec7b91c Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 16 May 2017 15:06:03 +0800 Subject: [PATCH] Check previous updating state to resume --- .../assets-manager/AssetsManagerEx.cpp | 13 +++++++--- native/extensions/assets-manager/Manifest.cpp | 25 +++++++++++++++++++ native/extensions/assets-manager/Manifest.h | 13 ++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/native/extensions/assets-manager/AssetsManagerEx.cpp b/native/extensions/assets-manager/AssetsManagerEx.cpp index a1abd71c9b..6e9ef4c6c4 100644 --- a/native/extensions/assets-manager/AssetsManagerEx.cpp +++ b/native/extensions/assets-manager/AssetsManagerEx.cpp @@ -802,8 +802,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 +838,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 +854,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 +890,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); diff --git a/native/extensions/assets-manager/Manifest.cpp b/native/extensions/assets-manager/Manifest.cpp index d6f8e3e126..3c8085639b 100644 --- a/native/extensions/assets-manager/Manifest.cpp +++ b/native/extensions/assets-manager/Manifest.cpp @@ -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(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; } diff --git a/native/extensions/assets-manager/Manifest.h b/native/extensions/assets-manager/Manifest.h index 8d9f15e779..f59bbcf1f8 100644 --- a/native/extensions/assets-manager/Manifest.h +++ b/native/extensions/assets-manager/Manifest.h @@ -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;