add set-save-files

This commit is contained in:
Martin Michelsen
2022-03-29 23:37:56 -07:00
parent 2e839fe70a
commit 5848beb6c2
3 changed files with 31 additions and 35 deletions
+20 -20
View File
@@ -45,7 +45,7 @@ static void flush_and_free_bufferevent(struct bufferevent* bev) {
ProxyServer::ProxyServer(
shared_ptr<struct event_base> base,
shared_ptr<ServerState> state)
: save_quests(false),
: save_files(false),
base(base),
state(state) { }
@@ -333,7 +333,7 @@ void ProxyServer::LinkedSession::resume(
this->server_input_crypt.reset();
this->server_output_crypt.reset();
this->saving_quest_files.clear();
this->saving_files.clear();
// Connect to the remote server. The command handlers will do the login steps
// and set up forwarding
@@ -368,7 +368,7 @@ void ProxyServer::LinkedSession::resume(
ProxyServer::LinkedSession::SavingQuestFile::SavingQuestFile(
ProxyServer::LinkedSession::SavingFile::SavingFile(
const std::string& basename,
const std::string& output_filename,
uint32_t remaining_bytes)
@@ -773,7 +773,7 @@ void ProxyServer::LinkedSession::on_server_input() {
case 0x44:
case 0xA6: {
if (!this->server->save_quests) {
if (!this->server->save_files) {
break;
}
@@ -804,16 +804,16 @@ void ProxyServer::LinkedSession::on_server_input() {
output_filename[0] = '_';
}
SavingQuestFile sqf(cmd->filename, output_filename, cmd->file_size);
this->saving_quest_files.emplace(cmd->filename, move(sqf));
log(INFO, "[ProxyServer/%08" PRIX32 "] Opened quest file %s",
SavingFile sf(cmd->filename, output_filename, cmd->file_size);
this->saving_files.emplace(cmd->filename, move(sf));
log(INFO, "[ProxyServer/%08" PRIX32 "] Opened file %s",
this->license->serial_number, output_filename.c_str());
break;
}
case 0x13:
case 0xA7: {
if (!this->server->save_quests) {
if (!this->server->save_files) {
break;
}
@@ -829,11 +829,11 @@ void ProxyServer::LinkedSession::on_server_input() {
}
const auto* cmd = reinterpret_cast<const WriteFileCommand*>(data.data());
SavingQuestFile* sqf = nullptr;
SavingFile* sf = nullptr;
try {
sqf = &this->saving_quest_files.at(cmd->filename);
sf = &this->saving_files.at(cmd->filename);
} catch (const out_of_range&) {
log(WARNING, "[ProxyServer/%08" PRIX32 "] Can\'t find saving quest file %s",
log(WARNING, "[ProxyServer/%08" PRIX32 "] Received data for non-open file %s",
this->license->serial_number, cmd->filename);
break;
}
@@ -847,26 +847,26 @@ void ProxyServer::LinkedSession::on_server_input() {
log(INFO, "[ProxyServer/%08" PRIX32 "] Writing %zu bytes to %s",
this->license->serial_number, bytes_to_write,
sqf->output_filename.c_str());
fwritex(sqf->f.get(), cmd->data, bytes_to_write);
if (bytes_to_write > sqf->remaining_bytes) {
sf->output_filename.c_str());
fwritex(sf->f.get(), cmd->data, bytes_to_write);
if (bytes_to_write > sf->remaining_bytes) {
log(WARNING, "[ProxyServer/%08" PRIX32 "] Chunk size extends beyond original file size; file may be truncated",
this->license->serial_number);
sqf->remaining_bytes = 0;
sf->remaining_bytes = 0;
} else {
sqf->remaining_bytes -= bytes_to_write;
sf->remaining_bytes -= bytes_to_write;
}
if (sqf->remaining_bytes == 0) {
if (sf->remaining_bytes == 0) {
log(INFO, "[ProxyServer/%08" PRIX32 "] File %s is complete",
this->license->serial_number, sqf->output_filename.c_str());
this->saving_quest_files.erase(cmd->filename);
this->license->serial_number, sf->output_filename.c_str());
this->saving_files.erase(cmd->filename);
}
break;
}
case 0xB8: {
if (!this->server->save_quests) {
if (!this->server->save_files) {
break;
}
if (data.size() < 4) {
+4 -4
View File
@@ -62,18 +62,18 @@ public:
std::shared_ptr<PSOEncryption> server_input_crypt;
std::shared_ptr<PSOEncryption> server_output_crypt;
struct SavingQuestFile {
struct SavingFile {
std::string basename;
std::string output_filename;
uint32_t remaining_bytes;
std::unique_ptr<FILE, std::function<void(FILE*)>> f;
SavingQuestFile(
SavingFile(
const std::string& basename,
const std::string& output_filename,
uint32_t remaining_bytes);
};
std::unordered_map<std::string, SavingQuestFile> saving_quest_files;
std::unordered_map<std::string, SavingFile> saving_files;
LinkedSession(
ProxyServer* server,
@@ -112,7 +112,7 @@ public:
std::shared_ptr<LinkedSession> get_session();
void delete_session(uint32_t serial_number);
bool save_quests;
bool save_files;
private:
struct ListeningSocket {
+7 -11
View File
@@ -97,8 +97,10 @@ Proxy commands (these will only work when exactly one client is connected):\n\
Send a lobby event update to yourself.\n\
warp <area-id>\n\
Send yourself to a specific area.\n\
ship\n\
Request the ship select menu from the server.\n\
set-save-files <on|off>\n\
Enable or disable saving of game files. When this is on, any file that the\n\
remote server sends to the client will be saved to the current directory.\n\
This includes data like quests, Episode 3 card definitions, and GBA games.\n\
");
@@ -293,12 +295,6 @@ Proxy commands (these will only work when exactly one client is connected):\n\
session->send_to_end(&cmds, sizeof(cmds), false);
session->send_to_end(&cmds, sizeof(cmds), true);
} else if (command_name == "ship") {
auto session = this->get_proxy_session();
static const string data("\xA0\x00\x04\x00", 4);
session->send_to_end(data, true);
} else if ((command_name == "info-board") || (command_name == "info-board-data")) {
auto session = this->get_proxy_session();
@@ -316,12 +312,12 @@ Proxy commands (these will only work when exactly one client is connected):\n\
session->send_to_end(data, true);
} else if (command_name == "set-save-quests") {
} else if (command_name == "set-save-files") {
if (this->state->proxy_server.get()) {
if (command_args == "on") {
this->state->proxy_server->save_quests = true;
this->state->proxy_server->save_files = true;
} else if (command_args == "off") {
this->state->proxy_server->save_quests = false;
this->state->proxy_server->save_files = false;
} else {
throw invalid_argument("argument must be \"on\" or \"off\"");
}