mirror of
https://github.com/suchmememanyskill/TegraExplorer.git
synced 2024-11-22 11:56:42 +00:00
Start script rewrite
This commit is contained in:
parent
bfcdc2ffd5
commit
9aff029ee4
5 changed files with 168 additions and 290 deletions
|
@ -13,7 +13,7 @@
|
|||
#include "../gfx/menu.h"
|
||||
#include "../common/types.h"
|
||||
#include "../../utils/sprintf.h"
|
||||
#include "../utils/script.h"
|
||||
#include "../script/script.h"
|
||||
#include "../emmc/emmcoperations.h"
|
||||
|
||||
extern char *currentpath;
|
||||
|
@ -165,7 +165,8 @@ int filemenu(menu_entry file){
|
|||
launch_payload(fsutil_getnextloc(currentpath, file.name));
|
||||
break;
|
||||
case FILE_SCRIPT:
|
||||
ParseScript(fsutil_getnextloc(currentpath, file.name));
|
||||
//ParseScript(fsutil_getnextloc(currentpath, file.name));
|
||||
tester(fsutil_getnextloc(currentpath, file.name));
|
||||
break;
|
||||
case FILE_HEXVIEW:
|
||||
viewbytes(fsutil_getnextloc(currentpath, file.name));
|
||||
|
|
161
source/tegraexplorer/script/script.c
Normal file
161
source/tegraexplorer/script/script.c
Normal file
|
@ -0,0 +1,161 @@
|
|||
#include <string.h>
|
||||
#include "../../mem/heap.h"
|
||||
#include "../gfx/gfxutils.h"
|
||||
#include "../emmc/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"
|
||||
#include "../common/common.h"
|
||||
#include "../fs/fsactions.h"
|
||||
|
||||
|
||||
u32 countchars(char* in, char target) {
|
||||
u32 len = strlen(in);
|
||||
u32 count = 0;
|
||||
for (u32 i = 0; i < len; i++) {
|
||||
if (in[i] == target)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
char **args = NULL;
|
||||
u32 splitargs(char* in) {
|
||||
// arg like '5, "6", @arg7'
|
||||
u32 i, current = 0, count = countchars(in, ',') + 1, len = strlen(in), curcount = 0;
|
||||
|
||||
if (args != NULL) {
|
||||
for (i = 0; args[i] != NULL; i++)
|
||||
free(args[i]);
|
||||
free(args);
|
||||
}
|
||||
|
||||
args = calloc(count + 1, sizeof(char*));
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
args[i] = calloc(128, sizeof(char));
|
||||
|
||||
|
||||
for (i = 0; i < len && curcount < count; i++) {
|
||||
if (in[i] == ',') {
|
||||
curcount++;
|
||||
current = 0;
|
||||
}
|
||||
else if (in[i] == '@' || in[i] == '$') {
|
||||
while (in[i] != ',' && in[i] != ' ' && in[i] != ')' && i < len) {
|
||||
args[curcount][current++] = in[i++];
|
||||
}
|
||||
i--;
|
||||
}
|
||||
else if (in[i] >= '0' && in[i] <= '9')
|
||||
args[curcount][current++] = in[i];
|
||||
else if (in[i] == '"') {
|
||||
i++;
|
||||
while (in[i] != '"') {
|
||||
args[curcount][current++] = in[i++];
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
FIL scriptin;
|
||||
UINT endByte = 0;
|
||||
int forceExit = false;
|
||||
char currentchar = 0;
|
||||
|
||||
char getnextchar(){
|
||||
f_read(&scriptin, ¤tchar, sizeof(char), &endByte);
|
||||
|
||||
if (sizeof(char) != endByte)
|
||||
forceExit = true;
|
||||
|
||||
gfx_printf("|%c|", currentchar);
|
||||
return currentchar;
|
||||
}
|
||||
|
||||
void getfollowingchar(char end){
|
||||
while (currentchar != end){
|
||||
getnextchar();
|
||||
if (currentchar == '"'){
|
||||
while (getnextchar() != '"');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void getnextvalidchar(){
|
||||
while (!((currentchar >= '0' && currentchar <= 'Z') || (currentchar >= 'a' && currentchar <= 'z') || currentchar == '#' || currentchar == '@'))
|
||||
getnextchar();
|
||||
}
|
||||
|
||||
void mainparser(){
|
||||
char *unsplitargs;
|
||||
FSIZE_t curoffset;
|
||||
u32 argsize = 0;
|
||||
|
||||
getnextvalidchar();
|
||||
|
||||
if (currentchar == '#'){
|
||||
getfollowingchar('\n');
|
||||
return;
|
||||
}
|
||||
|
||||
gfx_printf("Getting func name...\n");
|
||||
char *funcbuff = calloc(20, sizeof(char));
|
||||
for (int i = 0; i < 19 && currentchar != '(' && currentchar != ' '; i++){
|
||||
funcbuff[i] = currentchar;
|
||||
getnextchar();
|
||||
}
|
||||
gfx_printf("Skipping to next (...\n");
|
||||
getfollowingchar('(');
|
||||
getnextchar();
|
||||
|
||||
curoffset = f_tell(&scriptin);
|
||||
gfx_printf("Skipping to next )...\n");
|
||||
getfollowingchar(')');
|
||||
argsize = f_tell(&scriptin) - curoffset;
|
||||
f_lseek(&scriptin, curoffset - 1);
|
||||
|
||||
gfx_printf("Getting args...\nSize: %d\n", argsize);
|
||||
getnextchar();
|
||||
unsplitargs = calloc(argsize, sizeof(char));
|
||||
for (int i = 0; i < argsize; i++){
|
||||
unsplitargs[i] = currentchar;
|
||||
getnextchar();
|
||||
}
|
||||
|
||||
gfx_printf("Splitting args...\n");
|
||||
int argcount = splitargs(unsplitargs);
|
||||
|
||||
gfx_printf("\n\nFunc: %s\n", funcbuff);
|
||||
gfx_printf("Split: %s\n", unsplitargs);
|
||||
|
||||
for (int i = 0; i < argcount; i++)
|
||||
gfx_printf("%d | %s\n", i, args[i]);
|
||||
|
||||
free(unsplitargs);
|
||||
free(funcbuff);
|
||||
}
|
||||
|
||||
void tester(char *path){
|
||||
int res;
|
||||
|
||||
gfx_clearscreen();
|
||||
|
||||
res = f_open(&scriptin, path, FA_READ | FA_OPEN_EXISTING);
|
||||
if (res != FR_OK){
|
||||
gfx_errDisplay("ParseScript", res, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
printerrors = false;
|
||||
|
||||
|
||||
mainparser();
|
||||
btn_wait();
|
||||
}
|
3
source/tegraexplorer/script/script.h
Normal file
3
source/tegraexplorer/script/script.h
Normal file
|
@ -0,0 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
void tester(char *path);
|
|
@ -1,287 +0,0 @@
|
|||
#include <string.h>
|
||||
#include "../../mem/heap.h"
|
||||
#include "../gfx/gfxutils.h"
|
||||
#include "../emmc/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"
|
||||
#include "../common/common.h"
|
||||
#include "../fs/fsactions.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
char func[11] = "", args[2][128] = {"", ""};
|
||||
int res, errcode;
|
||||
const int scriptver = 140;
|
||||
bool forceExit = false;
|
||||
u32 currentcolor;
|
||||
|
||||
void Part_CheckFile(){
|
||||
FILINFO fno;
|
||||
errcode = f_stat(args[0], &fno);
|
||||
}
|
||||
|
||||
void Part_SetColor(){
|
||||
if (strcmpcheck(args[0], "RED"))
|
||||
currentcolor = COLOR_RED;
|
||||
else if (strcmpcheck(args[0], "ORANGE"))
|
||||
currentcolor = COLOR_ORANGE;
|
||||
else if (strcmpcheck(args[0], "YELLOW"))
|
||||
currentcolor = COLOR_YELLOW;
|
||||
else if (strcmpcheck(args[0], "GREEN"))
|
||||
currentcolor = COLOR_GREEN;
|
||||
else if (strcmpcheck(args[0], "BLUE"))
|
||||
currentcolor = COLOR_BLUE;
|
||||
else if (strcmpcheck(args[0], "VIOLET"))
|
||||
currentcolor = COLOR_VIOLET;
|
||||
else if (strcmpcheck(args[0], "WHITE"))
|
||||
currentcolor = COLOR_WHITE;
|
||||
}
|
||||
|
||||
void Part_Wait(){
|
||||
int waitamount, begintime;
|
||||
SWAPCOLOR(currentcolor);
|
||||
|
||||
waitamount = atoi(args[0]);
|
||||
begintime = get_tmr_s();
|
||||
|
||||
while (begintime + waitamount > get_tmr_s()){
|
||||
gfx_printf("\r<Wait %d seconds> ", (begintime + waitamount) - get_tmr_s());
|
||||
}
|
||||
|
||||
gfx_printf("\r \r");
|
||||
}
|
||||
|
||||
void Part_VersionCheck(){
|
||||
int givenversion = atoi(args[0]);
|
||||
|
||||
if (givenversion > scriptver){
|
||||
gfx_printf("Script required version is too high\nUpdate TegraExplorer!");
|
||||
btn_wait();
|
||||
forceExit = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Part_Move(){
|
||||
errcode = f_rename(args[0], args[1]);
|
||||
if (errcode)
|
||||
f_rename(args[0], args[1]);
|
||||
}
|
||||
|
||||
void Part_Delete(){
|
||||
errcode = f_unlink(args[0]);
|
||||
if (errcode)
|
||||
f_unlink(args[0]);
|
||||
}
|
||||
|
||||
void Part_DeleteRecursive(){
|
||||
errcode = fsact_del_recursive(args[0]);
|
||||
}
|
||||
|
||||
void Part_Copy(){
|
||||
errcode = fsact_copy(args[0], args[1], COPY_MODE_PRINT);
|
||||
}
|
||||
|
||||
void Part_RecursiveCopy(){
|
||||
errcode = fsact_copy_recursive(args[0], args[1]);
|
||||
}
|
||||
|
||||
void Part_MakeFolder(){
|
||||
errcode = f_mkdir(args[0]);
|
||||
if (errcode)
|
||||
f_mkdir(args[0]);
|
||||
}
|
||||
|
||||
void Part_ConnectMMC(){
|
||||
if (strcmpcheck(args[0], "SYSMMC"))
|
||||
connect_mmc(SYSMMC);
|
||||
if (strcmpcheck(args[0], "EMUMMC"))
|
||||
connect_mmc(EMUMMC);
|
||||
}
|
||||
|
||||
void Part_MountMMC(){
|
||||
errcode = mount_mmc(args[0], 2);
|
||||
}
|
||||
|
||||
void Part_Print(){
|
||||
RESETCOLOR;
|
||||
SWAPCOLOR(currentcolor);
|
||||
gfx_printf("%s\n", args[0]);
|
||||
}
|
||||
|
||||
void Part_ErrorPrint(){
|
||||
RESETCOLOR;
|
||||
SWAPCOLOR(COLOR_RED);
|
||||
gfx_printf("Errorcode: %d\n", errcode);
|
||||
}
|
||||
|
||||
void Part_Exit(){
|
||||
forceExit = true;
|
||||
}
|
||||
|
||||
u8 buttons_pressed = 0;
|
||||
void Part_WaitOnUser(){
|
||||
buttons_pressed = btn_wait();
|
||||
}
|
||||
|
||||
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},
|
||||
{"VERSION", Part_VersionCheck, 1},
|
||||
{"WAIT", Part_Wait, 1},
|
||||
{"COLOR", Part_SetColor, 1},
|
||||
{"CHECKPATH", Part_CheckFile, 1},
|
||||
{"NULL", NULL, -1}
|
||||
};
|
||||
|
||||
int ParsePart(){
|
||||
int i;
|
||||
for (i = 0; parts[i].arg_amount != -1; i++){
|
||||
if (strcmpcheck(func, parts[i].name))
|
||||
return i;
|
||||
}
|
||||
gfx_printf("Parsing error...\nPress any key to continue");
|
||||
btn_wait();
|
||||
forceExit = true;
|
||||
printerrors = 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;
|
||||
currentcolor = COLOR_WHITE;
|
||||
|
||||
gfx_clearscreen();
|
||||
|
||||
res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING);
|
||||
if (res != FR_OK){
|
||||
gfx_errDisplay("ParseScript", res, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
printerrors = false;
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
printerrors = true;
|
||||
f_close(&in);
|
||||
}
|
|
@ -8,4 +8,4 @@ typedef struct _script_parts {
|
|||
short arg_amount;
|
||||
} script_parts;
|
||||
|
||||
void ParseScript(char* path);
|
||||
//void ParseScript(char* path);
|
||||
|
|
Loading…
Reference in a new issue