mirror of https://github.com/axmolengine/axmol.git
Merge pull request #13960 from TimothyZhang/jsb-writeDataToFile
Add jsb.fileUtils.writeDataToFile()
This commit is contained in:
commit
6ab7f3804e
|
@ -4449,6 +4449,41 @@ bool js_cocos2dx_CCFileUtils_getDataFromFile(JSContext *cx, uint32_t argc, jsval
|
||||||
return false;
|
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)
|
bool js_cocos2dx_CCFileUtils_getSearchResolutionsOrder(JSContext *cx, uint32_t argc, jsval *vp)
|
||||||
{
|
{
|
||||||
JS::CallArgs args = JS::CallArgsFromVp(argc, 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, "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, "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, "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);
|
JS_GetProperty(cx, ccObj, "EventListenerTouchOneByOne", &tmpVal);
|
||||||
tmpObj = tmpVal.toObjectOrNull();
|
tmpObj = tmpVal.toObjectOrNull();
|
||||||
|
|
|
@ -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({
|
var TestWriteString = fileUtilsBase.extend({
|
||||||
_title:"fileUtils: TestWriteString to files",
|
_title:"fileUtils: TestWriteString to files",
|
||||||
ctor:function() {
|
ctor:function() {
|
||||||
|
@ -301,6 +366,7 @@ var TestWriteValueVector = fileUtilsBase.extend({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
var arrayOffileUtils = [
|
var arrayOffileUtils = [
|
||||||
|
TestWriteData,
|
||||||
TestWriteString,
|
TestWriteString,
|
||||||
TestWriteValueMap,
|
TestWriteValueMap,
|
||||||
TestWriteValueVector
|
TestWriteValueVector
|
||||||
|
|
Loading…
Reference in New Issue