From c9fdb650c31bb1613d08a5555ef6df84f1065886 Mon Sep 17 00:00:00 2001 From: suchmememanyskill Date: Mon, 4 Jan 2021 01:55:58 +0100 Subject: [PATCH] [script] Make equivalent to tsV1 Fixed: functions in functions Added functions: version, menu, pathCombine, pathEscFolder, fileMove, fileCopy, fileDel, mmcConnect, mmcMount, mkdir, dirRead, dirCopy, dirDel, mmcDump, mmcRestore --- source/fs/fscopy.c | 9 ++ source/fs/menus/filemenu.c | 2 + source/script/args.c | 4 + source/script/functions.c | 167 ++++++++++++++++++++++++++++++++-- source/script/types.h | 1 - source/storage/emmcfile.c | 3 + source/storage/mountmanager.c | 3 + source/utils/utils.c | 2 +- 8 files changed, 182 insertions(+), 9 deletions(-) diff --git a/source/fs/fscopy.c b/source/fs/fscopy.c index ab8530c..de997df 100644 --- a/source/fs/fscopy.c +++ b/source/fs/fscopy.c @@ -89,6 +89,15 @@ void BoxRestOfScreen(){ } ErrCode_t FolderCopy(const char *locin, const char *locout){ + if (TConf.explorerCopyMode >= CMODE_CopyFolder){ + if (strstr(locout, locin) != NULL) + return newErrCode(TE_ERR_PATH_IN_PATH); + } + + if (!strcmp(locin, locout)){ + return newErrCode(TE_ERR_SAME_LOC); + } + char *dstPath = CombinePaths(locout, strrchr(locin, '/') + 1); int res = 0; ErrCode_t ret = newErrCode(0); diff --git a/source/fs/menus/filemenu.c b/source/fs/menus/filemenu.c index 7299bf7..309f9d9 100644 --- a/source/fs/menus/filemenu.c +++ b/source/fs/menus/filemenu.c @@ -15,6 +15,7 @@ #include "../../script/parser.h" #include "../../script/variables.h" #include +#include "../../storage/emummc.h" MenuEntry_t FileMenuEntries[] = { {.optionUnion = COLORTORGB(COLOR_WHITE) | SKIPBIT, .name = "-- File menu --"}, @@ -83,6 +84,7 @@ void RunScript(char *path, FSEntry_t entry){ free(script); dictVectorAdd(&ctx.varDict, newDict(CpyStr("_CWD"), (newVar(StringType, 0, .stringType = path)))); + dictVectorAdd(&ctx.varDict, newDict(CpyStr("_EMU"), (newVar(IntType, 0, emu_cfg.enabled)))); printError(mainLoop(&ctx)); diff --git a/source/script/args.c b/source/script/args.c index fb7c847..9eb0cb9 100644 --- a/source/script/args.c +++ b/source/script/args.c @@ -42,6 +42,10 @@ Vector_t extractVars(scriptCtx_t* ctx, lexarToken_t* tokens, u32 len) { int distance = distanceBetweenTokens(&tokens[i + 1], len - i - 1, LSBracket, RSBracket); i += distance + 1; } + if (tokens[i].token == LBracket){ + int distance = distanceBetweenTokens(&tokens[i + 1], len - i - 1, LBracket, RBracket); + i += distance + 1; + } if (tokens[i].token == Seperator) { Variable_t res = solveEquation(ctx, &tokens[lastLoc], i - lastLoc, 0); lastLoc = i + 1; diff --git a/source/script/functions.c b/source/script/functions.c index a07991b..84c48d8 100644 --- a/source/script/functions.c +++ b/source/script/functions.c @@ -11,9 +11,17 @@ #include "../gfx/gfxutils.h" #include "../hid/hid.h" #include +#include "../fs/fscopy.h" +#include "../storage/mountmanager.h" +#include "../storage/emummc.h" +#include "../fs/readers/folderReader.h" +#include "../utils/utils.h" +#include "../storage/emmcfile.h" #define scriptFunction(name) Variable_t name(scriptCtx_t *ctx, Variable_t *vars, u32 varLen) +#define varInt(i) newVar(IntType, 0, i) + scriptFunction(funcIf) { setCurIndentInstruction(ctx, (vars[0].integerType == 0), 0, -1); return NullVar; @@ -192,19 +200,22 @@ ColorCombo_t combos[] = { {"YELLOW", COLOR_YELLOW}, {"GREEN", COLOR_GREEN}, {"BLUE", COLOR_BLUE}, - {"VIOLET", COLOR_VIOLET}, - {"WHITE", COLOR_WHITE} + {"VIOLET", COLOR_VIOLET} }; -// Args: Str -scriptFunction(funcSetColor){ +u32 GetColor(char *color){ for (int i = 0; i < ARR_LEN(combos); i++){ - if (!strcmp(combos[i].name, vars[0].stringType)){ - SETCOLOR(combos[i].color, COLOR_DEFAULT); - break; + if (!strcmp(combos[i].name, color)){ + return combos[i].color; } } + return COLOR_WHITE; +} + +// Args: Str +scriptFunction(funcSetColor){ + SETCOLOR(GetColor(vars[0].stringType), COLOR_DEFAULT); return NullVar; } @@ -222,6 +233,127 @@ scriptFunction(funcWait){ return NullVar; } +scriptFunction(funcGetVer){ + int *arr = malloc(3 * sizeof(int)); + arr[0] = LP_VER_MJ; + arr[1] = LP_VER_MN; + arr[2] = LP_VER_BF; + Vector_t res = vecFromArray(arr, 3, sizeof(int)); + return newVar(IntArrayType, 1, .vectorType = res); +} + + +// Args: vec(str), int, (optional) vec(str) +scriptFunction(funcMakeMenu){ + if (varLen == 3 && vars[2].vectorType.count != vars[0].vectorType.count) + return ErrVar(ERRSYNTAX); + + Vector_t menuEntries = newVec(sizeof(MenuEntry_t), vars[0].vectorType.count); + vecDefArray(char**, names, vars[0].vectorType); + char **colors; + if (varLen == 3) + colors = vecGetArray(char**, vars[2].vectorType); + + for (int i = 0; i < vars[0].vectorType.count; i++){ + u32 color = COLORTORGB(((varLen == 3) ? GetColor(colors[i]) : COLOR_WHITE)); + MenuEntry_t a = {.optionUnion = color, .name = names[i]}; + vecAddElem(&menuEntries, a); + } + + int res = newMenu(&menuEntries, vars[1].integerType, 78, 10, ENABLEB, menuEntries.count); + vecFree(menuEntries); + return varInt(res); +} + +// Args: Str, Str +scriptFunction(funcCombinePath){ + return newVar(StringType, 1, .stringType = CombinePaths(vars[0].stringType, vars[1].stringType)); +} + +// Args: Str +scriptFunction(funcEscFolder){ + return newVar(StringType, 1, .stringType = EscapeFolder(vars[0].stringType)); +} + +// Args: Str, Str +scriptFunction(funcFileMove){ + return varInt(f_rename(vars[0].stringType, vars[1].stringType)); +} + +// Args: Str, Str +scriptFunction(funcFileCopy){ + return varInt(FileCopy(vars[0].stringType, vars[1].stringType, COPY_MODE_PRINT).err); +} + +// Args: Str +scriptFunction(funcMmcConnect){ + int res = 0; + if (!strcmp(vars[0].stringType, "SYSMMC")) + res = connectMMC(MMC_CONN_EMMC); + else if (!strcmp(vars[0].stringType, "EMUMMC") && emu_cfg.enabled) + res = connectMMC(MMC_CONN_EMUMMC); + else + return ErrVar(ERRFATALFUNCFAIL); + + return varInt(res); +} + +// Args: Str +scriptFunction(funcMmcMount){ + return varInt((mountMMCPart(vars[0].stringType).err)); +} + +// Args: Str +scriptFunction(funcMkdir){ + return varInt((f_mkdir(vars[0].stringType))); +} + +scriptFunction(funcReadDir){ + int res = 0; + Vector_t files = ReadFolder(vars[0].stringType, &res); + if (res){ + clearFileVector(&files); + return ErrVar(ERRFATALFUNCFAIL); + } + + vecDefArray(FSEntry_t*, fsEntries, files); + + Vector_t fileNames = newVec(sizeof(char*), files.count); + Vector_t fileProperties = newVec(sizeof(int), files.count); + + for (int i = 0; i < files.count; i++){ + vecAddElem(&fileNames, fsEntries[i].name); + int isFolder = fsEntries[i].isDir; + vecAddElem(&fileProperties, isFolder); + } + + vecFree(files); + + dictVectorAdd(&ctx->varDict, newDict(CpyStr("fileProperties"), (newVar(IntArrayType, 1, .vectorType = fileProperties)))); + + return newVar(StringArrayType, 1, .vectorType = fileNames); +} + +scriptFunction(funcCopyDir){ + return varInt((FolderCopy(vars[0].stringType, vars[1].stringType).err)); +} + +scriptFunction(funcDelDir){ + return varInt((FolderDelete(vars[0].stringType).err)); +} + +scriptFunction(funcDelFile){ + return varInt((f_unlink(vars[0].stringType))); +} + +scriptFunction(funcMmcDump){ + return varInt((DumpOrWriteEmmcPart(vars[0].stringType, vars[1].stringType, 0, 1).err)); +} + +scriptFunction(funcMmcRestore){ + return varInt((DumpOrWriteEmmcPart(vars[0].stringType, vars[1].stringType, 1, vars[2].integerType).err)); +} + u8 fiveInts[] = {IntType, IntType, IntType, IntType, IntType}; u8 singleIntArray[] = { IntArrayType }; u8 singleInt[] = { IntType }; @@ -229,6 +361,9 @@ u8 singleAny[] = { varArgs }; u8 singleStr[] = { StringType }; u8 singleByteArr[] = { ByteArrayType }; u8 StrByteVec[] = { StringType, ByteArrayType}; +u8 MenuArgs[] = { StringArrayType, IntType, StringArrayType}; +u8 twoStrings[] = { StringType, StringType }; +u8 mmcReadWrite[] = { StringType, StringType, IntType}; functionStruct_t scriptFunctions[] = { {"if", funcIf, 1, singleInt}, @@ -252,6 +387,24 @@ functionStruct_t scriptFunctions[] = { {"color", funcSetColor, 1, singleStr}, {"pause", funcPause, 0, NULL}, {"wait", funcWait, 1, singleInt}, + {"version", funcGetVer, 0, NULL}, + {"menu", funcMakeMenu, 2, MenuArgs}, // for the optional arg + {"menu", funcMakeMenu, 3, MenuArgs}, + {"pathCombine", funcCombinePath, 2, twoStrings}, + {"pathEscFolder", funcEscFolder, 1, singleStr}, + {"fileMove", funcFileMove, 2, twoStrings}, + {"fileCopy", funcFileCopy, 2, twoStrings}, + {"fileDel", funcDelFile, 1, singleStr}, + {"mmcConnect", funcMmcConnect, 1, singleStr}, + {"mmcMount", funcMmcMount, 1, singleStr}, + {"mkdir", funcMkdir, 1, singleStr}, + {"dirRead", funcReadDir, 1, singleStr}, + {"dirCopy", funcCopyDir, 2, twoStrings}, + {"dirDel", funcDelDir, 1, singleStr}, + {"mmcDump", funcMmcDump, 2, mmcReadWrite}, + {"mmcRestore", funcMmcRestore, 3, mmcReadWrite}, + // Left from old: keyboard(?) + // Should implement still: saveSign, getNcaType }; Variable_t executeFunction(scriptCtx_t* ctx, char* func_name, lexarToken_t *start, u32 len) { diff --git a/source/script/types.h b/source/script/types.h index 103b0e2..c9f4063 100644 --- a/source/script/types.h +++ b/source/script/types.h @@ -134,7 +134,6 @@ typedef struct { } scriptResult_t; - #define newDict(strName, var) (dict_t) {strName, var} #define newVar(var, frii, value) (Variable_t) {.varType = var, .free = frii, value} diff --git a/source/storage/emmcfile.c b/source/storage/emmcfile.c index 86623e1..99b2455 100644 --- a/source/storage/emmcfile.c +++ b/source/storage/emmcfile.c @@ -125,6 +125,9 @@ ErrCode_t DumpOrWriteEmmcPart(const char *path, const char *part, u8 write, u8 f if (!sd_mount()) return newErrCode(TE_ERR_NO_SD); + if (TConf.currentMMCConnected == MMC_CONN_None) + return newErrCode(TE_ERR_PARTITION_NOT_FOUND); + if (!memcmp(part, "BOOT0", 5)){ emummc_storage_set_mmc_partition(&emmc_storage, 1); lba_end = (BOOT_PART_SIZE / NX_EMMC_BLOCKSIZE) - 1; diff --git a/source/storage/mountmanager.c b/source/storage/mountmanager.c index 447be23..2876abb 100644 --- a/source/storage/mountmanager.c +++ b/source/storage/mountmanager.c @@ -61,6 +61,9 @@ int connectMMC(u8 mmcType){ } ErrCode_t mountMMCPart(const char *partition){ + if (TConf.currentMMCConnected == MMC_CONN_None) + return newErrCode(TE_ERR_PARTITION_NOT_FOUND); + unmountMMCPart(); emummc_storage_set_mmc_partition(&emmc_storage, 0); // why i have to do this twice beats me diff --git a/source/utils/utils.c b/source/utils/utils.c index 10c8b2c..b83e8ef 100644 --- a/source/utils/utils.c +++ b/source/utils/utils.c @@ -64,7 +64,7 @@ char *ShowKeyboard(const char *toEdit, u8 alwaysRet){ int posOnWord = 0; bool shift = 0; - gfx_printf("* = exit | ~ = backspace | ^(left) = shift | _ = Space | + = add char\n\n"); + gfx_printf("* = exit | ~ = backspace | ^(left) = shift | + = add char\n\n"); u32 x, y; gfx_con_getpos(&x, &y);