// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later #include "io/PresetBundle.hpp" #include #include "io/PresetIo.hpp" #include "io/Zip.hpp" #include "sampler/SampleStore.hpp" namespace sizzle::io { namespace { constexpr int PADS = 16; std::string baseNameOf(const std::string &path) { const size_t slash = path.find_last_of("/\\"); return slash == std::string::npos ? path : path.substr(slash + 1); } bool endsWith(const std::string &s, const char *suffix) { const size_t n = std::char_traits::length(suffix); return s.size() >= n && s.compare(s.size() - n, n, suffix) == 0; } } // namespace bool PresetBundle::carriesSamples(const std::string &type) { return type == "SAMPLER" || type == "SOUNDFONT" || type == "GRAIN_CLOUD" || type == "WAVETABLE"; } bool PresetBundle::importInto(model::ToolboxSlot &slot, const uint8_t *zipBytes, size_t size, const std::function &keep) { std::vector entries; if (!zip::read(zipBytes, size, entries, 64ull << 20)) return false; // ZipLimits.forBundle std::string preset; bool havePreset = false; std::map *> files; for (const auto &e : entries) { const std::string name = baseNameOf(e.name); if (name.empty() || e.name.rfind("__MACOSX", 0) == 0 || name.rfind("._", 0) == 0) continue; if (!havePreset && endsWith(name, ".szp")) { preset.assign(e.data.begin(), e.data.end()); havePreset = true; } else files.emplace(name, &e.data); } if (!havePreset) return false; model::ToolboxSlot fresh = slot; if (!PresetIo::importText(fresh, preset)) return false; auto resolve = [&](const char *fileKey, const char *idKey) { const std::string ref = fresh.s(fileKey, ""); if (ref.empty()) return; auto it = files.find(baseNameOf(ref)); if (it == files.end()) return; auto sample = sampler::SampleStore::decodeWav(it->second->data(), it->second->size()); if (!sample) return; const std::string id = keep(it->first, std::move(*sample)); if (!id.empty()) fresh.set(idKey, id.c_str()); }; if (fresh.type == "SAMPLER") { for (int p = 0; p < PADS; ++p) { const std::string pad = "pad" + std::to_string(p); resolve((pad + "File").c_str(), pad.c_str()); } } else if (carriesSamples(fresh.type)) { resolve("sfFile", "sfSample"); } slot = fresh; return true; } std::vector PresetBundle::exportFrom(const model::ToolboxSlot &slot, const std::string &baseName, const std::function(const std::string &)> &wavOf) { model::ToolboxSlot copy = slot; std::vector entries; auto carry = [&](const char *idKey, const char *fileKey, const std::string &fileName) { const std::string id = copy.s(idKey, ""); copy.values.remove(fileKey); if (id.empty()) return; std::vector wav = wavOf(id); copy.values.remove(idKey); // a sample id means nothing on another device if (wav.empty()) return; copy.set(fileKey, fileName.c_str()); entries.push_back({fileName, std::move(wav)}); }; if (copy.type == "SAMPLER") { for (int p = 0; p < PADS; ++p) { const std::string pad = "pad" + std::to_string(p); carry(pad.c_str(), (pad + "File").c_str(), baseName + "_" + pad + ".wav"); } } else if (carriesSamples(copy.type)) { carry("sfSample", "sfFile", baseName + "_sf.wav"); } const std::string text = PresetIo::exportText(copy); entries.insert(entries.begin(), zip::Entry{baseName + ".szp", std::vector(text.begin(), text.end())}); return zip::write(entries); } } // namespace sizzle::io