Skip to content

Commit

Permalink
Merge pull request #852 from KrahJohlito/autobdm
Browse files Browse the repository at this point in the history
parse launch args to auto launch bdm games
  • Loading branch information
uyjulian committed Dec 20, 2022
2 parents b226c61 + afe1922 commit 26f5fe7
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 62 deletions.
2 changes: 2 additions & 0 deletions include/bdmsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ void bdmInit();
item_list_t *bdmGetObject(int initOnly);
int bdmFindPartition(char *target, const char *name, int write);
void bdmLoadModules(void);
void bdmLaunchGame(int id, config_set_t *configSet);
void bdmSetPrefix(void);

#endif
5 changes: 5 additions & 0 deletions include/opl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "config.h"

#include "include/hddsupport.h"
#include "include/supportbase.h"

// Last Played Auto Start
#include <time.h>
Expand Down Expand Up @@ -78,6 +79,9 @@ void moduleUpdateMenu(int mode, int themeChanged, int langChanged);
void handleLwnbdSrv();
void deinit(int exception, int modeSelected);

// Shutdown minimal services initiated for auto loading.
void miniDeinit(config_set_t *configSet);

extern char *gBaseMCDir;

enum ETH_OP_MODES {
Expand Down Expand Up @@ -199,6 +203,7 @@ extern unsigned char gDefaultSelTextColor[3];
extern unsigned char gDefaultUITextColor[3];

extern hdl_game_info_t *gAutoLaunchGame;
extern base_game_info_t *gAutoLaunchBDMGame;
extern char *gHDDPrefix;
extern char gOPLPart[128];

Expand Down
1 change: 1 addition & 0 deletions include/supportbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef struct
u8 unknown2[10]; // Always zero
} USBExtreme_game_entry_t;

int isValidIsoName(char *name, int *pNameLen);
int sbIsSameSize(const char *prefix, int prevSize);
int sbCreateSemaphore(void);
int sbReadList(base_game_info_t **list, const char *prefix, int *fsize, int *gamecount);
Expand Down
65 changes: 46 additions & 19 deletions src/bdmsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ static int mx4sioModLoaded = 0;
// forward declaration
static item_list_t bdmGameList;

void bdmSetPrefix(void)
{
if (gBDMPrefix[0] != '\0')
sprintf(bdmPrefix, "mass0:%s/", gBDMPrefix);
else
sprintf(bdmPrefix, "mass0:");
}

// Identifies the partition that the specified file is stored on and generates a full path to it.
int bdmFindPartition(char *target, const char *name, int write)
{
Expand Down Expand Up @@ -237,19 +245,24 @@ static void bdmRenameGame(int id, char *newName)
bdmULSizePrev = -2;
}

static void bdmLaunchGame(int id, config_set_t *configSet)
void bdmLaunchGame(int id, config_set_t *configSet)
{
int i, fd, index, compatmask = 0;
int EnablePS2Logo = 0;
int result;
unsigned int start;
unsigned int startCluster;
char partname[256], filename[32];
base_game_info_t *game = &bdmGames[id];
base_game_info_t *game;
struct cdvdman_settings_bdm *settings;
u32 layer1_start, layer1_offset;
unsigned short int layer1_part;

if (gAutoLaunchBDMGame == NULL)
game = &bdmGames[id];
else
game = gAutoLaunchBDMGame;

char vmc_name[32], vmc_path[256], have_error = 0;
int vmc_id, size_mcemu_irx = 0;
bdm_vmc_infos_t bdm_vmc_infos;
Expand Down Expand Up @@ -291,16 +304,19 @@ static void bdmLaunchGame(int id, config_set_t *configSet)
}
}

if (have_error) {
char error[256];
if (have_error == 2) // VMC file is fragmented
snprintf(error, sizeof(error), _l(_STR_ERR_VMC_FRAGMENTED_CONTINUE), vmc_name, (vmc_id + 1));
else
snprintf(error, sizeof(error), _l(_STR_ERR_VMC_CONTINUE), vmc_name, (vmc_id + 1));
if (!guiMsgBox(error, 1, NULL)) {
return;
if (gAutoLaunchBDMGame == NULL) {
if (have_error) {
char error[256];
if (have_error == 2) // VMC file is fragmented
snprintf(error, sizeof(error), _l(_STR_ERR_VMC_FRAGMENTED_CONTINUE), vmc_name, (vmc_id + 1));
else
snprintf(error, sizeof(error), _l(_STR_ERR_VMC_CONTINUE), vmc_name, (vmc_id + 1));
if (!guiMsgBox(error, 1, NULL)) {
return;
}
}
}
} else
LOG("VMC error\n");

for (i = 0; i < size_bdm_mcemu_irx; i++) {
if (((u32 *)&bdm_mcemu_irx)[i] == (0xC0DEFAC0 + vmc_id)) {
Expand Down Expand Up @@ -390,13 +406,16 @@ static void bdmLaunchGame(int id, config_set_t *configSet)
settings->common.zso_cache = bdmCacheSize;

if ((result = sbLoadCheats(bdmPrefix, game->startup)) < 0) {
switch (result) {
case -ENOENT:
guiWarning(_l(_STR_NO_CHEATS_FOUND), 10);
break;
default:
guiWarning(_l(_STR_ERR_CHEATS_LOAD_FAILED), 10);
}
if (gAutoLaunchBDMGame == NULL) {
switch (result) {
case -ENOENT:
guiWarning(_l(_STR_NO_CHEATS_FOUND), 10);
break;
default:
guiWarning(_l(_STR_ERR_CHEATS_LOAD_FAILED), 10);
}
} else
LOG("Cheats error\n");
}

if (gRememberLastPlayed) {
Expand All @@ -406,7 +425,15 @@ static void bdmLaunchGame(int id, config_set_t *configSet)

if (configGetStrCopy(configSet, CONFIG_ITEM_ALTSTARTUP, filename, sizeof(filename)) == 0)
strcpy(filename, game->startup);
deinit(NO_EXCEPTION, BDM_MODE); // CAREFUL: deinit will call bdmCleanUp, so bdmGames/game will be freed

if (gAutoLaunchBDMGame == NULL)
deinit(NO_EXCEPTION, BDM_MODE); // CAREFUL: deinit will call bdmCleanUp, so bdmGames/game will be freed
else {
miniDeinit(configSet);

free(gAutoLaunchBDMGame);
gAutoLaunchBDMGame = NULL;
}

if (!strcmp(bdmDriver, "usb")) {
settings->common.fakemodule_flags |= FAKE_MODULE_FLAG_USBD;
Expand Down
19 changes: 3 additions & 16 deletions src/hddsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
#include "include/cheatman.h"
#include "modules/iopcore/common/cdvd_config.h"

#ifdef PADEMU
#include <libds34bt.h>
#include <libds34usb.h>
#endif

#define NEWLIB_PORT_AWARE
#include <fileXio_rpc.h> // fileXioFormat, fileXioMount, fileXioUmount, fileXioDevctl
#include <io_common.h> // FIO_MT_RDWR
Expand Down Expand Up @@ -506,24 +501,16 @@ void hddLaunchGame(int id, config_set_t *configSet)
}
}

if (gAutoLaunchGame == NULL) {
if (gAutoLaunchGame == NULL)
deinit(NO_EXCEPTION, HDD_MODE); // CAREFUL: deinit will call hddCleanUp, so hddGames/game will be freed
} else {
ioBlockOps(1);
#ifdef PADEMU
ds34usb_reset();
ds34bt_reset();
#endif
configFree(configSet);
else {
miniDeinit(configSet);

free(gAutoLaunchGame);
gAutoLaunchGame = NULL;

fileXioUmount("pfs0:");
fileXioDevctl("pfs:", PDIOC_CLOSEALL, NULL, 0, NULL, 0);

ioEnd();
configEnd();
}

settings->common.fakemodule_flags |= FAKE_MODULE_FLAG_DEV9;
Expand Down
139 changes: 113 additions & 26 deletions src/opl.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "include/appsupport.h"

#include "include/cheatman.h"

#include "include/sound.h"

// FIXME: We should not need this function.
Expand Down Expand Up @@ -197,6 +196,7 @@ unsigned char gDefaultTextColor[3];
unsigned char gDefaultSelTextColor[3];
unsigned char gDefaultUITextColor[3];
hdl_game_info_t *gAutoLaunchGame;
base_game_info_t *gAutoLaunchBDMGame;
char gOPLPart[128];
char *gHDDPrefix;
char gExportName[32];
Expand Down Expand Up @@ -1579,6 +1579,7 @@ static void setDefaults(void)
clearIOModuleT(&list_support[APP_MODE]);

gAutoLaunchGame = NULL;
gAutoLaunchBDMGame = NULL;
gOPLPart[0] = '\0';
gHDDPrefix = "pfs0:";
gBaseMCDir = "mc?:OPL";
Expand Down Expand Up @@ -1725,48 +1726,81 @@ static void deferredAudioInit(void)
LOG("sfxInit: %d samples loaded.\n", ret);
}

static void autoLaunchHDDGame(char *argv[])
// ----------------------------------------------------------
// --------------------- Auto Loading -----------------------
// ----------------------------------------------------------

static void miniInit(int mode)
{
int ret;
char path[256];
config_set_t *configSet;

setDefaults();

gAutoLaunchGame = malloc(sizeof(hdl_game_info_t));
if (gAutoLaunchGame == NULL) {
PREINIT_LOG("Failed to allocate memory. Loading GUI\n");
return;
}

memset(gAutoLaunchGame, 0, sizeof(hdl_game_info_t));

snprintf(gAutoLaunchGame->startup, sizeof(gAutoLaunchGame->startup), argv[1]);
gAutoLaunchGame->start_sector = strtoul(argv[2], NULL, 0);
snprintf(gOPLPart, sizeof(gOPLPart), "hdd0:%s", argv[3]);

configInit(NULL);

ioInit();
LOG_ENABLE();

hddLoadModules();
if (mode == BDM_MODE) {
// Force load iLink & mx4sio modules.. we aren't using the gui so this is fine.
gEnableILK = 1; // iLink will break pcsx2 however.
gEnableMX4SIO = 1;
bdmLoadModules();
delay(3); // Wait for the device to be detected.
} else if (mode == HDD_MODE)
hddLoadModules();

InitConsoleRegionData();

ret = configReadMulti(CONFIG_ALL);
if (CONFIG_ALL & CONFIG_OPL) {
if (!(ret & CONFIG_OPL))
ret = checkLoadConfigHDD(CONFIG_ALL);
if (mode == BDM_MODE)
ret = checkLoadConfigBDM(CONFIG_ALL);
else if (mode == HDD_MODE)
ret = checkLoadConfigHDD(CONFIG_ALL);

if (ret & CONFIG_OPL) {
config_set_t *configOPL = configGetByType(CONFIG_OPL);

configGetInt(configOPL, CONFIG_OPL_PS2LOGO, &gPS2Logo);
configGetStrCopy(configOPL, CONFIG_OPL_EXIT_PATH, gExitPath, sizeof(gExitPath));
configGetInt(configOPL, CONFIG_OPL_HDD_SPINDOWN, &gHDDSpindown);
configGetInt(configOPL, CONFIG_OPL_HDD_CACHE, &hddCacheSize);
if (mode == BDM_MODE) {
configGetStrCopy(configOPL, CONFIG_OPL_BDM_PREFIX, gBDMPrefix, sizeof(gBDMPrefix));
configGetInt(configOPL, CONFIG_OPL_BDM_CACHE, &bdmCacheSize);
} else if (mode == HDD_MODE) {
configGetInt(configOPL, CONFIG_OPL_HDD_SPINDOWN, &gHDDSpindown);
configGetInt(configOPL, CONFIG_OPL_HDD_CACHE, &hddCacheSize);
}
}
}
}

void miniDeinit(config_set_t *configSet)
{
ioBlockOps(1);
#ifdef PADEMU
ds34usb_reset();
ds34bt_reset();
#endif
configFree(configSet);

ioEnd();
configEnd();
}

static void autoLaunchHDDGame(char *argv[])
{
char path[256];
config_set_t *configSet;

miniInit(HDD_MODE);

gAutoLaunchGame = malloc(sizeof(hdl_game_info_t));
memset(gAutoLaunchGame, 0, sizeof(hdl_game_info_t));

snprintf(gAutoLaunchGame->startup, sizeof(gAutoLaunchGame->startup), argv[1]);
gAutoLaunchGame->start_sector = strtoul(argv[2], NULL, 0);
snprintf(gOPLPart, sizeof(gOPLPart), "hdd0:%s", argv[3]);

snprintf(path, sizeof(path), "%sCFG/%s.cfg", gHDDPrefix, gAutoLaunchGame->startup);
configSet = configAlloc(0, NULL, path);
Expand All @@ -1775,6 +1809,52 @@ static void autoLaunchHDDGame(char *argv[])
hddLaunchGame(-1, configSet);
}

static void autoLaunchBDMGame(char *argv[])
{
char path[256];
config_set_t *configSet;

miniInit(BDM_MODE);
bdmSetPrefix();

gAutoLaunchBDMGame = malloc(sizeof(base_game_info_t));
memset(gAutoLaunchBDMGame, 0, sizeof(base_game_info_t));

int nameLen;
int format = isValidIsoName(argv[1], &nameLen);
if (format == GAME_FORMAT_OLD_ISO) {
strncpy(gAutoLaunchBDMGame->name, &argv[1][GAME_STARTUP_MAX], nameLen);
gAutoLaunchBDMGame->name[nameLen] = '\0';
strncpy(gAutoLaunchBDMGame->extension, &argv[1][GAME_STARTUP_MAX + nameLen], sizeof(gAutoLaunchBDMGame->extension));
gAutoLaunchBDMGame->extension[sizeof(gAutoLaunchBDMGame->extension) - 1] = '\0';
} else {
strncpy(gAutoLaunchBDMGame->name, argv[1], nameLen);
gAutoLaunchBDMGame->name[nameLen] = '\0';
strncpy(gAutoLaunchBDMGame->extension, &argv[1][nameLen], sizeof(gAutoLaunchBDMGame->extension));
gAutoLaunchBDMGame->extension[sizeof(gAutoLaunchBDMGame->extension) - 1] = '\0';
}

snprintf(gAutoLaunchBDMGame->startup, sizeof(gAutoLaunchBDMGame->startup), argv[2]);

if (strcasecmp("DVD", argv[3]) == 0)
gAutoLaunchBDMGame->media = SCECdPS2DVD;
else if (strcasecmp("CD", argv[3]) == 0)
gAutoLaunchBDMGame->media = SCECdPS2CD;

gAutoLaunchBDMGame->format = format;
gAutoLaunchBDMGame->parts = 1; // ul not supported.

if (gBDMPrefix[0] != '\0')
snprintf(path, sizeof(path), "mass0:%s/CFG/%s.cfg", gBDMPrefix, gAutoLaunchBDMGame->startup);
else
snprintf(path, sizeof(path), "mass0:CFG/%s.cfg", gAutoLaunchBDMGame->startup);

configSet = configAlloc(0, NULL, path);
configRead(configSet);

bdmLaunchGame(-1, configSet);
}

// --------------------- Main --------------------
int main(int argc, char *argv[])
{
Expand All @@ -1790,14 +1870,21 @@ int main(int argc, char *argv[])
// reset, load modules
reset();

/* argv[0] boot path
argv[1] game->startup
argv[2] str to u32 game->start_sector
argv[3] opl partition read from hdd0:__common/OPL/conf_hdd.cfg
argv[4] "mini" */
if (argc >= 5) {
/* argv[0] boot path
argv[1] game->startup
argv[2] str to u32 game->start_sector
argv[3] opl partition read from hdd0:__common/OPL/conf_hdd.cfg
argv[4] "mini" */
if (!strcmp(argv[4], "mini"))
autoLaunchHDDGame(argv);
/* argv[0] boot path
argv[1] file name (including extention)
argv[2] game->startup
argv[3] game->media ("CD" / "DVD")
argv[4] "bdm" */
if (!strcmp(argv[4], "bdm"))
autoLaunchBDMGame(argv);
}

init();
Expand Down

0 comments on commit 26f5fe7

Please sign in to comment.