138 lines
4.3 KiB
C++
138 lines
4.3 KiB
C++
// eterpack_pack — build a 40250 pack (<db>.eix/.epk) from a directory with the ported CEterPack, the way
|
|
// the original locale packs are made: every file COMPRESS (LZO), index LZO + TEA encrypted.
|
|
//
|
|
// eterpack_pack <db path without extension> <source dir> <pack prefix, e.g. locale/zh/>
|
|
// eterpack_pack --read-client <client dir> <registered entry> <new output file>
|
|
//
|
|
// Pack names are <prefix><path relative to the source dir>, lowercased with '/' (inlineConvertPackFilename).
|
|
// An existing <db>.eix/.epk is replaced. The result is reopened read-only and every file read back
|
|
// through CEterPack::Get and compared with its source.
|
|
#include "EterPack/StdAfx.h"
|
|
#include "EterPack/EterPack.h"
|
|
#include "EterPack/EterPackManager.h"
|
|
#include "EterBase/MappedFile.h"
|
|
#include "../../src/platform/UserInterface/UserInterface.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
static std::vector<char> read_file(const fs::path& path)
|
|
{
|
|
std::ifstream in(path, std::ios::binary);
|
|
return {std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()};
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
// PORT: resolve data through the actual runtime Index/key-fallback path.
|
|
// Pack-only reads prevent a loose file in the shell cwd from masking a pack.
|
|
if (argc > 1 && std::strcmp(argv[1], "--read-client") == 0)
|
|
{
|
|
if (argc != 5)
|
|
{
|
|
std::fprintf(stderr, "usage: %s --read-client <client> <entry> <new output>\n", argv[0]);
|
|
return 2;
|
|
}
|
|
PackSingletons();
|
|
const auto folder = fs::absolute(fs::path(argv[2]) / "pack");
|
|
if (!fs::is_regular_file(folder / "Index") || !PackInitialize(folder.string().c_str()))
|
|
return 1;
|
|
CMappedFile mapped;
|
|
LPCVOID data = nullptr;
|
|
if (!CEterPackManager::Instance().GetFromPack(mapped, argv[3], &data))
|
|
{
|
|
std::fprintf(stderr, "registered entry unavailable: %s\n", argv[3]);
|
|
return 1;
|
|
}
|
|
// Exclusive creation: a read-only probe must never overwrite user data.
|
|
FILE* output = std::fopen(argv[4], "wbx");
|
|
if (!output)
|
|
return 1;
|
|
const bool written = std::fwrite(data, 1, mapped.Size(), output) == mapped.Size();
|
|
const bool closed = std::fclose(output) == 0;
|
|
if (!written || !closed)
|
|
return 1;
|
|
std::printf("read registered entry %s: %u bytes\n", argv[3], unsigned(mapped.Size()));
|
|
return 0;
|
|
}
|
|
if (argc != 4)
|
|
{
|
|
std::fprintf(stderr, "usage: %s <db> <source dir> <pack prefix>\n", argv[0]);
|
|
return 2;
|
|
}
|
|
const std::string db = argv[1];
|
|
const fs::path source = argv[2];
|
|
std::string prefix = argv[3];
|
|
if (!prefix.empty() && prefix.back() != '/')
|
|
prefix += '/';
|
|
|
|
std::vector<std::pair<std::string, fs::path>> files;
|
|
for (const auto& e : fs::recursive_directory_iterator(source))
|
|
if (e.is_regular_file())
|
|
files.emplace_back(prefix + e.path().lexically_relative(source).generic_string(), e.path());
|
|
std::sort(files.begin(), files.end());
|
|
if (files.empty())
|
|
{
|
|
std::fprintf(stderr, "no files under %s\n", source.c_str());
|
|
return 1;
|
|
}
|
|
|
|
PackSingletons();
|
|
std::error_code ec;
|
|
fs::remove(db + ".eix", ec);
|
|
fs::remove(db + ".epk", ec);
|
|
{
|
|
CEterFileDict dict;
|
|
CEterPack pack;
|
|
if (!pack.Create(dict, db.c_str(), "", false))
|
|
{
|
|
std::fprintf(stderr, "cannot create %s.eix/.epk\n", db.c_str());
|
|
return 1;
|
|
}
|
|
for (const auto& [name, path] : files)
|
|
{
|
|
const std::vector<char> data = read_file(path);
|
|
if (name.size() > FILENAME_MAX_LEN || !pack.Put(name.c_str(), data.data(), long(data.size()), COMPRESSED_TYPE_COMPRESS))
|
|
{
|
|
std::fprintf(stderr, "cannot put %s\n", name.c_str());
|
|
return 1;
|
|
}
|
|
}
|
|
if (!pack.EncryptIndexFile())
|
|
return 1;
|
|
}
|
|
|
|
// Read back as the client does.
|
|
CEterFileDict dict;
|
|
CEterPack pack;
|
|
if (!pack.Create(dict, db.c_str(), prefix.c_str(), true))
|
|
{
|
|
std::fprintf(stderr, "cannot reopen %s\n", db.c_str());
|
|
return 1;
|
|
}
|
|
int failures = 0;
|
|
for (const auto& [name, path] : files)
|
|
{
|
|
const std::vector<char> want = read_file(path);
|
|
CMappedFile mapped;
|
|
LPCVOID data = nullptr;
|
|
if (!pack.Get(mapped, name.c_str(), &data) || mapped.Size() != want.size() ||
|
|
(!want.empty() && std::memcmp(data, want.data(), want.size()) != 0))
|
|
{
|
|
std::fprintf(stderr, "read-back mismatch: %s\n", name.c_str());
|
|
++failures;
|
|
}
|
|
}
|
|
if (failures)
|
|
return 1;
|
|
std::printf("%s: %zu files, COMPRESS, encrypted index\n", db.c_str(), files.size());
|
|
return 0;
|
|
}
|