Skip to content

Commit f29236e

Browse files
refactor: read manifest in memory
1 parent 1dffb4f commit f29236e

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/PluginManager.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,19 @@ void PluginManager::extractPackedPlugins() {
257257
if(fs::exists(datapath.utf8() + "/plugins_runtime/tmp"))
258258
fs::remove_all(datapath.utf8() + "/plugins_runtime/tmp");
259259

260-
int result = zip_extract(path.utf8().c_str(),
261-
BNString(datapath + L"/plugins_runtime/tmp").utf8().c_str(), nullptr, nullptr);
262-
if (result != 0)throw std::exception(("unzip err code:" + std::to_string(GetLastError())).c_str());
260+
const auto zip = zip_open(path.utf8().c_str(), 0, 'r');
261+
262+
auto code = zip_entry_open(zip, "manifest.json");
263+
if (code < 0) throw std::exception("manifest.json not found in plugin");
264+
265+
char* buf = nullptr;
266+
size_t size = 0;
267+
code = zip_entry_read(zip, (void**) & buf, &size);
268+
if (code < 0) throw std::exception("manifest.json read error");
269+
zip_entry_close(zip);
270+
zip_close(zip);
263271

264-
const auto modManifest = nlohmann::json::parse(
265-
util::read_to_string(datapath + L"/plugins_runtime/tmp/manifest.json"));
272+
const auto modManifest = nlohmann::json::parse(std::string(buf, size));
266273
modManifest.get_to(manifest);
267274
};
268275

@@ -277,28 +284,29 @@ void PluginManager::extractPackedPlugins() {
277284
if (std::ranges::find(disable_list, manifest.slug) != disable_list.end() ||
278285
(
279286
isNCM3 &&
280-
!manifest.ncm3Compatible // duplicated / not ncm3 / ncm3-compatible
287+
!manifest.ncm3Compatible // duplicated / ncm3 but not ncm3-compatible / do not meet version req
281288
) ||
282289
(
283290
!semver::range::satisfies(
284291
util::getNCMExecutableVersion(), manifest.ncm_version_req
285292
)
286293
)
287294
) {
288-
fs::remove_all(datapath.utf8() + "/plugins_runtime/tmp");
289295
continue;
290296
}
291297

292298
if (manifest.manifest_version == 1) {
293-
util::write_file_text(datapath + L"/plugins_runtime/tmp/.plugin.path.meta",
294-
pystring::slice(path, datapath.length()));
295-
auto realPath = datapath + L"/plugins_runtime/" + BNString(manifest.slug);
299+
BNString realPath = datapath + L"/plugins_runtime/" + BNString(manifest.slug);
296300

297301
std::error_code ec;
298-
if (fs::exists(realPath) && manifest.native_plugin[0] == '\0')
299-
fs::remove_all(realPath, ec);
302+
if (fs::exists(realPath.utf8()) && manifest.native_plugin[0] == '\0')
303+
fs::remove_all(realPath.utf8(), ec);
300304

301-
fs::rename(datapath + L"/plugins_runtime/tmp", realPath);
305+
const auto code = zip_extract(path.utf8().c_str(), realPath.utf8().c_str(), nullptr, nullptr);
306+
if (code != 0) throw std::exception(("unzip err code:" + std::to_string(code)).c_str());
307+
308+
util::write_file_text(realPath + L"/.plugin.path.meta",
309+
pystring::slice(path, datapath.length()));
302310
}
303311
else {
304312
throw std::exception("Unsupported manifest version.");

0 commit comments

Comments
 (0)