Skip to content

Commit

Permalink
Add choice of HDD partition
Browse files Browse the repository at this point in the history
  • Loading branch information
KrahJohlito committed Jan 21, 2021
1 parent c71e916 commit b1ca359
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 31 deletions.
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ enum CONFIG_INDEX {
#define CONFIG_OPL_AUTO_REFRESH "autorefresh"
#define CONFIG_OPL_DEFAULT_DEVICE "default_device"
#define CONFIG_OPL_ENABLE_WRITE "enable_delete_rename"
#define CONFIG_OPL_HDD_PARTITION "hdd_partition"
#define CONFIG_OPL_HDD_SPINDOWN "hdd_spindown"
#define CONFIG_OPL_USB_PREFIX "usb_prefix"
#define CONFIG_OPL_ETH_PREFIX "eth_prefix"
Expand Down
2 changes: 2 additions & 0 deletions include/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,6 @@ int guiConfirmVideoMode(void);

int guiGameShowRemoveSettings(config_set_t *configSet, config_set_t *configGame);

const char *guiChooseHDDPartition(void);

#endif
1 change: 1 addition & 0 deletions include/lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ enum _STR_IDS {
_STR_RUN,
_STR_GFX_SETTINGS,
_STR_ENABLE_WRITE,
_STR_HDDPARTITION,
_STR_LASTPLAYED,
_STR_SELECTBUTTON,
_STR_ERR_FRAGMENTED,
Expand Down
2 changes: 2 additions & 0 deletions include/opl.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ extern int gDefaultDevice;

extern int gEnableWrite;

extern char gOPLPartition[32];
extern char *gHDDPrefix;
//These prefixes are relative to the device's name (meaning that they do not include the device name).
extern char gUSBPrefix[32];
extern char gETHPrefix[32];
Expand Down
46 changes: 46 additions & 0 deletions src/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -1695,3 +1695,49 @@ int guiGameShowRemoveSettings(config_set_t *configSet, config_set_t *configGame)

return 1;
}

const char *guiChooseHDDPartition(void)
{
int terminate = 0;
int choice = 0;

const char *name[3];
name[0] = "__common";
name[1] = "PP.OPL";
name[2] = "+OPL";

sfxPlay(SFX_MESSAGE);

while (!terminate) {
guiStartFrame();

readPads();

if (getKeyOn(KEY_UP)) {
if (choice == 0)
choice = 2;
else
choice--;
} else if (getKeyOn(KEY_DOWN)) {
if (choice == 2)
choice = 0;
else
choice++;
} else if (getKeyOn(gSelectButton))
terminate = 1;

guiShow();

rmDrawRect(0, 0, screenWidth, screenHeight, gColDarker);
rmDrawLine(50, 75, screenWidth - 50, 75, gColWhite);
rmDrawLine(50, 410, screenWidth - 50, 410, gColWhite);

fntRenderString(gTheme->fonts[0], screenWidth >> 1, gTheme->usedHeight >> 1, ALIGN_CENTER, 0, 0, _l(_STR_HDDPARTITION), gTheme->textColor);
fntRenderString(gTheme->fonts[0], screenWidth >> 1, (gTheme->usedHeight >> 1) + 30, ALIGN_CENTER, 0, 0, name[choice], gTheme->textColor);
guiDrawIconAndText(gSelectButton == KEY_CIRCLE ? CIRCLE_ICON : CROSS_ICON, _STR_ACCEPT, gTheme->fonts[0], 500, 417, gTheme->selTextColor);

guiEndFrame();
}

return name[choice];
}
113 changes: 87 additions & 26 deletions src/hddsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
static unsigned char hddForceUpdate = 0;
static unsigned char hddHDProKitDetected = 0;
static unsigned char hddModulesLoaded = 0;
static unsigned char hddMounted = 0;

static char *hddPrefix = "pfs0:";
static hdl_games_list_t hddGames;

const char *oplPart = "hdd0:+OPL";
static hdl_games_list_t hddGames;

// forward declaration
static item_list_t hddGameList;
Expand All @@ -42,13 +42,13 @@ static void hddInitModules(void)

// update Themes
char path[256];
sprintf(path, "%sTHM", hddPrefix);
sprintf(path, "%sTHM", gHDDPrefix);
thmAddElements(path, "/", 1);

sprintf(path, "%sLNG", hddPrefix);
sprintf(path, "%sLNG", gHDDPrefix);
lngAddLanguages(path, "/", hddGameList.mode);

sbCreateFolders(hddPrefix, 0);
sbCreateFolders(gHDDPrefix, 0);
}

// HD Pro Kit is mapping the 1st word in ROM0 seg as a main ATA controller,
Expand Down Expand Up @@ -93,16 +93,65 @@ static int hddCheckHDProKit(void)
#define PFS_ZONE_SIZE 8192
#define PFS_FRAGMENT 0x00000000

static int CreateOPLPartition(const char *oplPart, const char *mountpoint)
static void hddFindOPLPartition(void)
{
int ret = 0;
DIR *pdir;

if (!hddMounted) {
//first try saved partition from cfg
ret = fileXioMount(hddPrefix, gOPLPartition, FIO_MT_RDWR);
if (ret == 0) {
if (!strcmp(gOPLPartition, "hdd0:__common"))
gHDDPrefix = "pfs0:OPL/";
hddMounted = 1;
return;
}

fileXioUmount(hddPrefix);

ret = fileXioMount(hddPrefix, "hdd0:__common", FIO_MT_RDWR);
pdir = opendir("pfs0:OPL");
if (pdir != NULL) {
closedir(pdir);
snprintf(gOPLPartition, sizeof(gOPLPartition), "hdd0:__common");
gHDDPrefix = "pfs0:OPL/";
hddMounted = 1;
return;
}

fileXioUmount(hddPrefix);

ret = fileXioMount(hddPrefix, "hdd0:PP.OPL", FIO_MT_RDWR);
if (ret == 0) {
snprintf(gOPLPartition, sizeof(gOPLPartition), "hdd0:PP.OPL");
hddMounted = 1;
return;
}

fileXioUmount(hddPrefix);

ret = fileXioMount(hddPrefix, "hdd0:+OPL", FIO_MT_RDWR);
if (ret == 0) {
snprintf(gOPLPartition, sizeof(gOPLPartition), "hdd0:+OPL");
hddMounted = 1;
return;
}

fileXioUmount(hddPrefix);
}
}

static int hddCreateOPLPartition(const char *name)
{
int formatArg[3] = {PFS_ZONE_SIZE, 0x2d66, PFS_FRAGMENT};
int fd, result;
char cmd[43];

sprintf(cmd, "%s,,,128M,PFS", oplPart);
sprintf(cmd, "%s,,,128M,PFS", name);
if ((fd = open(cmd, O_CREAT | O_TRUNC | O_WRONLY)) >= 0) {
close(fd);
result = fileXioFormat(mountpoint, oplPart, (const char *)&formatArg, sizeof(formatArg));
result = fileXioFormat(hddPrefix, name, (const char *)&formatArg, sizeof(formatArg));
} else {
result = fd;
}
Expand Down Expand Up @@ -168,20 +217,32 @@ void hddLoadModules(void)

LOG("HDDSUPPORT modules loaded\n");

ret = fileXioMount(hddPrefix, oplPart, FIO_MT_RDWR);
if (ret == -ENOENT) {
//Attempt to create the partition.
if ((CreateOPLPartition(oplPart, hddPrefix)) >= 0)
fileXioMount(hddPrefix, oplPart, FIO_MT_RDWR);
}
hddFindOPLPartition();
}
}

void hddInit(void)
{
char partition[32];

LOG("HDDSUPPORT Init\n");
hddForceUpdate = 0; //Use cache at initial startup.
configGetInt(configGetByType(CONFIG_OPL), "hdd_frames_delay", &hddGameList.delay);

if (!hddMounted) {
const char *name = guiChooseHDDPartition();
snprintf(partition, sizeof(partition), "hdd0:%s", name);
if ((hddCreateOPLPartition(partition)) >= 0) {
fileXioMount(hddPrefix, partition, FIO_MT_RDWR);
snprintf(gOPLPartition, sizeof(gOPLPartition), "%s", partition);
if (!strcmp(name, "__common")) {
mkdir("pfs0:OPL", 0777);
gHDDPrefix = "pfs0:OPL/";
}
hddMounted = 1;
}
}

ioPutRequest(IO_CUSTOM_SIMPLEACTION, &hddInitModules);
hddGameList.enabled = 1;
}
Expand Down Expand Up @@ -279,7 +340,7 @@ static void hddLaunchGame(int id, config_set_t *configSet)
configGetVMC(configSet, vmc_name[1], sizeof(vmc_name[1]), 1);

if (vmc_name[0][0] || vmc_name[1][0]) {
nparts = hddGetPartitionInfo(oplPart, parts);
nparts = hddGetPartitionInfo(gOPLPartition, parts);
if (nparts > 0 && nparts <= 5) {
for (i = 0; i < nparts; i++) {
hdd_vmc_infos.parts[i].start = parts[i].start;
Expand All @@ -301,15 +362,15 @@ static void hddLaunchGame(int id, config_set_t *configSet)
if (vmc_name[vmc_id][0]) {
have_error = 1;
hdd_vmc_infos.active = 0;
if (sysCheckVMC(hddPrefix, "/", vmc_name[vmc_id], 0, &vmc_superblock) > 0) {
if (sysCheckVMC(gHDDPrefix, "/", vmc_name[vmc_id], 0, &vmc_superblock) > 0) {
hdd_vmc_infos.flags = vmc_superblock.mc_flag & 0xFF;
hdd_vmc_infos.flags |= 0x100;
hdd_vmc_infos.specs.page_size = vmc_superblock.page_size;
hdd_vmc_infos.specs.block_size = vmc_superblock.pages_per_block;
hdd_vmc_infos.specs.card_size = vmc_superblock.pages_per_cluster * vmc_superblock.clusters_per_card;

// Check vmc inode block chain (write operation can cause damage)
snprintf(vmc_path, sizeof(vmc_path), "%sVMC/%s.bin", hddPrefix, vmc_name[vmc_id]);
snprintf(vmc_path, sizeof(vmc_path), "%sVMC/%s.bin", gHDDPrefix, vmc_name[vmc_id]);
if ((nparts = hddGetFileBlockInfo(vmc_path, parts, blocks, 11)) > 0) {
have_error = 0;
hdd_vmc_infos.active = 1;
Expand Down Expand Up @@ -380,7 +441,7 @@ static void hddLaunchGame(int id, config_set_t *configSet)

sbPrepare(NULL, configSet, size_irx, irx, &i);

if ((result = sbLoadCheats(hddPrefix, game->startup)) < 0) {
if ((result = sbLoadCheats(gHDDPrefix, game->startup)) < 0) {
switch (result) {
case -ENOENT:
guiWarning(_l(_STR_NO_CHEATS_FOUND), 10);
Expand Down Expand Up @@ -414,7 +475,7 @@ static config_set_t *hddGetConfig(int id)
char path[256];
hdl_game_info_t *game = &hddGames.games[id];

snprintf(path, sizeof(path), "%sCFG/%s.cfg", hddPrefix, game->startup);
snprintf(path, sizeof(path), "%sCFG/%s.cfg", gHDDPrefix, game->startup);
config_set_t *config = configAlloc(0, NULL, path);
configRead(config); //Does not matter if the config file exists or not.

Expand All @@ -431,7 +492,7 @@ static int hddGetImage(char *folder, int isRelative, char *value, char *suffix,
{
char path[256];
if (isRelative)
snprintf(path, sizeof(path), "%s%s/%s_%s", hddPrefix, folder, value, suffix);
snprintf(path, sizeof(path), "%s%s/%s_%s", gHDDPrefix, folder, value, suffix);
else
snprintf(path, sizeof(path), "%s%s_%s", folder, value, suffix);
return texDiscoverLoad(resultTex, path, -1, psm);
Expand Down Expand Up @@ -459,7 +520,7 @@ static void hddCleanUp(int exception)

static int hddCheckVMC(char *name, int createSize)
{
return sysCheckVMC(hddPrefix, "/", name, createSize, NULL);
return sysCheckVMC(gHDDPrefix, "/", name, createSize, NULL);
}

//This may be called, even if hddInit() was not.
Expand Down Expand Up @@ -500,7 +561,7 @@ static int hddLoadGameListCache(hdl_games_list_t *cache)

hddFreeHDLGamelist(cache);

sprintf(filename, "%s/games.bin", hddPrefix);
sprintf(filename, "%s/games.bin", gHDDPrefix);
file = fopen(filename, "rb");
if (file != NULL) {
fseek(file, 0, SEEK_END);
Expand Down Expand Up @@ -573,7 +634,7 @@ static int hddUpdateGameListCache(hdl_games_list_t *cache, hdl_games_list_t *gam
return 0;
LOG("hddUpdateGameListCache: caching new game list.\n");

sprintf(filename, "%s/games.bin", hddPrefix);
sprintf(filename, "%s/games.bin", gHDDPrefix);
if (game_list->count > 0) {
file = fopen(filename, "wb");
if (file != NULL) {
Expand All @@ -593,17 +654,17 @@ static int hddUpdateGameListCache(hdl_games_list_t *cache, hdl_games_list_t *gam

static void hddGetAppsPath(char *path, int max)
{
snprintf(path, max, "%s/APPS", hddPrefix);
snprintf(path, max, "%sAPPS", gHDDPrefix);
}

static void hddGetLegacyAppsPath(char *path, int max)
{
snprintf(path, max, "%sconf_apps.cfg", hddPrefix);
snprintf(path, max, "%sconf_apps.cfg", gHDDPrefix);
}

static void hddGetLegacyAppsInfo(char *path, int max, char *name)
{
snprintf(path, max, "%sCFG/%s.cfg", hddPrefix, name);
snprintf(path, max, "%sCFG/%s.cfg", gHDDPrefix, name);
}

static item_list_t hddGameList = {
Expand Down
1 change: 1 addition & 0 deletions src/lang.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ static char *internalEnglish[LANG_STR_COUNT] = {
"Run",
"Display Settings",
"Enable Write Operations",
"OPL Partition not found! Where would you like to create one?",
"Remember Last Played Game",
"Select Button",
"Error, the game is fragmented.",
Expand Down
Loading

0 comments on commit b1ca359

Please sign in to comment.