(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(); //检查版本更新

  //重写AssetsManagerDelegateProtocol中的三个虚函数

virtual void onError(AssetsManager::ErrorCode errorCode); //错误信息

virtual void onProgress(int percent); //更新下载进度

virtual void onSuccess(); //下载成功

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" //下载后保存的文件夹名

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 this call if you 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("检查新版本: %s","当前已是最新版本");

}

else if (errorCode == AssetsManager::ErrorCode::NETWORK)

{

CCLOG("检查新版本: %s","更新失败 请检查网络状态");

}

else if (errorCode == AssetsManager::ErrorCode::CREATE_FILE)

{

CCLOG("检查新版本: %s","创建临时文件失败");

}else if(errorCode == AssetsManager::ErrorCode::UNCOMPRESS){

CCLOG("检查新版本: %s","更新包解压失败");

}

}

void Upgrade::onProgress(int percent)

{

if (percent < 0)

return;

CCLOG("下载进度: %d%%",percent);

}

void Upgrade::onEnter(){

CCLOG("onEnter!");

upgrade();

}

void Upgrade::onSuccess()

{

CCLOG("下载完毕!");

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("文件路径:--------%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("文件路径:--------%s",fullPathFile.c_str());

#endif

CCLOG("配置文件内容:%s",fileContent.c_str());

JSONObject * object=JSONObject::create(fileContent.c_str());

ConfigAndroidParameters(object);

//获取更新包地址

string packageUrl=object->getString(KEY_PackageURL);

//获取版本号地址

string versionUrl=object->getString(KEY_VersionURL);

CCLOG("更新包地址:%s",packageUrl.c_str());

CCLOG("获取版本号地址:%s",versionUrl.c_str());

string storageDirectory=DirectoryPathCache_Resources;

CCLOG("下载存储路径:%s",storageDirectory.c_str());

assetManager = new AssetsManager(packageUrl.c_str(),versionUrl.c_str(), storageDirectory.c_str());

assetManager->setDelegate(this);

assetManager->setConnectionTimeout(8);

CCLOG("当前版本号:%s",assetManager->getVersion().c_str());

if (assetManager->checkUpdate()) {

assetManager->update();

}else{

this->enterScene();

}

}

}

return assetManager;

}

void Upgrade::upgrade()

{

getAssetManager();

}

#endif

(作者很懒,此处不做注释,后续修改)

cocos2dx lua 热更新方案的实现的更多相关文章

  1. 腾讯开源手游热更新方案,Unity3D下的Lua编程

    原文:http://www.sohu.com/a/123334175_355140 作者|车雄生 编辑|木环 腾讯最近在开源方面的动作不断:先是微信跨平台基础组件Mars宣布开源,腾讯手游又于近期开源 ...

  2. 【腾讯Bugly干货分享】手游热更新方案xLua开源:Unity3D下Lua编程解决方案

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com/s/2bY7A6ihK9IMcA0bOFyB-Q 导语 xL ...

  3. 移动端热更新方案(iOS+Android)

    PPT资源包含iOS+Android 各种方案分析:https://github.com/qiyer/Share/blob/master/%E7%83%AD%E6%9B%B4%E6%96%B0%E5% ...

  4. Unity代码热更新方案 JSBinding + SharpKit 首页

    目前Unity的代码更新方案有很多,主要以lua为主. JSBinding + SharpKit 是一种新的技术,他做了两件事情: JSBinding将C#导出到 JavaScript (引擎是 Mo ...

  5. Unity3D 热更新方案(集合各位专家的汇总)

    http://blog.csdn.net/guofeng526/article/details/52662994 热更新”这个词,在Unity3D的应用下,是有些语义错误的,但是作为大家都熟知的一项技 ...

  6. Unity实现c#热更新方案探究(三)

    转载请标明出处:http://www.cnblogs.com/zblade/ 前面两篇文章从头到尾讲解了C#热更新的一些方案,从程序域来加载和卸载DLL,到使用ILRuntime来实现安卓和IOS平台 ...

  7. Unity3D 热更新方案总结

    如何评价腾讯在Unity下的xLua(开源)热更方案? Unity 游戏用XLua的HotFix实现热更原理揭秘 腾讯开源手游热更新方案,Unity3D下的Lua编程 [Unity]基于IL代码注入的 ...

  8. Unity官方发布热更新方案性能对照

    孙广东  2016.3.11 Unity应用的iOS热更新 作者:丁治宇 Unity TechnologiesChina Agenda •  什么是热更新 •  为何要热更新 •  怎样在iOS 上对 ...

  9. unity热更新方案对比

    Unity应用的iOS热更新 •  什么是热更新 •  为何要热更新 •  怎样在iOS 上对Unity 应用进行热更新 •  支持Unity iOS 热更新的各种Lua 插件的对照 什么是热更新 • ...

随机推荐

  1. PV、UV、VV、IP是什么意思?

    PV.UV.VV.IP作为网站分析中最常见的基础指标,能够从宏观概括性地衡量网站的整体运营状况,也是检测网站运营是否正常的最直观的指标. 1.VV(来访次数/访问次数):VisitView 记录所有访 ...

  2. Centos7中给gitLab汉化

    第一步:安装git 下载补西,东西在清华大学开源中心 yum install –y git 第二步:下载 git clone https://gitlab.com/xhang/gitlab.git 第 ...

  3. jsp学习与提高(二)——JSP 隐式对象、表单处理及过滤器

    1.JSP 隐式对象 JSP隐式对象是JSP容器为每个页面提供的Java对象,开发者可以直接使用它们而不用显式声明.JSP隐式对象也被称为预定义变量. JSP所支持的九大隐式对象: 对象 描述 req ...

  4. BZOJ 1036 && Luogu P2590 [ZJOI2008]树的统计 树链剖分

    链剖裸题...你值得一做~ 用线段树多维护一个mx,少写一个tag #include<cstdio> #include<iostream> #define ll long lo ...

  5. 洛谷2414(构建ac自动机fail树dfs序后遍历Trie树维护bit及询问答案)

    要点 这是一道蔡队题,看我标题行事 任意询问y串上有多少个x串,暴力找每个节点是不是结尾肯定是炸的,考虑本质:如果某节点是x的结尾,根据ac自动机的性质,x一定是此(子)串后缀.又有每个Trie节点的 ...

  6. 用 scp 命令通过 SSH 互传文件

    上传单个文件到远程服务器 命令格式 scp [/path/local_dir/filename] [username@servername:/path/remote_dir] 上传本地的 vimrc ...

  7. 四则运算二(java web)

    最近我和我的小伙伴yaoyali结成对子,共同写网页版的四则运算.虽然现在还没弄好,但是比起上次用纯java写的四则运算有了很大改进. 一.存放四则运算题目和答案的类 package com.jaov ...

  8. (转)mysqldump: Got error: 1556: You can't use locks with log tables.

    mysqldump: Got error: 1556: You can't use locks with log tables. 原文:http://blog.51cto.com/oldboy/112 ...

  9. ms sqlserver 登录失败 错误:4064

    无法打开用户默认数据库.登录失败.用户‘sa’登录失败.(Microsoft SQL Server, 错误:4064) 解决方法:解决方法:先用windows身份验证的方式登录进去,然后在 安全性=& ...

  10. Azkaban2.5安装部署(系统时区设置 + 安装和配置mysql + Azkaban Web Server 安装 + Azkaban Executor Server安装 + Azkaban web server插件安装 + Azkaban Executor Server 插件安装)(博主推荐)(五)

    Azkaban是什么?(一) Azkaban的功能特点(二) Azkaban的架构(三) Hadoop工作流引擎之Azkaban与Oozie对比(四) 不多说,直接上干货! http://www.cn ...