Before I contacted the hot update, I was confused about the hot update. Later, I read some materials, tried it myself, and basically understood its process.
The so-called hot update is to update code and resources online.
The process of hot update
First, the client sends a request to the server, and the server tells the client that there is no update, and you are the latest, so just skip it. But if you are telling you that there is an update, then tell me what needs to be updated, right, you may need to update things, put them in a file, and send them to the client. The client will get this file and go one by one. The server wants, and finally downloads all the content to be updated to the local. But if the downloaded resource already exists before, will there be a problem?
Let’s take the win32 platform as an example
This is the project directory
For the download directory, a Normally
C:\Users\user\AppData< span class="hljs-symbol" style="color:rgb(203,75,22)">\Local\LuaGame4
But there is another problem. Usually we call resources directly under the project directory. This will be downloaded to the c drive. How can you call it?
This involves a priority issue. For example, the path of an image is img/sample.png. In the project The full path under the directory is
E/LuaGame4/res/img/sample.png
, then we usually call this in the code
local sp = cc.Sprite:create("img/sample.png")self:addChild( sp )
Going back to the previous question, now I’m going to update this picture, the above is downloading
C:\Users\user\AppData\ Local\LuaGame4
In this directory, we need to use the same The code can be adjusted To use new resources, as long as the resource path in this directory is consistent with our project, then set C:\Users\user\AppData\Local\LuaGame4
Join Search the path, and set its priority to the highest, then it can be called.
Specific implementation
know its rough The working process will be very clear after actual operation.
In cocos2dx, the class AssetsManagerEx is used. For AssetsManager this class is not recommended. There are many things in this class that we haven’t considered, so we won’t go into it.
Code first
local writablePath = cc.FileUtils:getInstance():getWritablePath() local storagePath = writablePath .. "new_version"--Set the src and res of the download directory as the search directory with the highest priority, so as to ensure that the downloaded file can cover the original code cc.FileUtils:getInstance():addSearchPath(storagePath..< span class="hljs-string" style="color:rgb(42,161,152)">"/src/",true) cc.FileUtils:getInstance():addSearchPath(storagePath.."/res/", true) - Create AssetsManagerEx object local as setsManagerEx = cc.AssetsManagerEx:create("src/version/project.manifest", storagePath) assetsManagerEx:retain()- -Set the download message listener local function handleAssetsManagerEx(event) if (cc.EventAssetsManagerEx.EventCode.ALREADY_UP_TO_DATE == event: getEventCode()) then print("is the latest version, enter the main interface of the game") - app:enterScene( "GameScene") end if (cc.EventAssetsManagerEx.EventCode.NEW_VERSION_FOUND == event:getEventCode()) then print("found a new version, Start upgrading") end if (cc.EventAssetsManagerEx.EventCode.UPDATE_PROGR ESSION == event:getEventCode()) then print("Update progress=" .. event:getPercent()) end if (cc.EventAssetsManagerEx.EventCode.UPDATE_FINISHED == event:getEventCode()) then print("Update complete, restart") app:run() end if (cc.EventAssetsManagerEx.EventCode.ERROR_NO_LOCAL_MANIFEST == event:getEventCode()) then print( "An error occurred: The manifest file cannot be found locally") end if (cc.EventAssetsManagerEx.EventCode .ERROR_DOWNLOAD_MANIFEST == event:getEventCode()) then print("An error occurred: Failed to download the manifest file") end if (cc.EventAssetsManagerEx.EventCode.ERROR_PARSE_MANIFEST == event:getEventCode()) then print("An error occurred: Failed to parse the manifest file") end if (cc.EventAssetsManagerEx.EventCode.ERROR_UPDATING == event:getEventCode()) then print("An error occurred: Update failed") end end local dispatcher = cc.Director:getInstance():getEventDispatcher() local eventListenerAssetsManagerEx = cc.EventListenerAssetsManagerEx:create(assetsManagerEx, handleAssetsManagerEx) dispatcher:addEventListenerWithFixedPriority(eventListenerAssetsManagerEx, 1) - check the version and Upgrade assetsManagerEx:update()
follow first
-- Create AssetsManagerEx objects local assetsManagerEx = cc. AssetsManagerEx:create("src /version/project.manifest", storagePath)
To start looking, first build a AssetsManagerEx:create object, which needs to be passed in two Parameters
- project.manifest path< /li>
- download path
The project.manifest path I passed in here is src/version/project.manifest
What kind of file is this, open it and see
{ "packageUrl": "http:// localhost:8080/examples/servlets/update/assets/", "remoteManifestUrl": "http://localhost:8080/examples/servlets/update/version/project.manifest", "remoteVersionUrl": "http://localhost:8080/examples/servlets/update/version/ /version.manifest", "version": "1.0.0", "engineVersion": "3.x dev", "assets": {"res/blocks.png": {"md5": "...."} }, "searchPaths": []}
< p style="margin-top:0px; margin-bottom:25px; word-break:break-word; col or:rgb(47,47,47); font-size:16px; line-height:27.2px"> This is a json format file, explain the meaning of each key
key | Role |
---|---|
packageUrl | Update the url of the package |
remoteManifestUrl | project.manifest url |
remoteVersionUrl | this file and project.manifest A meaning, but more concise than project.manifest, I will say later |
version | version, whether it needs to be updated is up to him |
engineVersion | Engine version, it doesn’t matter whether it is written or not |
assets | All file names and their md5 values will be compared with the local and remote md5 values when they are updated. If they are inconsistent, they will be updated, otherwise they will not be updated. |