From 2427ad407a692746fa7b32995de4841a46fd19f4 Mon Sep 17 00:00:00 2001 From: thomaslaurenson Date: Sun, 15 Nov 2026 06:49:36 +1300 Subject: [PATCH 1/4] Extracted every name once, under the locale it is stored in --- docs/commands/extract.md | 11 ++++ src/commands/extract.cpp | 27 +++++--- src/mpq/extract.cpp | 69 +++++++++++++++++---- src/mpq/extract.h | 33 ++++++++-- test/functional/test_extract.cpp | 94 +++++++++++++++++++--------- test/unit/mpq/test_extract.cpp | 103 +++++++++++++++++++++++++++++++ 6 files changed, 284 insertions(+), 53 deletions(-) diff --git a/docs/commands/extract.md b/docs/commands/extract.md index d34a02f..3a63cbd 100644 --- a/docs/commands/extract.md +++ b/docs/commands/extract.md @@ -17,6 +17,17 @@ $ mpqcli extract wow-patch.mpq [*] Extracted: realmlist.wtf ``` +## Extract all files from an archive with several locales + +An archive can store the same file name more than once, under different locales. Extracting every file writes each name once: the variant under the `--locale` given, else the default (neutral) one, else the one with the lowest locale ID. The variants left out are named on stderr. + +```bash +$ mpqcli extract --locale deDE patch.mpq +[*] Extracted: (listfile) +[*] Extracted: cats.txt +[*] Left out other locales of cats.txt: enUS, esES +``` + ## Extract all files to an output directory Extract files to a specific output directory, which will be created if it doesn't already exist. In this example, `patch-1.10` is the user-specified output directory. diff --git a/src/commands/extract.cpp b/src/commands/extract.cpp index b577951..923ef81 100644 --- a/src/commands/extract.cpp +++ b/src/commands/extract.cpp @@ -54,17 +54,28 @@ bool Extract(const ExtractOptions &options, std::ostream &err, const std::atomic LCID lcid = options.locale.has_value() ? LangToLocale(options.locale.value()) : default_locale; // Every name is settled before the output directory is created, so a - // rejected --flat run leaves nothing on disk - const std::vector file_names = - options.files.has_value() - ? options.files.value() - : ListArchivedFiles(archive.Handle(), UsableListfile(options.listfile, err), cancelled); - if (file_names.empty()) { + // rejected --flat run leaves nothing on disk. A named file is asked for under + // the given locale; extracting everything writes one variant of each name. + std::vector files; + if (options.files.has_value()) { + for (const auto &name : options.files.value()) { + files.push_back({name, lcid, {}}); + } + } else { + files = ChooseVariants( + ListArchivedFiles(archive.Handle(), UsableListfile(options.listfile, err), cancelled), + lcid); + } + if (files.empty()) { err << "[*] No files to extract" << std::endl; archive.Close(); return true; } if (options.flat) { + std::vector file_names; + for (const auto &file : files) { + file_names.push_back(file.name); + } const auto collision = FindFlatCollision(file_names); if (collision.has_value()) { err << "[!] --flat would extract " << collision->first << " and " << collision->second @@ -98,8 +109,8 @@ bool Extract(const ExtractOptions &options, std::ostream &err, const std::atomic << effective_output.string() << std::endl; } - const int result = ExtractFiles(archive.Handle(), effective_output, file_names, options.flat, - lcid, err, cancelled); + const int result = + ExtractFiles(archive.Handle(), effective_output, files, options.flat, err, cancelled); archive.Close(); if (result != 0) { diff --git a/src/mpq/extract.cpp b/src/mpq/extract.cpp index 614bb5d..de5f59e 100644 --- a/src/mpq/extract.cpp +++ b/src/mpq/extract.cpp @@ -1,6 +1,8 @@ #include "mpq/extract.h" +#include #include +#include #include #include #include @@ -36,9 +38,9 @@ std::string ExtractedName(const std::string &file_name, bool flat) { } // namespace -std::vector ListArchivedFiles(HANDLE archive, - const std::optional &listfile_name, - const std::atomic &cancelled) { +std::vector ListArchivedFiles(HANDLE archive, + const std::optional &listfile_name, + const std::atomic &cancelled) { const std::string listfile_string = listfile_name.has_value() ? listfile_name->string() : std::string(); const char *listfile = listfile_name.has_value() ? listfile_string.c_str() : nullptr; @@ -54,7 +56,7 @@ std::vector ListArchivedFiles(HANDLE archive, throw StormError("Failed to find first file in MPQ archive", error); } - std::vector file_names; + std::vector files; do { // Closed by hand rather than through ThrowIfCancelled, so the find handle // does not leak on the way out @@ -62,11 +64,50 @@ std::vector ListArchivedFiles(HANDLE archive, SFileFindClose(find_handle); throw Interrupted{}; } - file_names.emplace_back(find_data.cFileName); + files.push_back({find_data.cFileName, find_data.lcLocale}); } while (SFileFindNextFile(find_handle, &find_data)); SFileFindClose(find_handle); - return file_names; + return files; +} + +std::vector ChooseVariants(const std::vector &files, + LCID preferred_locale) { + std::vector names; + std::map> locales_by_name; + for (const auto &file : files) { + auto &locales = locales_by_name[file.name]; + if (locales.empty()) { + names.push_back(file.name); + } + locales.push_back(file.locale); + } + + // Compared without the platform StormLib packs into bits 16 to 23, as list + // prints them; the chosen locale keeps it, so the lookup finds that exact entry + const auto by_locale = [](LCID a, LCID b) { return SFILE_LOCALE(a) < SFILE_LOCALE(b); }; + const auto stored_under = [](const std::vector &locales, LCID wanted) { + return std::find_if(locales.begin(), locales.end(), [wanted](LCID locale) { + return SFILE_LOCALE(locale) == SFILE_LOCALE(wanted); + }); + }; + + std::vector chosen; + for (const auto &name : names) { + auto locales = locales_by_name[name]; + std::stable_sort(locales.begin(), locales.end(), by_locale); + auto pick = stored_under(locales, preferred_locale); + if (pick == locales.end()) { + pick = stored_under(locales, default_locale); + } + if (pick == locales.end()) { + pick = locales.begin(); + } + const LCID locale = *pick; + locales.erase(pick); + chosen.push_back({name, locale, locales}); + } + return chosen; } std::optional> @@ -86,13 +127,19 @@ FindFlatCollision(const std::vector &file_names) { return std::nullopt; } -int ExtractFiles(HANDLE archive, const fs::path &output, const std::vector &file_names, - bool flat, LCID preferred_locale, std::ostream &err, - const std::atomic &cancelled) { +int ExtractFiles(HANDLE archive, const fs::path &output, const std::vector &files, + bool flat, std::ostream &err, const std::atomic &cancelled) { int result = 0; - for (const auto &name : file_names) { + for (const auto &file : files) { ThrowIfCancelled(cancelled); - result |= ExtractFile(archive, output, name, flat, preferred_locale, err); + result |= ExtractFile(archive, output, file.name, flat, file.locale, err); + if (!file.left_out.empty()) { + err << "[*] Left out other locales of " << file.name << ": "; + for (std::size_t i = 0; i < file.left_out.size(); i++) { + err << (i == 0 ? "" : ", ") << LocaleToLang(SFILE_LOCALE(file.left_out[i])); + } + err << std::endl; + } } return result; } diff --git a/src/mpq/extract.h b/src/mpq/extract.h index 84e8f8e..d6fc2de 100644 --- a/src/mpq/extract.h +++ b/src/mpq/extract.h @@ -13,23 +13,48 @@ namespace mpqcli { +/// One stored variant of a file: its name, and the locale it is stored under +/// +/// The locale is as StormLib reports it, with the platform in bits 16 to 23. +struct ArchivedFile { + std::string name; + LCID locale = 0; +}; + +/// A file to extract, under the locale to ask for it in +struct FileToExtract { + std::string name; + LCID locale = 0; + /// The other locales the name is stored under, which are not extracted + std::vector left_out; +}; + /// Lists every file the archive can name, once per locale it is stored under /// /// An archive with no entries gives an empty list. /// /// @throws StormError if StormLib cannot enumerate the archive's files -std::vector +std::vector ListArchivedFiles(HANDLE archive, const std::optional &listfile_name, const std::atomic &cancelled); +/// Picks one variant of each name, in the order the names are first listed +/// +/// The variant under preferred_locale, else the neutral one, else the one with the +/// lowest locale. The others are named in left_out, lowest locale first. +std::vector ChooseVariants(const std::vector &files, + LCID preferred_locale); + /// Returns two different files that a flat extraction would write to the same path std::optional> FindFlatCollision(const std::vector &file_names); -/// Extracts each named file into output, returning non-zero if any failed +/// Extracts each file into output under its locale, returning non-zero if any failed +/// +/// A file with other variants left out is followed by a line naming them. int ExtractFiles(HANDLE archive, const std::filesystem::path &output, - const std::vector &file_names, bool flat, LCID preferred_locale, - std::ostream &err, const std::atomic &cancelled); + const std::vector &files, bool flat, std::ostream &err, + const std::atomic &cancelled); int ExtractFile(HANDLE archive, const std::filesystem::path &output, const std::string &file_name, bool flat, LCID preferred_locale, std::ostream &err); diff --git a/test/functional/test_extract.cpp b/test/functional/test_extract.cpp index 28f7ffd..12561ca 100644 --- a/test/functional/test_extract.cpp +++ b/test/functional/test_extract.cpp @@ -53,18 +53,6 @@ std::set ExtractedLines(const std::set &names) { return lines; } -// The lines of output that start with prefix, for output that also carries -// lines the test does not pin -std::set LinesStartingWith(const std::string &output, const std::string &prefix) { - std::set lines; - for (const auto &line : LinesToSet(output)) { - if (line.rfind(prefix, 0) == 0) { - lines.insert(line); - } - } - return lines; -} - // A nested entry's path as extract prints it: the archive's backslash turned // into the platform's own separator std::string Native(const std::string &archive_path) { @@ -488,15 +476,15 @@ TEST_CASE("extract: --flat extracts one name stored under two locales", "[extrac auto result = Run(mpqcli_binary, {"extract", "--flat", "-o", output_dir.string(), archive.string()}); - // cats.txt is listed once per locale, and both land on the same file without - // counting as a collision + // cats.txt is written once, from the default variant, which is no collision REQUIRE(result.returncode == 0); REQUIRE(result.stdout_output.empty()); - const auto stderr_lines = LinesToVector(result.stderr_output); - REQUIRE(stderr_lines.size() == 3); - REQUIRE(std::count(stderr_lines.begin(), stderr_lines.end(), "[*] Extracted: cats.txt") == 2); - REQUIRE(LinesToSet(result.stderr_output) == ExtractedLines({"cats.txt", "(listfile)"})); + auto expected = ExtractedLines({"cats.txt", "(listfile)"}); + expected.insert("[*] Left out other locales of cats.txt: deDE"); + REQUIRE(LinesToVector(result.stderr_output).size() == 3); + REQUIRE(LinesToSet(result.stderr_output) == expected); REQUIRE(DirectoryEntries(output_dir) == std::set{"cats.txt", "(listfile)"}); + REQUIRE(ReadFile(output_dir / "cats.txt") == "Cats in the default locale."); } TEST_CASE("extract: --listfile cannot be combined with named files", "[extract]") { @@ -640,18 +628,65 @@ TEST_CASE("extract: fails when neither the given nor the default locale has the REQUIRE_FALSE(fs::exists(output_dir)); } +TEST_CASE("extract: writes a name stored only under another locale", "[extract]") { + OneLocaleArchive one; + const auto output_dir = one.dir.Path() / "extracted"; + + auto result = Run(mpqcli_binary, {"extract", "-o", output_dir.string(), one.archive.string()}); + + REQUIRE(result.returncode == 0); + REQUIRE(result.stdout_output.empty()); + REQUIRE(LinesToSet(result.stderr_output) == ExtractedLines({"cats.txt", "(listfile)"})); + REQUIRE(ReadFile(output_dir / "cats.txt") == CatsContent(OneLocaleArchive::locale)); +} + +TEST_CASE("extract: writes the --locale variant of a name stored under two locales", "[extract]") { + ManyLocalesArchive locales; + const auto output_dir = locales.dir.Path() / "extracted"; + + auto result = Run(mpqcli_binary, {"extract", "-o", output_dir.string(), + locales.archive.string(), "--locale", "deDE"}); + + REQUIRE(result.returncode == 0); + REQUIRE(result.stdout_output.empty()); + auto expected = ExtractedLines({"cats.txt", "(listfile)"}); + expected.insert("[*] Left out other locales of cats.txt: enUS, esES, 041D"); + REQUIRE(LinesToSet(result.stderr_output) == expected); + REQUIRE(ReadFile(output_dir / "cats.txt") == CatsContent("deDE")); +} + +TEST_CASE("extract: writes the default variant and names the others on stderr", "[extract]") { + ManyLocalesArchive locales; + const auto output_dir = locales.dir.Path() / "extracted"; + + auto result = + Run(mpqcli_binary, {"extract", "-o", output_dir.string(), locales.archive.string()}); + + // The others are named lowest locale first + REQUIRE(result.returncode == 0); + REQUIRE(result.stdout_output.empty()); + auto expected = ExtractedLines({"cats.txt", "(listfile)"}); + expected.insert("[*] Left out other locales of cats.txt: deDE, esES, 041D"); + REQUIRE(LinesToSet(result.stderr_output) == expected); + REQUIRE(ReadFile(output_dir / "cats.txt") == CatsContent("")); +} + TEST_CASE("extract: all files from an archive without a listfile", "[extract]") { // External listfile content (none when empty), the locale asked for (the - // default when empty), and the names that come out. Entries under another - // locale fail, so every row exits 1; unnamed entries land as File0000000N.xxx + // default when empty), and the names that come out. Each name is stored under + // one locale, so every entry comes out whatever the locale asked for; unnamed + // entries land as File0000000N.xxx + const std::set unnamed = {"File00000000.xxx", "File00000001.xxx", + "File00000002.xxx"}; + const std::set named = {"capybaras.txt", "cats.txt", "dogs.txt"}; const auto [listfile_content, locale, expected] = - GENERATE(table>({ - {"", "", {"File00000000.xxx"}}, - {"", "041D", {"File00000000.xxx", "File00000002.xxx"}}, - {"capybaras.txt\n", "", {"capybaras.txt"}}, - {"cats.txt\n", "deDE", {"File00000000.xxx", "cats.txt"}}, - {"cats.txt\ndogs.txt\ncapybaras.txt\n", "", {"capybaras.txt"}}, - {"cats.txt\ndogs.txt\ncapybaras.txt\n", "041D", {"dogs.txt", "capybaras.txt"}}, + GENERATE_COPY(table>({ + {"", "", unnamed}, + {"", "041D", unnamed}, + {"capybaras.txt\n", "", {"capybaras.txt", "File00000001.xxx", "File00000002.xxx"}}, + {"cats.txt\n", "deDE", {"File00000000.xxx", "cats.txt", "File00000002.xxx"}}, + {"cats.txt\ndogs.txt\ncapybaras.txt\n", "", named}, + {"cats.txt\ndogs.txt\ncapybaras.txt\n", "041D", named}, })); CAPTURE(listfile_content, locale); NoListfileArchive archive; @@ -669,10 +704,9 @@ TEST_CASE("extract: all files from an archive without a listfile", "[extract]") auto result = Run(mpqcli_binary, args); - REQUIRE(result.returncode == 1); + REQUIRE(result.returncode == 0); REQUIRE(result.stdout_output.empty()); - REQUIRE(LinesStartingWith(result.stderr_output, "[*] Extracted: ") == ExtractedLines(expected)); - REQUIRE_THAT(result.stderr_output, ContainsSubstring(failed_all)); + REQUIRE(LinesToSet(result.stderr_output) == ExtractedLines(expected)); REQUIRE(DirectoryEntries(output_dir) == expected); // Whatever the name it came out under, the content is the stored one const std::map contents = { diff --git a/test/unit/mpq/test_extract.cpp b/test/unit/mpq/test_extract.cpp index d5b57f7..0c03450 100644 --- a/test/unit/mpq/test_extract.cpp +++ b/test/unit/mpq/test_extract.cpp @@ -1,10 +1,113 @@ +#include +#include #include +#include #include #include +#include + +#include "mpq/archive.h" #include "mpq/extract.h" +#include "raw_archive.h" +#include "temp_dir.h" +using mpqcli::Archive; +using mpqcli::ArchivedFile; +using mpqcli::ChooseVariants; using mpqcli::FindFlatCollision; +using mpqcli::ListArchivedFiles; +using mpqcli::testing::TempDir; +using mpqcli::testing::WriteRawArchive; + +namespace { + +constexpr LCID neutral = 0; +constexpr LCID german = 0x407; +constexpr LCID spanish = 0x40a; +constexpr LCID french = 0x40c; + +} // namespace + +TEST_CASE("ListArchivedFiles", "[mpq/extract]") { + SECTION("lists a name once per locale, with that locale") { + TempDir dir; + const auto path = dir.Path() / "locales.mpq"; + WriteRawArchive(path, {{"cats.txt", neutral}, {"cats.txt", german}}); + const auto archive = Archive::Open(path, MPQ_OPEN_READ_ONLY); + const std::atomic cancelled{false}; + + const auto files = ListArchivedFiles(archive.Handle(), std::nullopt, cancelled); + + std::vector cats_locales; + for (const auto &file : files) { + if (file.name == "cats.txt") { + cats_locales.push_back(file.locale); + } + } + std::sort(cats_locales.begin(), cats_locales.end()); + REQUIRE(cats_locales == std::vector{neutral, german}); + } +} + +TEST_CASE("ChooseVariants", "[mpq/extract]") { + const std::vector three = { + {"cats.txt", spanish}, {"cats.txt", neutral}, {"cats.txt", german}}; + + SECTION("picks the variant under the preferred locale") { + const auto chosen = ChooseVariants(three, german); + REQUIRE(chosen.size() == 1); + REQUIRE(chosen[0].name == "cats.txt"); + REQUIRE(chosen[0].locale == german); + REQUIRE(chosen[0].left_out == std::vector{neutral, spanish}); + } + + SECTION("picks the neutral variant when the preferred locale is not stored") { + const auto chosen = ChooseVariants(three, french); + REQUIRE(chosen.size() == 1); + REQUIRE(chosen[0].locale == neutral); + REQUIRE(chosen[0].left_out == std::vector{german, spanish}); + } + + SECTION("picks the only variant, stored under another locale") { + const auto chosen = ChooseVariants({{"cats.txt", german}}, neutral); + REQUIRE(chosen.size() == 1); + REQUIRE(chosen[0].locale == german); + REQUIRE(chosen[0].left_out.empty()); + } + + SECTION("picks the lowest locale when neither the preferred nor neutral is stored") { + const auto chosen = ChooseVariants({{"cats.txt", spanish}, {"cats.txt", german}}, french); + REQUIRE(chosen.size() == 1); + REQUIRE(chosen[0].locale == german); + REQUIRE(chosen[0].left_out == std::vector{spanish}); + } + + SECTION("matches the preferred locale whatever the platform, and keeps the platform") { + // SFILE_MAKE_LCID(german, 1), which -Wold-style-cast rejects + const LCID german_on_platform_1 = german | (1U << 16); + const auto chosen = + ChooseVariants({{"cats.txt", neutral}, {"cats.txt", german_on_platform_1}}, german); + REQUIRE(chosen.size() == 1); + REQUIRE(chosen[0].locale == german_on_platform_1); + } + + SECTION("lists each name once, in the order it first appears") { + const auto chosen = ChooseVariants( + {{"dogs.txt", neutral}, {"cats.txt", german}, {"dogs.txt", german}}, neutral); + REQUIRE(chosen.size() == 2); + REQUIRE(chosen[0].name == "dogs.txt"); + REQUIRE(chosen[0].locale == neutral); + REQUIRE(chosen[0].left_out == std::vector{german}); + REQUIRE(chosen[1].name == "cats.txt"); + REQUIRE(chosen[1].locale == german); + REQUIRE(chosen[1].left_out.empty()); + } + + SECTION("chooses nothing from an empty list") { + REQUIRE(ChooseVariants({}, neutral).empty()); + } +} TEST_CASE("FindFlatCollision", "[mpq/extract]") { SECTION("finds nothing when every file name differs") { From 661194fee4c098a568460ddc90f531765fed2372 Mon Sep 17 00:00:00 2001 From: thomaslaurenson Date: Fri, 20 Nov 2026 16:01:22 +1300 Subject: [PATCH 2/4] Reported the locale read and extract fall back to --- docs/commands/extract.md | 2 +- docs/commands/read.md | 2 +- src/mpq/extract.cpp | 9 +++++++-- src/mpq/query.cpp | 9 +++++++++ src/mpq/query.h | 6 ++++++ src/mpq/read.cpp | 7 +++++-- test/functional/test_extract.cpp | 4 +++- test/functional/test_read.cpp | 14 +++++++++++++- 8 files changed, 45 insertions(+), 8 deletions(-) diff --git a/docs/commands/extract.md b/docs/commands/extract.md index 3a63cbd..0f2a475 100644 --- a/docs/commands/extract.md +++ b/docs/commands/extract.md @@ -76,7 +76,7 @@ $ mpqcli list wow-patch.mpq | grep -i '\.exe$' | mpqcli extract wow-patch.mpq - ## Extract one specific file with locale -Use the `-e` or `--locale` argument to specify the locale of the file to extract. If there is no file with the requested name and locale, the default locale will be used instead. +Use the `-e` or `--locale` argument to specify the locale of the file to extract. If there is no file with the requested name and locale, the default locale is used instead, and a line on stderr says so. ```bash $ mpqcli extract Patch_rt.mpq "rez\gluBNRes.res" --locale deDE diff --git a/docs/commands/read.md b/docs/commands/read.md index e4c99cf..d79ff98 100644 --- a/docs/commands/read.md +++ b/docs/commands/read.md @@ -47,7 +47,7 @@ $ mpqcli read Patch_rt.mpq "rez/stat_txt.tbl" ## Read one specific file with locale -Use the `-e` or `--locale` argument to specify the locale of the file to read. If there is no file with the requested name and locale, the default locale will be used instead. +Use the `-e` or `--locale` argument to specify the locale of the file to read. If there is no file with the requested name and locale, the default locale is used instead, and a line on stderr says so. ```bash $ mpqcli read Patch_rt.mpq "rez\stat_txt.tbl" --locale ptPT diff --git a/src/mpq/extract.cpp b/src/mpq/extract.cpp index de5f59e..facaabe 100644 --- a/src/mpq/extract.cpp +++ b/src/mpq/extract.cpp @@ -148,13 +148,18 @@ int ExtractFile(HANDLE archive, const fs::path &output, const std::string &file_ LCID preferred_locale, std::ostream &err) { SFileSetLocale(preferred_locale); const std::string stored_name = StoredFileName(archive, file_name, preferred_locale); - if (!FileExistsInArchiveForLocale(archive, stored_name, preferred_locale) && - !FileExistsInArchiveForLocale(archive, stored_name, default_locale)) { + const bool has_preferred = FileExistsInArchiveForLocale(archive, stored_name, preferred_locale); + if (!has_preferred && !FileExistsInArchiveForLocale(archive, stored_name, default_locale)) { err << "[!] Failed: File doesn't exist" << PrettyPrintLocale(preferred_locale, " for locale ", true) << ": " << stored_name << std::endl; return 1; } + // Only a named file can fall back: extracting everything asks for each + // variant under the locale it is stored in + if (!has_preferred) { + ReportLocaleFallback(stored_name, preferred_locale, err); + } const std::string file_name_string = ExtractedName(stored_name, flat); diff --git a/src/mpq/query.cpp b/src/mpq/query.cpp index 08726d4..05a310e 100644 --- a/src/mpq/query.cpp +++ b/src/mpq/query.cpp @@ -12,6 +12,7 @@ #include #include "util/hash.h" +#include "util/locales.h" #include "util/path.h" namespace fs = std::filesystem; @@ -33,6 +34,14 @@ bool FileExistsInArchiveForLocale(const HANDLE archive, const std::string &file_ return file_exists; } +void ReportLocaleFallback(const std::string &file_path, LCID locale, std::ostream &err) { + if (locale == LangToLocale("enUS")) { + return; + } + err << "[*] " << file_path << " has no " << LocaleToLang(locale) << " variant, using " + << LocaleToLang(default_locale) << std::endl; +} + std::string StoredFileName(HANDLE archive, const std::string &file_path, LCID locale) { SFileSetLocale(locale); if (SFileHasFile(archive, file_path.c_str())) { diff --git a/src/mpq/query.h b/src/mpq/query.h index 21e0555..85f93fc 100644 --- a/src/mpq/query.h +++ b/src/mpq/query.h @@ -17,6 +17,12 @@ inline const std::vector special_mpq_files = {"(listfile)", "(signa bool FileExistsInArchiveForLocale(HANDLE archive, const std::string &file_path, LCID locale); +/// Reports on err that file_path has no variant under locale, so the default one is used +/// +/// Says nothing when locale is enUS: the default locale prints as enUS too, so the line +/// would say enUS fell back to enUS. +void ReportLocaleFallback(const std::string &file_path, LCID locale, std::ostream &err); + /// Returns the name the archive stores file_path under, looked up under locale /// /// An MPQ normally stores paths with backslashes, and StormLib hashes / and \ as different diff --git a/src/mpq/read.cpp b/src/mpq/read.cpp index 594021f..c3b973a 100644 --- a/src/mpq/read.cpp +++ b/src/mpq/read.cpp @@ -16,13 +16,16 @@ std::unique_ptr ReadArchivedFile(HANDLE archive, const char *file_name, std::ostream &err) { SFileSetLocale(preferred_locale); const std::string stored_name = StoredFileName(archive, file_name, preferred_locale); - if (!FileExistsInArchiveForLocale(archive, stored_name, preferred_locale) && - !FileExistsInArchiveForLocale(archive, stored_name, default_locale)) { + const bool has_preferred = FileExistsInArchiveForLocale(archive, stored_name, preferred_locale); + if (!has_preferred && !FileExistsInArchiveForLocale(archive, stored_name, default_locale)) { err << "[!] Failed: File doesn't exist" << PrettyPrintLocale(preferred_locale, " for locale ", true) << ": " << stored_name << std::endl; return nullptr; } + if (!has_preferred) { + ReportLocaleFallback(stored_name, preferred_locale, err); + } HANDLE file; if (!SFileOpenFileEx(archive, stored_name.c_str(), SFILE_OPEN_FROM_MPQ, &file)) { diff --git a/test/functional/test_extract.cpp b/test/functional/test_extract.cpp index 12561ca..063589a 100644 --- a/test/functional/test_extract.cpp +++ b/test/functional/test_extract.cpp @@ -596,7 +596,9 @@ TEST_CASE("extract: falls back to the default locale when the file has no such v REQUIRE(result.returncode == 0); REQUIRE(result.stdout_output.empty()); - REQUIRE(LinesToSet(result.stderr_output) == std::set{"[*] Extracted: cats.txt"}); + REQUIRE(LinesToVector(result.stderr_output) == + std::vector{"[*] cats.txt has no ptPT variant, using enUS", + "[*] Extracted: cats.txt"}); REQUIRE(ReadFile(output_dir / "cats.txt") == CatsContent("")); } diff --git a/test/functional/test_read.cpp b/test/functional/test_read.cpp index d044aa3..c761cb1 100644 --- a/test/functional/test_read.cpp +++ b/test/functional/test_read.cpp @@ -317,7 +317,19 @@ TEST_CASE("read: falls back to the default locale when the file has no such vari auto result = Run(mpqcli_binary, {"read", locales.archive.string(), "cats.txt", "--locale", "ptPT"}); - // Silently: a known locale the entry lacks is not worth a warning + // Stdout stays the file's bytes alone; the fallback is reported on stderr + REQUIRE(result.returncode == 0); + REQUIRE(result.stderr_output == "[*] cats.txt has no ptPT variant, using enUS\n"); + REQUIRE(result.stdout_output == CatsContent("")); +} + +TEST_CASE("read: says nothing when enUS falls back to the neutral locale", "[read]") { + ManyLocalesArchive locales; + + auto result = + Run(mpqcli_binary, {"read", locales.archive.string(), "cats.txt", "--locale", "enUS"}); + + // The neutral locale prints as enUS too, so a note would say enUS fell back to enUS REQUIRE(result.returncode == 0); REQUIRE(result.stderr_output.empty()); REQUIRE(result.stdout_output == CatsContent("")); From e65fc8ea365eb20a5388379b19aaa1f6b0e05176 Mon Sep 17 00:00:00 2001 From: thomaslaurenson Date: Fri, 20 Nov 2026 16:27:39 +1300 Subject: [PATCH 3/4] Added the 0.13.2 changelog entry and bumped the project version --- CHANGELOG.md | 13 +++++++++++++ CMakeLists.txt | 2 +- docs/changelog.md | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce680e0..01cd9c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # Changelog +## 0.13.2 - 2026-11-20 + +### Changed + +- Say on stderr when read or extract uses the default locale because the file has no copy in the one asked for + +### Fixed + +- Extract each name once when extracting every file, under the locale it is stored in, and name the left-out locales on stderr +- Extract files stored only under a non-default locale when extracting every file, instead of failing +- List every locale of a name stored under more than 32, instead of reading past a buffer +- Show an empty time in list for a file time that cannot be converted, instead of reading past a buffer + ## 0.13.1 - 2026-11-14 ### Changed diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bb71f4..1c69794 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.25) -project(mpqcli VERSION 0.13.1) +project(mpqcli VERSION 0.13.2) # An option() name is global to the CMake cache, including a consumer's, so these # carry the project prefix; a bare BUILD_STATIC claims a name anyone might want. diff --git a/docs/changelog.md b/docs/changelog.md index ce680e0..01cd9c8 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,18 @@ # Changelog +## 0.13.2 - 2026-11-20 + +### Changed + +- Say on stderr when read or extract uses the default locale because the file has no copy in the one asked for + +### Fixed + +- Extract each name once when extracting every file, under the locale it is stored in, and name the left-out locales on stderr +- Extract files stored only under a non-default locale when extracting every file, instead of failing +- List every locale of a name stored under more than 32, instead of reading past a buffer +- Show an empty time in list for a file time that cannot be converted, instead of reading past a buffer + ## 0.13.1 - 2026-11-14 ### Changed From c9b7cf73f1da4b32040d9b90752061c31c76df7b Mon Sep 17 00:00:00 2001 From: thomaslaurenson Date: Fri, 20 Nov 2026 16:35:04 +1300 Subject: [PATCH 4/4] Compared the read fallback test's stderr by line, so it passes on Windows --- test/functional/test_read.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/functional/test_read.cpp b/test/functional/test_read.cpp index c761cb1..6d56a80 100644 --- a/test/functional/test_read.cpp +++ b/test/functional/test_read.cpp @@ -319,7 +319,8 @@ TEST_CASE("read: falls back to the default locale when the file has no such vari // Stdout stays the file's bytes alone; the fallback is reported on stderr REQUIRE(result.returncode == 0); - REQUIRE(result.stderr_output == "[*] cats.txt has no ptPT variant, using enUS\n"); + REQUIRE(LinesToVector(result.stderr_output) == + std::vector{"[*] cats.txt has no ptPT variant, using enUS"}); REQUIRE(result.stdout_output == CatsContent("")); }