From d2a0786875e61909d1b0782d34005425f2b8b070 Mon Sep 17 00:00:00 2001 From: suchmememanyskill Date: Mon, 26 Jul 2021 01:45:35 +0200 Subject: [PATCH] add save.commit() and save.write() --- bdk/libs/nx_savedata/save_data_file.c | 7 +-- scripts/FirmwareDump.te | 2 +- scripts/HelloWorld.te | 23 +++++--- source/script/saveClass.c | 82 ++++++++++++++++++++++++++- source/script/scriptError.c | 1 + source/script/standardLibrary.c | 2 +- 6 files changed, 102 insertions(+), 15 deletions(-) diff --git a/bdk/libs/nx_savedata/save_data_file.c b/bdk/libs/nx_savedata/save_data_file.c index edf85ba..7f3645d 100644 --- a/bdk/libs/nx_savedata/save_data_file.c +++ b/bdk/libs/nx_savedata/save_data_file.c @@ -99,10 +99,9 @@ bool save_data_file_write(save_data_file_ctx_t *ctx, uint64_t *out_bytes_written if (!save_data_file_validate_write_params(ctx, offset, count, ctx->mode, &is_resize_needed)) return false; - if (is_resize_needed) { - if (!save_data_file_set_size(ctx, offset + count)) - return false; - } + if (!save_data_file_set_size(ctx, offset + count)) + return false; + *out_bytes_written = save_allocation_table_storage_write(&ctx->base_storage, buffer, offset, count); return true; diff --git a/scripts/FirmwareDump.te b/scripts/FirmwareDump.te index e41e29c..7c20079 100644 --- a/scripts/FirmwareDump.te +++ b/scripts/FirmwareDump.te @@ -3,7 +3,7 @@ p=println pe={pause() exit()} fwstr={fw=maj.str()+"."+min.str()+"."+pat.str()} fv={a=readsave("bis:/save/8000000000000120") - b=a.readFile("/meta/imkvdb.arc") + b=a.read("/meta/imkvdb.arc") c=["BYTE[]",9,8,0,0,0,0,0,1] d=b.find(c) if(d>0){ diff --git a/scripts/HelloWorld.te b/scripts/HelloWorld.te index d58366f..1d95158 100644 --- a/scripts/HelloWorld.te +++ b/scripts/HelloWorld.te @@ -5,13 +5,20 @@ a="Hello world!\n" a.print() -#i=0 -#while (i<10){ -# println(i) -# i=i+1 -#} -a = readdir("sd:/tegraexplorer") + +mountsys("SYSTEM") +s = readsave("sd:/8000000000000120") +f = s.read("/meta/imkvdb.arc") +println(f.len()) +println(f.slice(1,1).project()[0]) +s.write("/meta/imkvdb.arc", ["BYTE[]", 1,2]).print() +s.commit().print() + +#a = s.readdir("/") +pause() +exit() + println(a.result) println(a.files.len()) println(a.folders.len()) @@ -19,4 +26,6 @@ println(a.fileSizes.len()) a.files.foreach("b") { println(b) -} \ No newline at end of file +} + +pause() \ No newline at end of file diff --git a/source/script/saveClass.c b/source/script/saveClass.c index bcbc48b..eb7583a 100644 --- a/source/script/saveClass.c +++ b/source/script/saveClass.c @@ -1,14 +1,17 @@ #include "saveClass.h" #include "compat.h" +#include "intClass.h" +#include "dictionaryClass.h" u8 oneStringArgSave[] = {StringClass}; +u8 oneStrOneByteArrayArgSave[] = {StringClass, ByteArrayClass}; ClassFunction(readFile){ Variable_t *arg = (*args); save_data_file_ctx_t dataArc; if (!save_open_file(&caller->save->saveCtx, &dataArc, arg->string.value, OPEN_MODE_READ)) return NULL; - + u64 totalSize; save_data_file_get_size(&dataArc, &totalSize); @@ -21,8 +24,83 @@ ClassFunction(readFile){ return copyVariableToPtr(a); } +ClassFunction(writeFile){ + Variable_t *arg = (*args); + save_data_file_ctx_t dataArc; + if (!save_open_file(&caller->save->saveCtx, &dataArc, arg->string.value, OPEN_MODE_WRITE)) + return newIntVariablePtr(1); + + u64 outBytes = 0; + if (!save_data_file_write(&dataArc, &outBytes, 0, args[1]->solvedArray.vector.data, args[1]->solvedArray.vector.count)){ + return newIntVariablePtr(3); + }; + + if (outBytes != args[1]->solvedArray.vector.count){ + return newIntVariablePtr(4); + } + + return newIntVariablePtr(0); +} + +ClassFunction(getFiles){ + Variable_t* resPtr = newIntVariablePtr(0); + Variable_t ret = {.variableType = DictionaryClass, .dictionary.vector = newVec(sizeof(Dict_t), 4)}; + addVariableToDict(&ret, "result", resPtr); + + save_data_directory_ctx_t ctx; + if (!save_open_directory(&caller->save->saveCtx, &ctx, "/", OPEN_DIR_MODE_ALL)){ + resPtr->integer.value = 1; + return copyVariableToPtr(ret); + } + + u64 entryCount = 0; + if (!save_data_directory_get_entry_count(&ctx, &entryCount)){ + resPtr->integer.value = 2; + return copyVariableToPtr(ret); + } + + directory_entry_t* entries = malloc(sizeof(directory_entry_t) * entryCount); + u64 entryCountDirRead = 0; + if (!save_data_directory_read(&ctx, &entryCountDirRead, entries, entryCount)){ + resPtr->integer.value = 3; + + return copyVariableToPtr(ret); + } + + Variable_t fileNamesArray = {.variableType = StringArrayClass, .solvedArray.vector = newVec(sizeof(char*), 0)}; + Variable_t dirNamesArray = {.variableType = StringArrayClass, .solvedArray.vector = newVec(sizeof(char*), 0)}; + Variable_t fileSizeArray = {.variableType = IntArrayClass, .solvedArray.vector = newVec(sizeof(s64), 0)}; + + for (int i = 0; i < entryCountDirRead; i++){ + char *add = CpyStr(entries[i].name); + if (entries[i].type == DIR_ENT_TYPE_FILE){ + vecAdd(&fileNamesArray.solvedArray.vector, add); + s64 fileSize = entries[i].size; + vecAdd(&fileSizeArray.solvedArray.vector, fileSize); + } + else { + vecAdd(&dirNamesArray.solvedArray.vector, add); + } + } + + free(entries); + + addVariableToDict(&ret, "files", copyVariableToPtr(fileNamesArray)); + addVariableToDict(&ret, "folders", copyVariableToPtr(dirNamesArray)); + addVariableToDict(&ret, "fileSizes", copyVariableToPtr(fileSizeArray)); + + return copyVariableToPtr(ret); +} + +ClassFunction(saveClassCommit){ + return newIntVariablePtr(!save_commit(&caller->save->saveCtx)); +} + ClassFunctionTableEntry_t saveFunctions[] = { - {"readFile", readFile, 1, oneStringArgSave}, + {"read", readFile, 1, oneStringArgSave}, + {"write", writeFile, 2, oneStrOneByteArrayArgSave}, + //{"readdir", getFiles, 1, oneStringArgSave}, // Seems broken? + {"commit", saveClassCommit, 0, 0}, }; Variable_t getSaveMember(Variable_t* var, char* memberName) { diff --git a/source/script/scriptError.c b/source/script/scriptError.c index 0a2d957..8e218c9 100644 --- a/source/script/scriptError.c +++ b/source/script/scriptError.c @@ -14,4 +14,5 @@ void printScriptError(u8 errLevel, char* message, ...) { if (errLevel < SCRIPT_WARN) gfx_printf("\nError occured on or near line %d\n", (u32)scriptCurrentLine); va_end(args); + hidWait(); } \ No newline at end of file diff --git a/source/script/standardLibrary.c b/source/script/standardLibrary.c index 7896202..c81d309 100644 --- a/source/script/standardLibrary.c +++ b/source/script/standardLibrary.c @@ -145,7 +145,7 @@ ClassFunction(stdMountSave){ Variable_t *arg = (*args); Variable_t var = {.variableType = SaveClass}; SaveClass_t* save = calloc(1, sizeof(SaveClass_t)); - if (f_open(&save->saveFile, arg->string.value, FA_READ)) + if (f_open(&save->saveFile, arg->string.value, FA_READ | FA_WRITE)) return NULL; save_init(&save->saveCtx, &save->saveFile, dumpedKeys.save_mac_key, 0); if (!save_process(&save->saveCtx))