1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-22 11:56:42 +00:00

add scripts to the main menu, fix script parser reading beyond EOF

This commit is contained in:
suchmememanyskill 2021-07-18 23:51:27 +02:00
parent b06085915d
commit 3cd78d6efd
7 changed files with 94 additions and 33 deletions

View file

@ -1,4 +1,7 @@
#pragma once #pragma once
#include "../../utils/vector.h" #include "../../utils/vector.h"
#include "../../gfx/menu.h"
#include "../fstypes.h"
void FileExplorer(char *path); void FileExplorer(char *path);
MenuEntry_t MakeMenuOutFSEntry(FSEntry_t entry);

View file

@ -101,7 +101,7 @@ void RunScript(char *path, FSEntry_t entry){
gfx_printf("Init gc\n"); gfx_printf("Init gc\n");
initGarbageCollector(); initGarbageCollector();
gfx_printf("Parsing\n"); gfx_printf("Parsing\n");
ParserRet_t ret = parseScript(script); ParserRet_t ret = parseScript(script, size);
free(script); free(script);
gfx_printf("Init vars\n"); gfx_printf("Init vars\n");
setStaticVars(&ret.staticVarHolder); setStaticVars(&ret.staticVarHolder);

View file

@ -71,7 +71,7 @@ int main()
//parseScript("#REQUIRE VER 3.0.5\nmain = { two = 1 + 1 }"); //parseScript("#REQUIRE VER 3.0.5\nmain = { two = 1 + 1 }");
//ParserRet_t ret = parseScript("a.b.c(1){ a.b.c() }"); //ParserRet_t ret = parseScript("a.b.c(1){ a.b.c() }");
ParserRet_t ret = parseScript(script); ParserRet_t ret = parseScript(script, strlen(script));
free(script); free(script);
setStaticVars(&ret.staticVarHolder); setStaticVars(&ret.staticVarHolder);

View file

@ -71,6 +71,8 @@ typedef enum {
History_Array, History_Array,
} StackHistory_t; } StackHistory_t;
char* end;
u8 nextToken(char** inPtr, void** val) { u8 nextToken(char** inPtr, void** val) {
char* in = *inPtr; char* in = *inPtr;
u8 ret = Token_Err; u8 ret = Token_Err;
@ -191,7 +193,7 @@ u8 nextToken(char** inPtr, void** val) {
ret = Token_String; ret = Token_String;
*val = storage; *val = storage;
} }
else if (*in == '\0') { else if (*in == '\0' || in > end) {
*inPtr = in; *inPtr = in;
return ret; return ret;
} }
@ -258,7 +260,7 @@ int isLastVarCall(Operator_t* opHolder) {
return (opHolder->token == CallArgs && getLastRef(&opHolder->callArgs)->action == ActionCall); return (opHolder->token == CallArgs && getLastRef(&opHolder->callArgs)->action == ActionCall);
} }
ParserRet_t parseScript(char* in) { ParserRet_t parseScript(char* in, u32 len) {
Vector_t functionStack; // Function_t Vector_t functionStack; // Function_t
Vector_t stackHistoryHolder; // StaticHistory_t Vector_t stackHistoryHolder; // StaticHistory_t
Vector_t staticVariableHolder; // Variable_t Vector_t staticVariableHolder; // Variable_t
@ -275,8 +277,9 @@ ParserRet_t parseScript(char* in) {
u8 notNext = 0; u8 notNext = 0;
lineNumber = 1; lineNumber = 1;
scriptCurrentLine = 1; scriptCurrentLine = 1;
end = in + len;
while (*in) { while (*in && in <= end) {
Function_t* lastFunc = getStackEntry(&functionStack); Function_t* lastFunc = getStackEntry(&functionStack);
StackHistory_t* lastHistory = getStackEntry(&stackHistoryHolder); StackHistory_t* lastHistory = getStackEntry(&stackHistoryHolder);

View file

@ -10,6 +10,6 @@ typedef struct {
#define SCRIPT_PARSER_ERR(message, ...) printScriptError(SCRIPT_PARSER_FATAL, message, ##__VA_ARGS__); return (ParserRet_t){0} #define SCRIPT_PARSER_ERR(message, ...) printScriptError(SCRIPT_PARSER_FATAL, message, ##__VA_ARGS__); return (ParserRet_t){0}
ParserRet_t parseScript(char* in);
void exitStaticVars(Vector_t* v); void exitStaticVars(Vector_t* v);
void exitFunction(Operator_t* start, u32 len); void exitFunction(Operator_t* start, u32 len);
ParserRet_t parseScript(char* in, u32 len);

View file

@ -70,6 +70,12 @@ ClassFunction(stdPrint) {
return &emptyClass; return &emptyClass;
} }
ClassFunction(stdPrintLn) {
stdPrint(caller, args, argsLen);
gfx_printf("\n");
return &emptyClass;
}
ClassFunction(stdExit) { ClassFunction(stdExit) {
return NULL; return NULL;
} }
@ -128,33 +134,22 @@ ClassFunction(stdSetPixel) {
} }
#endif #endif
enum standardFunctionIndexes {
STD_IF = 0,
STD_WHILE,
STD_PRINT,
STD_MOUNTSYSMMC,
STD_MOUNTSAVE,
STD_EXIT,
STD_BREAK,
STD_DICT,
STD_SETPIXEL,
};
u8 oneIntoneFunction[] = { IntClass, FunctionClass }; u8 oneIntoneFunction[] = { IntClass, FunctionClass };
u8 doubleFunctionClass[] = { FunctionClass, FunctionClass }; u8 doubleFunctionClass[] = { FunctionClass, FunctionClass };
u8 oneStringArgStd[] = {StringClass}; u8 oneStringArgStd[] = {StringClass};
u8 threeIntsStd[] = { IntClass, IntClass, IntClass }; u8 threeIntsStd[] = { IntClass, IntClass, IntClass };
ClassFunctionTableEntry_t standardFunctionDefenitions[] = { ClassFunctionTableEntry_t standardFunctionDefenitions[] = {
[STD_IF] = {"if", stdIf, 2, oneIntoneFunction}, {"if", stdIf, 2, oneIntoneFunction},
[STD_WHILE] = {"while", stdWhile, 2, doubleFunctionClass}, {"while", stdWhile, 2, doubleFunctionClass},
[STD_PRINT] = {"print", stdPrint, VARARGCOUNT, 0}, {"print", stdPrint, VARARGCOUNT, 0},
[STD_MOUNTSYSMMC] = {"mountsys", stdMountSysmmc, 1, oneStringArgStd}, {"println", stdPrintLn, VARARGCOUNT, 0},
[STD_MOUNTSAVE] = {"readsave", stdMountSave, 1, oneStringArgStd}, {"mountsys", stdMountSysmmc, 1, oneStringArgStd},
[STD_EXIT] = {"exit", stdExit, 0, 0}, {"readsave", stdMountSave, 1, oneStringArgStd},
[STD_BREAK] = {"break", stdBreak, 0, 0}, {"exit", stdExit, 0, 0},
[STD_DICT] = {"dict", stdDict, 0, 0}, {"break", stdBreak, 0, 0},
[STD_SETPIXEL] = {"setpixel", stdSetPixel, 3, threeIntsStd}, {"dict", stdDict, 0, 0},
{"setpixel", stdSetPixel, 3, threeIntsStd},
}; };
ClassFunctionTableEntry_t* searchStdLib(char* funcName) { ClassFunctionTableEntry_t* searchStdLib(char* funcName) {

View file

@ -17,6 +17,10 @@
#include <soc/fuse.h> #include <soc/fuse.h>
#include "../utils/utils.h" #include "../utils/utils.h"
#include "../config.h" #include "../config.h"
#include "../fs/readers/folderReader.h"
#include <string.h>
#include <mem/heap.h>
#include "../fs/menus/filemenu.h"
extern hekate_config h_cfg; extern hekate_config h_cfg;
@ -36,7 +40,8 @@ enum {
MainRebootRCM, MainRebootRCM,
MainRebootNormal, MainRebootNormal,
MainRebootHekate, MainRebootHekate,
MainRebootAMS MainRebootAMS,
MainScripts,
}; };
MenuEntry_t mainMenuEntries[] = { MenuEntry_t mainMenuEntries[] = {
@ -55,7 +60,8 @@ MenuEntry_t mainMenuEntries[] = {
[MainRebootRCM] = {.optionUnion = COLORTORGB(COLOR_VIOLET), .name = "Reboot to RCM"}, [MainRebootRCM] = {.optionUnion = COLORTORGB(COLOR_VIOLET), .name = "Reboot to RCM"},
[MainRebootNormal] = {.optionUnion = COLORTORGB(COLOR_VIOLET), .name = "Reboot normally"}, [MainRebootNormal] = {.optionUnion = COLORTORGB(COLOR_VIOLET), .name = "Reboot normally"},
[MainRebootHekate] = {.optionUnion = COLORTORGB(COLOR_VIOLET), .name = "Reboot to bootloader/update.bin"}, [MainRebootHekate] = {.optionUnion = COLORTORGB(COLOR_VIOLET), .name = "Reboot to bootloader/update.bin"},
[MainRebootAMS] = {.optionUnion = COLORTORGB(COLOR_VIOLET), .name = "Reboot to atmosphere/reboot_payload.bin"} [MainRebootAMS] = {.optionUnion = COLORTORGB(COLOR_VIOLET), .name = "Reboot to atmosphere/reboot_payload.bin"},
[MainScripts] = {.optionUnion = COLORTORGB(COLOR_WHITE) | SKIPBIT, .name = "\n-- Scripts --"}
}; };
void HandleSD(){ void HandleSD(){
@ -108,6 +114,13 @@ void ViewCredits(){
if (hidRead()->r) if (hidRead()->r)
gfx_printf("%k\"I'm not even sure if it works\" - meme", COLOR_ORANGE); gfx_printf("%k\"I'm not even sure if it works\" - meme", COLOR_ORANGE);
/* Leaving this here for my debugging needs :)
heap_monitor_t a = {0};
heap_monitor(&a, false);
gfx_printf("\nUsed: %d\nTotal: %d\n", a.used, a.total);
*/
hidWait(); hidWait();
} }
@ -168,13 +181,60 @@ void EnterMainMenu(){
mainMenuEntries[MainRebootHekate].hide = (!sd_mounted || !FileExists("sd:/bootloader/update.bin")); mainMenuEntries[MainRebootHekate].hide = (!sd_mounted || !FileExists("sd:/bootloader/update.bin"));
mainMenuEntries[MainRebootRCM].hide = h_cfg.t210b01; mainMenuEntries[MainRebootRCM].hide = h_cfg.t210b01;
// -- Scripts --
mainMenuEntries[MainScripts].hide = (!sd_mounted || !FileExists("sd:/tegraexplorer/scripts"));
Vector_t ent = vecFromArray(mainMenuEntries, ARR_LEN(mainMenuEntries), sizeof(MenuEntry_t));
Vector_t scriptFiles = {0};
u8 hasScripts = 0;
if (!mainMenuEntries[MainScripts].hide){
int res = 0;
scriptFiles = ReadFolder("sd:/tegraexplorer/scripts", &res);
if (!res){
if (!scriptFiles.count){
free(scriptFiles.data);
mainMenuEntries[MainScripts].hide = 1;
}
else {
hasScripts = 1;
ent = newVec(sizeof(MenuEntry_t), ARRAY_SIZE(mainMenuEntries) + scriptFiles.count);
ent.count = ARRAY_SIZE(mainMenuEntries);
memcpy(ent.data, mainMenuEntries, sizeof(MenuEntry_t) * ARRAY_SIZE(mainMenuEntries));
vecForEach(FSEntry_t*, scriptFile, (&scriptFiles)){
if (!scriptFile->isDir && StrEndsWith(scriptFile->name, ".te")){
MenuEntry_t a = MakeMenuOutFSEntry(*scriptFile);
vecAdd(&ent, a);
}
}
if (ent.count == ARRAY_SIZE(mainMenuEntries)){
clearFileVector(&scriptFiles);
free(ent.data);
ent = vecFromArray(mainMenuEntries, ARR_LEN(mainMenuEntries), sizeof(MenuEntry_t));
hasScripts = 0;
mainMenuEntries[MainScripts].hide = 1;
}
}
}
}
gfx_clearscreen(); gfx_clearscreen();
gfx_putc('\n'); gfx_putc('\n');
Vector_t ent = vecFromArray(mainMenuEntries, ARR_LEN(mainMenuEntries), sizeof(MenuEntry_t));
res = newMenu(&ent, res, 79, 30, ALWAYSREDRAW, 0); res = newMenu(&ent, res, 79, 30, ALWAYSREDRAW, 0);
if (mainMenuPaths[res] != NULL) if (res < MainScripts && mainMenuPaths[res] != NULL)
mainMenuPaths[res](); mainMenuPaths[res]();
else if (hasScripts){
vecDefArray(FSEntry_t*, scriptFilesArray, scriptFiles);
RunScript("sd:/tegraexplorer/scripts", scriptFilesArray[res - ARRAY_SIZE(mainMenuEntries)]);
hidWait();
}
if (hasScripts){
clearFileVector(&scriptFiles);
free(ent.data);
}
} }
} }