diff --git a/source/tegraexplorer/script/functions.c b/source/tegraexplorer/script/functions.c index 7bbdb1e..9c20d41 100644 --- a/source/tegraexplorer/script/functions.c +++ b/source/tegraexplorer/script/functions.c @@ -288,6 +288,7 @@ int part_Pause(){ str_int_add("@BTN_DOWN", input->Ldown); str_int_add("@BTN_LEFT", input->Lleft); str_int_add("@BTN_RIGHT", input->Lright); + str_int_add("@JOYCONN", hidConnected()); return input->buttons; } @@ -550,7 +551,59 @@ int part_getPos(){ return (int)f_tell(&scriptin); } +int part_subString(){ + char *str, *sub; + int start, size; + if (parseStringInput(argv[0], &str)) + return INFUNC_FAIL; + if (parseIntInput(argv[1], &start)) + return INFUNC_FAIL; + if (parseIntInput(argv[2], &size)) + return INFUNC_FAIL; + if (argv[3][0] != '$') + return INFUNC_FAIL; + + if (start >= strlen(str)) + return INFUNC_FAIL; + + sub = utils_copyStringSize(str + start, size); + str_str_add(argv[3], sub); + free(sub); + return 0; +} + +int part_inputString(){ + char *start, *out; + int len; + + if (parseStringInput(argv[0], &start)) + return INFUNC_FAIL; + if (parseIntInput(argv[1], &len)) + return INFUNC_FAIL; + if (argv[2][0] != '$') + return INFUNC_FAIL; + + if (len > 39) + return INFUNC_FAIL; + + out = utils_InputText(start, len); + if (out == NULL) + return 1; + + str_str_add(argv[2], out); + free(out); + return 0; +} + +int part_strLen(){ + char *in; + + if (parseStringInput(argv[0], &in)) + return INFUNC_FAIL; + + return strlen(in); +} str_fnc_struct functions[] = { {"printf", part_printf, 255}, @@ -568,6 +621,9 @@ str_fnc_struct functions[] = { {"setColor", part_setColor, 1}, {"combineStrings", part_addstrings, 3}, {"compareStrings", part_stringcompare, 2}, + {"subString", part_subString, 4}, + {"inputString", part_inputString, 3}, + {"stringLength", part_strLen, 1}, {"invert", part_invert, 1}, {"fs_exists", part_fs_exists, 1}, {"fs_move", part_fs_Move, 2}, diff --git a/source/tegraexplorer/script/parser.c b/source/tegraexplorer/script/parser.c index 8789c54..cc7aac1 100644 --- a/source/tegraexplorer/script/parser.c +++ b/source/tegraexplorer/script/parser.c @@ -16,15 +16,15 @@ #include "variables.h" #include "../fs/fsreader.h" #include "../utils/utils.h" +#include "../../hid/hid.h" -u32 countchars(char* in, char target) { +u32 countchars(const char* in, char target) { u32 len = strlen(in); u32 count = 0; + for (u32 i = 0; i < len; i++) { if (in[i] == '"'){ - i++; - while (in[i] != '"'){ - i++; + while (in[++i] != '"'){ if (i >= len) return -1; } @@ -32,53 +32,46 @@ u32 countchars(char* in, char target) { if (in[i] == target) count++; } + return count; } char **argv = NULL; u32 argc; -u32 splitargs(char* in) { +u32 splitargs(const char* in) { // arg like '5, "6", @arg7' - u32 i, current = 0, count = 1, len = strlen(in), curcount = 0; + u32 i = 0, count = 1, len = strlen(in), curcount = 0, begin, end; if ((count += countchars(in, ',')) < 0){ return 0; } - - /* - if (argv != NULL) { - for (i = 0; argv[i] != NULL; i++) - free(argv[i]); - free(argv); - } - */ argv = calloc(count + 1, sizeof(char*)); - - for (i = 0; i < count; i++) - argv[i] = calloc(96, sizeof(char)); - - for (i = 0; i < len && curcount < count; i++) { - if (in[i] == ',') { - curcount++; - current = 0; - } - else if (in[i] == '@' || in[i] == '$') { - while (strrchr(", )", in[i]) == NULL && i < len) { - argv[curcount][current++] = in[i++]; - } - i--; - } - //else if ((in[i] >= '0' && in[i] <= '9') || (in[i] >= '<' && in[i] <= '>') || in[i] == '+' || in[i] == '-' || in[i] == '*' || in[i] == '/') - else if (strrchr("0123456789<=>+-*/", in[i]) != NULL) - argv[curcount][current++] = in[i]; - else if (in[i] == '"') { + while (i < len && curcount < count) { + while (in[i] == ' ' || in[i] == ',') i++; - while (in[i] != '"') { - argv[curcount][current++] = in[i++]; + + begin = i; + + while (strrchr(" ,)", in[i]) == NULL){ + if (in[i] == '"'){ + begin = i + 1; + while (in[++i] != '"'){ + if (in[i] == '\0') + return 0; + } } + i++; } + + end = i; + + if (in[i - 1] == '"'){ + end--; + } + + argv[curcount++] = utils_copyStringSize(in + begin, (u32)(end - begin)); } return count; } @@ -202,17 +195,6 @@ void mainparser(){ getnextvalidchar(); } - /* - if (currentchar == '?'){ - char *jumpname; - jumpname = readtilchar(';', ' '); - getnextchar(); - str_jmp_add(jumpname, f_tell(&scriptin)); - getfollowingchar('\n'); - return; - } - */ - functionparser(); res = run_function(funcbuff, &out); @@ -297,6 +279,7 @@ void runScript(char *path){ //add builtin vars str_int_add("@EMUMMC", emu_cfg.enabled); str_int_add("@RESULT", 0); + str_int_add("@JOYCONN", hidConnected()); str_str_add("$CURRENTPATH", currentpath); //str_int_printall(); diff --git a/source/tegraexplorer/utils/utils.c b/source/tegraexplorer/utils/utils.c index 55a6407..563a69a 100644 --- a/source/tegraexplorer/utils/utils.c +++ b/source/tegraexplorer/utils/utils.c @@ -192,4 +192,14 @@ char *utils_InputText(char *start, int maxLen){ } return buff; +} + +char *utils_copyStringSize(const char *in, int size){ + if (size > strlen(in) || size < 0) + size = strlen(in); + + char *out = calloc(size + 1, 1); + //strncpy(out, in, size); + memcpy(out, in, size); + return out; } \ No newline at end of file diff --git a/source/tegraexplorer/utils/utils.h b/source/tegraexplorer/utils/utils.h index ff74177..b987661 100644 --- a/source/tegraexplorer/utils/utils.h +++ b/source/tegraexplorer/utils/utils.h @@ -23,4 +23,5 @@ typedef struct _bmp_t int utils_mmcMenu(); void utils_copystring(const char *in, char **out); char *utils_InputText(char *start, int maxLen); -void utils_takeScreenshot(); \ No newline at end of file +void utils_takeScreenshot(); +char *utils_copyStringSize(const char *in, int size); \ No newline at end of file