-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.cpp
More file actions
185 lines (150 loc) · 6.09 KB
/
main.cpp
File metadata and controls
185 lines (150 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <cstring>
#include <algorithm>
#include <excmd.h>
#include "entities/RootEntry.h"
#include "entities/OSFileEntry.h"
#include "entities/BufferFileEntry.h"
#include "services/RomFSService.h"
#include "services/TgaGzService.h"
static void deinitializeFreeImage() {
FreeImage_DeInitialise();
}
static inline void addFolderIfNotEmpty(DirectoryEntry *parent, DirectoryEntry *child) {
if (!child->getChildren().empty()) {
parent->addChild(child);
} else {
delete child;
}
}
static void addImageResource(DirectoryEntry *parent, const char* name, int width, int height, int bpp, excmd::option_state &options, const char *optName) {
if (!options.has(optName))
return;
std::string path = options.get<std::string>(optName);
FileEntry *file = createTgaGzFileEntry(path.c_str(), width, height, bpp, name);
if (file) {
parent->addChild(file);
}
}
int main(int argc, char **argv) {
excmd::parser parser;
excmd::option_state options;
using excmd::description;
using excmd::value;
try {
parser.global_options()
.add_option("H,help",
description{"Show help."})
.add_option("content",
description{"Path to the /content directory"},
value<std::string>{})
.add_option("name",
description{"Long name of the application"},
value<std::string>{})
.add_option("short-name",
description{"Short name of the application"},
value<std::string>{})
.add_option("author",
description{"Author of the application"},
value<std::string>{})
.add_option("icon",
description{"Application icon (128x128)"},
value<std::string>{})
.add_option("tv-image",
description{"Splash Screen image shown on the TV (1280x720)"},
value<std::string>{})
.add_option("drc-image",
description{"Splash Screen image shown on the DRC (854x480)"},
value<std::string>{})
.add_option("boot-sound",
description{"Boot sound played on TV and the DRC (btsnd format)"},
value<std::string>{});
parser.default_command()
.add_argument("rpx-file",
description{"Path to RPX file"},
value<std::string>{})
.add_argument("output",
description{"Path to WUHB file"},
value<std::string>{});
options = parser.parse(argc, argv);
} catch (excmd::exception &ex) {
fprintf(stderr, "Error parsing options: %s\n", ex.what());
return EXIT_FAILURE;
}
if (options.empty() || options.has("help")) {
printf("%s <rpx-file> <output> [options]\n\n", argv[0]);
printf("%s\n", parser.format_help(argv[0]).c_str());
return EXIT_SUCCESS;
}
// Set up FreeImage
FreeImage_Initialise();
atexit(deinitializeFreeImage);
auto root = new RootEntry();
auto codeFolder = new DirectoryEntry("code");
auto metaFolder = new DirectoryEntry("meta");
std::string rpxFilePath = options.get<std::string>("rpx-file");
auto rpxFile = OSFileEntry::fromPath(rpxFilePath.c_str(), "root.rpx");
codeFolder->addChild(rpxFile);
{
std::string long_name = options.has("name") ? options.get<std::string>("name") : (
options.has("short-name") ? options.get<std::string>("short-name") : ""
);
std::string short_name = options.has("short-name") ? options.get<std::string>("short-name") : (
options.has("name") ? options.get<std::string>("name") : ""
);
std::string author = options.has("author") ? options.get<std::string>("author") : "Built with devkitPPC & wut";
if (long_name.empty() || short_name.empty()) {
size_t startpos = 0, endpos = rpxFilePath.length();
#ifndef _WIN32
size_t slash = rpxFilePath.find_last_of('/');
#else
size_t slash = rpxFilePath.find_last_of("/\\");
#endif
if (slash != std::string::npos) {
startpos = slash+1;
}
size_t dot = rpxFilePath.find_last_of('.');
if (dot != std::string::npos) {
endpos = dot;
}
long_name = short_name = rpxFilePath.substr(startpos, endpos-startpos);
}
#define MAKE_META_INI(_buf,_size) snprintf((_buf),(_size), \
"[menu]\n" \
"longname=%s\n" \
"shortname=%s\n" \
"author=%s\n", \
long_name.c_str(), \
short_name.c_str(), \
author.c_str())
std::vector<uint8_t> metaIniData;
metaIniData.reserve(MAKE_META_INI(NULL, 0)+1);
metaIniData.resize(metaIniData.capacity()-1);
MAKE_META_INI(reinterpret_cast<char*>(metaIniData.data()), metaIniData.capacity());
#undef MAKE_META_INI
FileEntry * metaIni = new BufferFileEntry("meta.ini", std::move(metaIniData));
if(!metaIni){
return EXIT_FAILURE;
}
metaFolder->addChild(metaIni);
}
addImageResource(metaFolder, "iconTex.tga.gz", 128, 128, 32, options, "icon");
addImageResource(metaFolder, "bootTvTex.tga.gz", 1280, 720, 24, options, "tv-image");
addImageResource(metaFolder, "bootDrcTex.tga.gz", 854, 480, 24, options, "drc-image");
std::string bootSoundPath = options.get<std::string>("boot-sound");
auto bootSoundFile = OSFileEntry::fromPath(bootSoundPath.c_str(), "bootSound.btsnd");
metaFolder->addChild(bootSoundFile);
addFolderIfNotEmpty(root, codeFolder);
addFolderIfNotEmpty(root, metaFolder);
if (options.has("content")) {
std::string contentPath = options.get<std::string>("content");
filepath_t dirpath;
filepath_init(&dirpath);
filepath_set(&dirpath, contentPath.c_str());
auto contentFolder = romfs::CreateFolderFromPath(dirpath, "content");
addFolderIfNotEmpty(root, contentFolder);
}
std::string outputPath = options.get<std::string>("output");
romfs::CreateArchive(root, outputPath.c_str());
delete root;
return EXIT_SUCCESS;
}