Skip to content

Commit

Permalink
Added region autodetect, header guarding and moved structs to their own
Browse files Browse the repository at this point in the history
file.
  • Loading branch information
ev1l0rd committed Apr 10, 2018
1 parent 9612a9c commit c832153
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 39 deletions.
12 changes: 12 additions & 0 deletions source/const.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _CONSTH_
#define _CONSTH_

#define PAL_TITLEID 0x00040000001BFB00
#define USA_TITLEID 0x00040000001BB200
#define JPN_TITLEID 0x00040000001BFC00

#define PAL_LOWID 0x001BFB00
#define USA_LOWID 0x001BB200
#define JPN_LOWID 0x001BFC00

#endif
20 changes: 1 addition & 19 deletions source/editprofile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdio.h>
#include <stdlib.h>

Result edit_profile(int profile_num, bool fusion_mode)
Result edit_profile(int profile_num, bool fusion_mode, u32 lowid)
{

// Determine region for the TID
Expand All @@ -12,24 +12,6 @@ Result edit_profile(int profile_num, bool fusion_mode)
if(R_FAILED(res)) return res;

u32 highid = 0x00040000;
u32 lowid = 0;

switch(region_code)
{
case CFG_REGION_JPN:
lowid = 0x001BFC00;
break;
case CFG_REGION_USA:
lowid = 0x001BB200;
break;
case CFG_REGION_EUR:
case CFG_REGION_AUS:
lowid = 0x001BFB00;
break;
default:
lowid = 0x00;
break;
}

// Open save archive
const u32 path[3] = { MEDIATYPE_SD, lowid, highid };
Expand Down
7 changes: 6 additions & 1 deletion source/editprofile.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#ifndef _EDITPROFILEH_
#define _EDITPROFILEH_

#include <3ds.h>
#include <stdio.h>

Result edit_profile(char profile_num, int fusion_mode);
Result edit_profile(char profile_num, bool fusion_mode);

#endif
14 changes: 10 additions & 4 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "editprofile.h"
#include "main.h"
#include "title.h"
#include "struct.h"

typedef enum {
/* Default sort but its commented to make it easy on _my_ mind. */
Expand All @@ -23,6 +24,7 @@ int main() {
consoleSelect(&topScreen);

States state = MAIN_SCREEN;
Regions regions_found = {false, false, false, 0};
int profile_num = 0;
bool fusion_mode = false;
bool not_busy = true;
Expand Down Expand Up @@ -53,12 +55,16 @@ int main() {
{
if (version_undetermined)
{
lowid = title_check();
if (lowid != 0) {
lowid = title_check(&regions_found);
if (regions_found.total_regions == 0)
{
printf("Press START to exit.\n");
state = SUCCESS;
} else if (regions_found.total_regions == 1)
{
state = SELECT_SAVE;
} else {
printf("Could not determine version automatically.\n");
version_undetermined = 1;
// Insert code here to display stuff about region selection
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion source/main.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#ifndef _MAINH_
#define _MAINH_

#include <stdio.h>
#include <3ds.h>
#include "editprofile.h"
int main();
void printSaveSelect();
void printFusionSelect();
void printFusionSelect();

#endif
11 changes: 11 additions & 0 deletions source/struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef _STRUCTH_
#define _STRUCTH_

typedef struct Regions {
bool PAL;
bool USA;
bool JPN;
int total_regions;
} Regions;

#endif
68 changes: 56 additions & 12 deletions source/title.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
#include <stdio.h>
#include <stdlib.h>
#include "title.h"
#include "const.h"
#include "struct.h"

/*
* title_check
* returns lowid if it can be automagically determined. otherwise returns 0
* Arg: Regions *regions_found
* Pointer to output found regions to.
*/
u32 title_check()
u32 title_check(Regions *regions_found)
{
u64 title_id; // This is for checking the title ID
u32 lowid = 0; // This is to be returned at the end.
Expand All @@ -27,7 +31,7 @@ u32 title_check()
if (card_type == CARD_CTR) {
// Yay! It's a CTR card, let's check the title.
AM_GetTitleList(&titles_read, MEDIATYPE_GAME_CARD, 1, &title_id);
int title_valid = valid_title(title_id, &lowid);
int title_valid = valid_title(title_id, &lowid, regions_found);
if (title_valid)
{
printf("Located a game card copy of SR, using that.\n");
Expand All @@ -45,31 +49,71 @@ u32 title_check()
AM_GetTitleList(&titles_read, MEDIATYPE_SD, title_count, sd_titles);
for (unsigned int i = 0; i < title_count; ++i)
{
title_valid = valid_title(sd_titles[i], &lowid);
title_valid = valid_title(sd_titles[i], &lowid, regions_found);
if (title_valid) {
printf("Located game copy with lowid: %ld\n", lowid);
break;
if (regions_found->PAL)
{
printf("PAL title found.\n");
}
if (regions_found->USA)
{
printf("USA title found.\n");
}
if (regions_found->JPN)
{
printf("JPN title found.\n");
}
}
}

printf("Found a total of %i\n regions.", regions_found->total_regions);

switch(regions_found->total_regions)
{
case 0:
printf("Unable to detect a copy of Samus Returns.\nMake sure you have the game installed or the game card inserted.\n");
break;

case 1:
printf("Able to automatically determine the region.\n");
break;

default:
printf("Unable to automatically determine the region.\nPlease select the appropriate region.\n");
lowid = 0;
break;
}

amExit();
return lowid;
}

int valid_title(u64 title_id, u32 *lowid)
/*
* valid_title: Checks if the title is valid
* args: title_id = the title id to check
* *lowid = pointer to the lowid to check
* *regions_found = pointer to store found regions in
*/
int valid_title(u64 title_id, u32 *lowid, Regions *regions_found)
{
switch(title_id)
{
case 0x00040000001BB200:
*lowid = 0x001BB200;
case PAL_TITLEID:
*lowid = PAL_LOWID;
regions_found->PAL = true;
regions_found->total_regions = regions_found->total_regions + 1;
return 1;

case 0x00040000001BFB00:
*lowid = 0x001BFB00;
case USA_TITLEID:
*lowid = USA_LOWID;
regions_found->USA = true;
regions_found->total_regions = regions_found->total_regions + 1;
return 1;

case 0x00040000001BFC00:
*lowid = 0x001BFC00;
case JPN_TITLEID:
*lowid = JPN_LOWID;
regions_found->JPN = true;
regions_found->total_regions = regions_found->total_regions + 1;
return 1;

default:
Expand Down
12 changes: 10 additions & 2 deletions source/title.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#ifndef _TITLEH_
#define _TITLEH_

#include <3ds.h>
#include <stdio.h>
u32 title_check();
int valid_title(u64 title_id, u32 *lowid);
#include "struct.h"
#include "const.h"

u32 title_check(Regions *regions_found);
int valid_title(u64 title_id, u32 *lowid, Regions *regions_found);

#endif

0 comments on commit c832153

Please sign in to comment.