Check previous updating state to resume

This commit is contained in:
pandamicro
2017-05-16 15:06:03 +08:00
parent 8e7c661291
commit 714ca23e47
3 changed files with 47 additions and 4 deletions
@@ -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);
@@ -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;