Skip to content

Commit

Permalink
Made the UI, fixed some things
Browse files Browse the repository at this point in the history
  • Loading branch information
TuxSH committed Dec 27, 2015
1 parent 7c8d626 commit 388c9d8
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 113 deletions.
6 changes: 3 additions & 3 deletions app/build-cia.rsf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
BasicInfo:
Title : "TSVT"
Title : "TWLSaveTool"
CompanyCode : "00"
ProductCode : "CTR-N-TSVT"
ProductCode : "TWLSaveTool"
ContentType : Application # Application / SystemUpdate / Manual / Child / Trial
Logo : Nintendo # Nintendo / Licensed / Distributed / iQue / iQueForSystem

Expand All @@ -23,7 +23,7 @@ CardInfo:
Option:
UseOnSD : true # true if App is to be installed to SD
EnableCompress : true # Compresses exefs code
FreeProductCode : false # Removes limitations on ProductCode
FreeProductCode : true # Removes limitations on ProductCode
EnableCrypt : false # Enables encryption for NCCH and CIA
MediaFootPadding : false # If true CCI files are created with padding

Expand Down
55 changes: 52 additions & 3 deletions include/TWLCard.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#pragma once

#include <3ds.h>
#include <string>
#include <utility>
#include <algorithm>
#include "errors.h"

extern const int validSizes[7];

namespace TWLCard {
struct Header {
std::string gameTitle, gameCode, makerCode;
std::string gameTitle, gameCode, makerCode;
bool isTWL; // "DSi enhanced"

std::string generateCodeName(void) const;
Header(u8* data = NULL);
};

Expand All @@ -19,8 +24,52 @@ namespace TWLCard {
void closeSaveFile(Handle f);

u64 getSaveFileSize(void);
void readSaveFile(u8* out, u64 nb = 0);
void writeToSaveFile(u8* in, u64 nb = 0);
u64 getSPISize(void);

template<class CallbackT>
void readSaveFile(u8* out, u64 nb, CallbackT cb){
Handle f = openSaveFile(FS_OPEN_READ);
Result res;

u32 bytesRead;

u64 offset = 0;
u64 spiSize = getSPISize();

for(offset = 0; offset < nb; offset += 512){
u64 expected = (nb-offset < 512) ? (nb-offset) : 512;
res = FSFILE_Read(f, &bytesRead, offset, out+offset, expected);
if(res != 0){ closeSaveFile(f); throw Error(res,__FILE__, __LINE__); }
if(expected != bytesRead) { closeSaveFile(f); throw std::runtime_error("too few bytes were read"); }
cb(offset, nb);
}
cb(nb,nb);

closeSaveFile(f);
}

template<class CallbackT>
void writeToSaveFile(u8* in, u64 nb, CallbackT cb){
Handle f = openSaveFile(FS_OPEN_WRITE);
Result res;

u32 bytesWritten;

u64 offset = 0;

for(offset = 0; offset < nb; offset += 512){
u64 expected = (nb-offset < 512) ? (nb-offset) : 512;
res = FSFILE_Write(f, &bytesWritten, offset, in+offset, expected, FS_WRITE_FLUSH);
if(res != 0){ closeSaveFile(f); throw Error(res,__FILE__, __LINE__); }
if(expected != bytesWritten) { closeSaveFile(f); throw std::runtime_error("too few bytes were written"); }
cb(offset, nb);
}

cb(nb, nb);

closeSaveFile(f);
}

}


Expand Down
23 changes: 17 additions & 6 deletions include/errors.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#pragma once
#include <3ds.h>
#include <stdexcept>

#pragma once
#include <3ds.h>
#include <stdexcept>


struct Error : public std::runtime_error {
Error(u32 code) : std::runtime_error("It failed"), errorCode(code) {}
Error(u32 code, const char* fn, int l) : std::runtime_error("It failed"), errorCode(code), fileName(fn), line(l) {}

u32 getErrorCode(void) const throw(){
u32 getErrorCode(void) const throw(){
return errorCode;
}

const char* getFileName(void) const throw(){
return fileName;
}

int getLine(void) const throw(){
return line;
}

virtual const char* what() const throw(){
return "It failed";
}

protected:
u32 errorCode;
const char* fileName;
int line;
};
95 changes: 47 additions & 48 deletions source/TWLCard.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "TWLCard.h"

#include "games.h"

const int validSizes[7] = { 512, 8192, 65536, 262144, 524288, 1048576, 8388608 };

#define __FILE__ "TWLCard.cpp"
namespace TWLCard {
Header::Header(u8* data) {
if(data == NULL) return;
Expand All @@ -12,85 +17,79 @@ namespace TWLCard {
gameTitle = std::string(in1);
gameCode = std::string(in2);
makerCode = std::string(in3);
isTWL = (data[0x12] & 0x2) != 0;
}

std::string Header::generateCodeName(void) const {
using std::string;
string first = (isTWL) ? string("TWL") : string("NTR");
return first + string("-") + gameCode + string("-") + makerCode;
}


bool isCardTWL(void) {
FS_CardType t;
Result res = FSUSER_GetCardType(&t);
if(res != 0) throw Error(res);
if(res != 0) throw Error(res,__FILE__, __LINE__);
return t == CARD_TWL;
}

Header getHeader(void) {
u8 data[18] = {0};
Result res = FSUSER_GetLegacyRomHeader2(18, MEDIATYPE_GAME_CARD, 0LL, data);
if(res != 0) throw Error(res);
return Header(data);
u8* data = new u8[0x3b4];
Result res = FSUSER_GetLegacyRomHeader(MEDIATYPE_GAME_CARD, 0LL, data);
if(res != 0) throw Error(res,__FILE__, __LINE__);
Header ret(data);
delete[] data;
return ret;
}

Handle openSaveFile(u32 openFlags) {
Handle f;
Result res = FSUSER_OpenFileDirectly(&f, (FS_Archive){ARCHIVE_CARD_SPIFS, fsMakePath(PATH_EMPTY, NULL)},
fsMakePath(PATH_UTF16, L"/"), openFlags, 0);

if(res != 0) throw Error(res);
if(res != 0) throw Error(res,__FILE__, __LINE__);
return f;
}

void closeSaveFile(Handle f) {
Result res = FSFILE_Close(f);
if(res != 0) throw Error(res);
if(res != 0) throw Error(res,__FILE__, __LINE__);
}

u64 getSaveFileSize(void) {
Handle f = openSaveFile(FS_OPEN_READ);
u64 sz;
Result res = FSFILE_GetSize(f, &sz);
if(res != 0) throw Error(res);
closeSaveFile(f);
return sz;
}

void readSaveFile(u8* out, u64 nb) {
Handle f = openSaveFile(FS_OPEN_READ);
Result res;

if(nb == 0){
FSFILE_GetSize(f, &nb);
if(res != 0) throw Error(res);
}
int sz = 0;
u8* buf = new u8[512];
u8* buf2 = new u8[512];

Handle f;
u32 bytesRead;
if(FSUSER_OpenFileDirectly(&f, (FS_Archive){ARCHIVE_CARD_SPIFS, fsMakePath(PATH_EMPTY, NULL)},
fsMakePath(PATH_UTF16, L"/"), FS_OPEN_READ, 0) != 0) goto end1;

res = FSFILE_Read(f, &bytesRead, 0LL, out, (u32)nb);

if(res != 0) throw Error(res);
if(nb != bytesRead) throw std::runtime_error("too few bytes were read");

closeSaveFile(f);
if(FSFILE_Read(f, &bytesRead, 0LL, buf, 512) != 0 || bytesRead != 512) goto end;

for(int offset : validSizes){
if(FSFILE_Read(f, &bytesRead, offset, buf2, 512) != 0 || bytesRead != 512) goto end;
if(std::equal(buf, buf+512, buf2)){
sz = offset;
break;
}
}
end: FSFILE_Close(f);
end1:
delete[] buf;
delete[] buf2;
return (u64) sz;
}

void writeToSaveFile(u8* out, u64 nb) {
if(nb == 0)
nb = getSaveFileSize();

Handle f = openSaveFile(FS_OPEN_WRITE);
Result res;

if(nb == 0){
FSFILE_GetSize(f, &nb);
if(res != 0) throw Error(res);
}

u32 bytesWritten;

res = FSFILE_Write(f, &bytesWritten, 0LL, out, (u32)nb, FS_WRITE_FLUSH);

if(res != 0) throw Error(res);
if(nb != bytesWritten) throw std::runtime_error("too few bytes were written");

u64 getSPISize(void){
Handle f = openSaveFile(FS_OPEN_READ);
u64 sz;
Result res = FSFILE_GetSize(f, &sz);
if(res != 0) throw Error(res,__FILE__, __LINE__);
closeSaveFile(f);
return sz;
}

}
Loading

2 comments on commit 388c9d8

@TuxSH
Copy link
Owner Author

@TuxSH TuxSH commented on 388c9d8 Jan 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for DSiWare, not for the NDS card manager I want to make.

Thanks anyways :)

@TuxSH
Copy link
Owner Author

@TuxSH TuxSH commented on 388c9d8 Jan 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you :D

0xD0688 (but not 0xE0284) is used by PokéTransporter. This function seems to have no less than 33 args ...

Please # to comment.