int fioRead(int fd, void *ptr, int size) { struct _fio_read_arg arg; int res; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.fd = fd; arg.ptr = ptr; arg.size = size; arg.read_data = (struct _fio_read_data *)_fio_intr_data; if (!IS_UNCACHED_SEG(ptr)) SifWriteBackDCache(ptr, size); SifWriteBackDCache(_fio_intr_data, 128); SifWriteBackDCache(&arg, sizeof(arg)); if ((res = SifCallRpc(&_fio_cd, FIO_F_READ, _fio_block_mode, &arg, sizeof arg, _fio_recv_data, 4, (void *)_fio_read_intr, _fio_intr_data)) < 0) return res; if(_fio_block_mode == FIO_NOWAIT) return 0; else return _fio_recv_data[0]; }
int fioOpen(const char *name, int mode) { struct _fio_open_arg arg; int res; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.mode = mode; strncpy(arg.name, name, FIO_PATH_MAX - 1); arg.name[FIO_PATH_MAX - 1] = 0; /* TODO: All of these can be cleaned up (get rid of res), and an appropiate error from errno.h be used instead. */ if ((res = SifCallRpc(&_fio_cd, FIO_F_OPEN, _fio_block_mode, &arg, sizeof arg, _fio_recv_data, 4, (void *)_fio_intr, NULL)) < 0) return res; if(_fio_block_mode == FIO_NOWAIT) return 0; else return _fio_recv_data[0]; }
int fioChstat(const char *name, fio_stat_t *buf, u32 cbit) { struct _fio_chstat_arg arg; int res, result; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.p.cbit = cbit; memcpy(&arg.stat, buf, sizeof(fio_stat_t)); strncpy(arg.name, name, FIO_PATH_MAX - 1); arg.name[FIO_PATH_MAX - 1] = 0; if ((res = SifCallRpc(&_fio_cd, FIO_F_CHSTAT, 0, &arg, sizeof arg, &arg, 4, (void *)_fio_intr, NULL)) >= 0) { result=arg.p.result; } else { result=res; } return result; }
int fioFormat(const char *name) { union { char path[FIO_PATH_MAX]; int result; } arg; int res, result; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); strncpy(arg.path, name, FIO_PATH_MAX - 1); arg.path[FIO_PATH_MAX - 1] = 0; if ((res = SifCallRpc(&_fio_cd, FIO_F_FORMAT, 0, &arg, sizeof arg, &arg, 4, (void *)_fio_intr, NULL)) >= 0) { result=arg.result; } else { result=res; } return result; }
int fioDread(int fd, fio_dirent_t *buf) { struct _fio_dread_arg arg; int res, result; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.p.fd = fd; arg.buf = buf; if (!IS_UNCACHED_SEG(buf)) SifWriteBackDCache(buf, sizeof(fio_dirent_t)); if ((res = SifCallRpc(&_fio_cd, FIO_F_DREAD, 0, &arg, sizeof arg, &arg, 4, (void *)_fio_intr, NULL)) >= 0) { result=arg.p.result; } else { result=res; } return result; }
int fioGetstat(const char *name, fio_stat_t *buf) { struct _fio_getstat_arg arg; int res, result; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.p.buf = buf; strncpy(arg.name, name, FIO_PATH_MAX - 1); arg.name[FIO_PATH_MAX - 1] = 0; if (!IS_UNCACHED_SEG(buf)) SifWriteBackDCache(buf, sizeof(fio_stat_t)); if ((res = SifCallRpc(&_fio_cd, FIO_F_GETSTAT, 0, &arg, sizeof arg, &arg, 4, (void *)_fio_intr, NULL)) >= 0) { result=arg.p.result; } else { result=res; } return result; }
int fioDclose(int fd) { union { int fd; int result; } arg; int res, result; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.fd = fd; if ((res = SifCallRpc(&_fio_cd, FIO_F_DCLOSE, 0, &arg, sizeof arg, &arg, 4, (void *)_fio_intr, NULL)) >= 0) { result=arg.result; } else { result=res; } return result; }
int fioIoctl(int fd, int request, void *data) { struct _fio_ioctl_arg arg; int res, result; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.p.fd = fd; arg.request = request; memcpy(arg.data, data, 1024); if ((res = SifCallRpc(&_fio_cd, FIO_F_IOCTL, 0, &arg, sizeof arg, &arg, 4, (void *)_fio_intr, NULL)) >= 0) { result=arg.p.result; } else { result=res; } return result; }
int fioLseek(int fd, int offset, int whence) { struct _fio_lseek_arg arg; int res, result; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.p.fd = fd; arg.offset = offset; arg.whence = whence; if ((res = SifCallRpc(&_fio_cd, FIO_F_LSEEK, 0, &arg, sizeof arg, &arg, 4, (void *)_fio_intr, NULL)) >= 0) { result=arg.p.result; } else { result=res; } return result; }
void init_basic_modules(const char *path) { char *gz; int gz_size; module_t basic_modules[7] = { { "sio2man", "freesio2.irx", NULL, 0, 0 }, { "mcman", "mcman.irx", NULL, 0, 0 }, { "mcserv", "mcserv.irx", NULL, 0, 0 }, { "mtapman", "freemtap.irx", NULL, 0, 0 }, { "padman", "freepad.irx", NULL, 0, 0 }, { "IOX/File_Manager", "iomanX.irx", NULL, 0, 0 }, { "IOX/File_Manager_Rpc", "fileXio.irx", NULL, 0, 0 } }; gz = init_modules_tgz(path,&gz_size); init_load_irx(gz, gz_size, basic_modules, 7); // Init various libraries mcInit(MC_TYPE_MC); mtapInit(); padInit(0); mtapPortOpen(0); mtapPortOpen(1); if (!basic_modules[6].result) { fileXioInit(); } else { // Problem initializing fileXio modules fioInit(); } if (gz != (char*)modules_tgz) { free(gz); } }
int fioClose(int fd) { union { int fd; int result; } arg; int res; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.fd = fd; if ((res = SifCallRpc(&_fio_cd, FIO_F_CLOSE, 0, &arg, 4, &arg, 4, (void *)_fio_intr, NULL)) < 0) return res; return arg.result; }
int fioWrite(int fd, const void *ptr, int size) { struct _fio_write_arg arg; int mis, res, result; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); arg.fd = fd; arg.ptr = ptr; arg.size = size; /* Copy the unaligned (16-byte) portion into the argument */ mis = 0; if ((u32)ptr & 0xf) { mis = 16 - ((u32)ptr & 0xf); if (mis > size) mis = size; } arg.mis = mis; if (mis) memcpy(arg.aligned, ptr, mis); if (!IS_UNCACHED_SEG(ptr)) SifWriteBackDCache((void*)ptr, size); if ((res = SifCallRpc(&_fio_cd, FIO_F_WRITE, _fio_block_mode, &arg, sizeof arg, _fio_recv_data, 4, (void *)_fio_intr, NULL)) >= 0) { result=(_fio_block_mode == FIO_NOWAIT)?0:_fio_recv_data[0]; } else { result=res; } return result; }
int fioDopen(const char *name) { union { char name[FIO_PATH_MAX]; int result; } arg; int res; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); strncpy(arg.name, name, FIO_PATH_MAX - 1); arg.name[FIO_PATH_MAX - 1] = 0; if ((res = SifCallRpc(&_fio_cd, FIO_F_DOPEN, 0, &arg, sizeof arg, &arg, 4, (void *)_fio_intr, NULL)) < 0) return res; return arg.result; }
int fioRmdir(const char* dirname) { union { char path[FIO_PATH_MAX]; int result; } arg; int res; if ((res = fioInit()) < 0) return res; WaitSema(_fio_completion_sema); strncpy(arg.path, dirname, FIO_PATH_MAX - 1); arg.path[FIO_PATH_MAX - 1] = 0; if ((res = SifCallRpc(&_fio_cd, FIO_F_RMDIR, 0, &arg, sizeof arg, &arg, 4, (void *)_fio_intr, NULL)) < 0) return res; return arg.result; }
int fioSync(int mode, int *retVal) { int res; if ((res = fioInit()) < 0) return res; if(_fio_block_mode != FIO_NOWAIT) return -E_LIB_UNSUPPORTED; switch(mode) { case FIO_WAIT: WaitSema(_fio_completion_sema); SignalSema(_fio_completion_sema); if(retVal != NULL) *retVal = *(int *)UNCACHED_SEG(&_fio_recv_data[0]); return FIO_COMPLETE; case FIO_NOWAIT: if(PollSema(_fio_completion_sema) < 0) return FIO_INCOMPLETE; SignalSema(_fio_completion_sema); if(retVal != NULL) *retVal = *(int *)UNCACHED_SEG(&_fio_recv_data[0]); return FIO_COMPLETE; default: return -E_LIB_UNSUPPORTED; } }
int main(int argc, char *argv[]) #endif { #ifdef PSP SetupCallbacks(); scePowerSetClockFrequency(333, 333, 166); #endif for (int i=0; i<FLAG_COUNT; i++) { GAME_FLAGS[i] = false; } UNUSED(argc); string argvString = ""; #ifndef WII argvString = string(argv[0]); #else if (!fatInitDefault()) { printf("fatInitDefault ERROR #1"); std::fflush(stdout); timer.delay(500); exit(-1); } #endif get_filepath(); // fallback in case getcwd returns null if (FILEPATH.size() == 0) { std::cout << "Could not read path, fallback to using argv" << std::endl; FILEPATH = argvString.substr(0, argvString.size()-EXEC_NAME.size()); } std::cout << "main - argvString: '" << argvString << "', FILEPATH: '" << FILEPATH << "'" << std::endl; std::fflush(stdout); #ifdef PLAYSTATION2 std::cout << "PS2.DEBUG #1" << std::endl; std::fflush(stdout); #ifndef PS2LINK SifIopReset(NULL, 0); // clean previous loading of irx by apps like ulaunchElf. Comment this line to get cout on ps2link #endif printf("DEBUG.PS2 #1.1\n"); /* SP193: Being creative (Do something while waiting for the slow IOP to be reset). =D */ int main_id = GetThreadId(); ChangeThreadPriority(main_id, 72); std::cout << "PS2.DEBUG #1.1" << std::endl; std::fflush(stdout); printf("DEBUG.PS2 #1.2\n"); #ifndef PS2LINK while(SifIopSync()) { std::cout << "PS2.SifIopSync()" << std::endl; } #endif /* Initialize and connect to all SIF services on the IOP. */ SifInitRpc(0); SifInitIopHeap(); SifLoadFileInit(); fioInit(); printf("DEBUG.PS2 #1.3\n"); /* Apply the SBV LMB patch to allow modules to be loaded from a buffer in EE RAM. */ sbv_patch_enable_lmb(); // --- DEBUG --- // //FILEPATH = "cdfs:/"; // --- DEBUG --- // std::cout << "PS2.DEBUG #2" << std::endl; std::fflush(stdout); if (FILEPATH.find("mass:") != std::string::npos) { printf("DEBUG.PS2 #1.4\n"); std::cout << "PS2.DEBUG Load USB" << std::endl; std::fflush(stdout); PS2_load_USB(); } if (FILEPATH.find("cdfs") != std::string::npos || FILEPATH.find("cdrom") != std::string::npos) { printf("DEBUG.PS2 #1.5\n"); std::cout << "PS2.DEBUG Load CDROM" << std::endl; std::fflush(stdout); FILEPATH = "cdfs:"; PS2_load_CDROM(); } printf("DEBUG.PS2 #2\n"); std::cout << "PS2.DEBUG #3" << std::endl; std::fflush(stdout); #endif // check command-line paramethers if (argc > 1) { for (int i=1; i<argc; i++) { std::string temp_argv(argv[i]); if (temp_argv == "--fullscreen") { } else if (temp_argv == "--quickload") { GAME_FLAGS[FLAG_QUICKLOAD] = true; } else if (temp_argv == "--invencible") { // player have infinite HP GAME_FLAGS[FLAG_INVENCIBLE] = true; } else if (temp_argv == "--allweapons") { // player have all weapons available even if GAME_FLAGS[FLAG_ALLWEAPONS] = true; } else if (temp_argv == "--infinitejump") { // player can jump again and again GAME_FLAGS[FLAG_INFINITE_JUMP] = true; } } } std::cout << "PS2.DEBUG #7" << std::endl; std::fflush(stdout); //fio.check_conversion(); fio.read_game(game_data); //GAME_FLAGS[FLAG_INFINITE_HP] = true; // DEBUG gameControl.get_drop_item_ids(); soundManager.init_audio_system(); // PS2 version have to load config AFTER SDL_Init due to SDK issues #ifdef LINUX SAVEPATH = std::string(getenv("HOME")) + "/.rockbot/"; mkdir(SAVEPATH.c_str(), 0777); //std::cout << "SAVEPATH: " << SAVEPATH << ", mkdir-res: " << res << ", errno: " << errno << std::endl; #elif WIN32 SAVEPATH = std::string(getenv("APPDATA")) + "/rockbot"; std::cout << "SAVEPATH: " << SAVEPATH << std::endl; _mkdir(SAVEPATH.c_str()); #else SAVEPATH = FILEPATH; #endif #ifndef PLAYSTATION2 fio.load_config(game_config); #endif // INIT GRAPHICS if (graphLib.initGraphics() != true) { printf("ERROR intializing graphic\n"); return -1; } // define SAVEPATH #ifdef PLAYSTATION2 PS2_load_MC(); SAVEPATH = "mc0:Rockbot/"; if (fioMkdir(SAVEPATH.c_str()) < 0) { std::cout << "main - warning: could not create '" << SAVEPATH << "' folder" << std::endl; std::fflush(stdout); /// @TODO - check if directory exists } else { std::cout << "Folder '" << SAVEPATH << "' created" << std::endl; std::fflush(stdout); } #endif have_save = fio.save_exists(); #ifndef DEBUG_OUTPUT // redirect output to null std::string cout_file = "/dev/null"; std::ofstream out(cout_file.c_str()); std::cout.rdbuf(out.rdbuf()); #else // --- REDIRECT STDOUT TO A FILE --- // #if defined(PSP) || defined(WII) || defined(ANDROID) || defined(DINGUX) || defined(PLAYSTATION2) //std::string cout_file = SAVEPATH + "/stdout.txt"; std::string cout_file = FILEPATH + "/stdout.txt"; std::streambuf *coutbuf = std::cout.rdbuf(); std::ofstream out(cout_file.c_str()); std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt! #endif #endif graphLib.preload(); #ifdef PLAYSTATION2 fio.load_config(game_config); PS2_create_save_icons(); #endif draw_lib.preload(); gameControl.currentStage = APEBOT; // INIT GAME if (GAME_FLAGS[FLAG_QUICKLOAD] == false) { if (gameControl.showIntro() == false) { std::cout << "ERROR SHOWING INTRO" << std::endl; return 0; } } else { gameControl.quick_load_game(); //ending end_obj; //end_obj.start(); //return 1; } input.clean(); input.p1_input[BTN_START] = 0; input.waitTime(200); input.clean(); bool run_game = true; while (run_game) { #if !defined(PSP) && !defined(DINGUX) timer.start_ticker(); #endif #ifdef PLAYSTATION2 RotateThreadReadyQueue(_MIXER_THREAD_PRIORITY); #endif gameControl.showGame(); #ifdef DEBUG_SHOW_FPS gameControl.fps_count(); #endif draw_lib.update_screen(); if (input.p1_input[BTN_QUIT] == 1) { //std::cout << "LEAVE #3" << std::endl; std::fflush(stdout); gameControl.leave_game(); } unsigned int now_ticks = timer.get_ticks(); if (now_ticks < (1000 / FRAMES_PER_SECOND)) { timer.delay((1000 / FRAMES_PER_SECOND) - now_ticks); } } /// @TODO: sdl quit sub-systems #ifdef PSP sceKernelExitGame(); return 0; #else SDL_Quit(); #endif return 1; }