Skip to content

Commit

Permalink
v1.2.0 - Changed config format to include...
Browse files Browse the repository at this point in the history
mediatype. No major features added besides this
  • Loading branch information
dragos240 committed Jan 21, 2017
1 parent 3fc671f commit 22e884c
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 83 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft
# -ffunction-sections \
# $(ARCH)

CFLAGS := -g -Wall -Wextra -O2 -mword-relocations \
-fomit-frame-pointer -ffunction-sections \
CFLAGS := -ggdb -Wall -Wextra -O2 -mword-relocations \
-ffunction-sections \
$(ARCH)

CFLAGS += $(INCLUDE) -DARM11 -D_3DS

CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11

ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
ASFLAGS := -ggdb $(ARCH)
LDFLAGS = -specs=3dsx.specs -ggdb $(ARCH) -Wl,-Map,$(notdir $*.map)

LIBS := -lsfil -lsftd -lfreetype -lpng -lz -lsf2d -lcitro3d -lctru -lm

Expand Down
1 change: 0 additions & 1 deletion include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#define EU_GAMEID 0x0004000000086400
#define JP_GAMEID 0x0004000000086200
#define SECOND_IN_NS (1000*1000*1000)
#define SIZE_GARDEN 522752
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

extern bool is3dsx;
Expand Down
7 changes: 7 additions & 0 deletions include/conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
typedef struct{
char* townname;
unsigned char mediatype_val;
} conftok_t;

int conf_parse(const char* data, conftok_t* token);
int conf_gen(char** data, conftok_t* token); //data must be a pointer!
6 changes: 5 additions & 1 deletion include/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ extern FS_Archive game_arch;
extern FS_Archive sdmc_arch;

Result fs_init();
Result open_archives();
Result open_sdmc_archive();
Result open_game_archive();
Result close_archives();
void fs_fini();

int load_config(char** current_town);

dir_t get_dirs(char* path);
file_t get_files(FS_Archive arch, char* path);

void get_mediatype();
void change_mediatype();
int get_titleid();

#endif
1 change: 1 addition & 0 deletions include/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

void run_tests();

void conf_parse_test();
void text_test();
void simple_gfx_test();
void gfx_test();
Expand Down
1 change: 1 addition & 0 deletions romfs/config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Main
1 change: 1 addition & 0 deletions source/backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ void backup_to_prev_folder(char* dirname){
ret = FSUSER_DeleteFile(sdmc_arch, fsMakePath(PATH_ASCII, filepath));
if(ret){
gfx_error(ret, __FILENAME__, __LINE__);
gfx_waitmessage("dirname: %s", dirname);
return;
}
free(filepath);
Expand Down
29 changes: 29 additions & 0 deletions source/conf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "common.h"
#include "gfx.h"
#include "conf.h"

//tokens must be array or pre-allocated pointer
int conf_parse(const char* data, conftok_t* token){
int len;

len = data[0];
token->townname = calloc(len+1, 1); //1 = '\0'
sprintf(token->townname, "%.*s", len, &data[1]);
token->mediatype_val = data[1+len];

return 0;
}

//data must not be a pointer to an array
int conf_gen(char** data, conftok_t* token){
//str format: (length of townname (1 bytes))townname(mediatype_val (1 byte))
*data = calloc(1+strlen(token->townname+1+1), 1);
sprintf(*data, "%c%s%c", strlen(token->townname), token->townname, token->mediatype_val);

return 0;
}
149 changes: 144 additions & 5 deletions source/fs.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include <3ds.h>

#include "common.h"
#include "gfx.h"
#include "menu.h"
#include "conf.h"
#include "fs.h"

FS_MediaType mediatype;
Expand All @@ -29,17 +32,22 @@ Result fs_init(){
return ret;
}

Result open_archives(){
Result open_sdmc_archive(){
Result ret;
u32* path;
FS_Path null_binpath, game_binpath;

//open sdmc card
null_binpath = (FS_Path){PATH_EMPTY, 1, (u8*)""};
ret = FSUSER_OpenArchive(&sdmc_arch, ARCHIVE_SDMC, null_binpath);
ret = FSUSER_OpenArchive(&sdmc_arch, ARCHIVE_SDMC, fsMakePath(PATH_EMPTY, ""));
if(ret)
return ret;

return ret;
}

Result open_game_archive(){
Result ret;
u32* path;
FS_Path game_binpath;

if(is3dsx){
ret = FSUSER_OpenArchive(&game_arch, ARCHIVE_SAVEDATA, fsMakePath(PATH_EMPTY, ""));
if(ret)
Expand Down Expand Up @@ -70,6 +78,97 @@ void fs_fini(){
fsExit();
}

int load_config(char** current_town){
//loads config file and sets *current_town
Result ret;
Handle handle;
FILE* f;
u64 size;
int tempsize;
u32 read; //bytes read
u32 written; //bytes written
char* buf;
conftok_t token;
const char* configpath = "/TownManager/config";

//if config file was just created now
if((ret = FSUSER_CreateFile(sdmc_arch, fsMakePath(PATH_ASCII, configpath), 0, 0)) == 0){
//open template config file
if(!(f = fopen("romfs:/config", "r"))){
gfx_waitmessage("Could not open template config file.");
return -1;
}
fseek(f, 0L, SEEK_END);
tempsize = ftell(f);
rewind(f);
if((buf = malloc(tempsize)) == NULL){
gfx_waitmessage("malloc failed! %s:%d", __FILENAME__, __LINE__);
return -1;
}
fread(buf, tempsize, 1, f);
fclose(f);

//open the newly created config file
if((ret = FSUSER_OpenFile(&handle, sdmc_arch, fsMakePath(PATH_ASCII, configpath), FS_OPEN_READ | FS_OPEN_WRITE, 0))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
conf_parse(buf, &token);
get_mediatype();
token.mediatype_val = mediatype;
free(buf);
buf = calloc(2+strlen(token.townname)+1+1, 1);
conf_gen(&buf, &token);
size = strlen(buf);
if((ret = FSFILE_Write(handle, &written, 0, buf, size, FS_WRITE_FLUSH | FS_WRITE_UPDATE_TIME))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
if(size != written){
gfx_waitmessage("size != written! %s:%d", __FILENAME__, __LINE__);
return -1;
}
if((ret = FSFILE_SetSize(handle, size))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
if((ret = FSFILE_Close(handle))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
*current_town = "Main";
return 0;
}
//if config file exists
else{
if((ret = FSUSER_OpenFile(&handle, sdmc_arch, fsMakePath(PATH_ASCII, configpath), FS_OPEN_READ, 0))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
if((ret = FSFILE_GetSize(handle, &size))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
if((buf = malloc(size)) == NULL){
gfx_waitmessage("malloc failed! %s:%d", __FILENAME__, __LINE__);
return -1;
}
if((ret = FSFILE_Read(handle, &read, 0, buf, size))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
if((ret = FSFILE_Close(handle))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
conf_parse(buf, &token);
mediatype = token.mediatype_val;
*current_town = token.townname;
}

return 0;
}

dir_t get_dirs(char* path){
Result ret;
Handle handle;
Expand All @@ -93,6 +192,7 @@ dir_t get_dirs(char* path){
dirs.dirs[dirs.numdirs-1] = u16str2str(get_ent.name);
}
while(read > 0);
FSDIR_Close(handle);

return dirs;
}
Expand Down Expand Up @@ -121,6 +221,7 @@ file_t get_files(FS_Archive arch, char* path){
files.files[files.numfiles-1] = u16str2str(get_ent.name);
}
while(read > 0);
FSDIR_Close(handle);

return files;
}
Expand All @@ -145,6 +246,44 @@ void get_mediatype(){
}
}

void change_mediatype(){
Result ret;
Handle handle;
u32 read;
u32 written;
u32 size;
char* buf;
conftok_t token;

get_mediatype();
if((ret = FSUSER_OpenFile(&handle, sdmc_arch, fsMakePath(PATH_ASCII, "/TownManager/config"), FS_OPEN_READ | FS_OPEN_WRITE, 0))){
gfx_error(ret, __FILENAME__, __LINE__);
return;
}
if((ret = FSFILE_GetSize(handle, (u64*)&size))){
gfx_error(ret, __FILENAME__, __LINE__);
return;
}
buf = malloc(size);
if((ret = FSFILE_Read(handle, &read, 0, buf, size))){
gfx_error(ret, __FILENAME__, __LINE__);
return;
}
conf_parse(buf, &token);
token.mediatype_val = mediatype;
conf_gen(&buf, &token);
if((ret = FSFILE_Write(handle, &written, 0, buf, size, FS_WRITE_FLUSH | FS_WRITE_UPDATE_TIME))){
gfx_error(ret, __FILENAME__, __LINE__);
return;
}
if((ret = FSFILE_Close(handle))){
gfx_error(ret, __FILENAME__, __LINE__);
return;
}

gfx_waitmessage("Changed media source.");
}

int get_titleid(){
Result ret;
u32 num_sdtitles;
Expand Down
4 changes: 4 additions & 0 deletions source/kb.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <string.h>

#include <3ds.h>

Expand All @@ -12,6 +13,9 @@ char* get_string(char* hint){
swkbdSetHintText(&key_state, hint);

swkbdInputText(&key_state, input, 64);
if(strcmp(input, "") == 0){
return NULL;
}

return input;
}
29 changes: 24 additions & 5 deletions source/launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "fs.h"
#include "gfx.h"
#include "backup.h"
#include "conf.h"
#include "launcher.h"

static Handle nssHandle = 0;
Expand All @@ -29,8 +30,10 @@ int prepare_to_launch(char* dirname){
char* townpath;
char* filepath;
char* buf;
u32 written;
u64 size;
u32 read;
u32 written;
conftok_t token;
file_t sdmc_files, game_files;
int i;
int error;
Expand Down Expand Up @@ -86,16 +89,32 @@ int prepare_to_launch(char* dirname){
if(is3dsx)
fs_init();
free(townpath);
//Write dirname as the sole contents of the /TownManager/tm.conf file
if((ret = FSUSER_OpenFile(&handle, sdmc_arch, fsMakePath(PATH_ASCII, "/TownManager/tm.conf"), FS_OPEN_WRITE, 0))){
if((ret = FSUSER_OpenFile(&handle, sdmc_arch, fsMakePath(PATH_ASCII, "/TownManager/config"), FS_OPEN_READ | FS_OPEN_WRITE, 0))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
if((ret = FSFILE_GetSize(handle, &size))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
if((buf = malloc(size)) == NULL){
gfx_waitmessage("%s:%d malloc failed!", __FILENAME__, __LINE__);
return -1;
}
if((ret = FSFILE_Read(handle, &read, 0, buf, size))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
if((ret = FSFILE_Write(handle, &written, 0, dirname, strlen(dirname)+1, FS_WRITE_FLUSH | FS_WRITE_UPDATE_TIME))){
conf_parse(buf, &token);
token.townname = dirname;
free(buf);
buf = calloc(2+strlen(token.townname)+1+1, 1); //1 = '\0'
conf_gen(&buf, &token);
if((ret = FSFILE_SetSize(handle, strlen(buf)))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
if((ret = FSFILE_SetSize(handle, (u64)(strlen(dirname)+1)))){
if((ret = FSFILE_Write(handle, &written, 0, buf, strlen(buf), FS_WRITE_FLUSH | FS_WRITE_UPDATE_TIME))){
gfx_error(ret, __FILENAME__, __LINE__);
return -1;
}
Expand Down
Loading

0 comments on commit 22e884c

Please sign in to comment.