Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b7ad88

Browse files
vtfpp: add callback for pack files that require a decryption key
1 parent 9516bd3 commit 9b7ad88

File tree

11 files changed

+112
-52
lines changed

11 files changed

+112
-52
lines changed

include/vpkpp/PackFile.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ constexpr std::string_view EXECUTABLE_EXTENSION2 = ".x86_64"; // | Godot
2424

2525
class PackFile {
2626
public:
27+
enum class OpenProperty {
28+
DECRYPTION_KEY, // The pack file is encrypted
29+
};
30+
31+
// Accepts the pack file GUID and request property, returns the requested value
32+
using OpenPropertyRequest = std::function<std::vector<std::byte>(std::string_view guid, OpenProperty property)>;
33+
2734
/// Accepts the entry's path and metadata
2835
template<typename R>
2936
using EntryCallbackBase = std::function<R(const std::string& path, const Entry& entry)>;
@@ -44,7 +51,7 @@ class PackFile {
4451
virtual ~PackFile() = default;
4552

4653
/// Open a generic pack file. The parser is selected based on the file extension
47-
[[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, const EntryCallback& callback = nullptr);
54+
[[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, const EntryCallback& callback = nullptr, const OpenPropertyRequest& requestProperty = nullptr);
4855

4956
/// Returns a sorted list of supported extensions for opening, e.g. {".bsp", ".vpk"}
5057
[[nodiscard]] static std::vector<std::string> getOpenableExtensions();
@@ -116,7 +123,7 @@ class PackFile {
116123
void addDirectory(const std::string& entryBaseDir, const std::string& dir, EntryOptions options = {});
117124

118125
/// Adds new entries using the contents of a given directory
119-
void addDirectory(const std::string& entryBaseDir, const std::string& dir, const EntryCreation& creation);
126+
void addDirectory(const std::string& entryBaseDir_, const std::string& dir, const EntryCreation& creation);
120127

121128
/// Rename an existing entry
122129
virtual bool renameEntry(const std::string& oldPath_, const std::string& newPath_); // NOLINT(*-use-nodiscard)
@@ -210,10 +217,13 @@ class PackFile {
210217

211218
[[nodiscard]] static std::optional<std::vector<std::byte>> readUnbakedEntry(const Entry& entry);
212219

213-
using OpenFactoryFunction = std::function<std::unique_ptr<PackFile>(const std::string& path, const EntryCallback& callback)>;
220+
using OpenFactoryFunctionBasic = std::function<std::unique_ptr<PackFile>(const std::string& path, const EntryCallback& callback)>;
221+
using OpenFactoryFunction = std::function<std::unique_ptr<PackFile>(const std::string& path, const EntryCallback& callback, const OpenPropertyRequest& requestProperty)>;
214222

215223
static std::unordered_map<std::string, std::vector<OpenFactoryFunction>>& getOpenExtensionRegistry();
216224

225+
static const OpenFactoryFunction& registerOpenExtensionForTypeFactory(std::string_view extension, const OpenFactoryFunctionBasic& factory);
226+
217227
static const OpenFactoryFunction& registerOpenExtensionForTypeFactory(std::string_view extension, const OpenFactoryFunction& factory);
218228

219229
std::string fullFilePath;

include/vpkpp/format/GCF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class GCF : public PackFileReadOnly {
110110
#pragma pack(pop)
111111

112112
public:
113-
[[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, const EntryCallback& callback = nullptr);
113+
[[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, const EntryCallback& callback = nullptr, const OpenPropertyRequest& requestProperty = nullptr);
114114

115115
static constexpr inline std::string_view GUID = "0C088488F666451E9361297528F2446D";
116116

lang/c/include/sourceppc/Convert.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ sourcepp_buffer_t toBuffer(const std::vector<T>& vec) {
2727
return buf;
2828
}
2929

30+
template<typename T>
31+
requires (std::is_trivially_copyable_v<T> && !std::is_pointer_v<T>)
32+
std::vector<T> fromBuffer(const sourcepp_buffer_t& buffer) {
33+
if (buffer.size % sizeof(T) != 0) {
34+
return {};
35+
}
36+
std::vector<T> vec(buffer.size / sizeof(T));
37+
std::memcpy(vec.data(), buffer.data, buffer.size / sizeof(T));
38+
return vec;
39+
}
40+
3041
sourcepp_string_t toString(std::string_view str);
3142

3243
sourcepp_string_array_t toStringArray(const std::vector<std::string>& stringVec);

lang/c/include/vpkppc/PackFile.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ extern "C" {
1313

1414
typedef void* vpkpp_pack_file_handle_t;
1515

16+
typedef enum {
17+
VPKPP_PACK_FILE_OPEN_PROPERTY_DECRYPTION_KEY,
18+
} vpkpp_pack_file_open_property_e;
19+
20+
typedef sourcepp_buffer_t(*vpkpp_pack_file_open_property_request_t)(const char* guid, vpkpp_pack_file_open_property_e property);
21+
1622
typedef void(*vpkpp_entry_callback_t)(const char* path, vpkpp_entry_handle_t entry);
1723

1824
typedef int(*vpkpp_entry_predicate_t)(const char* path, vpkpp_entry_handle_t entry);
@@ -24,7 +30,7 @@ typedef vpkpp_entry_options_t(*vpkpp_entry_creation_t)(const char* path);
2430
#endif
2531

2632
// REQUIRES MANUAL FREE: vpkpp_close
27-
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_open(const char* path, vpkpp_entry_callback_t callback /*= NULL*/);
33+
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_open(const char* path, vpkpp_entry_callback_t callback /*= NULL*/, vpkpp_pack_file_open_property_request_t requestProperty /*= NULL*/);
2834

2935
// REQUIRES MANUAL FREE: sourcepp_string_array_free
3036
SOURCEPP_API sourcepp_string_array_t vpkpp_get_openable_extensions();

lang/c/include/vpkppc/format/GCF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "../PackFile.h"
44

55
// REQUIRES MANUAL FREE: vpkpp_close
6-
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_gcf_open(const char* path, vpkpp_entry_callback_t callback);
6+
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_gcf_open(const char* path, vpkpp_entry_callback_t callback, vpkpp_pack_file_open_property_request_t requestProperty);
77

88
// REQUIRES MANUAL FREE: sourcepp_string_free
99
SOURCEPP_API sourcepp_string_t vpkpp_gcf_guid(vpkpp_pack_file_handle_t handle);

lang/c/src/vpkppc/PackFile.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313
using namespace vpkpp;
1414

15-
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_open(const char* path, vpkpp_entry_callback_t callback) {
15+
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_open(const char* path, vpkpp_entry_callback_t callback, vpkpp_pack_file_open_property_request_t requestProperty) {
1616
SOURCEPP_EARLY_RETURN_VAL(path, nullptr);
1717

1818
auto packFile = PackFile::open(path, callback ? [callback](const std::string& path, const Entry& entry) {
1919
callback(path.c_str(), const_cast<Entry*>(&entry));
20-
} : static_cast<PackFile::EntryCallback>(nullptr));
20+
} : static_cast<PackFile::EntryCallback>(nullptr), requestProperty ? [requestProperty](std::string_view guid, PackFile::OpenProperty property) {
21+
return Convert::fromBuffer<std::byte>(requestProperty(guid.data(), static_cast<vpkpp_pack_file_open_property_e>(property)));
22+
} : static_cast<PackFile::OpenPropertyRequest>(nullptr));
2123
if (!packFile) {
2224
return nullptr;
2325
}

lang/c/src/vpkppc/format/GCF.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
using namespace vpkpp;
99

10-
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_gcf_open(const char* path, vpkpp_entry_callback_t callback) {
10+
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_gcf_open(const char* path, vpkpp_entry_callback_t callback, vpkpp_pack_file_open_property_request_t requestProperty) {
1111
SOURCEPP_EARLY_RETURN_VAL(path, nullptr);
1212

1313
auto packFile = GCF::open(path, callback ? [callback](const std::string& path, const Entry& entry) {
1414
callback(path.c_str(), const_cast<Entry*>(&entry));
15-
} : static_cast<PackFile::EntryCallback>(nullptr));
15+
} : static_cast<PackFile::EntryCallback>(nullptr), requestProperty ? [requestProperty](std::string_view guid, PackFile::OpenProperty property) {
16+
return Convert::fromBuffer<std::byte>(requestProperty(guid.data(), static_cast<vpkpp_pack_file_open_property_e>(property)));
17+
} : static_cast<PackFile::OpenPropertyRequest>(nullptr));
1618
if (!packFile) {
1719
return nullptr;
1820
}

lang/csharp/src/vpkpp/Format/GCF.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal static unsafe partial class Extern
1010
internal static unsafe partial class GCF
1111
{
1212
[LibraryImport("sourcepp_vpkppc", EntryPoint = "vpkpp_gcf_open")]
13-
public static partial void* Open([MarshalAs(UnmanagedType.LPStr)] string path, IntPtr callback);
13+
public static partial void* Open([MarshalAs(UnmanagedType.LPStr)] string path, IntPtr callback, IntPtr requestProperty);
1414

1515
[LibraryImport("sourcepp_vpkppc", EntryPoint = "vpkpp_gcf_guid")]
1616
public static partial sourcepp.String GUID();
@@ -30,15 +30,15 @@ private protected unsafe GCF(void* handle) : base(handle) {}
3030
}
3131
}
3232

33-
public new static GCF? Open(string path, EntryCallback callback)
33+
public new static GCF? Open(string path, EntryCallback callback, OpenPropertyRequest requestProperty)
3434
{
3535
unsafe
3636
{
3737
EntryCallbackNative callbackNative = (path, entry) =>
3838
{
3939
callback(path, new Entry(entry, true));
4040
};
41-
var handle = Extern.GCF.Open(path, Marshal.GetFunctionPointerForDelegate(callbackNative));
41+
var handle = Extern.GCF.Open(path, Marshal.GetFunctionPointerForDelegate(callbackNative), Marshal.GetFunctionPointerForDelegate(requestProperty));
4242
return handle == null ? null : new GCF(handle);
4343
}
4444
}

lang/csharp/src/vpkpp/PackFile.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
namespace vpkpp
77
{
8+
public enum OpenProperty
9+
{
10+
DECRYPTION_KEY = 0,
11+
}
12+
13+
using OpenPropertyRequest = Func<string, OpenProperty, Buffer>
14+
815
using EntryCallback = Action<string, Entry>;
916

1017
using EntryPredicate = Func<string, Entry, bool>;
@@ -20,7 +27,7 @@ namespace vpkpp
2027
internal static unsafe partial class Extern
2128
{
2229
[LibraryImport("sourcepp_vpkppc", EntryPoint = "vpkpp_open")]
23-
public static partial void* Open([MarshalAs(UnmanagedType.LPStr)] string path, IntPtr callback);
30+
public static partial void* Open([MarshalAs(UnmanagedType.LPStr)] string path, IntPtr callback, IntPtr requestProperty);
2431

2532
[LibraryImport("sourcepp_vpkppc", EntryPoint = "vpkpp_get_openable_extensions")]
2633
public static partial sourcepp.StringArray GetOpenableExtensions();
@@ -171,7 +178,7 @@ private protected unsafe PackFile(void* handle)
171178
{
172179
unsafe
173180
{
174-
var handle = Extern.Open(path, 0);
181+
var handle = Extern.Open(path, 0, 0);
175182
return handle == null ? null : new PackFile(handle);
176183
}
177184
}
@@ -184,7 +191,20 @@ private protected unsafe PackFile(void* handle)
184191
{
185192
callback(path, new Entry(entry, true));
186193
};
187-
var handle = Extern.Open(path, Marshal.GetFunctionPointerForDelegate(callbackNative));
194+
var handle = Extern.Open(path, Marshal.GetFunctionPointerForDelegate(callbackNative), 0);
195+
return handle == null ? null : new PackFile(handle);
196+
}
197+
}
198+
199+
public static PackFile? Open(string path, EntryCallback callback, OpenPropertyRequest requestProperty)
200+
{
201+
unsafe
202+
{
203+
EntryCallbackNative callbackNative = (path, entry) =>
204+
{
205+
callback(path, new Entry(entry, true));
206+
};
207+
var handle = Extern.Open(path, Marshal.GetFunctionPointerForDelegate(callbackNative), Marshal.GetFunctionPointerForDelegate(requestProperty));
188208
return handle == null ? null : new PackFile(handle);
189209
}
190210
}

0 commit comments

Comments
 (0)