This commit is contained in:
Steven Smith 2015-09-20 15:23:05 -07:00
parent 1aa96b6a6c
commit 18dd20de89
2 changed files with 133 additions and 137 deletions

View file

@ -2,6 +2,8 @@
#include "../pc/lodepng.h"
#include <stdlib.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,
32, 33, 40, 41, 34, 35, 42, 43, 48, 49, 56, 57, 50, 51, 58, 59,
@ -81,5 +83,9 @@ u16* image_data_to_tiles(u8* img, u32 width, u32 height, PixelFormat format, u32
u16* image_to_tiles(const char* image, u32 width, u32 height, PixelFormat format, u32* size) {
u8* img = load_image(image, width, height);
if(img == NULL) {
return NULL;
}
return image_data_to_tiles(img, width, height, format, size);
}

View file

@ -4,14 +4,16 @@
#include "pc/wav.h"
#include "types.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <vector>
u8* convert_to_cgfx(const char* image, u32 width, u32 height, u32* size) {
u8* convert_to_cgfx(const std::string image, u32 width, u32 height, u32* size) {
u32 convertedSize = 0;
u16* converted = image_to_tiles(image, width, height, RGBA4444, &convertedSize);
u16* converted = image_to_tiles(image.c_str(), width, height, RGBA4444, &convertedSize);
if(converted == NULL) {
return NULL;
}
@ -24,9 +26,9 @@ u8* convert_to_cgfx(const char* image, u32 width, u32 height, u32* size) {
return ret;
}
u8* convert_to_cwav(const char* file, u32* size) {
WAV* wav = wav_read(file);
if(!wav) {
u8* convert_to_cwav(const std::string file, u32* size) {
WAV* wav = wav_read(file.c_str());
if(wav == NULL) {
return NULL;
}
@ -44,13 +46,14 @@ u8* convert_to_cwav(const char* file, u32* size) {
return ret;
}
int cmd_make_banner(const char* image, const char* audio, const char* cgfxFile, const char* cwavFile, const char* output) {
int cmd_make_banner(const std::string image, const std::string audio, const std::string cgfxFile, const std::string cwavFile, const std::string output) {
u32 cgfxSize = 0;
u8* cgfx = NULL;
if(cgfxFile != NULL) {
FILE* fd = fopen(cgfxFile, "r");
if(!cgfxFile.empty()) {
FILE* fd = fopen(cgfxFile.c_str(), "r");
if(!fd) {
printf("ERROR: Could not open CGFX file: %s\n", strerror(errno));
return 1;
}
fseek(fd, 0, SEEK_END);
@ -62,17 +65,18 @@ int cmd_make_banner(const char* image, const char* audio, const char* cgfxFile,
fclose(fd);
} else {
cgfx = convert_to_cgfx(image, 256, 128, &cgfxSize);
if(!cgfx) {
return 1;
if(cgfx == NULL) {
return 2;
}
}
u32 cwavSize = 0;
u8* cwav = NULL;
if(cwavFile != NULL) {
FILE* fd = fopen(cwavFile, "r");
if(!cwavFile.empty()) {
FILE* fd = fopen(cwavFile.c_str(), "r");
if(!fd) {
printf("ERROR: Could not open CWAV file: %s\n", strerror(errno));
return 3;
}
fseek(fd, 0, SEEK_END);
@ -84,8 +88,8 @@ int cmd_make_banner(const char* image, const char* audio, const char* cgfxFile,
fclose(fd);
} else {
cwav = convert_to_cwav(audio, &cwavSize);
if(!cwav) {
return 2;
if(cwav == NULL) {
return 4;
}
}
@ -100,10 +104,10 @@ int cmd_make_banner(const char* image, const char* audio, const char* cgfxFile,
free(cgfx);
free(cwav);
FILE* fd = fopen(output, "wb");
if(!fd) {
FILE* fd = fopen(output.c_str(), "wb");
if(fd == NULL) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 3;
return 5;
}
fwrite(bnr, 1, bnrSize, fd);
@ -111,12 +115,12 @@ int cmd_make_banner(const char* image, const char* audio, const char* cgfxFile,
free(bnr);
printf("Created banner \"%s\".\n", output);
printf("Created banner \"%s\".\n", output.c_str());
return 0;
}
int cmd_make_smdh(const char* shortTitle, const char* longTitle, const char* publisher, const char* icon, SMDHRegionFlag regionFlags, u64 matchMakerId, u32 smdhFlags, u16 eulaVersion, u32 optimalBannerFrame, u32 streetpassId, const char* output) {
u8* icon48Data = load_image(icon, 48, 48);
int cmd_make_smdh(const std::string shortTitle, const std::string longTitle, const std::string publisher, const std::string icon, SMDHRegionFlag regionFlags, u64 matchMakerId, u32 smdhFlags, u16 eulaVersion, u32 optimalBannerFrame, u32 streetpassId, const std::string output) {
u8* icon48Data = load_image(icon.c_str(), 48, 48);
if(icon48Data == NULL) {
return 1;
}
@ -168,9 +172,9 @@ int cmd_make_smdh(const char* shortTitle, const char* longTitle, const char* pub
SMDH smdh;
for(int i = 0; i < 0x10; i++) {
utf8_to_utf16(smdh.titles[i].shortTitle, shortTitle, 0x40);
utf8_to_utf16(smdh.titles[i].longTitle, longTitle, 0x80);
utf8_to_utf16(smdh.titles[i].publisher, publisher, 0x40);
utf8_to_utf16(smdh.titles[i].shortTitle, shortTitle.c_str(), 0x40);
utf8_to_utf16(smdh.titles[i].longTitle, longTitle.c_str(), 0x80);
utf8_to_utf16(smdh.titles[i].publisher, publisher.c_str(), 0x40);
}
smdh.settings.regionLock = regionFlags;
@ -184,8 +188,8 @@ int cmd_make_smdh(const char* shortTitle, const char* longTitle, const char* pub
memcpy(smdh.smallIcon, icon24, 0x480);
free(icon48);
FILE* fd = fopen(output, "wb");
if(!fd) {
FILE* fd = fopen(output.c_str(), "wb");
if(fd == NULL) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 2;
}
@ -193,19 +197,19 @@ int cmd_make_smdh(const char* shortTitle, const char* longTitle, const char* pub
fwrite(&smdh, 1, sizeof(SMDH), fd);
fclose(fd);
printf("Created SMDH \"%s\".\n", output);
printf("Created SMDH \"%s\".\n", output.c_str());
return 0;
}
int cmd_make_cwav(const char* input, const char* output) {
int cmd_make_cwav(const std::string input, const std::string output) {
u32 cwavSize = 0;
u8* cwav = convert_to_cwav(input, &cwavSize);
if(!cwav) {
if(cwav == NULL) {
return 1;
}
FILE* fd = fopen(output, "wb");
if(!fd) {
FILE* fd = fopen(output.c_str(), "wb");
if(fd == NULL) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 2;
}
@ -215,13 +219,13 @@ int cmd_make_cwav(const char* input, const char* output) {
free(cwav);
printf("Created CWAV \"%s\".\n", output);
printf("Created CWAV \"%s\".\n", output.c_str());
return 0;
}
int cmd_lz11(const char* input, const char* output) {
FILE* in = fopen(input, "r");
if(!in) {
int cmd_lz11(const std::string input, const std::string output) {
FILE* in = fopen(input.c_str(), "r");
if(in == NULL) {
printf("ERROR: Could not open input file: %s\n", strerror(errno));
return 1;
}
@ -230,20 +234,26 @@ int cmd_lz11(const char* input, const char* output) {
u32 size = (u32) ftell(in);
fseek(in, 0, SEEK_SET);
u8 data[size];
u8* data = (u8*) malloc(size);
if(data == NULL) {
printf("ERROR: Could not allocate data buffer.\n");
return 2;
}
fread(data, 1, size, in);
fclose(in);
u32 compressedSize;
u8* compressed = lz11_compress(data, size, &compressedSize);
if(!compressed) {
return 2;
free(data);
if(compressed == NULL) {
return 3;
}
FILE* fd = fopen(output, "wb");
if(!fd) {
FILE* fd = fopen(output.c_str(), "wb");
if(fd == NULL) {
printf("ERROR: Could not open output file: %s\n", strerror(errno));
return 3;
return 4;
}
fwrite(compressed, 1, compressedSize, fd);
@ -251,21 +261,15 @@ int cmd_lz11(const char* input, const char* output) {
free(compressed);
printf("Compressed to file \"%s\".\n", output);
printf("Compressed to file \"%s\".\n", output.c_str());
return 0;
}
struct compare_strings {
bool operator()(char const *a, char const *b) {
return strcmp(a, b) < 0;
}
};
std::map<char*, char*, compare_strings> cmd_get_args(int argc, char* argv[]) {
std::map<char*, char*, compare_strings> args;
std::map<std::string, std::string> cmd_get_args(int argc, char* argv[]) {
std::map<std::string, std::string> 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]));
args.insert(std::pair<std::string, std::string>(argv[i], argv[i + 1]));
i++;
}
}
@ -273,13 +277,11 @@ std::map<char*, char*, compare_strings> cmd_get_args(int argc, char* argv[]) {
return args;
}
const char* cmd_find_arg(std::map<char*, char*, compare_strings> args, const char* shortOpt, const char* longOpt, const char* def) {
char sopt[strlen(shortOpt) + 2];
sprintf(sopt, "-%s", shortOpt);
char lopt[strlen(longOpt) + 3];
sprintf(lopt, "--%s", longOpt);
std::string cmd_find_arg(std::map<std::string, std::string> args, const std::string shortOpt, const std::string longOpt, const std::string def) {
std::string sopt = "-" + shortOpt;
std::string lopt = "--" + longOpt;
std::map<char*, char*, compare_strings>::iterator match = args.find(sopt);
std::map<std::string, std::string>::iterator match = args.find(sopt);
if(match != args.end()) {
return (*match).second;
}
@ -292,40 +294,31 @@ const char* cmd_find_arg(std::map<char*, char*, compare_strings> args, const cha
return def;
}
std::vector<const char*> cmd_parse_list(const char* list) {
std::vector<const char*> ret;
const char* curr = list;
const char* found = NULL;
while((found = strchr(curr, ',')) != NULL) {
char* substr = (char*) malloc(found - curr + 1);
memcpy(substr, curr, found - curr);
ret.push_back(substr);
curr = found + 1;
std::vector<std::string> cmd_parse_list(const std::string list) {
std::vector<std::string> ret;
std::string::size_type lastPos = 0;
std::string::size_type pos = 0;
while((pos = list.find(',')) != std::string::npos) {
ret.push_back(list.substr(lastPos, pos - lastPos));
lastPos = pos + 1;
}
if(strlen(curr) > 0) {
ret.push_back(strdup(curr));
if(lastPos < list.size()) {
ret.push_back(list.substr(lastPos));
}
return ret;
}
void cmd_free_list(std::vector<const char*> list) {
for(std::vector<const char*>::iterator it = list.begin(); it != list.end(); it++) {
free((void*) *it);
}
}
void cmd_print_info(const char* command) {
if(strcmp(command, "makebanner") == 0) {
void cmd_print_info(const std::string command) {
if(command.compare("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) {
} else if(command.compare("makesmdh") == 0) {
printf("makesmdh - Creates a .smdh/.icn file.\n");
printf(" -s/--shorttitle: Short title of the application.\n");
printf(" -l/--longtitle: Long title of the application.\n");
@ -340,11 +333,11 @@ void cmd_print_info(const char* command) {
printf(" -ev/--eulaversion: Optional. Version of the EULA required to be accepted before launching.\n");
printf(" -obf/--optimalbannerframe: Optional. Optimal frame of the accompanying banner.\n");
printf(" -spid/--streetpassid: Optional. Streetpass ID of the SMDH.\n");
} else if(strcmp(command, "makecwav") == 0) {
} else if(command.compare("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) {
} else if(command.compare("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");
@ -359,23 +352,23 @@ void cmd_print_commands() {
cmd_print_info("lz11");
}
void cmd_print_usage(const char* executedFrom) {
printf("Usage: %s <command> <args>\n", executedFrom);
void cmd_print_usage(const std::string executedFrom) {
printf("Usage: %s <command> <args>\n", executedFrom.c_str());
cmd_print_commands();
}
void cmd_missing_args(const char* command) {
printf("Missing arguments for command \"%s\".\n", command);
void cmd_missing_args(const std::string command) {
printf("Missing arguments for command \"%s\".\n", command.c_str());
cmd_print_info(command);
}
void cmd_invalid_arg(const char* argument, const char* command) {
printf("Invalid value for argument \"%s\" in command \"%s\".\n", argument, command);
void cmd_invalid_arg(const std::string argument, const std::string command) {
printf("Invalid value for argument \"%s\" in command \"%s\".\n", argument.c_str(), command.c_str());
cmd_print_info(command);
}
void cmd_invalid_command(const char* command) {
printf("Invalid command \"%s\".\n", command);
void cmd_invalid_command(const std::string command) {
printf("Invalid command \"%s\".\n", command.c_str());
cmd_print_commands();
}
@ -386,56 +379,56 @@ int cmd_process_command(int argc, char* argv[]) {
}
char* command = argv[1];
std::map<char*, char*, compare_strings> args = cmd_get_args(argc, argv);
std::map<std::string, std::string> args = cmd_get_args(argc, argv);
if(strcmp(command, "makebanner") == 0) {
const char *banner = cmd_find_arg(args, "i", "image", NULL);
const char *audio = cmd_find_arg(args, "a", "audio", NULL);
const char *cgfxFile = cmd_find_arg(args, "ci", "cgfximage", NULL);
const char *cwavFile = cmd_find_arg(args, "ca", "cwavaudio", NULL);
const char *output = cmd_find_arg(args, "o", "output", NULL);
if(!(banner || cgfxFile) || !(audio || cwavFile) || !output) {
const std::string banner = cmd_find_arg(args, "i", "image", "");
const std::string audio = cmd_find_arg(args, "a", "audio", "");
const std::string cgfxFile = cmd_find_arg(args, "ci", "cgfximage", "");
const std::string cwavFile = cmd_find_arg(args, "ca", "cwavaudio", "");
const std::string output = cmd_find_arg(args, "o", "output", "");
if((banner.empty() && cgfxFile.empty()) || (audio.empty() && cwavFile.empty()) || output.empty()) {
cmd_missing_args(command);
return -1;
}
return cmd_make_banner(banner, audio, cgfxFile, cwavFile, output);
} else if(strcmp(command, "makesmdh") == 0) {
const char* shortTitle = cmd_find_arg(args, "s", "shorttitle", NULL);
const char* longTitle = cmd_find_arg(args, "l", "longtitle", NULL);
const char* publisher = cmd_find_arg(args, "p", "publisher", NULL);
const char* icon = cmd_find_arg(args, "i", "icon", NULL);
const char* output = cmd_find_arg(args, "o", "output", NULL);
if(!shortTitle || !longTitle || !publisher || !icon || !output) {
const std::string shortTitle = cmd_find_arg(args, "s", "shorttitle", "");
const std::string longTitle = cmd_find_arg(args, "l", "longtitle", "");
const std::string publisher = cmd_find_arg(args, "p", "publisher", "");
const std::string icon = cmd_find_arg(args, "i", "icon", "");
const std::string output = cmd_find_arg(args, "o", "output", "");
if(shortTitle.empty() || longTitle.empty() || publisher.empty() || icon.empty() || output.empty()) {
cmd_missing_args(command);
return -1;
}
std::vector<const char*> regions = cmd_parse_list(cmd_find_arg(args, "r", "regions", "regionfree"));
std::vector<const char*> flags = cmd_parse_list(cmd_find_arg(args, "f", "flags", "visible,allow3d,recordusage"));
u64 matchMakerId = (u64) atoll(cmd_find_arg(args, "mmid", "matchmakerid", "0"));
u16 eulaVersion = (u16) atoi(cmd_find_arg(args, "ev", "eulaversion", "0"));
u32 optimalBannerFrame = (u32) atoll(cmd_find_arg(args, "obf", "optimalbannerframe", "0"));
u32 streetpassId = (u32) atoll(cmd_find_arg(args, "spid", "streetpassid", "0"));
std::vector<std::string> regions = cmd_parse_list(cmd_find_arg(args, "r", "regions", "regionfree"));
std::vector<std::string> flags = cmd_parse_list(cmd_find_arg(args, "f", "flags", "visible,allow3d,recordusage"));
u64 matchMakerId = (u64) atoll(cmd_find_arg(args, "mmid", "matchmakerid", "0").c_str());
u16 eulaVersion = (u16) atoi(cmd_find_arg(args, "ev", "eulaversion", "0").c_str());
u32 optimalBannerFrame = (u32) atoll(cmd_find_arg(args, "obf", "optimalbannerframe", "0").c_str());
u32 streetpassId = (u32) atoll(cmd_find_arg(args, "spid", "streetpassid", "0").c_str());
u32 regionFlags = 0;
for(std::vector<const char*>::iterator it = regions.begin(); it != regions.end(); it++) {
const char* region = *it;
if(strcmp(region, "regionfree") == 0) {
for(std::vector<std::string>::iterator it = regions.begin(); it != regions.end(); it++) {
const std::string region = *it;
if(region.compare("regionfree") == 0) {
regionFlags = REGION_FREE;
break;
} else if(strcmp(region, "japan") == 0) {
} else if(region.compare("japan") == 0) {
regionFlags |= JAPAN;
} else if(strcmp(region, "northamerica") == 0) {
} else if(region.compare("northamerica") == 0) {
regionFlags |= NORTH_AMERICA;
} else if(strcmp(region, "europe") == 0) {
} else if(region.compare("europe") == 0) {
regionFlags |= EUROPE;
} else if(strcmp(region, "australia") == 0) {
} else if(region.compare("australia") == 0) {
regionFlags |= AUSTRALIA;
} else if(strcmp(region, "china") == 0) {
} else if(region.compare("china") == 0) {
regionFlags |= CHINA;
} else if(strcmp(region, "korea") == 0) {
} else if(region.compare("korea") == 0) {
regionFlags |= KOREA;
} else if(strcmp(region, "taiwan") == 0) {
} else if(region.compare("taiwan") == 0) {
regionFlags |= TAIWAN;
} else {
cmd_invalid_arg("regions", command);
@ -443,50 +436,47 @@ int cmd_process_command(int argc, char* argv[]) {
}
u32 smdhFlags = 0;
for(std::vector<const char*>::iterator it = flags.begin(); it != flags.end(); it++) {
const char* flag = *it;
if(strcmp(flag, "visible") == 0) {
for(std::vector<std::string>::iterator it = flags.begin(); it != flags.end(); it++) {
const std::string flag = *it;
if(flag.compare("visible") == 0) {
smdhFlags |= VISIBLE;
} else if(strcmp(flag, "autoboot") == 0) {
} else if(flag.compare("autoboot") == 0) {
smdhFlags |= AUTO_BOOT;
} else if(strcmp(flag, "allow3d") == 0) {
} else if(flag.compare("allow3d") == 0) {
smdhFlags |= ALLOW_3D;
} else if(strcmp(flag, "requireeula") == 0) {
} else if(flag.compare("requireeula") == 0) {
smdhFlags |= REQUIRE_EULA;
} else if(strcmp(flag, "autosave") == 0) {
} else if(flag.compare("autosave") == 0) {
smdhFlags |= AUTO_SAVE_ON_EXIT;
} else if(strcmp(flag, "extendedbanner") == 0) {
} else if(flag.compare("extendedbanner") == 0) {
smdhFlags |= USE_EXTENDED_BANNER;
} else if(strcmp(flag, "ratingrequired") == 0) {
} else if(flag.compare("ratingrequired") == 0) {
smdhFlags |= RATING_REQUIED;
} else if(strcmp(flag, "savedata") == 0) {
} else if(flag.compare("savedata") == 0) {
smdhFlags |= USE_SAVE_DATA;
} else if(strcmp(flag, "recordusage") == 0) {
} else if(flag.compare("recordusage") == 0) {
smdhFlags |= RECORD_USAGE;
} else if(strcmp(flag, "nosavebackups") == 0) {
} else if(flag.compare("nosavebackups") == 0) {
smdhFlags |= DISABLE_SAVE_BACKUPS;
} else {
cmd_invalid_arg("flags", command);
}
}
cmd_free_list(regions);
cmd_free_list(flags);
return cmd_make_smdh(shortTitle, longTitle, publisher, icon, (SMDHRegionFlag) regionFlags, matchMakerId, smdhFlags, eulaVersion, optimalBannerFrame, streetpassId, output);
} else if(strcmp(command, "makecwav") == 0) {
const char* input = cmd_find_arg(args, "i", "input", NULL);
const char* output = cmd_find_arg(args, "o", "output", NULL);
if(!input || !output) {
const std::string input = cmd_find_arg(args, "i", "input", "");
const std::string output = cmd_find_arg(args, "o", "output", "");
if(input.empty() || output.empty()) {
cmd_missing_args(command);
return -1;
}
return cmd_make_cwav(input, output);
} else if(strcmp(command, "lz11") == 0) {
const char* input = cmd_find_arg(args, "i", "input", NULL);
const char* output = cmd_find_arg(args, "o", "output", NULL);
if(!input || !output) {
const std::string input = cmd_find_arg(args, "i", "input", "");
const std::string output = cmd_find_arg(args, "o", "output", "");
if(input.empty() || output.empty()) {
cmd_missing_args(command);
return -1;
}