[SOLVED] How to use Assets Manager for hot update?

Hi,

My project is using Cocos2d-JS v3.8.1 and i would like to use Assets Manager for hot update.

I have try to follow the guide here to create manifest and upload to my server
http://cocos2d-x.org/docs/manual/framework/html5/v3/assets-manager/en

Then i study the JS Asset Manager test case and implement it in my project, the code success go into

case jsb.EventAssetsManager.UPDATE_FINISHED:

After that, i called cc.game.restart(); and success updated app.jsc file.

However, jsb.EventAssetsManager.ALREADY_UP_TO_DATE: is never called since it already updated, instead, it keep go into case jsb.EventAssetsManager.UPDATE_FINISHED: which make the hot update become infinite loop.

Anyone have experiences on using Assets Manager? Please give some guide on how to use it.

Here is the project.manifest on my server

{
"packageUrl" : "http://www.zinitt.com/test/",
"remoteManifestUrl" : "http://www.zinitt.com/test/project.manifest",
"remoteVersionUrl" : "http://www.zinitt.com/test/version.manifest",
"version" : "1.1.0",
"engineVersion" : "3.8.1",

"assets" : {
	"src/app.zip" : {
		"md5" : "...",
                "compressed" : true
	}
},

"searchPaths" : [
 ]
}

Thank you.

Edit : The code is working, just need md5

I update asset success but when i call cc.game.restart(), my console print “Cocos2d-JS v3.10” and do nothing, can anyone help me, thank all, below is my code

case jsb.EventAssetsManager.UPDATE_FINISHED:
    cc.log("Update finished. " + event.getMessage());

    // Register the manifest's search path
    var searchPaths = self.manager.getLocalManifest().getSearchPaths();
    cc.log("SearchPaths: " + searchPaths);
    // This value will be retrieved and appended to the default search path during game startup,
    // please refer to samples/js-tests/main.js for detailed usage.
    // !!! Re-add the search paths in main.js is very important, otherwise, new scripts won't take effect.
    cc.sys.localStorage.setItem("ExternalPaths", JSON.stringify(searchPaths));
    // Restart the game to make all scripts take effect.
    cc.game.restart();

    self.manager.release();
    break;

@huyhungkun

I think you don’t need to release the manager after cc.game.restart(); as the app is restarting.

self.manager.release();

However, i calling the cc.game.restart(); with a schedule.

self.scheduleOnce(function(){cc.game.restart();}, 3.0);

And make sure you have add related path and directory on main.js, you can refer to main.js of test project.

if(cc.sys){
    var ExternalPaths = cc.sys.localStorage.getItem("ExternalPaths");
    if (ExternalPaths)
        jsb.fileUtils.setSearchPaths(JSON.parse(ExternalPaths));
} 

cc.game.onStart = function(){

if (cc.sys.isNative) 
{
            //related paths and directory
	var searchPaths = jsb.fileUtils.getSearchPaths();
	searchPaths.push('script');
	searchPaths.push('src');
	searchPaths.push('res');
	jsb.fileUtils.setSearchPaths(searchPaths);
}

}, this);
};
cc.game.run();

I only tried with Cocos2d-JS v3.8.1, not sure about v3.10

Thank you, your solution is work. I loaded a background (“Images/background.jpg”). How to get and add it to screen. And I have a script is HandleBackground.js, I want hot update the new script, how to override old script with new script.

@huyhungkun

This is how i do.

Let’s say we have

"Images/background.jpg"
"src/HandleBackground.js"

1.All the resources need to update must compress them separately. I’m not sure why they must be in a zip.

"Images/background.zip"
"src/HandleBackground.zip"

2.Get the md5 of each file, in Mac, open terminal $md5 file can get.
3.version.manifest

{
	"packageUrl" : "http://www.zinitt.com/",
	"remoteManifestUrl" : "http://www.zinitt.com/project.manifest",
	"remoteVersionUrl" : "http://www.zinitt.com/version.manifest",
	"version" : "1.1.0",
	"engineVersion" : "3.8.1"
}

4.project.manifest

{
	
"packageUrl" : "http://www.zinitt.com/",
	"remoteManifestUrl" : "http://www.zinitt.com/project.manifest",
	"remoteVersionUrl" : "http://www.zinitt.com/version.manifest",
	"version" : "1.1.0",
	"engineVersion" : "3.8.1",
	"groupVersions" : 
	{
		"1" : "1.1.0"
    	},
	"assets" : 
	{
		"update110" : 
		{
            		"path" : "src/HandleBackground.zip",
           		"md5" : "3c635894bc2ed24d7c1f27887a2ff286",
           		"compressed" : true,
           		"group" : "1"
        	},
		"update1101" : 
		{
            		"path" : "Images/background.zip",
           		"md5" : "2405bd600aefa0fbf4df2f5583a33d26",
           		"compressed" : true,
           		"group" : "1"
        	}
	},
	"searchPaths" : []
}

5.As i mentioned above, you need to update the search path

var searchPaths = jsb.fileUtils.getSearchPaths();
	searchPaths.push('script');
	searchPaths.push('src');
	searchPaths.push('Images');
	jsb.fileUtils.setSearchPaths(searchPaths);

6.Then you can use the image as usual with Images/background.jpg

It become more complicated if you adding new .js file instead of replace js file.

You need to update “jsList” array but i’m not sure how as i compiled all source file into 1 .jsc file.

Yeah, I did it, thank you :smiley:

@huyhungkun

So you have solved your problem? That’s good.

The above link is not working, can you please provide some sample code for assets manager, like when to check for update , how to listen to asset manager update event etc.

Thanks

1 Like

The documentation for assets-manager can be accessed using http://www.cocos2d-x.org/docs/creator/manual/en/advanced-topics/assets-manager.html