cpk.hpp (2512B)
1 #ifndef UUID_6D829400_DD15_49CC_B55F_6F2565105828 2 #define UUID_6D829400_DD15_49CC_B55F_6F2565105828 3 #pragma once 4 5 #include <boost/filesystem/path.hpp> 6 #include <vector> 7 8 #include <libshit/except.hpp> 9 10 #include "../source.hpp" 11 12 #define WIN32_LEAN_AND_MEAN 13 #define NOMINMAX 14 #include <windows.h> 15 16 namespace Neptools 17 { 18 19 #define GEN_FWD(name, fld) \ 20 template <typename... Args> inline auto name(Args&&... args) \ 21 { return (this->*fld)(std::forward<Args>(args)...); } 22 23 struct PakEntry 24 { 25 unsigned field_000; 26 unsigned file_index; 27 char file_name[260]; 28 unsigned field_10c; 29 size_t compressed_size; 30 size_t uncompressed_size; 31 unsigned is_compressed; 32 unsigned field_11c; 33 }; 34 static_assert(sizeof(PakEntry) == 0x120); 35 36 struct CpkHandlerFileInfo 37 { 38 unsigned index; 39 HANDLE handle; 40 bool is_valid; 41 PakEntry entry; 42 unsigned data_start; 43 unsigned read_pos; 44 void* huffmann_hdr; 45 void* block; 46 int decoded_block_index; 47 }; 48 static_assert(sizeof(CpkHandlerFileInfo) == 0x140); 49 50 struct CpkHandler 51 { 52 unsigned vect0_begin, vect0_end, vect0_capacity; 53 using FileInfo = CpkHandlerFileInfo; 54 std::vector<FileInfo*> entry_vect; 55 unsigned vect2_begin, vect2_end, vect2_capacity; 56 char* data; 57 char* hash_entries; 58 char* names; 59 char* basename; 60 unsigned last_error; 61 CRITICAL_SECTION crit_sec; 62 63 char __thiscall OpenFile(const char* fname, size_t* out); 64 char __thiscall CloseFile(unsigned index); 65 char __thiscall Read(unsigned index, char* dst, size_t dst_size, 66 size_t* out_size_read); 67 68 using OpenFilePtr = decltype(&CpkHandler::OpenFile); 69 using CloseFilePtr = decltype(&CpkHandler::CloseFile); 70 using ReadPtr = decltype(&CpkHandler::Read); 71 72 static CpkHandler::OpenFilePtr orig_open_file; 73 static CpkHandler::CloseFilePtr orig_close_file; 74 static CpkHandler::ReadPtr orig_read; 75 76 GEN_FWD(OrigOpenFile, orig_open_file); 77 GEN_FWD(OrigCloseFile, orig_close_file); 78 GEN_FWD(OrigRead, orig_read); 79 80 static void Init(); 81 82 Source GetSource(const char* fname); 83 84 private: 85 FileInfo& GetEntryVect(size_t* out); 86 bool OpenFsFile( 87 const char* fname, const boost::filesystem::path& pth, size_t* out); 88 bool OpenTxtFile( 89 const char* fname, const boost::filesystem::path& pth, size_t* out); 90 }; 91 static_assert(sizeof(CpkHandler) == 0x50); 92 93 LIBSHIT_GEN_EXCEPTION_TYPE(CpkError, std::runtime_error); 94 95 } 96 #endif