http://www.cnblogs.com/tuituIEr/articles/3859501.HTML 这也是一篇热更新的文章
来到园子这么久了,竟然连博客写博客的权限才开通,相当的汗颜啊,之前在百度空间写的博客,发现还是术业有专攻最好,程序员还是老老实实呆在程序员的圈子里吧!对了,之前百度空间的地址顺便宣传下,哈哈:
http://hi.baidu.com/awming520
---------------------------------正文分割线------------------------------------
本文是探索cocos-x热更新,其实就是探索cocos-lua或者Js的脚本和资源热更新,热更的作用也不用我在这里为大家普及了。
文中主要使用python作为服务器的脚本,lua作为客户端脚本,是用cocos code IDe作为开发环境。
在此之前可以参考我之前发表在cocoachina上关于热更新后台的实现,里面大概讲解了实现与原理(文件结构有小小变动,现在增加了项目名称,可以发布多个项目):
http://www.cocoachina.com/bbs/read.php?tid=213054
后台环境:
python 2.7 + webpy0.37
webpy安装:
1、解压webpy,cmd至webpy路径下
2、cmd运行python setup.py install
详细了解webpy,请访问其官网:http://webpy.org/
为了保持客户端最安全与最小化热更新,我们需要判断出每个版本的差异,将差异文件下载到客户端。为了差异识别,我们将生产res与src下所有文件的MD5,并将其保存至相关文件夹,保存内容如下:
src/test1.lua:e6e8feb29a41a0d83c29c834c10a42a2
src/test2.lua:b87957f382a4a292db91bd937b66a000
res/ani/enemy/monster.csb:6a42885d985f777418d97ef956758f2e
res/ani/enemy/monster.exportJson:1c26ec5818281794e98a4d05c3d871c4
后面我们只需要对比客户端当前版本与服务器最新版本的资源列表的md5以及判断哪些是新增文件,这样我们就可最小化获取需要更新的列表。
到此,我们可以将此文件连同当前版本文件放至服务器,让客户端下载更新列表,并在客户端判断需要下载那些文件,但是这样真的好么,如果一次更新n个文件,那样的连接将会有n次,可能是我多虑了。但我考虑到我们的版本不可能无限次发布,因此我决定将版本差异的包放在服务器,这样客户端只用告诉我们他当前什么版本,就可以获取到升级至最新版本的更新包。
比如客户端当前版本为1.0,服务器最新版本为2.0,通过url传至服务器,客户端就可以获取一个1.0升级到2.0的资源包,客户端通过assertManager可以文件解压,我们再将解压目录文件移动至我们的查找目录就ok了。
服务器通过各个版本的资源列表,获取改动和新增的文件,并将其打包压缩至zip文件。而且由于我们发布的版本是确定的,因此这些更新文件在服务器只会生成一次,对服务器来说基本没什么压力,而且我们可以将其预先生成,保存至服务器。
在服务端,我采用如下路径配置:
App:(项目名称)
--project:(保存最新项目资源,手工配置)
----res:(资源路径)
----src:(脚本路径)
--publish:(更新包路径,目录下文件自动生成)
----version.txt(当前最新版本号,手工修改)
----1.0(1.0版本包)
------List.txt(当前版本1.0的资源列表及MD5)
------update.zip(只有基本版本才会在当前路径层级下有更新包)
----2.0
------List.txt
------1.0
--------update.zip(1.0升级到2.0需要的更新资源包)
...
我们需要手工配置下project文件,这里面保存我们所发布的最新资源和脚本;publish下保存的是各种版本的更新包。
最终我们运行程序后,可以通过url访问版本与获取更新资源包:
获取最新版本号:http://127.0.0.1:8080/?action=ver
下载更新包:http://127.0.0.1:8080/?action=update.zip&ver=1.1(ver为本地当前版本,url中需要有.zip,cocos2d-x终对url做了判断)
下载地址:http://pan.baidu.com/s/1eQ7WJxK
其余信息可以阅读readme.txt
简单说下思路就是,将客户端当前版本与服务器最新版本做md5比对,将最新版本与客户端版本差异的文件打包,并下载至客户端,完成资源的热更新。
这种后台实现主要解决一下几个问题:
1.如果将资源包完全打包为一个文件,需要下载的文件势必会大很多,每次都会下载很多相同的文体。
2.如果按照版本差异更新,则需要将最新版本的资源包挨个下载一遍,多麻烦呀!
3.如果对服务器文件作md5记录,而单独下载相关文件。这样将会造成复杂的客户端代码,而且每个需要更新的文件都去连接服务器,给服务器造成了压力,而且包的传输肯定要比单个小文件来的快,而且更不容易出错吧!(这完全是个人猜想)
4.第三条也说了,我想让客户端代码变得简单。最简单的方式就是不写或者少些代码,我就想是用cocos-x自带的assertmanager来实现热更新。(其实我是偷懒而已)
说说我认为这样做的好处吧:
1.需要更新的资源减少了。
2.服务器需要保存的版本减少了,只需要保存最新两个版本就ok了。
3.支持版本回退(只需要将上一个版本编号改为最新版本号,并生成更新资源包)。
4.客户端代码减少,并且安全。
--------------------------------------------------------------------------------
今天我带来的是我将之前在cocoachina发表的版本完善后东西,包括服务端与客户端实现:
服务端包含了assertmanager需要的两个链接(版本与包地址的url,而且包地址必须还有.zip这样的字符串,不然会被判断无效,郁闷!)
也有预生成资源包的接口,可以在新版本发布的时候调用,用来生成最新的版本差异包。
客户端代码我是用了cocos code IDe,不过也没怎么用它framework里面的东西,修改起来很简单,其实我提供的客户端代码更多算个代码示例(demo)。
废话不多说了。服务端使用可以参考server.zip中的readme.txt。
对了:欢迎大家对我这个新手批评与指正!
------------------------------------下载地址-----------------------------------
server:http://pan.baidu.com/s/1dD8Faxj
clIEnt:http://pan.baidu.com/s/1qW16ZZA
--------------------------------------------------------------------------------
注:由于code IDe中对lua自带的文件系统lfs不支持,因此我使用了cocos2d-x自带luatest中的文件创建的接口,并将其导出至项目中。因此如果要使用客户端项目时需要注意将createDownloadDir与deleteDownloadDir两个函数导出,c++代码位置为:%engine_root%\tests\lua-tests\project\Classes\lua_assetsmanager_test_sample.cpp(.h),当然你可以选择不创建文件夹,而将更新文件直接解压到可写路径之下!貌似assertmanager可以自己创建存储路径,不过我还未测试。
另外,强烈希望cocos2d-lua能支持lfs或者写出自己功能更强的文件系统!
================================================================================================================================================================
转载于http://blog.csdn.net/linchaolong/article/details/42321767
在cocos2dx 3.4 运行通过
下载地址为:在我的下载里面
目录(?)[+]
什么是热更新?
游戏客户端启动时,主动请求服务端检查版本号,并更新资源到本地。
应用场景:
情况一:游戏客户端已经发布了,但突然发现有个比较严重的BUG需要修复。这时需要更新游戏的代码(Lua代码)。
情况二:情人节到了,需要搞个活动,在游戏中营造一个节日氛围。这时,需要更新游戏资源或增加一些功能。
好处:不需要重新打包和提交应用到市场等待审核。
在Cocos2d-x中已经封装了用于实现热更新功能的类,就是AssetsManager。
API说明:
// 检测是否有版本更新
virtual bool checkUpdate();
// 下载更新的资源包并解压到下载路径
virtual voID update();
// 获取当前客户端版本号
std::string getVersion();
// 删除客户端版本号
voID deleteVersion();
// 设置下载回调(AssetsManagerDelegateProtocol)
voID setDelegate(AssetsManagerDelegateProtocol *delegate);
// 设置连接超时时间(单位:秒)
voID setConnectionTimeout(unsigned int timeout);
// 设置从服务端下载资源包的url
voID setPackageUrl(const char* packageUrl);
// 设置服务端获取版本号的url
voID setVersionfileUrl(const char* versionfileUrl);
// 设置资源保存路径
voID setStoragePath(const char* storagePath);
因为AssetsManager使用了pthread库,所以需要在win32工程中需要包含pthread库所在目录。
VS在工程属性——C/C++——常规——附加包含目录中添加:$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread
该工程使用Cocos2d-x2.1.6和VS2012。【点击下载源码】
把工程放到引擎projects目录下即可。
<a target=_blank ID="L1" href="http://blog.csdn.net/linchaolong/article/details/42321767#L1" rel="#L1" > 1</a><a target=_blank ID="L2" href="http://blog.csdn.net/linchaolong/article/details/42321767#L2" rel="#L2" > 2</a><a target=_blank ID="L3" href="http://blog.csdn.net/linchaolong/article/details/42321767#L3" rel="#L3" > 3</a><a target=_blank ID="L4" href="http://blog.csdn.net/linchaolong/article/details/42321767#L4" rel="#L4" > 4</a><a target=_blank ID="L5" href="http://blog.csdn.net/linchaolong/article/details/42321767#L5" rel="#L5" > 5</a><a target=_blank ID="L6" href="http://blog.csdn.net/linchaolong/article/details/42321767#L6" rel="#L6" > 6</a><a target=_blank ID="L7" href="http://blog.csdn.net/linchaolong/article/details/42321767#L7" rel="#L7" > 7</a><a target=_blank ID="L8" href="http://blog.csdn.net/linchaolong/article/details/42321767#L8" rel="#L8" > 8</a><a target=_blank ID="L9" href="http://blog.csdn.net/linchaolong/article/details/42321767#L9" rel="#L9" > 9</a><a target=_blank ID="L10" href="http://blog.csdn.net/linchaolong/article/details/42321767#L10" rel="#L10" > 10</a><a target=_blank ID="L11" href="http://blog.csdn.net/linchaolong/article/details/42321767#L11" rel="#L11" > 11</a><a target=_blank ID="L12" href="http://blog.csdn.net/linchaolong/article/details/42321767#L12" rel="#L12" > 12</a><a target=_blank ID="L13" href="http://blog.csdn.net/linchaolong/article/details/42321767#L13" rel="#L13" > 13</a><a target=_blank ID="L14" href="http://blog.csdn.net/linchaolong/article/details/42321767#L14" rel="#L14" > 14</a><a target=_blank ID="L15" href="http://blog.csdn.net/linchaolong/article/details/42321767#L15" rel="#L15" > 15</a><a target=_blank ID="L16" href="http://blog.csdn.net/linchaolong/article/details/42321767#L16" rel="#L16" > 16</a><a target=_blank ID="L17" href="http://blog.csdn.net/linchaolong/article/details/42321767#L17" rel="#L17" > 17</a><a target=_blank ID="L18" href="http://blog.csdn.net/linchaolong/article/details/42321767#L18" rel="#L18" > 18</a><a target=_blank ID="L19" href="http://blog.csdn.net/linchaolong/article/details/42321767#L19" rel="#L19" > 19</a><a target=_blank ID="L20" href="http://blog.csdn.net/linchaolong/article/details/42321767#L20" rel="#L20" > 20</a><a target=_blank ID="L21" href="http://blog.csdn.net/linchaolong/article/details/42321767#L21" rel="#L21" > 21</a><a target=_blank ID="L22" href="http://blog.csdn.net/linchaolong/article/details/42321767#L22" rel="#L22" > 22</a><a target=_blank ID="L23" href="http://blog.csdn.net/linchaolong/article/details/42321767#L23" rel="#L23" > 23</a><a target=_blank ID="L24" href="http://blog.csdn.net/linchaolong/article/details/42321767#L24" rel="#L24" > 24</a><a target=_blank ID="L25" href="http://blog.csdn.net/linchaolong/article/details/42321767#L25" rel="#L25" > 25</a><a target=_blank ID="L26" href="http://blog.csdn.net/linchaolong/article/details/42321767#L26" rel="#L26" > 26</a><a target=_blank ID="L27" href="http://blog.csdn.net/linchaolong/article/details/42321767#L27" rel="#L27" > 27</a><a target=_blank ID="L28" href="http://blog.csdn.net/linchaolong/article/details/42321767#L28" rel="#L28" > 28</a><a target=_blank ID="L29" href="http://blog.csdn.net/linchaolong/article/details/42321767#L29" rel="#L29" > 29</a><a target=_blank ID="L30" href="http://blog.csdn.net/linchaolong/article/details/42321767#L30" rel="#L30" > 30</a><a target=_blank ID="L31" href="http://blog.csdn.net/linchaolong/article/details/42321767#L31" rel="#L31" > 31</a><a target=_blank ID="L32" href="http://blog.csdn.net/linchaolong/article/details/42321767#L32" rel="#L32" > 32</a><a target=_blank ID="L33" href="http://blog.csdn.net/linchaolong/article/details/42321767#L33" rel="#L33" > 33</a><a target=_blank ID="L34" href="http://blog.csdn.net/linchaolong/article/details/42321767#L34" rel="#L34" > 34</a><a target=_blank ID="L35" href="http://blog.csdn.net/linchaolong/article/details/42321767#L35" rel="#L35" > 35</a><a target=_blank ID="L36" href="http://blog.csdn.net/linchaolong/article/details/42321767#L36" rel="#L36" > 36</a><a target=_blank ID="L37" href="http://blog.csdn.net/linchaolong/article/details/42321767#L37" rel="#L37" > 37</a><a target=_blank ID="L38" href="http://blog.csdn.net/linchaolong/article/details/42321767#L38" rel="#L38" > 38</a><a target=_blank ID="L39" href="http://blog.csdn.net/linchaolong/article/details/42321767#L39" rel="#L39" > 39</a><a target=_blank ID="L40" href="http://blog.csdn.net/linchaolong/article/details/42321767#L40" rel="#L40" > 40</a><a target=_blank ID="L41" href="http://blog.csdn.net/linchaolong/article/details/42321767#L41" rel="#L41" > 41</a><a target=_blank ID="L42" href="http://blog.csdn.net/linchaolong/article/details/42321767#L42" rel="#L42" > 42</a><a target=_blank ID="L43" href="http://blog.csdn.net/linchaolong/article/details/42321767#L43" rel="#L43" > 43</a><a target=_blank ID="L44" href="http://blog.csdn.net/linchaolong/article/details/42321767#L44" rel="#L44" > 44</a><a target=_blank ID="L45" href="http://blog.csdn.net/linchaolong/article/details/42321767#L45" rel="#L45" > 45</a><a target=_blank ID="L46" href="http://blog.csdn.net/linchaolong/article/details/42321767#L46" rel="#L46" > 46</a><a target=_blank ID="L47" href="http://blog.csdn.net/linchaolong/article/details/42321767#L47" rel="#L47" > 47</a><a target=_blank ID="L48" href="http://blog.csdn.net/linchaolong/article/details/42321767#L48" rel="#L48" > 48</a><a target=_blank ID="L49" href="http://blog.csdn.net/linchaolong/article/details/42321767#L49" rel="#L49" > 49</a><a target=_blank ID="L50" href="http://blog.csdn.net/linchaolong/article/details/42321767#L50" rel="#L50" > 50</a><a target=_blank ID="L51" href="http://blog.csdn.net/linchaolong/article/details/42321767#L51" rel="#L51" > 51</a><a target=_blank ID="L52" href="http://blog.csdn.net/linchaolong/article/details/42321767#L52" rel="#L52" > 52</a><a target=_blank ID="L53" href="http://blog.csdn.net/linchaolong/article/details/42321767#L53" rel="#L53" > 53</a><a target=_blank ID="L54" href="http://blog.csdn.net/linchaolong/article/details/42321767#L54" rel="#L54" > 54</a><a target=_blank ID="L55" href="http://blog.csdn.net/linchaolong/article/details/42321767#L55" rel="#L55" > 55</a><a target=_blank ID="L56" href="http://blog.csdn.net/linchaolong/article/details/42321767#L56" rel="#L56" > 56</a><a target=_blank ID="L57" href="http://blog.csdn.net/linchaolong/article/details/42321767#L57" rel="#L57" > 57</a><a target=_blank ID="L58" href="http://blog.csdn.net/linchaolong/article/details/42321767#L58" rel="#L58" > 58</a><a target=_blank ID="L59" href="http://blog.csdn.net/linchaolong/article/details/42321767#L59" rel="#L59" > 59</a><a target=_blank ID="L60" href="http://blog.csdn.net/linchaolong/article/details/42321767#L60" rel="#L60" > 60</a><a target=_blank ID="L61" href="http://blog.csdn.net/linchaolong/article/details/42321767#L61" rel="#L61" > 61</a><a target=_blank ID="L62" href="http://blog.csdn.net/linchaolong/article/details/42321767#L62" rel="#L62" > 62</a><a target=_blank ID="L63" href="http://blog.csdn.net/linchaolong/article/details/42321767#L63" rel="#L63" > 63</a><a target=_blank ID="L64" href="http://blog.csdn.net/linchaolong/article/details/42321767#L64" rel="#L64" > 64</a><a target=_blank ID="L65" href="http://blog.csdn.net/linchaolong/article/details/42321767#L65" rel="#L65" > 65</a><a target=_blank ID="L66" href="http://blog.csdn.net/linchaolong/article/details/42321767#L66" rel="#L66" > 66</a><a target=_blank ID="L67" href="http://blog.csdn.net/linchaolong/article/details/42321767#L67" rel="#L67" > 67</a><a target=_blank ID="L68" href="http://blog.csdn.net/linchaolong/article/details/42321767#L68" rel="#L68" > 68</a><a target=_blank ID="L69" href="http://blog.csdn.net/linchaolong/article/details/42321767#L69" rel="#L69" > 69</a><a target=_blank ID="L70" href="http://blog.csdn.net/linchaolong/article/details/42321767#L70" rel="#L70" > 70</a><a target=_blank ID="L71" href="http://blog.csdn.net/linchaolong/article/details/42321767#L71" rel="#L71" > 71</a><a target=_blank ID="L72" href="http://blog.csdn.net/linchaolong/article/details/42321767#L72" rel="#L72" > 72</a><a target=_blank ID="L73" href="http://blog.csdn.net/linchaolong/article/details/42321767#L73" rel="#L73" > 73</a><a target=_blank ID="L74" href="http://blog.csdn.net/linchaolong/article/details/42321767#L74" rel="#L74" > 74</a><a target=_blank ID="L75" href="http://blog.csdn.net/linchaolong/article/details/42321767#L75" rel="#L75" > 75</a><a target=_blank ID="L76" href="http://blog.csdn.net/linchaolong/article/details/42321767#L76" rel="#L76" > 76</a><a target=_blank ID="L77" href="http://blog.csdn.net/linchaolong/article/details/42321767#L77" rel="#L77" > 77</a><a target=_blank ID="L78" href="http://blog.csdn.net/linchaolong/article/details/42321767#L78" rel="#L78" > 78</a><a target=_blank ID="L79" href="http://blog.csdn.net/linchaolong/article/details/42321767#L79" rel="#L79" > 79</a><a target=_blank ID="L80" href="http://blog.csdn.net/linchaolong/article/details/42321767#L80" rel="#L80" > 80</a><a target=_blank ID="L81" href="http://blog.csdn.net/linchaolong/article/details/42321767#L81" rel="#L81" > 81</a><a target=_blank ID="L82" href="http://blog.csdn.net/linchaolong/article/details/42321767#L82" rel="#L82" > 82</a><a target=_blank ID="L83" href="http://blog.csdn.net/linchaolong/article/details/42321767#L83" rel="#L83" > 83</a><a target=_blank ID="L84" href="http://blog.csdn.net/linchaolong/article/details/42321767#L84" rel="#L84" > 84</a><a target=_blank ID="L85" href="http://blog.csdn.net/linchaolong/article/details/42321767#L85" rel="#L85" > 85</a><a target=_blank ID="L86" href="http://blog.csdn.net/linchaolong/article/details/42321767#L86" rel="#L86" > 86</a><a target=_blank ID="L87" href="http://blog.csdn.net/linchaolong/article/details/42321767#L87" rel="#L87" > 87</a><a target=_blank ID="L88" href="http://blog.csdn.net/linchaolong/article/details/42321767#L88" rel="#L88" > 88</a><a target=_blank ID="L89" href="http://blog.csdn.net/linchaolong/article/details/42321767#L89" rel="#L89" > 89</a><a target=_blank ID="L90" href="http://blog.csdn.net/linchaolong/article/details/42321767#L90" rel="#L90" > 90</a><a target=_blank ID="L91" href="http://blog.csdn.net/linchaolong/article/details/42321767#L91" rel="#L91" > 91</a><a target=_blank ID="L92" href="http://blog.csdn.net/linchaolong/article/details/42321767#L92" rel="#L92" > 92</a><a target=_blank ID="L93" href="http://blog.csdn.net/linchaolong/article/details/42321767#L93" rel="#L93" > 93</a><a target=_blank ID="L94" href="http://blog.csdn.net/linchaolong/article/details/42321767#L94" rel="#L94" > 94</a><a target=_blank ID="L95" href="http://blog.csdn.net/linchaolong/article/details/42321767#L95" rel="#L95" > 95</a><a target=_blank ID="L96" href="http://blog.csdn.net/linchaolong/article/details/42321767#L96" rel="#L96" > 96</a><a target=_blank ID="L97" href="http://blog.csdn.net/linchaolong/article/details/42321767#L97" rel="#L97" > 97</a><a target=_blank ID="L98" href="http://blog.csdn.net/linchaolong/article/details/42321767#L98" rel="#L98" > 98</a><a target=_blank ID="L99" href="http://blog.csdn.net/linchaolong/article/details/42321767#L99" rel="#L99" > 99</a><a target=_blank ID="L100" href="http://blog.csdn.net/linchaolong/article/details/42321767#L100" rel="#L100" > 100</a><a target=_blank ID="L101" href="http://blog.csdn.net/linchaolong/article/details/42321767#L101" rel="#L101" > 101</a><a target=_blank ID="L102" href="http://blog.csdn.net/linchaolong/article/details/42321767#L102" rel="#L102" > 102</a><a target=_blank ID="L103" href="http://blog.csdn.net/linchaolong/article/details/42321767#L103" rel="#L103" > 103</a><a target=_blank ID="L104" href="http://blog.csdn.net/linchaolong/article/details/42321767#L104" rel="#L104" > 104</a><a target=_blank ID="L105" href="http://blog.csdn.net/linchaolong/article/details/42321767#L105" rel="#L105" > 105</a><a target=_blank ID="L106" href="http://blog.csdn.net/linchaolong/article/details/42321767#L106" rel="#L106" > 106</a><a target=_blank ID="L107" href="http://blog.csdn.net/linchaolong/article/details/42321767#L107" rel="#L107" > 107</a><a target=_blank ID="L108" href="http://blog.csdn.net/linchaolong/article/details/42321767#L108" rel="#L108" > 108</a><a target=_blank ID="L109" href="http://blog.csdn.net/linchaolong/article/details/42321767#L109" rel="#L109" > 109</a><a target=_blank ID="L110" href="http://blog.csdn.net/linchaolong/article/details/42321767#L110" rel="#L110" > 110</a><a target=_blank ID="L111" href="http://blog.csdn.net/linchaolong/article/details/42321767#L111" rel="#L111" > 111</a><a target=_blank ID="L112" href="http://blog.csdn.net/linchaolong/article/details/42321767#L112" rel="#L112" > 112</a><a target=_blank ID="L113" href="http://blog.csdn.net/linchaolong/article/details/42321767#L113" rel="#L113" > 113</a><a target=_blank ID="L114" href="http://blog.csdn.net/linchaolong/article/details/42321767#L114" rel="#L114" > 114</a><a target=_blank ID="L115" href="http://blog.csdn.net/linchaolong/article/details/42321767#L115" rel="#L115" > 115</a><a target=_blank ID="L116" href="http://blog.csdn.net/linchaolong/article/details/42321767#L116" rel="#L116" > 116</a><a target=_blank ID="L117" href="http://blog.csdn.net/linchaolong/article/details/42321767#L117" rel="#L117" > 117</a><a target=_blank ID="L118" href="http://blog.csdn.net/linchaolong/article/details/42321767#L118" rel="#L118" > 118</a><a target=_blank ID="L119" href="http://blog.csdn.net/linchaolong/article/details/42321767#L119" rel="#L119" > 119</a><a target=_blank ID="L120" href="http://blog.csdn.net/linchaolong/article/details/42321767#L120" rel="#L120" > 120</a><a target=_blank ID="L121" href="http://blog.csdn.net/linchaolong/article/details/42321767#L121" rel="#L121" > 121</a><a target=_blank ID="L122" href="http://blog.csdn.net/linchaolong/article/details/42321767#L122" rel="#L122" > 122</a><a target=_blank ID="L123" href="http://blog.csdn.net/linchaolong/article/details/42321767#L123" rel="#L123" > 123</a><a target=_blank ID="L124" href="http://blog.csdn.net/linchaolong/article/details/42321767#L124" rel="#L124" > 124</a><a target=_blank ID="L125" href="http://blog.csdn.net/linchaolong/article/details/42321767#L125" rel="#L125" > 125</a><a target=_blank ID="L126" href="http://blog.csdn.net/linchaolong/article/details/42321767#L126" rel="#L126" > 126</a><a target=_blank ID="L127" href="http://blog.csdn.net/linchaolong/article/details/42321767#L127" rel="#L127" > 127</a><a target=_blank ID="L128" href="http://blog.csdn.net/linchaolong/article/details/42321767#L128" rel="#L128" > 128</a><a target=_blank ID="L129" href="http://blog.csdn.net/linchaolong/article/details/42321767#L129" rel="#L129" > 129</a><a target=_blank ID="L130" href="http://blog.csdn.net/linchaolong/article/details/42321767#L130" rel="#L130" > 130</a><a target=_blank ID="L131" href="http://blog.csdn.net/linchaolong/article/details/42321767#L131" rel="#L131" > 131</a><a target=_blank ID="L132" href="http://blog.csdn.net/linchaolong/article/details/42321767#L132" rel="#L132" > 132</a><a target=_blank ID="L133" href="http://blog.csdn.net/linchaolong/article/details/42321767#L133" rel="#L133" > 133</a><a target=_blank ID="L134" href="http://blog.csdn.net/linchaolong/article/details/42321767#L134" rel="#L134" > 134</a><a target=_blank ID="L135" href="http://blog.csdn.net/linchaolong/article/details/42321767#L135" rel="#L135" > 135</a><a target=_blank ID="L136" href="http://blog.csdn.net/linchaolong/article/details/42321767#L136" rel="#L136" > 136</a><a target=_blank ID="L137" href="http://blog.csdn.net/linchaolong/article/details/42321767#L137" rel="#L137" > 137</a><a target=_blank ID="L138" href="http://blog.csdn.net/linchaolong/article/details/42321767#L138" rel="#L138" > 138</a><a target=_blank ID="L139" href="http://blog.csdn.net/linchaolong/article/details/42321767#L139" rel="#L139" > 139</a><a target=_blank ID="L140" href="http://blog.csdn.net/linchaolong/article/details/42321767#L140" rel="#L140" > 140</a><a target=_blank ID="L141" href="http://blog.csdn.net/linchaolong/article/details/42321767#L141" rel="#L141" > 141</a><a target=_blank ID="L142" href="http://blog.csdn.net/linchaolong/article/details/42321767#L142" rel="#L142" > 142</a><a target=_blank ID="L143" href="http://blog.csdn.net/linchaolong/article/details/42321767#L143" rel="#L143" > 143</a><a target=_blank ID="L144" href="http://blog.csdn.net/linchaolong/article/details/42321767#L144" rel="#L144" > 144</a><a target=_blank ID="L145" href="http://blog.csdn.net/linchaolong/article/details/42321767#L145" rel="#L145" > 145</a><a target=_blank ID="L146" href="http://blog.csdn.net/linchaolong/article/details/42321767#L146" rel="#L146" > 146</a><a target=_blank ID="L147" href="http://blog.csdn.net/linchaolong/article/details/42321767#L147" rel="#L147" > 147</a><a target=_blank ID="L148" href="http://blog.csdn.net/linchaolong/article/details/42321767#L148" rel="#L148" > 148</a><a target=_blank ID="L149" href="http://blog.csdn.net/linchaolong/article/details/42321767#L149" rel="#L149" > 149</a><a target=_blank ID="L150" href="http://blog.csdn.net/linchaolong/article/details/42321767#L150" rel="#L150" > 150</a><a target=_blank ID="L151" href="http://blog.csdn.net/linchaolong/article/details/42321767#L151" rel="#L151" > 151</a><a target=_blank ID="L152" href="http://blog.csdn.net/linchaolong/article/details/42321767#L152" rel="#L152" > 152</a><a target=_blank ID="L153" href="http://blog.csdn.net/linchaolong/article/details/42321767#L153" rel="#L153" > 153</a><a target=_blank ID="L154" href="http://blog.csdn.net/linchaolong/article/details/42321767#L154" rel="#L154" > 154</a><a target=_blank ID="L155" href="http://blog.csdn.net/linchaolong/article/details/42321767#L155" rel="#L155" > 155</a><a target=_blank ID="L156" href="http://blog.csdn.net/linchaolong/article/details/42321767#L156" rel="#L156" > 156</a><a target=_blank ID="L157" href="http://blog.csdn.net/linchaolong/article/details/42321767#L157" rel="#L157" > 157</a><a target=_blank ID="L158" href="http://blog.csdn.net/linchaolong/article/details/42321767#L158" rel="#L158" > 158</a><a target=_blank ID="L159" href="http://blog.csdn.net/linchaolong/article/details/42321767#L159" rel="#L159" > 159</a><a target=_blank ID="L160" href="http://blog.csdn.net/linchaolong/article/details/42321767#L160" rel="#L160" > 160</a><a target=_blank ID="L161" href="http://blog.csdn.net/linchaolong/article/details/42321767#L161" rel="#L161" > 161</a><a target=_blank ID="L162" href="http://blog.csdn.net/linchaolong/article/details/42321767#L162" rel="#L162" > 162</a><a target=_blank ID="L163" href="http://blog.csdn.net/linchaolong/article/details/42321767#L163" rel="#L163" > 163</a><a target=_blank ID="L164" href="http://blog.csdn.net/linchaolong/article/details/42321767#L164" rel="#L164" > 164</a><a target=_blank ID="L165" href="http://blog.csdn.net/linchaolong/article/details/42321767#L165" rel="#L165" > 165</a><a target=_blank ID="L166" href="http://blog.csdn.net/linchaolong/article/details/42321767#L166" rel="#L166" > 166</a><a target=_blank ID="L167" href="http://blog.csdn.net/linchaolong/article/details/42321767#L167" rel="#L167" > 167</a><a target=_blank ID="L168" href="http://blog.csdn.net/linchaolong/article/details/42321767#L168" rel="#L168" > 168</a><a target=_blank ID="L169" href="http://blog.csdn.net/linchaolong/article/details/42321767#L169" rel="#L169" > 169</a><a target=_blank ID="L170" href="http://blog.csdn.net/linchaolong/article/details/42321767#L170" rel="#L170" > 170</a><a target=_blank ID="L171" href="http://blog.csdn.net/linchaolong/article/details/42321767#L171" rel="#L171" > 171</a><a target=_blank ID="L172" href="http://blog.csdn.net/linchaolong/article/details/42321767#L172" rel="#L172" > 172</a><a target=_blank ID="L173" href="http://blog.csdn.net/linchaolong/article/details/42321767#L173" rel="#L173" > 173</a><a target=_blank ID="L174" href="http://blog.csdn.net/linchaolong/article/details/42321767#L174" rel="#L174" > 174</a><a target=_blank ID="L175" href="http://blog.csdn.net/linchaolong/article/details/42321767#L175" rel="#L175" > 175</a><a target=_blank ID="L176" href="http://blog.csdn.net/linchaolong/article/details/42321767#L176" rel="#L176" > 176</a><a target=_blank ID="L177" href="http://blog.csdn.net/linchaolong/article/details/42321767#L177" rel="#L177" > 177</a><a target=_blank ID="L178" href="http://blog.csdn.net/linchaolong/article/details/42321767#L178" rel="#L178" > 178</a><a target=_blank ID="L179" href="http://blog.csdn.net/linchaolong/article/details/42321767#L179" rel="#L179" > 179</a><a target=_blank ID="L180" href="http://blog.csdn.net/linchaolong/article/details/42321767#L180" rel="#L180" > 180</a><a target=_blank ID="L181" href="http://blog.csdn.net/linchaolong/article/details/42321767#L181" rel="#L181" > 181</a><a target=_blank ID="L182" href="http://blog.csdn.net/linchaolong/article/details/42321767#L182" rel="#L182" > 182</a><a target=_blank ID="L183" href="http://blog.csdn.net/linchaolong/article/details/42321767#L183" rel="#L183" > 183</a><a target=_blank ID="L184" href="http://blog.csdn.net/linchaolong/article/details/42321767#L184" rel="#L184" > 184</a><a target=_blank ID="L185" href="http://blog.csdn.net/linchaolong/article/details/42321767#L185" rel="#L185" > 185</a><a target=_blank ID="L186" href="http://blog.csdn.net/linchaolong/article/details/42321767#L186" rel="#L186" > 186</a> | #include "UpdateLayer.h" #include "HelloWorldScene.h" #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) #include <dirent.h> #include <sys/stat.h> #endif @H_359_419@ bool UpdateLayer :: init (){ if ( cclayer :: init ()) { // 设置资源包下载目录 m_downloadDir = CCfileUtils :: sharedfileUtils () -> getWritablePath (); m_downloadDir += "download" ; // 设置代理 getAssetsManager () -> setDelegate ( this ); // 添加资源包下载路径到搜索路径,优先搜索更新的资源 std :: vector < std :: string > searchPaths = CCfileUtils :: sharedfileUtils () -> getSearchPaths (); searchPaths . insert ( searchPaths . begin (), m_downloadDir ); CCfileUtils :: sharedfileUtils () -> setSearchPaths ( searchPaths ); // 提示 m_label = cclabelTTF :: create ( "" , "Arial" , 18 ); m_label -> setAnchorPoint ( ccp ( 1 , 0.5 )); m_label -> setposition ( ccp ( 465 , 20 )); addChild ( m_label ); // 菜单 Ccmenu * menu = Ccmenu :: create (); menu -> setposition ( CCPointZero ); addChild ( menu ); CCSize visibleSize = CCDirector :: sharedDirector () -> getVisibleSize (); // 重置 CcmenuItemFont * itemreset = CcmenuItemFont :: create ( "reset" , this , menu_selector ( UpdateLayer :: reset )); itemreset -> setposition ( ccp ( visibleSize . wIDth / 2 ,153)">50 )); menu -> addChild ( itemreset ); // 获取当前版本号 CcmenuItemFont * itemGetClIEntVersion = CcmenuItemFont :: create ( "getClIEntVersion" , menu_selector ( UpdateLayer :: getClIEntVersion )); itemGetClIEntVersion -> setposition ( ccp ( visibleSize . wIDth / 100 )); menu -> addChild ( itemGetClIEntVersion ); // 获取服务器最新版本 CcmenuItemFont * itemGetServerVersion = CcmenuItemFont :: create ( "checkUpdate" , menu_selector ( UpdateLayer :: checkUpdate )); itemGetServerVersion -> setposition ( ccp ( visibleSize . wIDth / 150 )); menu -> addChild ( itemGetServerVersion ); // 更新版本 CcmenuItemFont * itemUpdateVersion = CcmenuItemFont :: create ( "updateVersion" , menu_selector ( UpdateLayer :: update )); itemUpdateVersion -> setposition ( ccp ( visibleSize . wIDth / 200 )); menu -> addChild ( itemUpdateVersion ); // 进入场景 CcmenuItemFont * itemEnterScene = CcmenuItemFont :: create ( "enterScene" , menu_selector ( UpdateLayer :: enterScene )); itemEnterScene -> setposition ( ccp ( visibleSize . wIDth / 250 )); menu -> addChild ( itemEnterScene ); return true ; } false ; } AssetsManager * UpdateLayer :: getAssetsManager (){ static AssetsManager * s_assetsManager = NulL ; if ( s_assetsManager == NulL ) { s_assetsManager = new AssetsManager ( "https://Coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git/raw/master/test.zip" , //下载资源包的url "https://Coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git/raw/master/version" , // 获取服务端版本号的url m_downloadDir . c_str ()); // 资源保存路径 s_assetsManager -> setDelegate ( this ); s_assetsManager -> setConnectionTimeout ( 3 ); } cclOG ( "save path : %s" , s_assetsManager -> getStoragePath ()); return s_assetsManager ; } voID UpdateLayer :: initDownloadDir (){ // 如果下载目录不存在,则创建下载目录 @H_301_1343@ #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) DIR * pDir = NulL ; pDir = opendir ( m_downloadDir . c_str ()); if ( ! pDir ) { mkdir ( m_downloadDir . c_str (), S_IRWXU | S_IRWXG | S_IRWXO ); } #else if (( GetfileAttributesA ( m_downloadDir . c_str ())) == INVALID_file_ATTRIBUTES ) { CreateDirectoryA ( m_downloadDir . c_str (),153)">0 ); } #endif } voID UpdateLayer :: deleteDir ( std :: string dir ){ // Remove downloaded files #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) std :: string command = "rm -r " ; // Path may include space. command += " \" " + dir + " ; system ( command . c_str ()); #else std :: string command = "rd /s /q " ; // Path may include space. command += " ; system ( command . c_str ()); #endif } voID UpdateLayer :: onError ( cocos2d :: extension :: AssetsManager :: ErrorCode errorCode ){ switch ( errorCode ) { case cocos2d: : extension :: AssetsManager :: kCreatefile : cclOG ( "error : create file failure" ); m_label -> setString ( "error : create file failure" ); break ; case cocos2d: : extension :: AssetsManager :: kNetwork : cclOG ( "error : no network" ); m_label -> setString ( "error : no network" ); break ; case cocos2d: : extension :: AssetsManager :: kNoNewVersion : cclOG ( "error : no new version" ); m_label -> setString ( "error : no new version" ); break ; case cocos2d: : extension :: AssetsManager :: kUncompress : cclOG ( "error : uncompress file error" ); m_label -> setString ( "error : uncompress file error" ); break ; default: break ; } } voID UpdateLayer :: onProgress ( int percent ){ char progress [ 80 ]; memset ( progress , ',' sizeof ( progress ) ); snprintf ( progress ( ,0); Font-weight:bold">sizeof progress , ),240)">"hotupdate downloading %d%%" percent ); cclOG ( "percent=%d %s" , percent , progress ); m_label -> setString ( progress ); } voID UpdateLayer :: onSuccess (){ cclOG ( "download success." ); m_label -> setString ( "download success." ); } voID UpdateLayer :: update ( CCObject * pSender ){ // 初始化下载目录 initDownloadDir (); // 下载更新包 getAssetsManager () -> update (); } voID UpdateLayer :: reset ( CCObject * pSender ){ if ( "" != m_downloadDir ) { // 删除下载目录 deleteDir ( m_downloadDir ); } // 删除版本号 getAssetsManager () -> deleteVersion (); } voID UpdateLayer :: getClIEntVersion ( CCObject * pSender ){ CCString * msg = CCString :: createWithFormat ( "current clIEnt version : %s" , getAssetsManager () -> getVersion (). c_str ()); cclOG ( "%s" , msg -> getCString ()); m_label -> setString ( msg -> getCString ()); } voID UpdateLayer :: checkUpdate ( CCObject * pSender ){ if @H_344_2301@ ( getAssetsManager () -> checkUpdate ()) { cclOG ( "has new version" ); m_label -> setString ( "has new version" ); } else { cclOG ( "has not new version" ); m_label -> setString ( "has not new version" ); } } voID UpdateLayer :: enterScene ( CCObject * pSender ){ CCDirector :: sharedDirector () -> replaceScene ( HelloWorld :: scene ()); } |
项目地址:https://coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git
总结以上是内存溢出为你收集整理的【Cocos2d-x】实现资源热更新全部内容,希望文章能够帮你解决【Cocos2d-x】实现资源热更新所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)