boxscript-interperter-c/source/wiiapp.cpp

233 lines
5.1 KiB
C++
Raw Normal View History

2022-12-01 03:27:30 +00:00
#if defined(GEKKO)
#include "lexer.hpp"
#include "parser.hpp"
#include <cstdio>
#include <cstdlib>
#include <gccore.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fat.h>
#if defined(HW_RVL)
#include <wiiuse/wpad.h>
#endif
#include <ogc/pad.h>
static void *xfb = NULL;
static GXRModeObj *rmode = NULL;
BoxScript::ListNode* _root_node;
void clear_screen_ogc()
{
VIDEO_ClearFrameBuffer(rmode,xfb,0);
}
int appstate=0;
std::string read_file(std::string name)
{
FILE* f=fopen(name.c_str(),"r");
std::string text="";
while(true)
{
int read = fgetc(f);
if(read == EOF) break;
text += (char)read;
}
fclose(f);
return text;
}
bool DirExists(std::string path)
{
DIR* dir;
bool exists = false;
dir = opendir(path.c_str());
if(dir != NULL)
{
exists=true;
(void)closedir(dir);
}
return exists;
}
void LoadSubscripts(BoxScript::ListNode* nodes,std::string path)
{
std::vector<std::string> files = get_files(path);
for(std::string file : files)
{
std::string newPath = path + "/" + file;
std::vector<BoxScript::Lexer::LexToken> tokens=BoxScript::Lexer::Lex(read_file(newPath));
BoxScript::Parse(nodes,tokens);
}
}
void PrintElement(std::string text,bool checked)
{
printf("\t[%c] %s\n",checked ? '*' : ' ', text.c_str());
}
void LoadScript(BoxScript::ListNode* nodes)
{
if(!DirExists("/BoxScript"))
{
mkdir("/BoxScript",0755);
}
if(DirExists("/BoxScript/Autoboot"))
{
if(DirExists("/BoxScript/Autoboot/deps"))
LoadSubscripts(nodes,"/BoxScript/Autoboot/deps");
std::vector<BoxScript::Lexer::LexToken> tokens=BoxScript::Lexer::Lex(read_file("/BoxScript/Autoboot/app.bs"));
BoxScript::Parse(nodes,tokens);
}else{
std::vector<std::string> dirs= get_directories("/BoxScript");
int item=0;
while(appstate != -1)
{
#if defined(HW_RVL)
WPAD_ScanPads();
#endif
PAD_ScanPads();
if(PAD_ButtonsDown(0) & PAD_BUTTON_UP)
{
item--;
if(item < 0)
{
item = dirs.size()-1;
}
}
if(PAD_ButtonsDown(0) & PAD_BUTTON_DOWN)
{
item++;
if(item >= dirs.size())
{
item=0;
}
}
if(PAD_ButtonsDown(0) & PAD_BUTTON_A)
{
break;
}
#if defined(HW_RVL)
if(WPAD_ButtonsDown(0) & WPAD_BUTTON_UP)
{
item--;
if(item < 0)
{
item = dirs.size()-1;
}
}
if(WPAD_ButtonsDown(0) & WPAD_BUTTON_DOWN)
{
item++;
if(item >= dirs.size())
{
item=0;
}
}
if(WPAD_ButtonsDown(0) & WPAD_BUTTON_A)
{
break;
}
#endif
clear_screen_ogc();
printf("\x1b[2;2H");
printf("\n\n");
printf("\tBoxScript 1.0 Wii Version\n");
int indexOnScreen = item % 14;
int screenOffset = (item - indexOnScreen);
int i=0;
for(i = 0;i<14;i++)
{
if(screenOffset + i < dirs.size())
PrintElement(dirs[i+screenOffset],i == indexOnScreen);
}
//printf("\tNo Items: %i",i);
VIDEO_WaitVSync();
}
if(appstate == -1) exit(0);
if(DirExists("/BoxScript/" + dirs[item]))
LoadSubscripts(nodes,"/BoxScript/" + dirs[item] + "/deps");
std::vector<BoxScript::Lexer::LexToken> tokens=BoxScript::Lexer::Lex(read_file("/BoxScript/" + dirs[item] + "/app.bs"));
BoxScript::Parse(nodes,tokens);
}
}
BoxScript::ApplicationState* state;
void WiiResetPressed(unsigned int a,void* ptr)
{
if(appstate <= 0)
{
appstate= -1;
}else{
state->StopRunning();
}
}
//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------
BoxScript::NetworkInit();
fatInitDefault();
SYS_SetResetCallback(WiiResetPressed);
// Initialise the video system
VIDEO_Init();
PAD_Init();
#if defined(HW_RVL)
WPAD_Init();
#endif
// This function initialises the attached controllers
// Obtain the preferred video mode from the system
// This will correspond to the settings in the Wii menu
rmode = VIDEO_GetPreferredMode(NULL);
// Allocate memory for the display in the uncached region
xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
// Initialise the console, required for printf
console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
// Set up the video registers with the chosen mode
VIDEO_Configure(rmode);
// Tell the video hardware where our display memory is
VIDEO_SetNextFramebuffer(xfb);
// Make the display visible
VIDEO_SetBlack(FALSE);
// Flush the video register changes to the hardware
VIDEO_Flush();
// Wait for Video setup to complete
VIDEO_WaitVSync();
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
// The console understands VT terminal escape codes
// This positions the cursor on row 2, column 0
// we can use variables for this with format codes too
// e.g. printf ("\x1b[%d;%dH", row, column );
printf("\x1b[2;2H");
_root_node = new BoxScript::ListNode();
LoadScript(_root_node);
appstate=1;
state=new BoxScript::ApplicationState();
RegisterMainRuntimeFunctions(state);
_root_node->Evaluate(state);
DestroyApp(0);
}
void DestroyApp(int a)
{
delete _root_node;
exit(a);
}
#endif