From caaf912a28ceffbd53c40a0a36a2841d9a0b6631 Mon Sep 17 00:00:00 2001 From: zhangting Date: Sun, 20 Sep 2015 15:38:15 +0800 Subject: [PATCH 1/2] add writeDataToFile() for jsb --- .../js-bindings/manual/cocos2d_specifics.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp b/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp index d5a08d88f6..00dd8e7cd4 100644 --- a/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp +++ b/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp @@ -4449,6 +4449,41 @@ bool js_cocos2dx_CCFileUtils_getDataFromFile(JSContext *cx, uint32_t argc, jsval return false; } +bool js_cocos2dx_CCFileUtils_writeDataToFile(JSContext *cx, uint32_t argc, jsval *vp) +{ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + bool ok = true; + JS::RootedObject obj(cx, args.thisv().toObjectOrNull()); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + cocos2d::FileUtils* cobj = (cocos2d::FileUtils *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_CCFileUtils_writeDataToFile : Invalid Native Object"); + if (argc == 2) { + Data arg0; + std::string arg1; + + // copy bytes from UInit8Array + JSObject &obj0 = args.get(0).toObject(); + if( JS_IsUint8Array(&obj0) ) { + uint32_t len = JS_GetArrayBufferViewByteLength(&obj0); + uint8_t* bufdata = (uint8_t*)JS_GetArrayBufferViewData(&obj0); + arg0.copy(bufdata, len); + } else { + ok = false; + } + + ok &= jsval_to_std_string(cx, args.get(1), &arg1); + JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_CCFileUtils_writeDataToFile : Error processing arguments"); + bool ret = cobj->writeDataToFile(arg0, arg1); + jsval jsret = JSVAL_NULL; + jsret = BOOLEAN_TO_JSVAL(ret); + args.rval().set(jsret); + return true; + } + + JS_ReportError(cx, "js_cocos2dx_CCFileUtils_writeDataToFile : wrong number of arguments: %d, was expecting %d", argc, 2); + return false; +} + bool js_cocos2dx_CCFileUtils_getSearchResolutionsOrder(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); @@ -6257,6 +6292,7 @@ void register_cocos2dx_js_core(JSContext* cx, JS::HandleObject global) JS_DefineFunction(cx, tmpObj, "getSearchResolutionsOrder", js_cocos2dx_CCFileUtils_getSearchResolutionsOrder, 0, JSPROP_PERMANENT ); JS_DefineFunction(cx, tmpObj, "createDictionaryWithContentsOfFile", js_cocos2dx_FileUtils_createDictionaryWithContentsOfFile, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "getDataFromFile", js_cocos2dx_CCFileUtils_getDataFromFile, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT); + JS_DefineFunction(cx, tmpObj, "writeDataToFile", js_cocos2dx_CCFileUtils_writeDataToFile, 2, JSPROP_ENUMERATE | JSPROP_PERMANENT); JS_GetProperty(cx, ccObj, "EventListenerTouchOneByOne", &tmpVal); tmpObj = tmpVal.toObjectOrNull(); From bb8d9a414592234e12253d2e3c098907b8d37c30 Mon Sep 17 00:00:00 2001 From: zhangting Date: Sun, 20 Sep 2015 16:46:54 +0800 Subject: [PATCH 2/2] add testcase for writeDataToFile to jstests --- .../src/NativeTest/FileUtils/FileUtilsTest.js | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/js-tests/src/NativeTest/FileUtils/FileUtilsTest.js b/tests/js-tests/src/NativeTest/FileUtils/FileUtilsTest.js index 4a2dcf7231..94e0d69079 100644 --- a/tests/js-tests/src/NativeTest/FileUtils/FileUtilsTest.js +++ b/tests/js-tests/src/NativeTest/FileUtils/FileUtilsTest.js @@ -72,6 +72,71 @@ var fileUtilsBase = BaseTestLayer.extend({ }); +var TestWriteData = fileUtilsBase.extend({ + _title:"fileUtils: TestWriteData(Binary) to files", + ctor:function() { + this._super(); + + var winSize = cc.winSize; + + var writeResult = new cc.LabelTTF("", "Arial", 18); + this.addChild(writeResult); + writeResult.setPosition(winSize.width / 2, winSize.height * 3 / 4); + + var readResult = new cc.LabelTTF("", "Arial", 18); + this.addChild(readResult); + readResult.setPosition(winSize.width / 2, winSize.height / 3); + + var writablePath = jsb.fileUtils.getWritablePath(); + writablePath += "cocos/fileUtilTest/"; + var fileName = "writeDataTest.bin"; + var fullPath = writablePath + fileName; + + // writeTest + var writeData = new Uint8Array(8); + for(var i = 0; i < writeData.length; i ++) { + writeData[i] = i; + } + jsb.fileUtils.createDirectory(writablePath); + if (jsb.fileUtils.writeDataToFile(writeData, fullPath)) + { + log("see the file at %s", fullPath); + writeResult.setString("write success:\n" + fullPath); + } + else + { + log("write file failed"); + writeResult.setString("write fail"); + } + + // readTest + var readData = jsb.fileUtils.getDataFromFile(fullPath); + if(!readData || typeof readData != typeof writeData) { + log("read file failed"); + readResult.setString("read failed"); + } else { + var match = true; + if(readData.length != writeData.length){ + log("data size not match"); + match = false; + } else { + for(var i = 0; i < readData.length; i ++) { + if(readData[i] != writeData[i]) { + log("data not match"); + match = false; + break; + } + } + } + if(!match) { + readResult.setString("read success, but data not correct"); + } else { + readResult.setString("read test success"); + } + } + }, +}); + var TestWriteString = fileUtilsBase.extend({ _title:"fileUtils: TestWriteString to files", ctor:function() { @@ -301,6 +366,7 @@ var TestWriteValueVector = fileUtilsBase.extend({ }, }); var arrayOffileUtils = [ + TestWriteData, TestWriteString, TestWriteValueMap, TestWriteValueVector