Skip to content

Commit

Permalink
fix: compatible with NCM player
Browse files Browse the repository at this point in the history
  • Loading branch information
pnck committed May 6, 2024
1 parent 906ce94 commit 40ae275
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
47 changes: 45 additions & 2 deletions src/common/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
#include "spdlog/sinks/base_sink.h"
#include "spdlog/sinks/dist_sink.h"
#include "spdlog/details/null_mutex.h"

#include "nlohmann/json.hpp"

#include "common/consts.hpp"

#include <string>
#include <memory>
#include <mutex>
#include <span>

// https://fmt.dev/latest/api.html#udt
template <>
Expand Down Expand Up @@ -55,7 +57,7 @@ struct fmt::formatter<nlohmann::json> : formatter<std::string> {
template <>
struct fmt::formatter<GUID> : formatter<string_view> {
auto format(const GUID &guid, format_context &ctx) const {
// claud-3-sonnet
// by claud-3-sonnet
auto formatted = fmt::format("{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
guid.Data1,
guid.Data2,
Expand All @@ -72,6 +74,47 @@ struct fmt::formatter<GUID> : formatter<string_view> {
}
};

// by claud-3-sonnet
template <>
struct fmt::formatter<fb2k_ncm::ncm_file_st> : fmt::formatter<std::string> {
auto format(const fb2k_ncm::ncm_file_st &file, format_context &ctx) {
const uint8_t stub[] = {0xde, 0xad, 0xc0, 0xdd}; // DEADC0DE
auto &&fmt_stub = fmt::join(std::span(stub, sizeof(stub)), "");
auto formatted = fmt::format(
"{{"
"magic: {:#x}, "
"unknown_gap_2b: [{:#x}, {:#x}], "
"rc4_seed_len: {}, "
"rc4_seed: {:X}, "
"meta_len: {}, "
"meta_content: {:X}, "
"unknown_gap_5b: [{:#x}, {:#x}, {:#x}, {:#x}, {:#x}], "
"album_image_size: [{}, {}], "
"album_image: {:X}... , "
"audio_content: {:X}..."
"}}",
file.magic,
file.unknown_gap_2b[0],
file.unknown_gap_2b[1],
file.rc4_seed_len,
(!!file.rc4_seed) ? fmt::join(std::span(file.rc4_seed, file.rc4_seed_len), "") : std::move(fmt_stub),
file.meta_len,
(!!file.meta_content) ? fmt::join(std::span(file.meta_content, file.meta_len), "") : std::move(fmt_stub),
file.unknown_gap_5b[0],
file.unknown_gap_5b[1],
file.unknown_gap_5b[2],
file.unknown_gap_5b[3],
file.unknown_gap_5b[4],
file.album_image_size[0],
file.album_image_size[1],
(!!file.album_image) ? fmt::join(std::span(file.album_image, file.album_image_size[0] <= 32 ?: 32), "") : std::move(fmt_stub),
(!!file.audio_content) ? fmt::join(std::span(file.audio_content, 32), "") : std::move(fmt_stub));
return fmt::format_to(ctx.out(), "{}", formatted);
}
};

// --------- SPDLOG SINKS ---------

template <typename Mutex>
class fb2k_console_sink : public spdlog::sinks::base_sink<Mutex> {
using base_t = spdlog::sinks::base_sink<Mutex>;
Expand Down
21 changes: 17 additions & 4 deletions src/input_ncm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,32 @@ inline void fb2k_ncm::input_ncm::retag(const file_info &p_info, abort_callback &
DEBUG_LOG("input_ncm::retag()");
// Replay Gain
file_info_impl cur_file_info;
decoder_->get_info(0, cur_file_info, p_abort);
source_info_writer_->get_info(0, cur_file_info, p_abort);
cur_file_info.set_replaygain(p_info.get_replaygain());
source_info_writer_->set_info(0, cur_file_info, p_abort);
source_info_writer_->commit(p_abort);

// Meta tags
auto meta = meta_processor(p_info);
ncm_file_->overwrite_meta(meta.dump(), p_abort);
// NOTE: Always do differential update, to maximally avoid appending "overwrite" key,
// which will change the "163 key xxxx" content.
auto target_json = meta_processor(p_info).dump();
this->get_info(cur_file_info, p_abort);
auto current_json = meta_processor(cur_file_info).dump();

if (current_json.contains(overwrite_key)) {
current_json.erase(overwrite_key);
}

auto diff = json_t::diff(current_json, target_json);
ncm_file_->overwrite_meta(json_t::object().patch(diff), p_abort);
}

/// @brief "Clear tags" => restore the meta field (163 key) to its original state
/// @attention ReplayGain and embedded tags in the audio content are left untouched.
/// @note ReplayGain can be wiped by retagging, since the meta diff would be empty then.
inline void fb2k_ncm::input_ncm::remove_tags(abort_callback &p_abort) {
DEBUG_LOG("input_ncm::remove_tags()");
source_info_writer_->remove_tags_fallback(p_abort);
// source_info_writer_->remove_tags_fallback(p_abort);
ncm_file_->overwrite_meta(nlohmann::json(), p_abort);
}

Expand Down
10 changes: 6 additions & 4 deletions src/ncm_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,23 +327,25 @@ void ncm_file::overwrite_meta(const nlohmann::json &meta, abort_callback &p_abor
// passed

// embed overwriting meta into the source
json_t live_meta = json_t::parse(meta_str_); // NOTE: use unmodified raw json string.

auto meta_str_to_write = std::string("music:");
nlohmann::ordered_json live_meta = nlohmann::ordered_json::parse(meta_str_); // NOTE: use unmodified raw json string to keep stricted order
if (live_meta.contains(overwrite_key)) {
live_meta.erase(overwrite_key); // replace the old key
}

if (!meta.is_null()) {
if (meta.size()) {
live_meta[overwrite_key] = meta;
live_meta[overwrite_key].emplace(foo_input_ncm_comment_key, foo_input_ncm_comment);
}
auto meta_str_to_write = std::string("music:") + live_meta.dump();
meta_str_to_write += live_meta.dump();

DEBUG_LOG_F("Overwriting meta (to {}): {}", this->path(), meta_str_to_write);

// NOTE: meta json processing:
// 1. read <meta_len>
// 2. XOR each byte by 0x63
// 3. format: "164 key(Don't modify):<base64>"
// 3. format: "163 key(Don't modify):<base64>"
// 4. base64 decode
// 5. AES ECB decrypt (pkcs#7 padding)
// 6. "music:" <json>
Expand Down
3 changes: 0 additions & 3 deletions src/ncm_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ namespace fb2k_ncm
const char *this_path_ = nullptr;
ncm_file_parsed_st parsed_file_{};
file_ptr source_;
// std::unique_ptr<std::basic_fstream<char>> source_;
// using fschar = decltype(source_)::element_type::char_type;

std::string meta_str_;
nlohmann::json meta_json_;
cipher::abnormal_RC4 rc4_decryptor_;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/context_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ namespace fb2k_ncm::ui
retagger->commit(fb2k::noAbort); // do not interrupt committing
retagger = nullptr; // release, to let album editor reopen the file

// embeded album art
// embedded album art
auto album_extractor = album_art_extractor::g_open(ncm_file, ncm_file->path(), p_abort);
auto album_writer = album_art_editor::g_open(nullptr, to_retag, p_abort);
if (album_extractor.is_valid() && album_writer.is_valid()) {
Expand Down

0 comments on commit 40ae275

Please sign in to comment.