Implementation of COCOS2DX LUA thermal update scheme

Source: Internet
Author: User

(Upgrade.h)

#include <stdio.h>

#include "Cocos2d.h"

#include "Framework/utils/utils.h"

#include "Framework/json/jsonobject.h"

USING_NS_CC;

#include "Ide-support/simpleconfigparser.h"

#include "Extensions/cocos-ext.h"

Using_ns_cc_ext;

using namespace Std;

Class Upgrade:public Layer, public assetsmanagerdelegateprotocol

{

Public

Static scene* Createscene ();

Upgrade ();

virtual void onEnter ();

Virtual ~upgrade ();

virtual BOOL init ();

void Enterscene ();

void upgrade (); //Check for version updates

overriding three virtual functions in a Assetsmanagerdelegateprotocol

virtual void OnError (Assetsmanager::errorcode ErrorCode); //Error Messages

virtual void onprogress (int percent); //Update download Progress

virtual void onsuccess (); //Download Successful

Create_func (Upgrade);

Private

assetsmanager* Getassetmanager ();

String Directorypathcache_res;

String directorypathcache_src;

String directorypathcache_resources;

};

#include "framework/updater/upgrade.h"

#include "SimpleAudioEngine.h"

#include "CCLuaEngine.h"

#include "Lua_module_register.h"

#include "Cocos2d.h"

#if (Cc_target_platform! = cc_platform_linux)

#include "Ide-support/codeidesupport.h"

#endif

#if (Cocos2d_debug > 0) && (Cc_code_ide_debug_support > 0)

#include "runtime/runtime.h"

#include "ide-support/runtimeluaimpl.h"

#endif

(Upgrade.cpp)

USING_NS_CC;

Using_ns_cc_ext;

using namespace Cocosdenshion;

#define TEMP_PACKAGE_FILE_NAME "Scripts" //The folder name saved after download

static const char * updaterconfig= "Res/updaterconfig.json";

static const char * key_packageurl= "PackageURL";

static const char * key_versionurl= "versionurl";

Upgrade::upgrade ()

{

Std::vector<std::string> searchpaths;

#if (Cc_target_platform = = cc_platform_android)

Fileutils::getinstance ()->setpopupnotify (false);

Searchpaths.push_back ("ccs-res/");

Searchpaths.push_back ("ccs-res/res/");

String path= stringutils::format ("%s%s", Fileutils::getinstance ()->getwritablepath (). C_STR (), "debugruntime/");

Directorypathcache_resources=path;

Directorypathcache_res=stringutils::format ("%s/res/", Path.c_str ());

Directorypathcache_src=stringutils::format ("%s/src/", Path.c_str ());;

#elif (Cc_target_platform = = Cc_platform_ios)

Directorypathcache_res=fileutils::getinstance ()->fullpathforfilename ("Res");

Directorypathcache_resources=stringutils::format ("%s/", Directorypathcache_res.substr (0, DirectoryPathCache_ Res.find_last_of ("/")). C_str ());

Directorypathcache_src=fileutils::getinstance ()->fullpathforfilename ("SRC");

#endif

    

Searchpaths.push_back (DIRECTORYPATHCACHE_SRC);

Searchpaths.push_back (Directorypathcache_res);

Fileutils::getinstance ()->setsearchpaths (searchpaths);

}

scene* Upgrade::createscene ()

{

Auto scene = Scene::create ();

Auto layer = Upgrade::create ();

Scene->addchild (layer);

return scene;

}

Upgrade::~upgrade ()

{

assetsmanager* Assetmanager = Getassetmanager ();

Cc_safe_delete (Assetmanager);

#if (Cocos2d_debug > 0) && (Cc_code_ide_debug_support > 0)

Note:please don ' t remove the If want to debug with Cocos Code IDE

Runtimeengine::getinstance ()->end ();

#endif

}

BOOL Upgrade::init ()

{

if (! Cclayer::init ())

{

return false;

}

return true;

}

void Upgrade::onerror (Assetsmanager::errorcode ErrorCode)

{

if (ErrorCode = = assetsmanager::errorcode::no_new_version)

{

Cclog ("Check for new version:%s", "Currently the latest version");

}

else if (ErrorCode = = assetsmanager::errorcode::network)

{

Cclog ("Check for new version:%s", "Update failed please check network status");

}

else if (ErrorCode = = assetsmanager::errorcode::create_file)

{

Cclog ("Check for new version:%s", "Create temporary file failed");

}else if (ErrorCode = = assetsmanager::errorcode::uncompress) {

Cclog ("Check for new version:%s", "Update package decompression failed");

}

}

void upgrade::onprogress (int percent)

{

If (Percent < 0)

Return

Cclog ("Download Progress:%d%%", percent);

}

void Upgrade::onenter () {

Cclog ("onenter!");

Upgrade ();

}

void Upgrade::onsuccess ()

{

Cclog ("Download complete!");

This->enterscene ();

}

void Upgrade::enterscene () {

#if (Cocos2d_debug > 0) && (Cc_code_ide_debug_support > 0)

Runtimeengine::getinstance ()->addruntime (Runtimeluaimpl::create (), Kruntimeenginelua);

Runtimeengine::getinstance ()->start ();

Cclog ("ishow!");

#else

Auto engine = Luaengine::getinstance ();

Scriptenginemanager::getinstance ()->setscriptengine (engine);

Lua_module_register (Engine->getluastack ()->getluastate ());

Engine->getluastack ()->setxxteakeyandsign ("2dxLua", strlen ("2dxLua"), "Xxtea", strlen ("Xxtea"));

Engine->executescriptfile ("Src/main.lua");

#endif

}

static void Configandroidparameters (Jsonobject * object) {

#if (Cc_target_platform = = cc_platform_android)

Jsonobject * Json=jsonobject::create ();

Json->put ("Netlog_url", Object->getstring ("Netlog_url"). C_STR ());

Utils::configparameters (Json->tostring ());

#endif

}

assetsmanager* Upgrade::getassetmanager ()

{

static Assetsmanager *assetmanager = NULL;

if (!assetmanager)

{

static Assetsmanager *assetmanager = NULL;

if (!assetmanager)

{

String filecontent = "";

#if (Cc_target_platform = = cc_platform_android)

String Fullpathfile=stringutils::format ("%s%s%s", Fileutils::getinstance ()->getwritablepath (). C_STR (), " debugruntime/", updaterconfig);

Cclog ("File path:--------%s", Fullpathfile.c_str ());

Filecontent = Fileutils::getinstance ()->getstringfromfile (fullpathfile);

#elif (Cc_target_platform = = Cc_platform_ios)

String Fullpathfile=fileutils::getinstance ()->fullpathforfilename (updaterconfig);

Filecontent = Fileutils::getinstance ()->getstringfromfile (fullpathfile);

Cclog ("File path:--------%s", Fullpathfile.c_str ());

#endif

Cclog ("config file contents:%s", Filecontent.c_str ());

Jsonobject * Object=jsonobject::create (FILECONTENT.C_STR ());

Configandroidparameters (object);

Get update package Address

String packageurl=object->getstring (Key_packageurl);

Get the address of the version number

String versionurl=object->getstring (Key_versionurl);

Cclog ("Update package Address:%s", Packageurl.c_str ());

Cclog ("Get version number address:%s", Versionurl.c_str ());

String storagedirectory=directorypathcache_resources;

Cclog ("Download storage path:%s", Storagedirectory.c_str ());

Assetmanager = new Assetsmanager (Packageurl.c_str (), Versionurl.c_str (), Storagedirectory.c_str ());

Assetmanager->setdelegate (this);

Assetmanager->setconnectiontimeout (8);

Cclog ("Current version number:%s", Assetmanager->getversion (). C_STR ());

if (Assetmanager->checkupdate ()) {

Assetmanager->update ();

}else{

This->enterscene ();

}

}

}

return assetmanager;

}

void Upgrade::upgrade ()

{

Getassetmanager ();

}

#endif

(The author is lazy, do not comment here, subsequent modifications)

Implementation of COCOS2DX LUA thermal update scheme

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.