5 Commits
Author SHA1 Message Date
Deorder 7acca3d47d Fixed memory corruption in bsarch by zeroing allocated memory
Added test for fo4 dds
2019-09-02 23:56:27 +02:00
Deorder 0f114677b1 Added context to FO4 DDS info callback 2019-08-28 14:39:10 +02:00
Deorder f276cd1825 DDS Info callback was missing a pointer for returning the info 2019-08-17 00:30:14 +02:00
Deorder c68e39d2a1 Added way to add files from any source path (without using a shared/common root)
bsa_add_file_from_disk is now bsa_add_file_from_disk_root
Fixed issues with using libbsarch with GCC
2019-05-08 23:48:46 +02:00
Deorder d3d36d2694 Fixed issue with releasing file buffer result causing access violation while extracting 2019-05-01 00:17:42 +02:00
10 changed files with 352 additions and 150 deletions
+7
View File
@@ -0,0 +1,7 @@
// Hint files help the Visual Studio IDE interpret Visual C++ identifiers
// such as names of functions and macros.
// For more information see https://go.microsoft.com/fwlink/?linkid=865984
#define PACKED(datastructure) datastructure __attribute__((__packed__))
#define PACKED(datastructure) __pragma(pack(push, 1)) datastructure __pragma(pack(pop))
#define BSARCH_DLL_API(ReturnType) extern "C" __declspec(dllexport) ReturnType __stdcall
#define BSARCH_DLL_API(ReturnType) extern "C" __declspec(dllimport) ReturnType __stdcall
+160 -12
View File
@@ -1,26 +1,174 @@
#include <iostream>
#include <windows.h>
#include "libbsarch.h"
#pragma comment(lib, "libbsarch")
static const std::wstring separators(L"\\/");
std::wstring dirname(const std::wstring& path) {
size_t slash_pos = path.find_last_of(separators);
return path.substr(0, slash_pos);
}
std::wstring basename(const std::wstring& path) {
size_t slash_pos = path.find_last_of(separators);
return path.substr(slash_pos + 1);
}
bool mkdirp(const std::wstring& directory, bool basedir = true) {
DWORD attributes = ::GetFileAttributesW(directory.c_str());
if(attributes == INVALID_FILE_ATTRIBUTES) {
std::size_t slash_pos = directory.find_last_of(separators);
if(slash_pos != std::wstring::npos) {
mkdirp(directory.substr(0, slash_pos));
}
return ::CreateDirectoryW(directory.c_str(), nullptr);
} else {
return true;
}
}
void dds_info(bsa_archive_t archive, const wchar_t* file_path, bsa_dds_info_t* dds_info, void* context) {
// Mock result
dds_info->width = 2048;
dds_info->height = 2048;
dds_info->mipmaps = 12;
printf("ba2 dds_info callback file: %ls, context: %s\n", file_path, (char*)context);
}
int main() {
bsa_result_message_t result = { 0 };
bsa_archive_t archive = bsa_create();
bsa_result_message_t result = bsa_load_from_file(archive, L"test_read.bsa");
if(result.code < 0)
printf("%ls\n", result.text);
bsa_close(archive);
bsa_entry_list_t entries = bsa_entry_list_create();
bsa_entry_list_add(entries, L"textures\\grass\\test.dds");
{
result = bsa_load_from_file(archive, L"test_read.bsa");
if(result.code < 0)
printf("ba1 %ls\n", result.text);
bsa_create_archive(archive, L"test_write.bsa", baSSE, entries);
bsa_add_file_from_disk(archive, L"files", L"files\\textures\\grass\\test.dds");
bsa_save(archive);
bsa_close(archive);
bsa_entry_list_t entries = bsa_entry_list_create();
bsa_get_resource_list(archive, entries, L"");
bsa_entry_list_free(entries);
for(size_t index = 0; index < bsa_entry_list_count(entries); index++) {
wchar_t filename[2048];
wchar_t dest_filename[2048] = L"test_ba1\\";
bsa_entry_list_get(entries, index, 2048, filename);
wcscat_s(dest_filename, 2048, filename);
printf("ba1 file: %ls %ls\n", filename, dest_filename);
if(mkdirp(dirname(dest_filename))) {
result = bsa_extract_file(archive, filename, dest_filename);
if(result.code < 0)
printf("ba1 %ls\n", result.text);
} else {
printf("ba1 could not create: %ls\n", dirname(dest_filename).c_str());
}
}
bsa_entry_list_free(entries);
bsa_close(archive);
}
{
bsa_entry_list_t entries = bsa_entry_list_create();
bsa_entry_list_add(entries, L"textures\\grass\\test.dds");
bsa_entry_list_add(entries, L"textures\\grass\\test2.dds");
bsa_create_archive(archive, L"test_write.bsa", baSSE, entries);
bsa_add_file_from_disk_root(archive, L"test_ba1\\", L"test_ba1\\textures\\grass\\test.dds");
result = bsa_add_file_from_disk(archive, L"textures\\grass\\test2.dds", L"test_ba1\\textures\\grass\\test.dds");
if (result.code < 0)
printf("ba1 %ls\n", result.text);
bsa_save(archive);
bsa_close(archive);
bsa_entry_list_free(entries);
}
bsa_file_dds_info_callback_set(archive, dds_info, (void*)"I was shared");
{
result = bsa_load_from_file(archive, L"test_read.ba2");
if (result.code < 0)
printf("ba2 %ls\n", result.text);
bsa_entry_list_t entries = bsa_entry_list_create();
bsa_get_resource_list(archive, entries, L"");
for (size_t index = 0; index < bsa_entry_list_count(entries); index++) {
wchar_t filename[2048];
wchar_t dest_filename[2048] = L"test_ba2\\";
bsa_entry_list_get(entries, index, 2048, filename);
wcscat_s(dest_filename, 2048, filename);
printf("ba2 file: %ls %ls\n", filename, dest_filename);
if (mkdirp(dirname(dest_filename))) {
result = bsa_extract_file(archive, filename, dest_filename);
if (result.code < 0)
printf("ba2 %ls\n", result.text);
}
else {
printf("ba2 could not create: %ls\n", dirname(dest_filename).c_str());
}
}
bsa_entry_list_free(entries);
bsa_close(archive);
}
{
bsa_entry_list_t entries = bsa_entry_list_create();
bsa_entry_list_add(entries, L"textures\\grass\\test.dds");
bsa_entry_list_add(entries, L"textures\\grass\\test2.dds");
bsa_create_archive(archive, L"test_write.ba2", baFO4dds, entries);
bsa_add_file_from_disk_root(archive, L"test_ba2\\", L"test_ba2\\textures\\grass\\test.dds");
result = bsa_add_file_from_disk(archive, L"textures\\grass\\test2.dds", L"test_ba2\\textures\\grass\\test.dds");
if (result.code < 0)
printf("ba2 %ls\n", result.text);
bsa_save(archive);
bsa_close(archive);
bsa_entry_list_free(entries);
}
/*
{
result = bsa_load_from_file(archive, L"test_large.ba2");
if (result.code < 0)
printf("ba2 large %ls\n", result.text);
bsa_entry_list_t entries = bsa_entry_list_create();
bsa_get_resource_list(archive, entries, L"");
for (size_t index = 0; index < bsa_entry_list_count(entries); index++) {
wchar_t filename[2048];
wchar_t dest_filename[2048] = L"test_ba2_large\\";
bsa_entry_list_get(entries, index, 2048, filename);
wcscat_s(dest_filename, 2048, filename);
printf("ba2 file large: %ls %ls\n", filename, dest_filename);
if (mkdirp(dirname(dest_filename))) {
result = bsa_extract_file(archive, filename, dest_filename);
if (result.code < 0)
printf("ba2 large %ls\n", result.text);
}
else {
printf("ba2 large could not create: %ls\n", dirname(dest_filename).c_str());
}
}
}
*/
bsa_free(archive);
}
+13 -9
View File
@@ -32,11 +32,11 @@ BSARCH_DLL_API(bsa_result_message_t) bsa_free(bsa_archive_t archive) {
return { 0 };
}
BSARCH_DLL_API(bsa_result_message_t) bsa_load_from_file(bsa_archive_t archive, const wchar_t *filename) {
BSARCH_DLL_API(bsa_result_message_t) bsa_load_from_file(bsa_archive_t archive, const wchar_t *file_path) {
return { 0 };
}
BSARCH_DLL_API(bsa_result_message_t) bsa_create_archive(bsa_archive_t archive, const wchar_t *filename, bsa_archive_type_t archive_type, bsa_entry_list_t entry_list) {
BSARCH_DLL_API(bsa_result_message_t) bsa_create_archive(bsa_archive_t archive, const wchar_t *file_path, bsa_archive_type_t archive_type, bsa_entry_list_t entry_list) {
return { 0 };
}
@@ -44,15 +44,19 @@ BSARCH_DLL_API(bsa_result_message_t) bsa_save(bsa_archive_t archive) {
return { 0 };
}
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_disk(bsa_archive_t archive, const wchar_t *root_dir, const wchar_t *filename) {
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_disk(bsa_archive_t archive, const wchar_t *file_path, const wchar_t *source_path) {
return { 0 };
}
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_memory(bsa_archive_t archive, const wchar_t *filename, uint32_t size, bsa_buffer_t data) {
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_disk_root(bsa_archive_t archive, const wchar_t* root_dir, const wchar_t* source_path) {
return { 0 };
}
BSARCH_DLL_API(bsa_file_record_t) bsa_find_file_record(bsa_archive_t archive, const wchar_t *filename) {
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_memory(bsa_archive_t archive, const wchar_t *file_path, uint32_t size, bsa_buffer_t data) {
return { 0 };
}
BSARCH_DLL_API(bsa_file_record_t) bsa_find_file_record(bsa_archive_t archive, const wchar_t *file_path) {
return NULL;
}
@@ -60,7 +64,7 @@ BSARCH_DLL_API(bsa_result_message_buffer_t) bsa_extract_file_data_by_record(bsa_
return { 0 };
}
BSARCH_DLL_API(bsa_result_message_buffer_t) bsa_extract_file_data_by_filename(bsa_archive_t archive, const wchar_t *filename) {
BSARCH_DLL_API(bsa_result_message_buffer_t) bsa_extract_file_data_by_filename(bsa_archive_t archive, const wchar_t *file_path) {
return { 0 };
}
@@ -68,7 +72,7 @@ BSARCH_DLL_API(bsa_result_message_t) bsa_file_data_free(bsa_archive_t archive, b
return { 0 };
}
BSARCH_DLL_API(bsa_result_message_t) bsa_extract_file(bsa_archive_t archive, const wchar_t *filename, const wchar_t *save_as) {
BSARCH_DLL_API(bsa_result_message_t) bsa_extract_file(bsa_archive_t archive, const wchar_t *file_path, const wchar_t *save_as) {
return { 0 };
}
@@ -76,7 +80,7 @@ BSARCH_DLL_API(bsa_result_message_t) bsa_iterate_files(bsa_archive_t archive, bs
return { 0 };
}
BSARCH_DLL_API(bool) bsa_file_exists(bsa_archive_t archive, const wchar_t *filename) {
BSARCH_DLL_API(bool) bsa_file_exists(bsa_archive_t archive, const wchar_t *file_path) {
return false;
}
@@ -144,6 +148,6 @@ BSARCH_DLL_API(void) bsa_share_data_set(bsa_archive_t archive, bool flags) {
return;
}
BSARCH_DLL_API(void) bsa_file_dds_info_callback_set(bsa_archive_t archive, bsa_file_dds_info_proc_t file_dds_info_proc) {
BSARCH_DLL_API(void) bsa_file_dds_info_callback_set(bsa_archive_t archive, bsa_file_dds_info_proc_t file_dds_info_proc, void *context) {
return;
}
+1
View File
@@ -12,6 +12,7 @@ EXPORTS
bsa_create_archive
bsa_save
bsa_add_file_from_disk
bsa_add_file_from_disk_root
bsa_add_file_from_memory
bsa_find_file_record
bsa_extract_file_data_by_record
+31 -18
View File
@@ -137,22 +137,22 @@ begin
end;
end;
function bsa_load_from_file(obj: Pointer; const aFileName: PChar): TwbBSResultMessage; stdcall;
function bsa_load_from_file(obj: Pointer; const aFilePath: PChar): TwbBSResultMessage; stdcall;
begin
Result.code := Ord(BSA_RESULT_NONE);
try
TwbBSArchive(obj).LoadFromFile(String(aFileName));
TwbBSArchive(obj).LoadFromFile(String(aFilePath));
except
on E: Exception do
exception_handler(E, Result);
end;
end;
function bsa_create_archive(obj: Pointer; const aFileName: PChar; aType: TBSArchiveType; aFileList: TwbBSEntryList): TwbBSResultMessage; stdcall;
function bsa_create_archive(obj: Pointer; const aFilePath: PChar; aType: TBSArchiveType; aFileList: TwbBSEntryList): TwbBSResultMessage; stdcall;
begin
Result.code := Ord(BSA_RESULT_NONE);
try
TwbBSArchive(obj).CreateArchiveCompat(String(aFileName), aType, aFileList);
TwbBSArchive(obj).CreateArchiveCompat(String(aFilePath), aType, aFileList);
except
on E: Exception do
exception_handler(E, Result);
@@ -170,32 +170,43 @@ begin
end;
end;
function bsa_add_file_from_disk(obj: Pointer; const aRootDir, aFileName: PChar): TwbBSResultMessage; stdcall;
function bsa_add_file_from_disk_root(obj: Pointer; const aRootDir, aSourcePath: PChar): TwbBSResultMessage; stdcall;
begin
Result.code := Ord(BSA_RESULT_NONE);
try
TwbBSArchive(obj).AddFile(String(aRootDir), String(aFileName));
TwbBSArchive(obj).AddFileDiskRoot(String(aRootDir), String(aSourcePath));
except
on E: Exception do
exception_handler(E, Result);
end;
end;
function bsa_add_file_from_memory(obj: Pointer; const aFileName: PChar; const aSize: Cardinal; const aData: PByte): TwbBSResultMessage; stdcall;
function bsa_add_file_from_disk(obj: Pointer; const aFilePath, aSourcePath: PChar): TwbBSResultMessage; stdcall;
begin
Result.code := Ord(BSA_RESULT_NONE);
try
TwbBSArchive(obj).AddFileCompat(String(aFileName), aSize, aData);
TwbBSArchive(obj).AddFileDisk(String(aFilePath), String(aSourcePath));
except
on E: Exception do
exception_handler(E, Result);
end;
end;
function bsa_find_file_record(obj: Pointer; const aFileName: PChar): Pointer; stdcall;
function bsa_add_file_from_memory(obj: Pointer; const aFilePath: PChar; const aSize: Cardinal; const aData: PByte): TwbBSResultMessage; stdcall;
begin
Result.code := Ord(BSA_RESULT_NONE);
try
TwbBSArchive(obj).AddFileDataCompat(String(aFilePath), aSize, aData);
except
on E: Exception do
exception_handler(E, Result);
end;
end;
function bsa_find_file_record(obj: Pointer; const aFilePath: PChar): Pointer; stdcall;
begin
try
Result := TwbBSArchive(obj).FindFileRecord(String(aFileName));
Result := TwbBSArchive(obj).FindFileRecord(String(aFilePath));
except
Result := nil
end;
@@ -212,11 +223,11 @@ begin
end;
end;
function bsa_extract_file_data_by_filename(obj: Pointer; const aFileName: PChar): TwbBSResultMessageBuffer; stdcall;
function bsa_extract_file_data_by_filename(obj: Pointer; const aFilePath: PChar): TwbBSResultMessageBuffer; stdcall;
begin
Result.message.code := Ord(BSA_RESULT_NONE);
try
Result.buffer := TwbBSArchive(obj).ExtractFileDataCompat(String(aFileName));
Result.buffer := TwbBSArchive(obj).ExtractFileDataCompat(String(aFilePath));
except
on E: Exception do
buffer_exception_handler(E, Result);
@@ -234,11 +245,11 @@ begin
end;
end;
function bsa_extract_file(obj: Pointer; const aFileName, aSaveAs: PChar): TwbBSResultMessage; stdcall;
function bsa_extract_file(obj: Pointer; const aFilePath, aSaveAs: PChar): TwbBSResultMessage; stdcall;
begin
Result.code := Ord(BSA_RESULT_NONE);
try
TwbBSArchive(obj).ExtractFile(String(aFileName), String(aSaveAs));
TwbBSArchive(obj).ExtractFile(String(aFilePath), String(aSaveAs));
except
on E: Exception do
exception_handler(E, Result);
@@ -256,12 +267,12 @@ begin
end;
end;
function bsa_file_exists(obj: Pointer; const aFileName: string): Boolean; stdcall;
function bsa_file_exists(obj: Pointer; const aFilePath: string): Boolean; stdcall;
begin
try
Result := TwbBSArchive(obj).FileExists(aFileName);
Result := TwbBSArchive(obj).FileExists(aFilePath);
except
Result := False;
Result := False;
end;
end;
@@ -363,9 +374,10 @@ begin
TwbBSArchive(obj).ShareData := shareData;
end;
procedure bsa_file_dds_info_callback_set(obj: Pointer; aProc: TBSFileDDSInfoProcCompat); stdcall;
procedure bsa_file_dds_info_callback_set(obj: Pointer; aProc: TBSFileDDSInfoProcCompat; aContext: Pointer); stdcall;
begin
TwbBSArchive(obj).DDSInfoProc := aProc;
TwbBSArchive(obj).DDSInfoProcContext := aContext;
end;
exports
@@ -401,6 +413,7 @@ exports
bsa_extract_file_data_by_record,
bsa_find_file_record,
bsa_add_file_from_memory,
bsa_add_file_from_disk_root,
bsa_add_file_from_disk,
bsa_save,
bsa_create_archive,
+1
View File
@@ -157,6 +157,7 @@
<Platform value="Win32">True</Platform>
<Platform value="Win64">True</Platform>
</Platforms>
<ModelSupport>False</ModelSupport>
</BorlandProject>
<ProjectFileVersion>12</ProjectFileVersion>
</ProjectExtensions>
+28 -23
View File
@@ -34,32 +34,36 @@ typedef struct bsa_dds_info_s {
} bsa_dds_info_t;
PACKED (
typedef struct bsa_dds_header_s {
struct bsa_dds_header_s {
uint32_t magic; // DDS_MAGIC
struct DirectX::DDS_HEADER header;
} bsa_dds_header_t;
)
});
typedef struct bsa_dds_header_s bsa_dds_header_t;
PACKED (
typedef struct bsa_result_message_s {
struct bsa_result_message_s {
int8_t code; // bsa_result_code_t
wchar_t text[1024];
} bsa_result_message_t;
)
});
typedef struct bsa_result_message_s bsa_result_message_t;
PACKED (
typedef struct bsa_result_buffer_s {
struct bsa_result_buffer_s {
uint32_t size;
bsa_buffer_t data;
} bsa_result_buffer_t;
)
});
typedef struct bsa_result_buffer_s bsa_result_buffer_t;
PACKED (
typedef struct bsa_result_message_buffer_s {
struct bsa_result_message_buffer_s {
bsa_result_buffer_t buffer;
bsa_result_message_t message;
} bsa_result_message_buffer_t;
)
});
typedef struct bsa_result_message_buffer_s bsa_result_message_buffer_t;
typedef enum bsa_archive_state_e {
stReading, stWriting
@@ -69,8 +73,8 @@ typedef enum bsa_archive_type_e {
baNone, baTES3, baTES4, baFO3, baSSE, baFO4, baFO4dds
} bsa_archive_type_t;
typedef void (*bsa_file_dds_info_proc_t)(bsa_archive_t archive, const wchar_t *filename, bsa_dds_info_t dds_info);
typedef bool (*bsa_file_iteration_proc_t)(bsa_archive_t archive, const wchar_t *filename, bsa_file_record_t file_record, bsa_folder_record_t folder_record, void *context);
typedef void (*bsa_file_dds_info_proc_t)(bsa_archive_t archive, const wchar_t *file_path, bsa_dds_info_t *dds_info, void *context);
typedef bool (*bsa_file_iteration_proc_t)(bsa_archive_t archive, const wchar_t *file_path, bsa_file_record_t file_record, bsa_folder_record_t folder_record, void *context);
BSARCH_DLL_API(bsa_entry_list_t) bsa_entry_list_create();
BSARCH_DLL_API(bsa_result_message_t) bsa_entry_list_free(bsa_entry_list_t entry_list);
@@ -80,18 +84,19 @@ BSARCH_DLL_API(uint32_t) bsa_entry_list_get(bsa_entry_list_t entry_list, uint32_
BSARCH_DLL_API(bsa_archive_t) bsa_create();
BSARCH_DLL_API(bsa_result_message_t) bsa_free(bsa_archive_t archive);
BSARCH_DLL_API(bsa_result_message_t) bsa_load_from_file(bsa_archive_t archive, const wchar_t *filename);
BSARCH_DLL_API(bsa_result_message_t) bsa_create_archive(bsa_archive_t archive, const wchar_t *filename, bsa_archive_type_t archive_type, bsa_entry_list_t entry_list);
BSARCH_DLL_API(bsa_result_message_t) bsa_load_from_file(bsa_archive_t archive, const wchar_t *file_path);
BSARCH_DLL_API(bsa_result_message_t) bsa_create_archive(bsa_archive_t archive, const wchar_t *file_path, bsa_archive_type_t archive_type, bsa_entry_list_t entry_list);
BSARCH_DLL_API(bsa_result_message_t) bsa_save(bsa_archive_t archive);
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_disk(bsa_archive_t archive, const wchar_t *root_dir, const wchar_t *filename);
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_memory(bsa_archive_t archive, const wchar_t *filename, uint32_t size, bsa_buffer_t data);
BSARCH_DLL_API(bsa_file_record_t) bsa_find_file_record(bsa_archive_t archive, const wchar_t *filename);
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_disk(bsa_archive_t archive, const wchar_t *file_path, const wchar_t *source_path);
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_disk_root(bsa_archive_t archive, const wchar_t *root_dir, const wchar_t *source_path);
BSARCH_DLL_API(bsa_result_message_t) bsa_add_file_from_memory(bsa_archive_t archive, const wchar_t *file_path, uint32_t size, bsa_buffer_t data);
BSARCH_DLL_API(bsa_file_record_t) bsa_find_file_record(bsa_archive_t archive, const wchar_t *file_path);
BSARCH_DLL_API(bsa_result_message_buffer_t) bsa_extract_file_data_by_record(bsa_archive_t archive, bsa_file_record_t file_record);
BSARCH_DLL_API(bsa_result_message_buffer_t) bsa_extract_file_data_by_filename(bsa_archive_t archive, const wchar_t *filename);
BSARCH_DLL_API(bsa_result_message_buffer_t) bsa_extract_file_data_by_filename(bsa_archive_t archive, const wchar_t *file_path);
BSARCH_DLL_API(bsa_result_message_t) bsa_file_data_free(bsa_archive_t archive, bsa_result_buffer_t file_data_result);
BSARCH_DLL_API(bsa_result_message_t) bsa_extract_file(bsa_archive_t archive, const wchar_t *filename, const wchar_t *save_as);
BSARCH_DLL_API(bsa_result_message_t) bsa_extract_file(bsa_archive_t archive, const wchar_t *file_path, const wchar_t *save_as);
BSARCH_DLL_API(bsa_result_message_t) bsa_iterate_files(bsa_archive_t archive, bsa_file_iteration_proc_t file_iteration_proc, void *context);
BSARCH_DLL_API(bool) bsa_file_exists(bsa_archive_t archive, const wchar_t *filename);
BSARCH_DLL_API(bool) bsa_file_exists(bsa_archive_t archive, const wchar_t *file_path);
BSARCH_DLL_API(bsa_result_message_t) bsa_get_resource_list(bsa_archive_t archive, bsa_entry_list_t entry_result_list, const wchar_t *folder);
BSARCH_DLL_API(bsa_result_message_t) bsa_resolve_hash(bsa_archive_t archive, uint64_t hash, bsa_entry_list_t entry_result_list);
BSARCH_DLL_API(bsa_result_message_t) bsa_close(bsa_archive_t archive);
@@ -110,4 +115,4 @@ BSARCH_DLL_API(void) bsa_compress_set(bsa_archive_t archive, bool flags);
BSARCH_DLL_API(bool) bsa_share_data_get(bsa_archive_t archive);
BSARCH_DLL_API(void) bsa_share_data_set(bsa_archive_t archive, bool flags);
BSARCH_DLL_API(void) bsa_file_dds_info_callback_set(bsa_archive_t archive, bsa_file_dds_info_proc_t file_dds_info_proc);
BSARCH_DLL_API(void) bsa_file_dds_info_callback_set(bsa_archive_t archive, bsa_file_dds_info_proc_t file_dds_info_proc, void *context);
+1
View File
@@ -149,6 +149,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<None Include=".vscode\ipch\c1c5ff332820ca89\mmap_address.bin" />
<None Include="cpp.hint" />
<None Include="libbsarch.def" />
<None Include="tforge\TFL.inc" />
</ItemGroup>
+1
View File
@@ -24,6 +24,7 @@
<None Include=".vscode\ipch\c1c5ff332820ca89\mmap_address.bin">
<Filter>Resource Files</Filter>
</None>
<None Include="cpp.hint" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="DDS.h">
+109 -88
View File
@@ -193,12 +193,12 @@ type
TBSArchiveType = (baNone, baTES3, baTES4, baFO3, baSSE, baFO4, baFO4dds);
TBSArchiveState = (stReading, stWriting);
TBSArchiveStates = set of TBSArchiveState;
TBSFileIterationProcCompat = function(aArchive: Pointer; const aFileName: PChar;
TBSFileIterationProcCompat = function(aArchive: Pointer; const aFilePath: PChar;
aFileRecord: Pointer; aFolderRecord: Pointer; aContext: Pointer): Boolean; stdcall;
TDDSInfo = record Width, Height, MipMaps: Integer; end;
TBSFileDDSInfoProcCompat = procedure(aArchive: Pointer; const aFileName: PChar;
var aInfo: TDDSInfo); stdcall;
TBSFileDDSInfoProcCompat = procedure(aArchive: Pointer; const aFilePath: PChar;
var aInfo: TDDSInfo; aContext: Pointer); stdcall;
TwbBSHeaderTES3 = packed record
HashOffset: Cardinal;
@@ -308,7 +308,9 @@ type
fVersion: Cardinal;
fCompress: Boolean;
fShareData: Boolean;
fDDSInfoProc: TBSFileDDSInfoProcCompat;
fDDSInfoProcContext: Pointer;
fHeaderTES3: TwbBSHeaderTES3;
fFilesTES3: array of TwbBSFileTES3;
@@ -330,9 +332,9 @@ type
function GetArchiveFormatName: string;
function GetFileCount: Cardinal;
procedure SetArchiveFlags(aFlags: Cardinal);
function FindFileRecordTES3(const aFileName: string; var aFileIdx: Integer): Boolean;
function FindFileRecordTES4(const aFileName: string; var aFolderIdx, aFileIdx: Integer): Boolean;
function FindFileRecordFO4(const aFileName: string; var aFileIdx: Integer): Boolean;
function FindFileRecordTES3(const aFilePath: string; var aFileIdx: Integer): Boolean;
function FindFileRecordTES4(const aFilePath: string; var aFolderIdx, aFileIdx: Integer): Boolean;
function FindFileRecordFO4(const aFilePath: string; var aFileIdx: Integer): Boolean;
function GetDDSMipChunkNum(var aDDSInfo: TDDSInfo): Integer;
function CalcDataHash(aData: Pointer; aLen: Cardinal): TPackedDataHash;
function FindPackedData(aSize: Cardinal; aHash: TPackedDataHash; aFileRecord: Pointer): Boolean;
@@ -342,19 +344,20 @@ type
Sync: IReadWriteSync;
constructor Create;
destructor Destroy; override;
procedure LoadFromFile(const aFileName: string);
procedure CreateArchiveCompat(const aFileName: string; aType: TBSArchiveType;
procedure LoadFromFile(const aFilePath: string);
procedure CreateArchiveCompat(const aFilePath: string; aType: TBSArchiveType;
aFilesList: TwbBSEntryList = nil);
procedure Save;
procedure AddFile(const aRootDir, aFileName: string); overload;
procedure AddFileCompat(const aFileName: string; const aSize: Cardinal; const aData: PByte);
function FindFileRecord(const aFileName: string): Pointer;
procedure AddFileDisk(const aFilePath, aSourcePath: string);
procedure AddFileDiskRoot(const aRootDir, aSourcePath: string);
procedure AddFileDataCompat(const aFilePath: string; const aSize: Cardinal; const aData: PByte);
function FindFileRecord(const aFilePath: string): Pointer;
function ExtractFileDataCompat(aFileRecord: Pointer): TwbBSResultBuffer; overload;
function ExtractFileDataCompat(const aFileName: string): TwbBSResultBuffer; overload;
function ExtractFileDataCompat(const aFilePath: string): TwbBSResultBuffer; overload;
procedure ReleaseFileDataCompat(fileDataResult: TwbBSResultBuffer);
procedure ExtractFile(const aFileName, aSaveAs: string);
procedure ExtractFile(const aFilePath, aSaveAs: string);
procedure IterateFilesCompat(aProc: TBSFileIterationProcCompat; aContext: Pointer = nil);
function FileExists(const aFileName: string): Boolean;
function FileExists(const aFilePath: string): Boolean;
procedure ResourceListCompat(const aEntryResultList: TwbBSEntryList; aFolder: string = '');
procedure ResolveHashCompat(const aHash: UInt64; const aEntryResultList: TwbBSEntryList);
procedure Close;
@@ -369,13 +372,14 @@ type
property Compress: Boolean read fCompress write fCompress;
property ShareData: Boolean read fShareData write fShareData;
property DDSInfoProc: TBSFileDDSInfoProcCompat read fDDSInfoProc write fDDSInfoProc;
property DDSInfoProcContext: Pointer read fDDSInfoProcContext write fDDSInfoProcContext;
end;
function SplitDirName(const aFileName: string; var Dir, Name: string): Integer;
function SplitNameExt(const aFileName: string; var Name, Ext: string): Integer;
function CreateHashTES3(const aFileName: string): UInt64;
function CreateHashTES4(const aFileName: string): UInt64; overload;
function CreateHashFO4(const aFileName: string): Cardinal;
function SplitDirName(const aFilePath: string; var Dir, Name: string): Integer;
function SplitNameExt(const aFilePath: string; var Name, Ext: string): Integer;
function CreateHashTES3(const aFilePath: string): UInt64;
function CreateHashTES4(const aFilePath: string): UInt64; overload;
function CreateHashFO4(const aFilePath: string): Cardinal;
implementation
@@ -594,35 +598,35 @@ begin
end;
end;
function SplitDirName(const aFileName: string; var Dir, Name: string): Integer;
function SplitDirName(const aFilePath: string; var Dir, Name: string): Integer;
begin
Result := LastCharPos(aFileName, '\');
Result := LastCharPos(aFilePath, '\');
if Result = 0 then
Result := LastCharPos(aFileName, '/');
Dir := Copy(aFileName, 1, Pred(Result));
Name := Copy(aFileName, Succ(Result), Length(aFileName) - Result);
Result := LastCharPos(aFilePath, '/');
Dir := Copy(aFilePath, 1, Pred(Result));
Name := Copy(aFilePath, Succ(Result), Length(aFilePath) - Result);
end;
function SplitNameExt(const aFileName: string; var Name, Ext: string): Integer;
function SplitNameExt(const aFilePath: string; var Name, Ext: string): Integer;
begin
Result := LastCharPos(aFileName, '.');
Result := LastCharPos(aFilePath, '.');
if Result <> 0 then begin
Name := Copy(aFileName, 1, Pred(Result));
Ext := Copy(aFileName, Result, Length(aFileName) - Result + 2);
Name := Copy(aFilePath, 1, Pred(Result));
Ext := Copy(aFilePath, Result, Length(aFilePath) - Result + 2);
end
else begin
Name := aFileName;
Name := aFilePath;
Ext := '';
end;
end;
function CreateHashTES3(const aFileName: string): UInt64;
function CreateHashTES3(const aFilePath: string): UInt64;
var
s: AnsiString;
i, l: integer;
sum, off, temp, n: Cardinal;
begin
s := AnsiString(aFileName);
s := AnsiString(aFilePath);
l := Length(s) shr 1;
sum := 0; off := 0;
@@ -689,22 +693,22 @@ begin
Result := Result + UInt64(hash) shl 32;
end;
function CreateHashTES4(const aFileName: string): UInt64; overload;
function CreateHashTES4(const aFilePath: string): UInt64; overload;
var
fname, fext: string;
begin
SplitNameExt(aFileName, fname, fext);
SplitNameExt(aFilePath, fname, fext);
Result := CreateHashTES4(fname, fext);
end;
function CreateHashFO4(const aFileName: string): Cardinal;
function CreateHashFO4(const aFilePath: string): Cardinal;
var
i: Integer;
s: AnsiString;
c: AnsiChar;
begin
Result := 0;
s := AnsiString(aFileName);
s := AnsiString(aFilePath);
for i := 1 to Length(s) do begin
c := s[i];
if Byte(c) > 127 then Continue;
@@ -777,12 +781,12 @@ begin
fHeaderTES4.Flags := fHeaderTES4.Flags or ARCHIVE_COMPRESS;
end;
function TwbBSArchive.FindFileRecordTES3(const aFileName: string; var aFileIdx: Integer): Boolean;
function TwbBSArchive.FindFileRecordTES3(const aFilePath: string; var aFileIdx: Integer): Boolean;
var
h: UInt64;
i: integer;
begin
h := CreateHashTES3(aFileName);
h := CreateHashTES3(aFilePath);
Result := False;
for i := Low(fFilesTES3) to High(fFilesTES3) do
@@ -793,13 +797,13 @@ begin
end;
end;
function TwbBSArchive.FindFileRecordTES4(const aFileName: string; var aFolderIdx, aFileIdx: Integer): Boolean;
function TwbBSArchive.FindFileRecordTES4(const aFilePath: string; var aFolderIdx, aFileIdx: Integer): Boolean;
var
fdir, fname, name, ext: string;
h: UInt64;
i, j: integer;
begin
if SplitDirName(aFileName, fdir, fname) = 0 then
if SplitDirName(aFilePath, fdir, fname) = 0 then
Exit(False);
Result := False;
@@ -829,14 +833,14 @@ begin
end;
end;
function TwbBSArchive.FindFileRecordFO4(const aFileName: string; var aFileIdx: Integer): Boolean;
function TwbBSArchive.FindFileRecordFO4(const aFilePath: string; var aFileIdx: Integer): Boolean;
var
fdir, fname, name, ext: string;
hdir, hfile: Cardinal;
hext: TMagic4;
i: integer;
begin
if SplitDirName(aFileName, fdir, fname) = 0 then
if SplitDirName(aFilePath, fdir, fname) = 0 then
Exit(False);
SplitNameExt(fname, name, ext);
@@ -855,7 +859,7 @@ begin
end;
end;
function TwbBSArchive.FindFileRecord(const aFileName: string): Pointer;
function TwbBSArchive.FindFileRecord(const aFilePath: string): Pointer;
var
i, j: integer;
begin
@@ -863,11 +867,11 @@ begin
case fType of
baTES3:
if FindFileRecordTES3(aFileName, i) then Result := @fFilesTES3[i];
if FindFileRecordTES3(aFilePath, i) then Result := @fFilesTES3[i];
baTES4, baFO3, baSSE:
if FindFileRecordTES4(aFileName, i, j) then Result := @fFoldersTES4[i].Files[j];
if FindFileRecordTES4(aFilePath, i, j) then Result := @fFoldersTES4[i].Files[j];
baFO4, baFO4dds:
if FindFileRecordFO4(aFileName, i) then Result := @fFilesFO4[i];
if FindFileRecordFO4(aFilePath, i) then Result := @fFilesFO4[i];
end;
end;
@@ -951,14 +955,14 @@ begin
Inc(fPackedDataCount);
end;
procedure TwbBSArchive.LoadFromFile(const aFileName: string);
procedure TwbBSArchive.LoadFromFile(const aFilePath: string);
var
i, j: Integer;
begin
if fStates * [stReading, stWriting] <> [] then
Close;
fStream := TwbReadOnlyCachedFileStream.Create(aFileName, fmOpenRead or fmShareDenyWrite);
fStream := TwbReadOnlyCachedFileStream.Create(aFilePath, fmOpenRead or fmShareDenyWrite);
// magic
fMagic := Int2Magic(fStream.ReadCardinal);
@@ -1099,7 +1103,7 @@ begin
end;
fFileName := aFileName;
fFileName := aFilePath;
Include(fStates, stReading);
end;
@@ -1131,7 +1135,7 @@ begin
Result := CompareStr(List[Index1], List[Index2]);
end;
procedure TwbBSArchive.CreateArchiveCompat(const aFileName: string; aType: TBSArchiveType;
procedure TwbBSArchive.CreateArchiveCompat(const aFilePath: string; aType: TBSArchiveType;
aFilesList: TwbBSEntryList = nil);
var
HashPairs: array of THashPair;
@@ -1363,16 +1367,15 @@ begin
raise Exception.Create('DDS archive requires DDS file information callback');
for i := 0 to Pred(aFilesList.Count) do begin
fDDSInfoProc(Self, PChar(aFilesList[i]), ddsinfo);
fDDSInfoProc(Self, PChar(aFilesList[i]), ddsinfo, Self.fDDSInfoProcContext);
fDataOffset := fDataOffset + 24 {size of file record} + 24 {size of each texchunk} * GetDDSMipChunkNum(ddsinfo);
end;
end;
end;
fStream := TwbWriteCachedFileStream.Create(aFileName, fmCreate);
fFileName := aFileName;
fStream := TwbWriteCachedFileStream.Create(aFilePath, fmCreate);
fFileName := aFilePath;
Include(fStates, stWriting);
// reserve space for the header
@@ -1532,7 +1535,9 @@ begin
Close;
end;
procedure TwbBSArchive.AddFile(const aRootDir, aFileName: string);
procedure TwbBSArchive.AddFileDisk(const aFilePath, aSourcePath: string);
var
fname: string;
i: integer;
@@ -1542,18 +1547,13 @@ begin
if not (stWriting in fStates) then
raise Exception.Create('Archive is not in writing mode');
i := Length(aRootDir);
if (i > 1) and (aRootDir[Length(aRootDir)] <> '\') then
Inc(i);
fname := Copy(aFileName, i + 1, Length(aFileName));
stream := TFileStream.Create(aFileName, fmOpenRead + fmShareDenyNone);
stream := TFileStream.Create(aSourcePath, fmOpenRead + fmShareDenyNone);
try
GetMem(buffer, stream.Size);
// Modified: Make sure memory is zeroed when allocated
buffer := AllocMem(stream.Size);
try
stream.Read(buffer^, stream.Size);
AddFileCompat(fname, stream.Size, buffer);
AddFileDataCompat(aFilePath, stream.Size, buffer);
finally
if Assigned(buffer) then
FreeMem(buffer);
@@ -1563,8 +1563,22 @@ begin
end;
end;
procedure TwbBSArchive.AddFileDiskRoot(const aRootDir, aSourcePath: string);
var
fname: string;
i: integer;
begin
i := Length(aRootDir);
if (i > 1) and (aRootDir[Length(aRootDir)] <> '\') then
Inc(i);
fname := Copy(aSourcePath, i + 1, Length(aSourcePath));
AddFileDisk(fname, aSourcePath);
end;
// Modified: Version for use in non-Borland C/C++
procedure TwbBSArchive.AddFileCompat(const aFileName: string; const aSize: Cardinal; const aData: PByte);
procedure TwbBSArchive.AddFileDataCompat(const aFilePath: string; const aSize: Cardinal; const aData: PByte);
var
i, j, Off, MipSize, BitsPerPixel: integer;
fdir, fname, name, ext: string;
@@ -1589,8 +1603,8 @@ begin
try
case fType of
baTES3: begin
if not FindFileRecordTES3(aFileName, i) then
raise Exception.Create('File not found in files table: ' + aFileName);
if not FindFileRecordTES3(aFilePath, i) then
raise Exception.Create('File not found in files table: ' + aFilePath);
if FindPackedData(aSize, DataHash, @fFilesTES3[i]) then
Exit;
@@ -1602,8 +1616,8 @@ begin
end;
baTES4, baFO3, baSSE: begin
if not FindFileRecordTES4(aFileName, i, j) then
raise Exception.Create('File not found in files table: ' + aFileName);
if not FindFileRecordTES4(aFilePath, i, j) then
raise Exception.Create('File not found in files table: ' + aFilePath);
if FindPackedData(aSize, DataHash, @fFoldersTES4[i].Files[j]) then
Exit;
@@ -1669,8 +1683,8 @@ begin
end;
baFO4: begin
if SplitDirName(aFileName, fdir, fname) = 0 then
raise Exception.Create('File is missing the folder part: ' + aFileName);
if SplitDirName(aFilePath, fdir, fname) = 0 then
raise Exception.Create('File is missing the folder part: ' + aFilePath);
SplitNameExt(fname, name, ext);
if Length(ext) > 1 then Delete(ext, 1, 1); // no dot in extension
@@ -1678,7 +1692,7 @@ begin
i := fHeaderFO4.FileCount;
Inc(fHeaderFO4.FileCount);
// archive2.exe uses /
fFilesFO4[i].Name := StringReplace(aFileName, '\', '/', [rfReplaceAll]);
fFilesFO4[i].Name := StringReplace(aFilePath, '\', '/', [rfReplaceAll]);
fFilesFO4[i].DirHash := CreateHashFO4(fdir);
fFilesFO4[i].NameHash := CreateHashFO4(name);
fFilesFO4[i].Ext := Int2Magic(Str2MagicInt(ext));
@@ -1711,8 +1725,8 @@ begin
end;
baFO4dds: begin
if SplitDirName(aFileName, fdir, fname) = 0 then
raise Exception.Create('File is missing the folder part: ' + aFileName);
if SplitDirName(aFilePath, fdir, fname) = 0 then
raise Exception.Create('File is missing the folder part: ' + aFilePath);
SplitNameExt(fname, name, ext);
if Length(ext) > 1 then Delete(ext, 1, 1); // no dot in extension
@@ -1721,7 +1735,7 @@ begin
i := fHeaderFO4.FileCount;
Inc(fHeaderFO4.FileCount);
// archive2.exe uses /
fFilesFO4[i].Name := StringReplace(aFileName, '\', '/', [rfReplaceAll]);
fFilesFO4[i].Name := StringReplace(aFilePath, '\', '/', [rfReplaceAll]);
fFilesFO4[i].DirHash := CreateHashFO4(fdir);
fFilesFO4[i].NameHash := CreateHashFO4(name);
fFilesFO4[i].Ext := Int2Magic(Str2MagicInt(ext));
@@ -1893,8 +1907,8 @@ begin
FileTES3 := aFileRecord;
fStream.Position := fDataOffset + FileTES3.Offset;
Result.size := FileTES3.Size;
GetMem(Result.data, FileTES3.Size);
fStream.ReadBuffer(Result.data[0], Result.size);
// Modified: Make sure memory is zeroed when allocated
Result.data := AllocMem(FileTES3.Size);
end;
baTES4, baFO3, baSSE: begin
@@ -1915,7 +1929,8 @@ begin
if bCompressed then begin
// reading uncompressed size
Result.size := fStream.ReadCardinal;
GetMem(Result.data, Result.size);
// Modified: Make sure memory is zeroed when allocated
Result.data := AllocMem(Result.size);
dec(size, SizeOf(Cardinal));
if (Result.size > 0) and (size > 0) then begin
SetLength(Buffer, size);
@@ -1940,7 +1955,8 @@ begin
end
else begin
Result.size := size;
GetMem(Result.data, Result.size);
// Modified: Make sure memory is zeroed when allocated
Result.data := AllocMem(Result.size);
if size > 0 then
fStream.ReadBuffer(Result.data[0], Result.size);
end;
@@ -1953,7 +1969,8 @@ begin
SetLength(Buffer, FileFO4.PackedSize);
fStream.ReadBuffer(Buffer[0], Length(Buffer));
Result.size := FileFO4.Size;
GetMem(Result.data, Result.size);
// Modified: Make sure memory is zeroed when allocated
Result.data := AllocMem(Result.size);
if Assigned(Sync) then
Sync.EndWrite;
try
@@ -1965,7 +1982,8 @@ begin
end
else begin
Result.size := FileFO4.Size;
GetMem(Result.data, Result.size);
// Modified: Make sure memory is zeroed when allocated
Result.data := AllocMem(Result.size);
fStream.ReadBuffer(Result.data[0], Result.size);
end;
end;
@@ -1974,11 +1992,14 @@ begin
FileFO4 := aFileRecord;
TexSize := SizeOf(TDDSHeader);
for i := Low(FileFO4.TexChunks) to High(FileFO4.TexChunks) do
Inc(TexSize, FileFO4.TexChunks[i].Size);
Result.size := Texsize;
GetMem(Result.data, Result.size);
// Modified: Make sure memory is zeroed when allocated
Result.data := AllocMem(Result.size);
DDSHeader := @Result.data[0];
DDSHeader.Magic := MAGIC_DDS;
@@ -2197,14 +2218,14 @@ begin
end;
// Modified: Version for use in non-Borland C/C++
function TwbBSArchive.ExtractFileDataCompat(const aFileName: string): TwbBSResultBuffer;
function TwbBSArchive.ExtractFileDataCompat(const aFilePath: string): TwbBSResultBuffer;
var
FileRecord: Pointer;
begin
if not (stReading in fStates) then
raise Exception.Create('Archive is not loaded');
FileRecord := FindFileRecord(aFileName);
FileRecord := FindFileRecord(aFilePath);
if not Assigned(FileRecord) then
raise Exception.Create('File not found in archive');
@@ -2215,11 +2236,11 @@ end;
// Addded: For use in non-Borland C/C++
procedure TwbBSArchive.ReleaseFileDataCompat(fileDataResult: TwbBSResultBuffer);
begin
FreeAndNil(fileDataResult.data);
FreeMem(fileDataResult.data);
fileDataResult.size := 0;
end;
procedure TwbBSArchive.ExtractFile(const aFileName, aSaveAs: string);
procedure TwbBSArchive.ExtractFile(const aFilePath, aSaveAs: string);
var
fs: TFileStream;
fileData: TwbBSResultBuffer;
@@ -2229,7 +2250,7 @@ begin
fs := TFileStream.Create(aSaveAs, fmCreate);
try
fileData := ExtractFileDataCompat(aFileName);
fileData := ExtractFileDataCompat(aFilePath);
fs.Write(fileData.data[0], fileData.size);
finally
ReleaseFileDataCompat(fileData);
@@ -2285,9 +2306,9 @@ begin
end;
end;
function TwbBSArchive.FileExists(const aFileName: string): Boolean;
function TwbBSArchive.FileExists(const aFilePath: string): Boolean;
begin
Result := Assigned(FindFileRecord(aFileName));
Result := Assigned(FindFileRecord(aFilePath));
end;
procedure TwbBSArchive.Close;