1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-09-20 05:53:25 +01:00
TegraExplorer/source/tegraexplorer/script.c
2020-02-07 17:43:20 +01:00

235 lines
5.8 KiB
C

#include <string.h>
#include "../mem/heap.h"
#include "gfx.h"
#include "fs.h"
#include "io.h"
#include "emmc.h"
#include "../utils/types.h"
#include "../libs/fatfs/ff.h"
#include "../utils/sprintf.h"
#include "../utils/btn.h"
#include "../gfx/gfx.h"
#include "../utils/util.h"
#include "../storage/emummc.h"
#include "script.h"
char func[11] = "", args[2][128] = {"", ""};
int res, errcode;
int Part_Move(){
errcode = f_rename(args[0], args[1]);
f_rename(args[0], args[1]);
return errcode;
}
int Part_Delete(){
errcode = f_unlink(args[0]);
f_unlink(args[0]);
return errcode;
}
int Part_DeleteRecursive(){
errcode = del_recursive(args[0]);
return errcode;
}
int Part_Copy(){
errcode = copy(args[0], args[1], true, false);
return errcode;
}
int Part_RecursiveCopy(){
errcode = copy_recursive(args[0], args[1]);
return errcode;
}
int Part_MakeFolder(){
errcode = f_mkdir(args[0]);
f_mkdir(args[0]);
return errcode;
}
int Part_ConnectMMC(){
if (strcmpcheck(args[0], "SYSMMC"))
connect_mmc(SYSMMC);
if (strcmpcheck(args[0], "EMUMMC"))
connect_mmc(EMUMMC);
return 0;
}
int Part_MountMMC(){
errcode = mount_mmc(args[0], 2);
return errcode;
}
int Part_Print(){
RESETCOLOR;
gfx_printf(args[0]);
gfx_printf("\n");
return 0;
}
int Part_ErrorPrint(){
RESETCOLOR;
gfx_printf("Errorcode: %d\n", errcode);
return 0;
}
bool forceExit = false;
int Part_Exit(){
forceExit = true;
return 0;
}
u8 buttons_pressed = 0;
int Part_WaitOnUser(){
buttons_pressed = btn_wait();
return (buttons_pressed & BTN_POWER);
}
script_parts parts[] = {
{"COPY", Part_Copy, 2},
{"COPY-R", Part_RecursiveCopy, 2},
{"MKDIR", Part_MakeFolder, 1},
{"CON_MMC", Part_ConnectMMC, 1},
{"MNT_MMC", Part_MountMMC, 1},
{"PRINT", Part_Print, 1},
{"ERRPRINT", Part_ErrorPrint, 0},
{"EXIT", Part_Exit, 0},
{"PAUSE", Part_WaitOnUser, 0},
{"DEL", Part_Delete, 1},
{"DEL-R", Part_DeleteRecursive, 1},
{"MOVE", Part_Move, 2},
{"NULL", NULL, -1}
};
int ParsePart(){
int i;
for (i = 0; parts[i].arg_amount != -1; i++){
if (strcmpcheck(func, parts[i].name))
return i;
}
forceExit = true;
return -1;
}
FIL in;
UINT endByte = 0;
char GetNextByte(){
char single;
f_read(&in, &single, sizeof(char), &endByte);
if (sizeof(char) != endByte)
forceExit = true;
return single;
}
void ParseScript(char* path){
char currentchar;
int strlength;
bool inifstatement = false;
forceExit = false;
clearscreen();
res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING);
if (res != FR_OK){
message(COLOR_RED, "File Opening Failed\nErrcode %d", res);
return;
}
while (!forceExit){
currentchar = GetNextByte();
if (endByte == 0 || currentchar == (char)EOF)
break;
switch(currentchar){
case '{':
if (!inifstatement)
while (currentchar != '}')
currentchar = GetNextByte();
break;
case '}':
if (inifstatement)
inifstatement = false;
break;
case '<':
strlength = 0;
currentchar = GetNextByte();
while (currentchar != '>'){
func[strlength++] = currentchar;
currentchar = GetNextByte();
}
func[strlength] = '\0';
res = ParsePart();
if (res == -1)
break;
for (int i = 0; i < parts[res].arg_amount; i++){
while (currentchar != 0x22)
currentchar = GetNextByte();
strlength = 0;
currentchar = GetNextByte();
while (currentchar != 0x22){
args[i][strlength++] = currentchar;
currentchar = GetNextByte();
}
args[i][strlength] = '\0';
if (i < parts[res].arg_amount)
currentchar = GetNextByte();
}
parts[res].handler();
break;
case '$':
strlength = 0;
currentchar = GetNextByte();
while (currentchar != '$'){
func[strlength++] = currentchar;
currentchar = GetNextByte();
}
func[strlength] = '\0';
if (strcmpcheck(func, "ERROR") || strcmpcheck(func, "TRUE")){
inifstatement = (errcode);
}
else if (strcmpcheck(func, "NOERROR") || strcmpcheck(func, "FALSE")){
inifstatement = (!errcode);
}
else if (strcmpcheck(func, "BTN_POWER")){
inifstatement = (buttons_pressed & BTN_POWER);
}
else if (strcmpcheck(func, "BTN_VOL+")){
inifstatement = (buttons_pressed & BTN_VOL_UP);
}
else if (strcmpcheck(func, "BTN_VOL-")){
inifstatement = (buttons_pressed & BTN_VOL_DOWN);
}
else if (strcmpcheck(func, "EMUMMC")){
inifstatement = (emu_cfg.enabled);
}
else if (strcmpcheck(func, "NOEMUMMC")){
inifstatement = (!emu_cfg.enabled);
}
if (inifstatement)
while(currentchar != '{')
currentchar = GetNextByte();
break;
}
}
f_close(&in);
}