Skip to content

Commit

Permalink
Merge pull request cocos2d#953 from WenhaiLin/rt-preload
Browse files Browse the repository at this point in the history
前台加载对接资源下载
  • Loading branch information
WenhaiLin committed Nov 3, 2015
2 parents 52122c1 + 170644e commit 51e5534
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
47 changes: 36 additions & 11 deletions build/CocosJSRuntime/jsb_cocosruntime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@
#include "jsb_cocosruntime.h"
#include "cocos2d_specifics.hpp"
#include "network/NetworkHelper.h"
#include "sdk/LoadingDelegate.h"
#import "sdk/CocosRuntimeGroup.h"

std::function<void (int percent, bool isFailed)> s_downloadCallback;

@interface PreloadCallbackWapper : NSObject<LoadingDelegate>

@end

@implementation PreloadCallbackWapper

- (void) onLoadingProgress:(NSInteger)progress :(bool) isFailed;
{
if (s_downloadCallback) {
s_downloadCallback((int)progress, isFailed);
}
}

- (void) onPreRunGameCompleted
{
if (s_downloadCallback) {
s_downloadCallback(100, false);
}
}

@end

static PreloadCallbackWapper* s_preloadWapper = nil;

USING_NS_CC;

Expand Down Expand Up @@ -42,7 +70,6 @@ static bool JSB_runtime_preload(JSContext *cx, uint32_t argc, jsval *vp)

JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");

std::function<void (int percent, bool isFailed)> downloadCallback;
do {
if (JS_TypeOfValue(cx, args[1]) == JSTYPE_FUNCTION) {
JSObject* cbObject;
Expand Down Expand Up @@ -73,21 +100,19 @@ static bool JSB_runtime_preload(JSContext *cx, uint32_t argc, jsval *vp)
});

};
downloadCallback = lambda;
s_downloadCallback = lambda;
}
else {
downloadCallback = nullptr;
s_downloadCallback = nullptr;
}
} while (false);

//todo:download
if (downloadCallback != nullptr) {
std::thread([downloadCallback](){
for (int index = 1; index <= 100; ++index) {
std::this_thread::sleep_for(std::chrono::milliseconds(20));
downloadCallback(index, false);
}
}).detach();
if (s_downloadCallback) {
NSString* groups = [NSString stringWithUTF8String:arg0.c_str()];
if (s_preloadWapper == nil) {
s_preloadWapper = [[PreloadCallbackWapper alloc] init];
}
[CocosRuntimeGroup preloadResGroups:groups delegate:s_preloadWapper];
}

args.rval().setUndefined();
Expand Down
2 changes: 1 addition & 1 deletion build/CocosJSRuntime/sdk/CocosRuntimeGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ + (void) notifyProgress: (NSInteger) progressOffset unzipDone: (BOOL) unzipDone
if (isFailed) {
[resDownloadDelegate onLoadingProgress:(downloadGroupSize + progressOffset) :true];
} else {
NSInteger progress = (downloadGroupSize + progressOffset) / totalSize * 100;
NSInteger progress = 1.0 * (downloadGroupSize + progressOffset) / totalSize * 100;
[resDownloadDelegate onLoadingProgress:progress :false];

if (unzipDone) {
Expand Down

0 comments on commit 51e5534

Please sign in to comment.