Skip to content

Commit 27ecde2

Browse files
committed
Ensure WAD compatibility for vWii
1 parent b7e0c5a commit 27ecde2

File tree

3 files changed

+91
-20
lines changed

3 files changed

+91
-20
lines changed

include/ff.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ namespace ff {
331331
};
332332

333333
WADInfo get_info_from_wad(const std::string& wad_path);
334+
void replace_dol_in_wad(const std::string& wad, const std::string& dol);
335+
void set_ios_in_wad(const std::string& wad, int ios);
336+
void set_title_id_in_wad(const std::string& wad, const std::string& title_id);
334337

335338
inline static const std::string virtual_stylesheet_path = "/css/index.css";
336339
inline static const std::string virtual_font_path = "/fonts/font.ttf";
@@ -375,8 +378,6 @@ namespace ff {
375378
bool generate_thumbnail(const std::string& input, const std::string& output);
376379
std::string get_temp_path();
377380

378-
bool replace_dol_in_wad(const std::string& wad, const std::string& dol);
379-
380381
std::string get_default_profile();
381382

382383
void update_to_latest(database& db);

src/upload_manager.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ std::pair<ff::UploadStatus, std::string> ff::try_upload_forwarder(const limhamn:
120120
db_json["meta"]["description"] = limhamn::http::utils::htmlspecialchars(meta.at("description"));
121121
}
122122
std::string title{};
123-
std::string title_id{};
124-
125-
db_json["meta"]["title_id"] = title_id = wad_info.title_id;
123+
std::string title_id = wad_info.title_id;
126124

127125
if (meta.find("title") != meta.end() && meta.at("title").is_string() && !meta.at("title").get<std::string>().empty()) {
128126
if (meta.at("title").size() > 255) {
@@ -196,7 +194,32 @@ std::pair<ff::UploadStatus, std::string> ff::try_upload_forwarder(const limhamn:
196194
} else {
197195
return {ff::UploadStatus::Failure, ""};
198196
}
199-
db_json["meta"]["vwii_compatible"] = wad_info.supports_vwii;
197+
198+
if (wad_info.ios == 61) {
199+
try {
200+
ff::set_ios_in_wad(wad_path, 36);
201+
} catch (const std::exception&) {
202+
return {ff::UploadStatus::Failure, ""};
203+
}
204+
}
205+
206+
bool changed = false;
207+
for (auto& it : title_id) {
208+
if (it == 'E' || it == 'F' || it == 'J' || it == 'L' || it == 'M' || it == 'N' || it == 'P' || it == 'Q' || it == 'X') {
209+
it = 'K';
210+
changed = true;
211+
}
212+
}
213+
if (changed) {
214+
try {
215+
ff::set_title_id_in_wad(wad_path, title_id);
216+
} catch (const std::exception&) {
217+
return {ff::UploadStatus::Failure, ""};
218+
}
219+
}
220+
221+
db_json["meta"]["title_id"] = title_id;
222+
db_json["meta"]["vwii_compatible"] = true;
200223

201224
// replace dol in forwarder if applicable
202225
if (is_forwarder && !location.empty()) {
@@ -215,12 +238,7 @@ std::pair<ff::UploadStatus, std::string> ff::try_upload_forwarder(const limhamn:
215238
return {ff::UploadStatus::Failure, ""};
216239
}
217240

218-
if (!ff::replace_dol_in_wad(wad_path, out_p)) {
219-
#if FF_DEBUG
220-
logger.write_to_log(limhamn::logger::type::notice, "Failed to replace dol in wad\n");
221-
#endif
222-
return {ff::UploadStatus::Failure, ""};
223-
}
241+
ff::replace_dol_in_wad(wad_path, out_p);
224242
} catch (const std::exception& e) {
225243
#if FF_DEBUG
226244
logger.write_to_log(limhamn::logger::type::notice, "Exception while generating DOL for forwarder: " + location + ", error: " + e.what() + "\n");

src/wadinfo.cpp

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,66 @@ ff::WADInfo ff::get_info_from_wad(const std::string& wad_path) {
5555
return ret;
5656
}
5757

58-
bool ff::replace_dol_in_wad(const std::string& wad, const std::string& dol) {
58+
void ff::set_ios_in_wad(const std::string& wad, int ios) {
59+
auto temp_dir = std::filesystem::temp_directory_path();
60+
std::string input_file = (temp_dir / ("in-" + scrypto::generate_random_string(12))).string();
61+
std::string output_file_base = (temp_dir / ("out-" + scrypto::generate_random_string(12))).string();
62+
std::string output_file = output_file_base + ".wad";
63+
64+
try {
65+
std::filesystem::copy(wad, input_file);
66+
} catch (const std::exception& e) {
67+
throw std::runtime_error{"Failed to copy WAD file: " + std::string(e.what())};
68+
}
69+
70+
std::string command = "Sharpii WAD -e \"" + input_file + "\" \"" + output_file_base + "\" -ios " + std::to_string(ios);
71+
#ifdef FF_DEBUG
72+
ff::logger.write_to_log(limhamn::logger::type::notice, "Command: " + command + "\n");
73+
#endif
74+
if (std::system(command.c_str()) != 0) {
75+
throw std::runtime_error{"Failed to set IOS in WAD file. Command failed."};
76+
}
77+
78+
try {
79+
std::filesystem::rename(output_file, wad);
80+
} catch (const std::exception& e) {
81+
throw std::runtime_error{"Rename failed: " + std::string(e.what())};
82+
}
83+
}
84+
85+
void ff::set_title_id_in_wad(const std::string& wad, const std::string& title_id) {
86+
auto temp_dir = std::filesystem::temp_directory_path();
87+
std::string input_file = (temp_dir / ("in-" + scrypto::generate_random_string(12))).string();
88+
std::string output_file_base = (temp_dir / ("out-" + scrypto::generate_random_string(12))).string();
89+
std::string output_file = output_file_base + ".wad";
90+
91+
try {
92+
std::filesystem::copy(wad, input_file);
93+
} catch (const std::exception& e) {
94+
throw std::runtime_error{"Failed to copy WAD file: " + std::string(e.what())};
95+
}
96+
97+
std::string command = "Sharpii WAD -e \"" + input_file + "\" \"" + output_file_base + "\" -id " + title_id;
98+
#ifdef FF_DEBUG
99+
ff::logger.write_to_log(limhamn::logger::type::notice, "Command: " + command + "\n");
100+
#endif
101+
if (std::system(command.c_str()) != 0) {
102+
throw std::runtime_error{"Failed to set title ID in WAD file. Command failed."};
103+
}
104+
105+
try {
106+
std::filesystem::rename(output_file, wad);
107+
} catch (const std::exception& e) {
108+
throw std::runtime_error{"Rename failed: " + std::string(e.what())};
109+
}
110+
}
111+
112+
void ff::replace_dol_in_wad(const std::string& wad, const std::string& dol) {
59113
if (!std::filesystem::exists(dol)) {
60114
#ifdef FF_DEBUG
61115
ff::logger.write_to_log(limhamn::logger::type::notice, "DOL file does not exist: " + dol + "\n");
62116
#endif
63-
return false;
117+
throw std::runtime_error{"DOL file does not exist: " + dol};
64118
}
65119

66120
auto temp_dir = std::filesystem::temp_directory_path();
@@ -76,14 +130,14 @@ bool ff::replace_dol_in_wad(const std::string& wad, const std::string& dol) {
76130
#ifdef FF_DEBUG
77131
ff::logger.write_to_log(limhamn::logger::type::error, "Failed to copy files: " + std::string(e.what()) + "\n");
78132
#endif
79-
return false;
133+
throw std::runtime_error{"Failed to copy files: " + std::string(e.what())};
80134
}
81135

82136
if (!std::filesystem::exists(input_file) || !std::filesystem::exists(input_dol)) {
83137
#ifdef FF_DEBUG
84138
ff::logger.write_to_log(limhamn::logger::type::error, "Input file or DOL file does not exist after copy.\n");
85139
#endif
86-
return false;
140+
throw std::runtime_error{"Input file or DOL file does not exist after copy."};
87141
}
88142

89143
std::string command = "Sharpii WAD -e \"" + input_file + "\" \"" + output_file_base + "\" -dol \"" + input_dol + "\"";
@@ -104,7 +158,7 @@ bool ff::replace_dol_in_wad(const std::string& wad, const std::string& dol) {
104158
#endif
105159

106160
if (result_code != 0 || !std::filesystem::exists(output_file)) {
107-
return false;
161+
throw std::runtime_error{"Failed to replace DOL in WAD file. Command failed or output file does not exist."};
108162
}
109163

110164
try {
@@ -113,8 +167,6 @@ bool ff::replace_dol_in_wad(const std::string& wad, const std::string& dol) {
113167
#ifdef FF_DEBUG
114168
ff::logger.write_to_log(limhamn::logger::type::error, "Rename failed: " + std::string(e.what()) + "\n");
115169
#endif
116-
return false;
170+
throw std::runtime_error{"Rename failed: " + std::string(e.what())};
117171
}
118-
119-
return std::filesystem::exists(wad);
120172
}

0 commit comments

Comments
 (0)