Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ineluki MP3 patch improvements #3087

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/filesystem_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ StringView Filesystem_Stream::InputStream::GetName() const {
return name;
}

std::streampos Filesystem_Stream::InputStream::GetSize() const {
if (!size_cached) {
size_cached = true;
auto cur_pos = rdbuf()->pubseekoff(0, std::ios_base::cur, std::ios_base::in);
size = rdbuf()->pubseekoff(0, std::ios_base::end, std::ios_base::in);
rdbuf()->pubseekoff(cur_pos, std::ios_base::beg, std::ios_base::in);
}
return size;
}

void Filesystem_Stream::InputStream::Close() {
delete rdbuf();
set_rdbuf(nullptr);
Expand Down
3 changes: 3 additions & 0 deletions src/filesystem_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Filesystem_Stream {
InputStream& operator=(InputStream&& is) noexcept;

StringView GetName() const;
std::streampos GetSize() const;
void Close();

template <typename T>
Expand All @@ -47,6 +48,8 @@ namespace Filesystem_Stream {
bool Read0(T& obj);

std::string name;
mutable bool size_cached = false;
mutable std::streampos size = 0;
};

class OutputStream final : public std::ostream {
Expand Down
68 changes: 50 additions & 18 deletions src/game_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ void Game_System::ResetSystemGraphic() {
ReloadSystemGraphic();
}


template <typename T>
static const T& GetAudio(const T& save, const T& db) {
return save.name.empty() ? db : save;
Expand Down Expand Up @@ -509,6 +508,21 @@ void Game_System::SetTransition(int which, int transition) {
}
}

std::string Game_System::InelukiReadLink(Filesystem_Stream::InputStream& stream) {
// The first line contains the path to the actual audio file to play
std::string line;
if (!Utils::ReadLine(stream, line)) {
Output::Warning("Ineluki MP3: Link file is empty: {}", stream.GetName());
return {};
}
line = lcf::ReaderUtil::Recode(line, Player::encoding);

Output::Debug("Ineluki MP3: Link file: {} -> {}", stream.GetName(), line);
std::string line_canonical = FileFinder::MakeCanonical(line, 1);

return line_canonical;
}

void Game_System::OnBgmReady(FileRequestResult* result) {
// Take from current_music, params could have changed over time
bgm_pending = false;
Expand All @@ -522,27 +536,13 @@ void Game_System::OnBgmReady(FileRequestResult* result) {
return;
}

if (StringView(result->file).ends_with(".link")) {
if (Player::IsPatchKeyPatch() && StringView(result->file).ends_with(".link") && stream.GetSize() < 500) {
// Handle Ineluki's MP3 patch
if (!stream) {
Output::Warning("Ineluki MP3: Link read error: {}", stream.GetName());
return;
}

// The first line contains the path to the actual audio file to play
std::string line;
if (!Utils::ReadLine(stream, line)) {
Output::Warning("Ineluki MP3: Link file is empty: {}", stream.GetName());
return;
}
line = lcf::ReaderUtil::Recode(line, Player::encoding);

Output::Debug("Ineluki MP3: Link file: {} -> {}", stream.GetName(), line);
std::string line_canonical = FileFinder::MakeCanonical(line, 1);
std::string line = InelukiReadLink(stream);

// Needs another Async roundtrip
bgm_pending = true;
FileRequestAsync *request = AsyncHandler::RequestFile(line_canonical);
FileRequestAsync *request = AsyncHandler::RequestFile(line);
music_request_id = request->Bind(&Game_System::OnBgmInelukiReady, this);
request->Start();
return;
Expand Down Expand Up @@ -580,6 +580,38 @@ void Game_System::OnSeReady(FileRequestResult* result, lcf::rpg::Sound se, bool
Output::Debug("Sound not found: {}", result->file);
return;
}

if (Player::IsPatchKeyPatch() && StringView(result->file).ends_with(".link") && stream.GetSize() < 500) {
// Handle Ineluki's MP3 patch
std::string line = InelukiReadLink(stream);

// Needs another Async roundtrip
FileRequestAsync* request = AsyncHandler::RequestFile(line);
se_request_ids[line] = request->Bind(&Game_System::OnSeInelukiReady, this, se);
request->Start();
return;
}

se_cache = AudioSeCache::Create(std::move(stream), result->file);
}

if (!se_cache) {
Output::Warning("Sound {}: Format not supported", result->file);
Copy link
Contributor

@fdelapena fdelapena Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this brings warnings where RPG_RT.exe won't? Use Output::Debug instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is copy-pasted from the non-Ineluki SE handler.
The warning will only trigger when a SE file is unsupported format wise. Missing files won't trigger it so should be save imo

return;
}

Audio().SE_Play(std::move(se_cache), se.volume, se.tempo);
}

void Game_System::OnSeInelukiReady(FileRequestResult* result, lcf::rpg::Sound se) {
auto item = se_request_ids.find(result->file);
if (item != se_request_ids.end()) {
se_request_ids.erase(item);
}

auto se_cache = AudioSeCache::GetCachedSe(result->file);
if (!se_cache) {
auto stream = FileFinder::Game().OpenFile(result->file);
se_cache = AudioSeCache::Create(std::move(stream), result->file);
}

Expand Down
3 changes: 3 additions & 0 deletions src/game_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,12 @@ class Game_System {
bool IsLoadedThisFrame() const;

private:
std::string InelukiReadLink(Filesystem_Stream::InputStream& stream);

void OnBgmReady(FileRequestResult* result);
void OnBgmInelukiReady(FileRequestResult* result);
void OnSeReady(FileRequestResult* result, lcf::rpg::Sound se, bool stop_sounds);
void OnSeInelukiReady(FileRequestResult* result, lcf::rpg::Sound se);
void OnChangeSystemGraphicReady(FileRequestResult* result);
private:
lcf::rpg::SaveSystem data;
Expand Down
Loading