Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
#include "eterpack/types.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <span>
|
||||
#include <memory>
|
||||
#include <fstream>
|
||||
|
||||
namespace eterpack {
|
||||
|
||||
class EterPackArchive {
|
||||
public:
|
||||
EterPackArchive();
|
||||
~EterPackArchive();
|
||||
|
||||
// Open from file path (e.g., "pack/root" or "root.eix")
|
||||
bool open(const std::string& base_or_eix_path,
|
||||
const uint32_t index_key[4] = DEFAULT_ETERPACK_KEY,
|
||||
const uint32_t security_key[4] = DEFAULT_SECURITY_KEY);
|
||||
|
||||
// Open from memory buffers (for tests and embedded use)
|
||||
bool open_memory(std::span<const uint8_t> eix_data,
|
||||
std::span<const uint8_t> epk_data,
|
||||
const uint32_t index_key[4] = DEFAULT_ETERPACK_KEY,
|
||||
const uint32_t security_key[4] = DEFAULT_SECURITY_KEY);
|
||||
|
||||
void close();
|
||||
|
||||
bool is_open() const { return m_is_open; }
|
||||
const std::string& get_name() const { return m_archive_name; }
|
||||
size_t get_entry_count() const { return m_entries.size(); }
|
||||
const std::vector<EterPackIndexEntry>& get_entries() const { return m_entries; }
|
||||
|
||||
bool has_file(std::string_view filename) const;
|
||||
bool has_file(uint32_t crc) const;
|
||||
|
||||
const EterPackIndexEntry* find_entry(std::string_view filename) const;
|
||||
const EterPackIndexEntry* find_entry(uint32_t crc) const;
|
||||
|
||||
bool extract(std::string_view filename, std::vector<uint8_t>& out_data) const;
|
||||
bool extract(const EterPackIndexEntry& entry, std::vector<uint8_t>& out_data) const;
|
||||
|
||||
// Pack creator / writer helper (writes .eix and .epk pair)
|
||||
static bool create_archive(const std::string& base_path,
|
||||
const std::vector<std::pair<std::string, std::vector<uint8_t>>>& files,
|
||||
bool encrypt_index = true,
|
||||
const uint32_t index_key[4] = DEFAULT_ETERPACK_KEY,
|
||||
CompressionType comp_type = CompressionType::Compress,
|
||||
const uint32_t security_key[4] = DEFAULT_SECURITY_KEY);
|
||||
|
||||
private:
|
||||
bool parse_index_buffer(std::span<const uint8_t> eix_bytes, const uint32_t index_key[4]);
|
||||
std::string resolve_case_insensitive(const std::string& path);
|
||||
|
||||
std::string m_archive_name;
|
||||
std::string m_epk_path;
|
||||
bool m_is_open = false;
|
||||
uint32_t m_security_key[4] = {0};
|
||||
|
||||
// Stored either as file or in-memory buffer
|
||||
std::vector<uint8_t> m_epk_memory;
|
||||
mutable std::ifstream m_epk_file;
|
||||
|
||||
std::vector<EterPackIndexEntry> m_entries;
|
||||
std::unordered_map<uint32_t, size_t> m_crc_map;
|
||||
std::unordered_map<std::string, size_t> m_name_map;
|
||||
};
|
||||
|
||||
} // namespace eterpack
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <string_view>
|
||||
#include <span>
|
||||
|
||||
namespace eterpack {
|
||||
|
||||
// Standard Metin2 / IEEE 802.3 CRC32 (polynomial 0xEDB88320)
|
||||
uint32_t GetCRC32(const char* buf, size_t len);
|
||||
uint32_t GetCaseCRC32(const char* buf, size_t len);
|
||||
|
||||
inline uint32_t GetCRC32(std::string_view str) {
|
||||
return GetCRC32(str.data(), str.size());
|
||||
}
|
||||
|
||||
inline uint32_t GetCaseCRC32(std::string_view str) {
|
||||
return GetCaseCRC32(str.data(), str.size());
|
||||
}
|
||||
|
||||
inline uint32_t GetCRC32(std::span<const uint8_t> data) {
|
||||
return GetCRC32(reinterpret_cast<const char*>(data.data()), data.size());
|
||||
}
|
||||
|
||||
} // namespace eterpack
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "eterpack/types.h"
|
||||
#include "eterpack/tea.h"
|
||||
#include "eterpack/crc32.h"
|
||||
#include "eterpack/lzo.h"
|
||||
#include "eterpack/archive.h"
|
||||
#include "eterpack/vfs.h"
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "eterpack/types.h"
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <optional>
|
||||
#include <cstdint>
|
||||
|
||||
namespace eterpack {
|
||||
|
||||
class LZO {
|
||||
public:
|
||||
static LZO& instance();
|
||||
|
||||
// Decompress an MCOZ formatted buffer (with optional TEA decryption)
|
||||
bool decompress_mcoz(std::span<const uint8_t> input,
|
||||
std::vector<uint8_t>& output,
|
||||
const uint32_t key[4] = nullptr);
|
||||
|
||||
// Compress data into MCOZ format (with optional TEA encryption)
|
||||
bool compress_mcoz(std::span<const uint8_t> input,
|
||||
std::vector<uint8_t>& output,
|
||||
const uint32_t key[4] = nullptr);
|
||||
|
||||
// Raw LZO1X decompression
|
||||
bool raw_decompress(const uint8_t* in, size_t in_len,
|
||||
uint8_t* out, size_t out_len);
|
||||
|
||||
// Raw LZO1X-1 compression
|
||||
bool raw_compress(const uint8_t* in, size_t in_len,
|
||||
std::vector<uint8_t>& out);
|
||||
|
||||
private:
|
||||
LZO();
|
||||
~LZO();
|
||||
void* m_workmem = nullptr;
|
||||
};
|
||||
|
||||
} // namespace eterpack
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
namespace eterpack {
|
||||
|
||||
// Low-level TEA word routines (32 rounds, 128-bit key)
|
||||
void tea_code(uint32_t sz, uint32_t sy, const uint32_t key[4], uint32_t dest[2]);
|
||||
void tea_decode(uint32_t sz, uint32_t sy, const uint32_t key[4], uint32_t dest[2]);
|
||||
|
||||
// Encrypt/decrypt buffer of 32-bit words with 8-byte alignment padding
|
||||
int tea_encrypt(uint32_t* dest, const uint32_t* src, const uint32_t key[4], int size);
|
||||
int tea_decrypt(uint32_t* dest, const uint32_t* src, const uint32_t key[4], int size);
|
||||
|
||||
// Modern C++ span/vector helpers
|
||||
std::vector<uint8_t> tea_encrypt_bytes(std::span<const uint8_t> data, const uint32_t key[4]);
|
||||
std::vector<uint8_t> tea_decrypt_bytes(std::span<const uint8_t> data, const uint32_t key[4]);
|
||||
|
||||
} // namespace eterpack
|
||||
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <string_view>
|
||||
|
||||
namespace eterpack {
|
||||
|
||||
inline constexpr uint32_t FOURCC_EPKD = 0x444b5045; // 'E', 'P', 'K', 'D'
|
||||
inline constexpr uint32_t FOURCC_MCOZ = 0x5a4f434d; // 'M', 'C', 'O', 'Z'
|
||||
inline constexpr uint32_t ETERPACK_VERSION = 2;
|
||||
inline constexpr size_t ETERPACK_HEADER_SIZE = 12;
|
||||
|
||||
inline constexpr size_t FILENAME_MAX_LEN = 160;
|
||||
|
||||
// Standard Metin2 encryption keys
|
||||
inline constexpr uint32_t DEFAULT_ETERPACK_KEY[4] = {
|
||||
45129401, 92367215, 681285731, 1710201
|
||||
};
|
||||
|
||||
inline constexpr uint32_t DEFAULT_SECURITY_KEY[4] = {
|
||||
78952482, 527348324, 1632942, 486274726
|
||||
};
|
||||
|
||||
inline constexpr uint32_t DEFAULT_ITEM_PROTO_KEY[4] = {
|
||||
173217, 72619434, 408587239, 27973291
|
||||
};
|
||||
|
||||
inline constexpr uint32_t DEFAULT_MOB_PROTO_KEY[4] = {
|
||||
4813894, 18955, 552631, 6822045
|
||||
};
|
||||
|
||||
enum class CompressionType : uint8_t {
|
||||
None = 0,
|
||||
Compress = 1,
|
||||
Security = 2,
|
||||
Panama = 3,
|
||||
HybridCrypt = 4,
|
||||
HybridCryptWithSDB = 5
|
||||
};
|
||||
|
||||
#pragma pack(push, 4)
|
||||
struct EterPackIndexEntry {
|
||||
int32_t id = 0; // 0..3
|
||||
char filename[FILENAME_MAX_LEN + 1]; // 4..164 (161 bytes)
|
||||
// 3 bytes padding (165..167)
|
||||
uint32_t filename_crc = 0; // 168..171
|
||||
int32_t real_data_size = 0; // 172..175
|
||||
int32_t data_size = 0; // 176..179
|
||||
uint32_t data_crc = 0; // 180..183
|
||||
int32_t data_position = 0; // 184..187
|
||||
uint8_t compressed_type = 0; // 188
|
||||
// 3 bytes padding (189..191)
|
||||
};
|
||||
|
||||
struct LZHeader {
|
||||
uint32_t fourcc = FOURCC_MCOZ;
|
||||
uint32_t encrypt_size = 0;
|
||||
uint32_t compressed_size = 0;
|
||||
uint32_t real_size = 0;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
static_assert(sizeof(EterPackIndexEntry) == 192, "EterPackIndexEntry must be 192 bytes");
|
||||
static_assert(sizeof(LZHeader) == 16, "LZHeader must be 16 bytes");
|
||||
|
||||
} // namespace eterpack
|
||||
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include "eterpack/archive.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
|
||||
namespace eterpack {
|
||||
|
||||
enum class VFSSearchMode {
|
||||
PackFirst,
|
||||
DiskFirst
|
||||
};
|
||||
|
||||
class EterPackVFS {
|
||||
public:
|
||||
EterPackVFS();
|
||||
~EterPackVFS();
|
||||
|
||||
void set_search_mode(VFSSearchMode mode) { m_search_mode = mode; }
|
||||
VFSSearchMode get_search_mode() const { return m_search_mode; }
|
||||
|
||||
// Mount an individual archive (e.g., "pack/root")
|
||||
bool mount(const std::string& base_path,
|
||||
const uint32_t index_key[4] = DEFAULT_ETERPACK_KEY,
|
||||
const uint32_t security_key[4] = DEFAULT_SECURITY_KEY,
|
||||
bool override_existing = false);
|
||||
|
||||
// Mount all packs listed in a pack index file (e.g., "pack/Index")
|
||||
bool mount_index_file(const std::string& index_file_path,
|
||||
const std::string& pack_dir = "pack");
|
||||
|
||||
// Clear all mounted archives
|
||||
void clear();
|
||||
|
||||
size_t get_archive_count() const;
|
||||
size_t get_total_entry_count() const;
|
||||
|
||||
bool exists(std::string_view path) const;
|
||||
bool read_file(std::string_view path, std::vector<uint8_t>& out_data) const;
|
||||
|
||||
// Direct lookup by CRC32
|
||||
bool exists_crc(uint32_t crc) const;
|
||||
bool read_file_crc(uint32_t crc, std::vector<uint8_t>& out_data) const;
|
||||
|
||||
std::vector<std::string> list_files() const;
|
||||
|
||||
private:
|
||||
struct FileLocation {
|
||||
size_t archive_index = 0;
|
||||
const EterPackIndexEntry* entry = nullptr;
|
||||
};
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
VFSSearchMode m_search_mode = VFSSearchMode::PackFirst;
|
||||
std::vector<std::unique_ptr<EterPackArchive>> m_archives;
|
||||
std::unordered_map<std::string, FileLocation> m_file_map;
|
||||
std::unordered_map<uint32_t, FileLocation> m_crc_map;
|
||||
};
|
||||
|
||||
using VFS = EterPackVFS;
|
||||
|
||||
} // namespace eterpack
|
||||
Reference in New Issue
Block a user