Code cleanup.

This commit is contained in:
Steveice10 2015-01-25 19:04:27 -08:00
parent 6f2f2201db
commit 102afb5bd9
17 changed files with 443 additions and 414 deletions

View File

@ -3,5 +3,5 @@ project(bannertool)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES source/main.cpp source/commandline.cpp source/wav.cpp source/3ds/cbmd.cpp source/3ds/cwav.cpp source/3ds/lz11.cpp source/3ds/util.cpp source/lodepng/lodepng.cpp)
set(SOURCE_FILES source/main.cpp source/cmd.cpp source/pc/wav.cpp source/3ds/cbmd.cpp source/3ds/cwav.cpp source/3ds/lz11.cpp source/3ds/util.cpp source/pc/lodepng.cpp)
add_executable(bannertool ${SOURCE_FILES})

View File

@ -4,6 +4,8 @@
#include "cbmd.h"
#include "cwav.h"
#include "smdh.h"
#include "data.h"
#include "lz11.h"
#include "util.h"

View File

@ -13,11 +13,11 @@ typedef struct {
u32 cwavOffset = 0;
} CBMDHeader;
u8* build_cbmd_data(CBMD cbmd, u32* size, bool bnr) {
u8* cbmd_build_data(CBMD cbmd, u32* size, bool bnr) {
u32 headerSize = sizeof(CBMDHeader);
CBMDHeader header;
u8* compressedCGFXs[14] = {0};
u8* compressedCGFXs[14] = {NULL};
u32 compressedCGFXSizes[14] = {0};
u32 offset = headerSize;
@ -72,10 +72,10 @@ u8* build_cbmd_data(CBMD cbmd, u32* size, bool bnr) {
return output;
}
u8* build_cbmd(CBMD cbmd, u32* size) {
return build_cbmd_data(cbmd, size, false);
u8* cbmd_build(CBMD cbmd, u32* size) {
return cbmd_build_data(cbmd, size, false);
}
u8* build_bnr(CBMD cbmd, u32* size) {
return build_cbmd_data(cbmd, size, true);
u8* bnr_build(CBMD cbmd, u32* size) {
return cbmd_build_data(cbmd, size, true);
}

View File

@ -27,7 +27,7 @@ typedef struct {
u32 cwavSize = 0;
} CBMD;
u8* build_cbmd(CBMD cbmd, u32* size);
u8* build_bnr(CBMD cbmd, u32* size);
u8* cbmd_build(CBMD cbmd, u32* size);
u8* bnr_build(CBMD cbmd, u32* size);
#endif

View File

@ -3,6 +3,12 @@
#include <stdlib.h>
#include <string.h>
typedef enum {
PCM8,
PCM16,
TODO
} CWAVType;
typedef struct {
char magic[4] = {'C', 'W', 'A', 'V'};
u16 endianess = 0xFEFF;
@ -17,7 +23,7 @@ typedef struct {
u32 dataChunkOffset;
u32 dataChunkLength;
u8 reserved[0x14] = {0};
} Header;
} CWAVHeader;
typedef struct {
char magic[4] = {'I', 'N', 'F', 'O'};
@ -28,12 +34,12 @@ typedef struct {
u32 totalSamples;
u32 unknown2 = 0;
u32 totalChannels;
} InfoHeader;
} CWAVInfoHeader;
typedef struct {
u32 flags = 0x7100;
u32 offset;
} ChannelDataPointer;
} CWAVChannelDataPointer;
typedef struct {
u32 flags = 0x1F00;
@ -41,25 +47,25 @@ typedef struct {
u32 unknown3 = 0;
u32 unknown4 = 0;
u32 padding = 0;
} ChannelData;
} CWAVChannelData;
typedef struct {
char magic[4] = {'D', 'A', 'T', 'A'};
u32 length;
} DataHeader;
} CWAVDataHeader;
u8* build_cwav(WAV wav, u32* size) {
Header header;
u32 offset = sizeof(Header);
u8* cwav_build(CWAV cwav, u32* size) {
CWAVHeader header;
u32 offset = sizeof(CWAVHeader);
header.infoChunkOffset = offset;
header.infoChunkLength = sizeof(InfoHeader);
header.infoChunkLength = sizeof(CWAVInfoHeader);
offset += header.infoChunkLength;
offset += (sizeof(ChannelDataPointer) + sizeof(ChannelData)) * wav.format.numChannels;
offset += (sizeof(CWAVChannelDataPointer) + sizeof(CWAVChannelData)) * cwav.channels;
header.dataChunkOffset = offset;
header.dataChunkLength = (u32) sizeof(DataHeader) + wav.data.chunkSize;
header.dataChunkLength = (u32) sizeof(CWAVDataHeader) + cwav.dataSize;
offset += header.dataChunkLength;
header.fileSize = offset;
@ -67,45 +73,46 @@ u8* build_cwav(WAV wav, u32* size) {
u8* output = (u8*) malloc(offset);
u32 pos = 0;
memcpy(output + pos, &header, sizeof(Header));
pos += sizeof(Header);
memcpy(output + pos, &header, sizeof(CWAVHeader));
pos += sizeof(CWAVHeader);
InfoHeader infoHeader;
infoHeader.type = wav.format.bitsPerSample == 16 ? 1 : 0;
infoHeader.sampleRate = wav.format.sampleRate;
infoHeader.totalSamples = wav.data.chunkSize / (wav.format.bitsPerSample / 8) / wav.format.numChannels;
infoHeader.totalChannels = wav.format.numChannels;
memcpy(output + pos, &infoHeader, sizeof(InfoHeader));
pos += sizeof(InfoHeader);
u32 bytesPerSample = (u32) cwav.bitsPerSample / 8;
for(int i = 0; i < wav.format.numChannels; i++) {
ChannelDataPointer pointer;
pointer.offset = 0x4 + (wav.format.numChannels * (u32) sizeof(ChannelDataPointer)) + (i * (u32) sizeof(ChannelData));
memcpy(output + pos, &pointer, sizeof(ChannelDataPointer));
pos += sizeof(ChannelDataPointer);
CWAVInfoHeader infoHeader;
infoHeader.type = cwav.bitsPerSample == 16 ? PCM16 : PCM8;
infoHeader.sampleRate = cwav.sampleRate;
infoHeader.totalSamples = cwav.dataSize / bytesPerSample / cwav.channels;
infoHeader.totalChannels = cwav.channels;
memcpy(output + pos, &infoHeader, sizeof(CWAVInfoHeader));
pos += sizeof(CWAVInfoHeader);
for(int i = 0; i < cwav.channels; i++) {
CWAVChannelDataPointer pointer;
pointer.offset = 0x4 + (cwav.channels * (u32) sizeof(CWAVChannelDataPointer)) + (i * (u32) sizeof(CWAVChannelData));
memcpy(output + pos, &pointer, sizeof(CWAVChannelDataPointer));
pos += sizeof(CWAVChannelDataPointer);
}
for(int i = 0; i < wav.format.numChannels; i++) {
ChannelData data;
data.offset = i * (wav.data.chunkSize / wav.format.numChannels);
memcpy(output + pos, &data, sizeof(ChannelData));
pos += sizeof(ChannelData);
u32 bytesPerChannel = cwav.dataSize / cwav.channels;
for(int i = 0; i < cwav.channels; i++) {
CWAVChannelData data;
data.offset = i * bytesPerChannel;
memcpy(output + pos, &data, sizeof(CWAVChannelData));
pos += sizeof(CWAVChannelData);
}
DataHeader dataHeader;
dataHeader.length = (u32) sizeof(DataHeader) + wav.data.chunkSize;
memcpy(output + pos, &dataHeader, sizeof(DataHeader));
pos += sizeof(DataHeader);
CWAVDataHeader dataHeader;
dataHeader.length = (u32) sizeof(CWAVDataHeader) + cwav.dataSize;
memcpy(output + pos, &dataHeader, sizeof(CWAVDataHeader));
pos += sizeof(CWAVDataHeader);
u32 bytesPerChannel = wav.data.chunkSize / wav.format.numChannels;
u32 bytesPerSample = (u32) wav.format.bitsPerSample / 8;
for(int i = 0; i < wav.data.chunkSize; i += wav.format.numChannels * bytesPerSample) {
for(int c = 0; c < wav.format.numChannels; c++) {
memcpy(output + pos + (bytesPerChannel * c) + (i / wav.format.numChannels), wav.dataBytes + i + (c * bytesPerSample), bytesPerSample);
for(int i = 0; i < cwav.dataSize; i += cwav.channels * bytesPerSample) {
for(int c = 0; c < cwav.channels; c++) {
memcpy(output + pos + (bytesPerChannel * c) + (i / cwav.channels), cwav.data + i + (c * bytesPerSample), bytesPerSample);
}
}
pos += wav.data.chunkSize;
pos += cwav.dataSize;
if(size != NULL) {
*size = pos;

View File

@ -2,8 +2,15 @@
#define __CWAV_H__
#include "../types.h"
#include "../wav.h"
u8* build_cwav(WAV wav, u32* size);
typedef struct {
u32 channels;
u32 sampleRate;
u32 bitsPerSample;
u32 dataSize;
u8* data;
} CWAV;
u8* cwav_build(CWAV wav, u32* size);
#endif

File diff suppressed because one or more lines are too long

View File

@ -18,12 +18,6 @@ typedef enum {
TRADITIONAL_CHINESE
} SMDHTitleLanguage;
typedef struct {
u16 shortDescription[0x40] = {0};
u16 longDescription[0x80] = {0};
u16 publisher[0x40] = {0};
} SMDHTitle;
// TODO: Provide values to set ratings to.
typedef enum {
CERO = 0,
@ -47,7 +41,7 @@ typedef enum {
TAIWAN = 0x40,
// Not a bitmask, but a value.
REGION_FREE = 0x7FFFFFFF
REGION_FREE = 0x7FFFFFFF
} SMDHRegionFlag;
typedef enum {
@ -63,6 +57,12 @@ typedef enum {
DISABLE_SAVE_BACKUPS = 0x0400
} SMDHFlag;
typedef struct {
u16 shortDescription[0x40] = {0};
u16 longDescription[0x80] = {0};
u16 publisher[0x40] = {0};
} SMDHTitle;
typedef struct {
u8 gameRatings[0x10] = {0};
u32 regionLock = REGION_FREE;

View File

@ -1,6 +1,6 @@
#include "util.h"
#include "../lodepng/lodepng.h"
#include "../pc/lodepng.h"
u8 TILE_ORDER[64] = { 0, 1, 8, 9, 2, 3, 10, 11, 16, 17, 24, 25, 18, 19, 26, 27,
4, 5, 12, 13, 6, 7, 14, 15, 20, 21, 28, 29, 22, 23, 30, 31,

342
source/cmd.cpp Normal file
View File

@ -0,0 +1,342 @@
#include "cmd.h"
#include "3ds/3ds.h"
#include "pc/wav.h"
#include "types.h"
u8* convert_to_cgfx(const char* image, u32 width, u32 height, u32* size) {
u32 convertedSize = 0;
u16* converted = image_to_tiles(image, width, height, RGBA4444, &convertedSize);
if(converted == NULL) {
return NULL;
}
u8* ret = (u8*) malloc(BANNER_CGFX_HEADER_LENGTH + convertedSize);
memcpy(ret, BANNER_CGFX_HEADER, BANNER_CGFX_HEADER_LENGTH);
memcpy(ret + BANNER_CGFX_HEADER_LENGTH, converted, convertedSize);
*size = BANNER_CGFX_HEADER_LENGTH + convertedSize;
return ret;
}
u8* convert_to_cwav(const char* file, u32* size) {
WAV* wav = wav_read(file);
if(!wav) {
return NULL;
}
CWAV cwav;
cwav.channels = wav->format.numChannels;
cwav.sampleRate = wav->format.sampleRate;
cwav.bitsPerSample = wav->format.bitsPerSample;
cwav.dataSize = wav->data.chunkSize;
cwav.data = wav->data.data;
u8* ret = cwav_build(cwav, size);
wav_free(wav);
return ret;
}
int cmd_make_banner(const char* image, const char* audio, char* cgfxFile, char* cwavFile, const char* output) {
u32 cgfxSize = 0;
u8* cgfx = NULL;
if(cgfxFile != NULL) {
FILE* fd = fopen(cgfxFile, "r");
if(!fd) {
printf("ERROR: Could not open CGFX file: %s\n", strerror(errno));
}
fseek(fd, 0, SEEK_END);
cgfxSize = (u32) ftell(fd);
fseek(fd, 0, SEEK_SET);
cgfx = (u8*) malloc(cgfxSize);
fread(cgfx, 1, cgfxSize, fd);
fclose(fd);
} else {
cgfx = convert_to_cgfx(image, 256, 128, &cgfxSize);
if(!cgfx) {
return 1;
}
}
u32 cwavSize = 0;
u8* cwav = NULL;
if(cwavFile != NULL) {
FILE* fd = fopen(cwavFile, "r");
if(!fd) {
printf("ERROR: Could not open CWAV file: %s\n", strerror(errno));
}
fseek(fd, 0, SEEK_END);
cwavSize = (u32) ftell(fd);
fseek(fd, 0, SEEK_SET);
cwav = (u8*) malloc(cwavSize);
fread(cwav, 1, cwavSize, fd);
fclose(fd);
} else {
cwav = convert_to_cwav(audio, &cwavSize);
if(!cwav) {
return 2;
}
}
CBMD cbmd;
cbmd.cgfxs[CGFX_COMMON] = cgfx;
cbmd.cgfxSizes[CGFX_COMMON] = cgfxSize;
cbmd.cwav = cwav;
cbmd.cwavSize = cwavSize;
u32 bnrSize = 0;
u8* bnr = bnr_build(cbmd, &bnrSize);
free(cgfx);
free(cwav);
FILE* fd = fopen(output, "wb");
if(!fd) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 3;
}
fwrite(bnr, 1, bnrSize, fd);
fclose(fd);
free(bnr);
printf("Created banner \"%s\".\n", output);
return 0;
}
int cmd_make_smdh(char* shortDescription, char* longDescription, char* publisher, char* icon, char* output) {
u16* icon48 = image_to_tiles(icon, 48, 48, RGB565, NULL);
if(icon48 == NULL) {
return 1;
}
u16 icon24[24 * 24];
for(int y = 0; y < 24; y++) {
for(int x = 0; x < 24; x++) {
icon24[y * 24 + x] = icon48[y * 48 + x];
}
}
SMDH smdh;
for(int i = 0; i < 0x10; i++) {
utf8_to_utf16(smdh.titles[i].shortDescription, shortDescription, 0x40);
utf8_to_utf16(smdh.titles[i].longDescription, longDescription, 0x80);
utf8_to_utf16(smdh.titles[i].publisher, publisher, 0x40);
}
memcpy(smdh.largeIcon, icon48, 0x1200);
memcpy(smdh.smallIcon, icon24, 0x480);
free(icon48);
FILE* fd = fopen(output, "wb");
if(!fd) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 2;
}
fwrite(&smdh, 1, sizeof(SMDH), fd);
fclose(fd);
printf("Created SMDH \"%s\".\n", output);
return 0;
}
int cmd_make_cwav(char* input, char* output) {
u32 cwavSize = 0;
u8* cwav = convert_to_cwav(input, &cwavSize);
if(!cwav) {
return 1;
}
FILE* fd = fopen(output, "wb");
if(!fd) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 2;
}
fwrite(cwav, 1, cwavSize, fd);
fclose(fd);
free(cwav);
printf("Created CWAV \"%s\".\n", output);
return 0;
}
int cmd_lz11(char* input, char* output) {
FILE* in = fopen(input, "r");
if(!in) {
printf("ERROR: Could not open input file: %s\n", strerror(errno));
return 1;
}
fseek(in, 0, SEEK_END);
u32 size = (u32) ftell(in);
fseek(in, 0, SEEK_SET);
u8 data[size];
fread(data, 1, size, in);
fclose(in);
u32 compressedSize;
u8* compressed = lz11_compress(data, size, &compressedSize);
if(!compressed) {
return 2;
}
FILE* fd = fopen(output, "wb");
if(!fd) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 3;
}
fwrite(compressed, 1, compressedSize, fd);
fclose(fd);
free(compressed);
printf("Compressed to file \"%s\".\n", output);
return 0;
}
std::map<char*, char*, compare_strings> cmd_get_args(int argc, char* argv[]) {
std::map<char*, char*, compare_strings> args;
for(int i = 0; i < argc; i++) {
if((strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0) && argc != i + 1) {
args.insert(std::pair<char*, char*>(argv[i], argv[i + 1]));
i++;
}
}
return args;
}
char* cmd_find_arg(std::map<char*, char*, compare_strings> args, const char* shortOpt, const char* longOpt) {
char sopt[strlen(shortOpt) + 2];
sprintf(sopt, "-%s", shortOpt);
char lopt[strlen(longOpt) + 3];
sprintf(lopt, "--%s", longOpt);
std::map<char*, char*, compare_strings>::iterator match = args.find(sopt);
if(match != args.end()) {
return (*match).second;
}
match = args.find(lopt);
if(match != args.end()) {
return (*match).second;
}
return NULL;
}
void cmd_print_usage(const char* executedFrom) {
printf("Usage: %s <command> <args>\n", executedFrom);
cmd_print_commands();
}
void cmd_print_info(const char* command) {
if(strcmp(command, "makebanner") == 0) {
printf("makebanner - Creates a .bnr file.\n");
printf(" -i/--image: PNG file to use as the banner's image. Interchangeable with -ci.\n");
printf(" -a/--audio: WAV file to use as the banner's tune. Interchangeable with -ca.\n");
printf(" -ci/--cgfximage: CGFX file to use as the banner's image. Interchangeable with -i.\n");
printf(" -ca/--cwavaudio: CWAV file to use as the banner's tune. Interchangeable with -a.\n");
printf(" -o/--output: File to output the created banner to.\n");
} else if(strcmp(command, "makesmdh") == 0) {
printf("makesmdh - Creates a .smdh/.icn file.\n");
printf(" -s/--shortdescription: Short description of the application.\n");
printf(" -l/--longdescription: Long description of the application.\n");
printf(" -p/--publisher: Publisher of the application.\n");
printf(" -i/--icon: PNG file to use as an icon.\n");
printf(" -o/--output: File to output the created SMDH/ICN to.\n");
} else if(strcmp(command, "makecwav") == 0) {
printf("makecwav - Creates a CWAV file from a WAV.\n");
printf(" -i/--input: WAV file to convert.\n");
printf(" -o/--output: File to output the created CWAV to.\n");
} else if(strcmp(command, "lz11") == 0) {
printf("lz11 - Compresses a file with LZ11.\n");
printf(" -i/--input: File to compress.\n");
printf(" -o/--output: File to output the compressed data to.\n");
}
}
void cmd_print_commands() {
printf("Available commands:\n");
cmd_print_info("makebanner");
cmd_print_info("makesmdh");
cmd_print_info("makecwav");
cmd_print_info("lz11");
}
void cmd_missing_args(const char* command) {
printf("Missing arguments for command \"%s\".\n", command);
cmd_print_info(command);
}
void cmd_invalid_command(const char* command) {
printf("Invalid command \"%s\".\n", command);
cmd_print_commands();
}
int cmd_process_command(int argc, char* argv[]) {
if(argc < 2) {
cmd_print_usage(argv[0]);
return -1;
}
char* command = argv[1];
std::map<char*, char*, compare_strings> args = cmd_get_args(argc, argv);
if(strcmp(command, "makebanner") == 0) {
char *banner = cmd_find_arg(args, "i", "image");
char *audio = cmd_find_arg(args, "a", "audio");
char *cgfxFile = cmd_find_arg(args, "ci", "cgfximage");
char *cwavFile = cmd_find_arg(args, "ca", "cwavaudio");
char *output = cmd_find_arg(args, "o", "output");
if(!(banner || cgfxFile) || !(audio || cwavFile) || !output) {
cmd_missing_args(command);
return -1;
}
return cmd_make_banner(banner, audio, cgfxFile, cwavFile, output);
} else if(strcmp(command, "makesmdh") == 0) {
char* shortDescription = cmd_find_arg(args, "s", "shortdescription");
char* longDescription = cmd_find_arg(args, "l", "longdescription");
char* publisher = cmd_find_arg(args, "p", "publisher");
char* icon = cmd_find_arg(args, "i", "icon");
char* output = cmd_find_arg(args, "o", "output");
if(!shortDescription || !longDescription || !publisher || !icon || !output) {
cmd_missing_args(command);
return -1;
}
return cmd_make_smdh(shortDescription, longDescription, publisher, icon, output);
} else if(strcmp(command, "makecwav") == 0) {
char* input = cmd_find_arg(args, "i", "input");
char* output = cmd_find_arg(args, "o", "output");
if(!input || !output) {
cmd_missing_args(command);
return -1;
}
return cmd_make_cwav(input, output);
} else if(strcmp(command, "lz11") == 0) {
char* input = cmd_find_arg(args, "i", "input");
char* output = cmd_find_arg(args, "o", "output");
if(!input || !output) {
cmd_missing_args(command);
return -1;
}
return cmd_lz11(input, output);
} else {
cmd_invalid_command(command);
return -1;
}
}

View File

@ -1,5 +1,5 @@
#ifndef __COMMANDLINE_H__
#define __COMMANDLINE_H__
#ifndef __CMD_H__
#define __CMD_H__
#include <string.h>
@ -18,5 +18,6 @@ void cmd_print_info(const char* command);
void cmd_print_commands();
void cmd_missing_args(const char* command);
void cmd_invalid_command(const char* command);
int cmd_process_command(int argc, char* argv[]);
#endif

View File

@ -1,81 +0,0 @@
#include "commandline.h"
std::map<char*, char*, compare_strings> cmd_get_args(int argc, char* argv[]) {
std::map<char*, char*, compare_strings> args;
for(int i = 0; i < argc; i++) {
if((strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0) && argc != i + 1) {
args.insert(std::pair<char*, char*>(argv[i], argv[i + 1]));
i++;
}
}
return args;
}
char* cmd_find_arg(std::map<char*, char*, compare_strings> args, const char* shortOpt, const char* longOpt) {
char sopt[strlen(shortOpt) + 2];
sprintf(sopt, "-%s", shortOpt);
char lopt[strlen(longOpt) + 3];
sprintf(lopt, "--%s", longOpt);
std::map<char*, char*, compare_strings>::iterator match = args.find(sopt);
if(match != args.end()) {
return (*match).second;
}
match = args.find(lopt);
if(match != args.end()) {
return (*match).second;
}
return NULL;
}
void cmd_print_usage(const char* executedFrom) {
printf("Usage: %s <command> <args>\n", executedFrom);
cmd_print_commands();
}
void cmd_print_info(const char* command) {
if(strcmp(command, "makebanner") == 0) {
printf("makebanner - Creates a .bnr file.\n");
printf(" -i/--image: PNG file to use as the banner's image. Interchangeable with -ci.\n");
printf(" -a/--audio: WAV file to use as the banner's tune. Interchangeable with -ca.\n");
printf(" -ci/--cgfximage: CGFX file to use as the banner's image. Interchangeable with -i.\n");
printf(" -ca/--cwavaudio: CWAV file to use as the banner's tune. Interchangeable with -a.\n");
printf(" -o/--output: File to output the created banner to.\n");
} else if(strcmp(command, "makesmdh") == 0) {
printf("makesmdh - Creates a .smdh/.icn file.\n");
printf(" -s/--shortdescription: Short description of the application.\n");
printf(" -l/--longdescription: Long description of the application.\n");
printf(" -p/--publisher: Publisher of the application.\n");
printf(" -i/--icon: PNG file to use as an icon.\n");
printf(" -o/--output: File to output the created SMDH/ICN to.\n");
} else if(strcmp(command, "makecwav") == 0) {
printf("makecwav - Creates a CWAV file from a WAV.\n");
printf(" -i/--input: WAV file to convert.\n");
printf(" -o/--output: File to output the created CWAV to.\n");
} else if(strcmp(command, "lz11") == 0) {
printf("lz11 - Compresses a file with LZ11.\n");
printf(" -i/--input: File to compress.\n");
printf(" -o/--output: File to output the compressed data to.\n");
}
}
void cmd_print_commands() {
printf("Available commands:\n");
cmd_print_info("makebanner");
cmd_print_info("makesmdh");
cmd_print_info("makecwav");
cmd_print_info("lz11");
}
void cmd_missing_args(const char* command) {
printf("Missing arguments for command \"%s\".\n", command);
cmd_print_info(command);
}
void cmd_invalid_command(const char* command) {
printf("Invalid command \"%s\".\n", command);
cmd_print_commands();
}

View File

@ -1,261 +1,5 @@
#include "3ds/3ds.h"
#include "commandline.h"
#include "data.h"
#include "types.h"
#include "wav.h"
#include "3ds/smdh.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
u8* convert_to_cgfx(const char* image, u32 width, u32 height, u32* size) {
u32 convertedSize = 0;
u16* converted = image_to_tiles(image, width, height, RGBA4444, &convertedSize);
if(converted == NULL) {
return NULL;
}
u8* ret = (u8*) malloc(BANNER_CGFX_HEADER_LENGTH + convertedSize);
memcpy(ret, BANNER_CGFX_HEADER, BANNER_CGFX_HEADER_LENGTH);
memcpy(ret + BANNER_CGFX_HEADER_LENGTH, converted, convertedSize);
*size = BANNER_CGFX_HEADER_LENGTH + convertedSize;
return ret;
}
u8* convert_to_cwav(const char* file, u32* size) {
WAV* wav = read_wav(file);
if(!wav) {
return NULL;
}
u8* cwav = build_cwav(*wav, size);
free(wav->dataBytes);
free(wav);
return cwav;
}
int make_banner(const char* image, const char* audio, char* cgfxFile, char* cwavFile, const char* output) {
u32 cgfxSize = 0;
u8* cgfx = NULL;
if(cgfxFile != NULL) {
FILE* fd = fopen(cgfxFile, "r");
if(!fd) {
printf("ERROR: Could not open CGFX file: %s\n", strerror(errno));
}
fseek(fd, 0, SEEK_END);
cgfxSize = (u32) ftell(fd);
fseek(fd, 0, SEEK_SET);
cgfx = (u8*) malloc(cgfxSize);
fread(cgfx, 1, cgfxSize, fd);
fclose(fd);
} else {
cgfx = convert_to_cgfx(image, 256, 128, &cgfxSize);
if(!cgfx) {
return 1;
}
}
u32 cwavSize = 0;
u8* cwav = NULL;
if(cwavFile != NULL) {
FILE* fd = fopen(cwavFile, "r");
if(!fd) {
printf("ERROR: Could not open CWAV file: %s\n", strerror(errno));
}
fseek(fd, 0, SEEK_END);
cwavSize = (u32) ftell(fd);
fseek(fd, 0, SEEK_SET);
cwav = (u8*) malloc(cwavSize);
fread(cwav, 1, cwavSize, fd);
fclose(fd);
} else {
cwav = convert_to_cwav(audio, &cwavSize);
if(!cwav) {
return 2;
}
}
CBMD cbmd;
cbmd.cgfxs[CGFX_COMMON] = cgfx;
cbmd.cgfxSizes[CGFX_COMMON] = cgfxSize;
cbmd.cwav = cwav;
cbmd.cwavSize = cwavSize;
u32 bnrSize = 0;
u8* bnr = build_bnr(cbmd, &bnrSize);
free(cgfx);
free(cwav);
FILE* fd = fopen(output, "wb");
if(!fd) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 3;
}
fwrite(bnr, 1, bnrSize, fd);
fclose(fd);
free(bnr);
printf("Created banner \"%s\".\n", output);
return 0;
}
int make_smdh(char* shortDescription, char* longDescription, char* publisher, char* icon, char* output) {
u16* icon48 = image_to_tiles(icon, 48, 48, RGB565, NULL);
if(icon48 == NULL) {
return 1;
}
u16 icon24[24 * 24];
for(int y = 0; y < 24; y++) {
for(int x = 0; x < 24; x++) {
icon24[y * 24 + x] = icon48[y * 48 + x];
}
}
SMDH smdh;
for(int i = 0; i < 0x10; i++) {
utf8_to_utf16(smdh.titles[i].shortDescription, shortDescription, 0x40);
utf8_to_utf16(smdh.titles[i].longDescription, longDescription, 0x80);
utf8_to_utf16(smdh.titles[i].publisher, publisher, 0x40);
}
memcpy(smdh.largeIcon, icon48, 0x1200);
memcpy(smdh.smallIcon, icon24, 0x480);
free(icon48);
FILE* fd = fopen(output, "wb");
if(!fd) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 2;
}
fwrite(&smdh, 1, sizeof(SMDH), fd);
fclose(fd);
printf("Created SMDH \"%s\".\n", output);
return 0;
}
int make_cwav(char* input, char* output) {
u32 cwavSize = 0;
u8* cwav = convert_to_cwav(input, &cwavSize);
if(!cwav) {
return 1;
}
FILE* fd = fopen(output, "wb");
if(!fd) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 2;
}
fwrite(cwav, 1, cwavSize, fd);
fclose(fd);
free(cwav);
printf("Created CWAV \"%s\".\n", output);
return 0;
}
int lz11(char* input, char* output) {
FILE* in = fopen(input, "r");
if(!in) {
printf("ERROR: Could not open input file: %s\n", strerror(errno));
return 1;
}
fseek(in, 0, SEEK_END);
u32 size = (u32) ftell(in);
fseek(in, 0, SEEK_SET);
u8 data[size];
fread(data, 1, size, in);
fclose(in);
u32 compressedSize;
u8* compressed = lz11_compress(data, size, &compressedSize);
if(!compressed) {
return 2;
}
FILE* fd = fopen(output, "wb");
if(!fd) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 3;
}
fwrite(compressed, 1, compressedSize, fd);
fclose(fd);
free(compressed);
printf("Compressed to file \"%s\".\n", output);
return 0;
}
#include "cmd.h"
int main(int argc, char* argv[]) {
if(argc < 2) {
cmd_print_usage(argv[0]);
return -1;
}
char* command = argv[1];
std::map<char*, char*, compare_strings> args = cmd_get_args(argc, argv);
if(strcmp(command, "makebanner") == 0) {
char *banner = cmd_find_arg(args, "i", "image");
char *audio = cmd_find_arg(args, "a", "audio");
char *cgfxFile = cmd_find_arg(args, "ci", "cgfximage");
char *cwavFile = cmd_find_arg(args, "ca", "cwavaudio");
char *output = cmd_find_arg(args, "o", "output");
if(!(banner || cgfxFile) || !(audio || cwavFile) || !output) {
cmd_missing_args(command);
return -1;
}
return make_banner(banner, audio, cgfxFile, cwavFile, output);
} else if(strcmp(command, "makesmdh") == 0) {
char* shortDescription = cmd_find_arg(args, "s", "shortdescription");
char* longDescription = cmd_find_arg(args, "l", "longdescription");
char* publisher = cmd_find_arg(args, "p", "publisher");
char* icon = cmd_find_arg(args, "i", "icon");
char* output = cmd_find_arg(args, "o", "output");
if(!shortDescription || !longDescription || !publisher || !icon || !output) {
cmd_missing_args(command);
return -1;
}
return make_smdh(shortDescription, longDescription, publisher, icon, output);
} else if(strcmp(command, "makecwav") == 0) {
char* input = cmd_find_arg(args, "i", "input");
char* output = cmd_find_arg(args, "o", "output");
if(!input || !output) {
cmd_missing_args(command);
return -1;
}
return make_cwav(input, output);
} else if(strcmp(command, "lz11") == 0) {
char* input = cmd_find_arg(args, "i", "input");
char* output = cmd_find_arg(args, "o", "output");
if(!input || !output) {
cmd_missing_args(command);
return -1;
}
return lz11(input, output);
} else {
cmd_invalid_command(command);
return -1;
}
return cmd_process_command(argc, argv);
}

View File

@ -5,7 +5,7 @@
#include <string.h>
#include <errno.h>
bool find_chunk(FILE* fd, const char* magic) {
bool wav_find_chunk(FILE* fd, const char* magic) {
char curr[5] = {0};
while(strcmp(curr, magic) != 0) {
u32 read = (u32) fread(curr, 1, 4, fd);
@ -18,14 +18,14 @@ bool find_chunk(FILE* fd, const char* magic) {
return true;
}
WAV* read_wav(const char* file) {
WAV* wav_read(const char* file) {
FILE* fd = fopen(file, "r");
if(!fd) {
printf("ERROR: Could not open WAV file: %s\n", strerror(errno));
return NULL;
}
if(!find_chunk(fd, "RIFF")) {
if(!wav_find_chunk(fd, "RIFF")) {
printf("ERROR: Could not find WAV RIFF chunk.\n");
return NULL;
}
@ -33,7 +33,7 @@ WAV* read_wav(const char* file) {
Riff riff;
fread(&riff, sizeof(Riff), 1, fd);
if(!find_chunk(fd, "fmt ")) {
if(!wav_find_chunk(fd, "fmt ")) {
printf("ERROR: Could not find WAV format chunk.\n");
return NULL;
}
@ -41,16 +41,16 @@ WAV* read_wav(const char* file) {
Format format;
fread(&format, sizeof(Format), 1, fd);
if(!find_chunk(fd, "data")) {
if(!wav_find_chunk(fd, "data")) {
printf("ERROR: Could not find WAV data chunk.\n");
return NULL;
}
Data data;
fread(&data, sizeof(Data), 1, fd);
u8* dataBytes = (u8*) malloc(data.chunkSize);
fread(dataBytes, 1, data.chunkSize, fd);
fread(&(data.chunkId), sizeof(data.chunkId), 1, fd);
fread(&(data.chunkSize), sizeof(data.chunkSize), 1, fd);
data.data = (u8*) malloc(data.chunkSize);
fread(data.data, 1, data.chunkSize, fd);
fclose(fd);
@ -58,6 +58,12 @@ WAV* read_wav(const char* file) {
wav->riff = riff;
wav->format = format;
wav->data = data;
wav->dataBytes = dataBytes;
return wav;
}
void wav_free(WAV* wav) {
if(wav != NULL) {
free(wav->data.data);
free(wav);
}
}

View File

@ -1,7 +1,7 @@
#ifndef __WAV_H__
#define __WAV_H__
#include "types.h"
#include "../types.h"
typedef struct {
char chunkId[4];
@ -23,15 +23,16 @@ typedef struct {
typedef struct {
char chunkId[4];
u32 chunkSize;
u8* data;
} Data;
typedef struct {
Riff riff;
Format format;
Data data;
u8* dataBytes;
} WAV;
WAV* read_wav(const char* file);
WAV* wav_read(const char* file);
void wav_free(WAV* wav);
#endif