summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-09-11 19:19:32 +0200
committerAki <please@ignore.pl>2022-09-11 19:19:32 +0200
commit5310ea3564fe98b2940a63b72621b79de9290f77 (patch)
tree0d5b7a09d9cbef287d3af71f48f31f1935d0a075
parenteb1433d1c9d87470952a8ef626c790a8a60b8d76 (diff)
downloadstarshatter-5310ea3564fe98b2940a63b72621b79de9290f77.zip
starshatter-5310ea3564fe98b2940a63b72621b79de9290f77.tar.gz
starshatter-5310ea3564fe98b2940a63b72621b79de9290f77.tar.bz2
Replaced Datafile and StarsEx/Archive with dat and ArchiveEx
-rw-r--r--ArchiveEx/CMakeLists.txt1
-rw-r--r--CMakeLists.txt1
-rw-r--r--Datafile/Archive.cpp480
-rw-r--r--Datafile/Archive.h84
-rw-r--r--Datafile/CMakeLists.txt19
-rw-r--r--Datafile/Main.cpp422
-rw-r--r--StarsEx/Archive.cpp597
-rw-r--r--StarsEx/Archive.h93
-rw-r--r--StarsEx/CMakeLists.txt2
-rw-r--r--StarsEx/DataLoader.cpp5
-rw-r--r--StarsEx/DataLoader.h1
-rw-r--r--StarsEx/DataSource.cpp24
-rw-r--r--StarsEx/DataSource.h6
-rw-r--r--StarsEx/ModInfo.cpp12
-rw-r--r--StarsEx/Starshatter.cpp1
-rw-r--r--cmake/modules/AddDatafile.cmake6
16 files changed, 26 insertions, 1728 deletions
diff --git a/ArchiveEx/CMakeLists.txt b/ArchiveEx/CMakeLists.txt
index 158e2b3..6010cea 100644
--- a/ArchiveEx/CMakeLists.txt
+++ b/ArchiveEx/CMakeLists.txt
@@ -25,3 +25,4 @@ if(WIN32)
PRIVATE -l:libshlwapi.a
)
endif()
+install(TARGETS dat RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fea3356..dd698d5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,7 +17,6 @@ endif()
add_subdirectory(ArchiveEx)
add_subdirectory(contrib)
add_subdirectory(data)
-add_subdirectory(Datafile)
add_subdirectory(DefinitionEx)
add_subdirectory(FoundationEx)
if(MSVC)
diff --git a/Datafile/Archive.cpp b/Datafile/Archive.cpp
deleted file mode 100644
index 5e4fdac..0000000
--- a/Datafile/Archive.cpp
+++ /dev/null
@@ -1,480 +0,0 @@
-/* Starshatter: The Open Source Project
- Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
- Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
- Copyright (c) 1997-2006, Destroyer Studios LLC.
-
- AUTHOR: John DiCamillo
-*/
-
-#include <string>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <io.h>
-#include <stdio.h>
-#include <math.h>
-#include <time.h>
-#include <windows.h>
-#include <windowsx.h>
-
-#include "zlib.h"
-#include <mmsystem.h>
-#include "Archive.h"
-
-int verbose = 1;
-int err;
-
-#define CHECK_ERR(err, msg) { \
- if (err != Z_OK) { \
- fprintf(stderr, "%s error: %d\n", msg, err); \
- exit(1); \
- } \
-}
-
-// +--------------------------------------------------------------------+
-
-DataArchive::DataArchive(const char* name)
-{
- ZeroMemory(this, sizeof(DataArchive));
-
- if (name)
- LoadDatafile(name);
-}
-
-DataArchive::~DataArchive()
-{
- delete [] block_map;
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::WriteEntry(int index, BYTE* buf)
-{
- int f = _open(datafile, _O_RDWR|_O_CREAT|_O_BINARY, _S_IREAD|_S_IWRITE);
-
- if (f != -1) {
- header.dir_size_comp = DirBlocks() * BLOCK_SIZE;
- dirbuf = new BYTE[header.dir_size_comp];
-
- err = compress(dirbuf, &header.dir_size_comp,
- (BYTE*) directory, header.nfiles * sizeof(DataEntry));
- CHECK_ERR(err, "compress");
-
- header.dir_blocks = Blocks(header.dir_size_comp) * BLOCK_SIZE;
-
- _lseek(f, 0, SEEK_SET);
- _write(f, &header, sizeof(DataHeader));
- _lseek(f, sizeof(DataHeader) + header.dir_offset, SEEK_SET);
- _write(f, dirbuf, header.dir_blocks);
-
- delete [] dirbuf;
-
- if (buf && directory[index].size_comp) {
- _lseek(f, sizeof(DataHeader) + directory[index].offset, SEEK_SET);
- _write(f, buf, directory[index].size_comp);
- }
- _close(f);
- }
- else
- perror("WriteEntry");
-}
-
-// +--------------------------------------------------------------------+
-
-DWORD DataArchive::Blocks(DWORD raw_size)
-{
- int full_blocks = raw_size / BLOCK_SIZE;
- int part_blocks = (raw_size % BLOCK_SIZE) > 0;
-
- return full_blocks + part_blocks;
-}
-
-DWORD DataArchive::DirBlocks()
-{
- DWORD result = Blocks(header.nfiles * sizeof(DataEntry));
- if (result == 0) result = 1;
- return result;
-}
-
-DWORD DataArchive::FileBlocks(int index)
-{
- return Blocks(directory[index].size_comp);
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::CreateBlockMap()
-{
- delete [] block_map;
- block_map = 0;
-
- if (header.nfiles == 0) return;
-
- DWORD i,j;
- DWORD dir_usage = header.dir_offset + DirBlocks() * BLOCK_SIZE;
- DWORD max_usage = dir_usage;
-
- for (i = 0; i < header.nfiles; i++) {
- DWORD last_block = directory[i].offset + FileBlocks(i) * BLOCK_SIZE;
- if (last_block > max_usage)
- max_usage = last_block;
- }
-
- nblocks = max_usage/BLOCK_SIZE;
- block_map = new DWORD[nblocks];
- ZeroMemory(block_map, nblocks*sizeof(DWORD));
-
- DWORD first_block = header.dir_offset/BLOCK_SIZE +
- (header.dir_offset%BLOCK_SIZE > 0);
-
- for (j = 0; j < DirBlocks(); j++)
- block_map[first_block+j] = 1;
-
- for (i = 0; i < header.nfiles; i++) {
- DWORD first_block = directory[i].offset/BLOCK_SIZE +
- (directory[i].offset%BLOCK_SIZE > 0);
-
- for (j = 0; j < FileBlocks(i); j++)
- block_map[first_block+j] = i+2;
- }
-}
-
-// +--------------------------------------------------------------------+
-
-int DataArchive::FindDataBlocks(int need)
-{
- if ((int) (nblocks)-need > 0) {
- DWORD start;
- int i;
-
- for (start = 0; start < nblocks-need; start++) {
- for (i = 0; block_map[start+i] == 0 && i < need; i++);
-
- if (i == need) return start*BLOCK_SIZE;
-
- start += i;
- }
- }
-
- return nblocks*BLOCK_SIZE;
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::LoadDatafile(const char* name)
-{
- strncpy(datafile, name, NAMELEN-1);
- header.nfiles = 0;
-
- FILE* f = fopen(datafile, "rb");
- if (f) {
- fread(&header, sizeof(DataHeader), 1, f);
-
- if (header.version != VERSION) {
- printf("ERROR: datafile '%s' invalid version '%d'\n", //-V576
- datafile, header.version);
- fclose(f);
- exit(-2);
- }
-
- DWORD len = DirBlocks() * BLOCK_SIZE;
-
- dirbuf = new BYTE[len];
- fseek(f, sizeof(DataHeader) + header.dir_offset, SEEK_SET);
- fread(dirbuf, header.dir_size_comp, 1, f);
-
- int err = uncompress((BYTE*) directory, &len,
- dirbuf, header.dir_size_comp);
- if (err != Z_OK)
- ZeroMemory(directory, sizeof(directory));
-
- delete [] dirbuf;
- CreateBlockMap();
- }
- else {
- printf("Creating Archive '%s'...\n", datafile);
-
- header.version = VERSION;
- header.nfiles = 0;
- header.dir_blocks = 0;
- header.dir_size_comp = 0;
- header.dir_offset = 0;
-
- nblocks = DirBlocks();
-
- delete [] block_map;
- block_map = 0;
- }
-}
-
-// +--------------------------------------------------------------------+
-
-int DataArchive::FindEntry(const char* req_name)
-{
- int entry = -1;
-
- for (DWORD i = 0; i < header.nfiles; i++)
- if (!_stricmp(directory[i].name, req_name))
- return i;
-
- return entry;
-}
-
-// +--------------------------------------------------------------------+
-
-BYTE* DataArchive::CompressEntry(int i)
-{
- char* name = directory[i].name;
-
- FILE* f = fopen(name, "rb");
-
- if (f) {
- fseek(f, 0, SEEK_END);
- DWORD len = ftell(f);
- fseek(f, 0, SEEK_SET);
-
- BYTE* buf = new BYTE[len];
-
- fread(buf, len, 1, f);
- fclose(f);
-
- directory[i].size_orig = len;
-
- directory[i].size_comp = (int) (len * 1.2);
- BYTE* cbuf = new BYTE[directory[i].size_comp];
-
- err = compress(cbuf, &directory[i].size_comp, buf, len);
- CHECK_ERR(err, "compress");
-
- delete [] buf;
- return cbuf;
- }
-
- return 0;
-}
-
-// +--------------------------------------------------------------------+
-
-int DataArchive::ExpandEntry(int i, BYTE*& buf)
-{
- DWORD len = 0;
-
- FILE* f = fopen(datafile, "rb");
-
- if (f) {
- DWORD clen = directory[i].size_comp;
- BYTE* cbuf = new BYTE[clen];
-
- fseek(f, sizeof(DataHeader) + directory[i].offset, SEEK_SET);
- fread(cbuf, clen, 1, f);
-
- len = directory[i].size_orig;
- buf = new BYTE[len];
-
- int err = uncompress(buf, &len, cbuf, clen);
- if (err != Z_OK) {
- delete [] buf;
- buf = 0;
- }
-
- delete [] cbuf;
- fclose(f);
- }
-
- return len;
-}
-
-// +--------------------------------------------------------------------+
-
-int DataArchive::InsertEntry(const char* name)
-{
- if (!name) return -1;
-
- DWORD len = strlen(name);
-
- for (int i = 0; i < MAX_FILES; i++) {
- if (directory[i].size_orig == 0) {
- strncpy(directory[i].name, name, NAMELEN);
- directory[i].name[NAMELEN-1] = '\0';
- directory[i].size_orig = 1;
-
- return i;
- }
- }
-
- return -1;
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::RemoveEntry(int index)
-{
- ZeroMemory(&directory[index], sizeof(DataEntry));
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::Insert(const char* name)
-{
- DWORD old_blocks = 0, old_offset = 0, new_blocks = 0;
- DWORD old_dir_blocks = 0, old_dir_offset = 0, new_dir_blocks = 0;
- int added = 0;
-
- int index = FindEntry(name);
-
- if (index < 0) {
- old_dir_blocks = DirBlocks();
- old_dir_offset = header.dir_offset;
-
- index = InsertEntry(name);
-
- if (index >= (int) header.nfiles) {
- header.nfiles = index+1;
- added = 1;
- }
-
- new_dir_blocks = DirBlocks();
-
- if (new_dir_blocks > old_dir_blocks) {
- header.dir_offset = FindDataBlocks(new_dir_blocks);
- CreateBlockMap();
- }
- }
- else {
- old_blocks = FileBlocks(index);
- old_offset = directory[index].offset;
- }
-
- if (index >= 0) {
- DataEntry& e = directory[index];
-
- if (verbose) printf(" Inserting: %-48s ", e.name);
-
- BYTE* buf = CompressEntry(index);
-
- if (!buf) {
- // this is (almost) unrecoverable,
- // so we quit before screwing things up:
- printf("ERROR: Could not compress %d:%s\n", index, directory[index].name);
- exit(1);
- }
-
- new_blocks = FileBlocks(index);
-
- // the file is new, or got bigger,
- // need to find room for the data:
- if (new_blocks > old_blocks) {
- directory[index].offset = FindDataBlocks(new_blocks);
- CreateBlockMap();
- }
-
- WriteEntry(index, buf);
- delete [] buf;
-
- if (verbose) {
- int ratio = (int) (100.0 * (double) e.size_comp / (double) e.size_orig);
- printf("%9d => %9d (%2d%%)\n", e.size_orig, e.size_comp, ratio); //-V576
- }
- }
- else if (added)
- header.nfiles--;
-}
-
-// +--------------------------------------------------------------------+
-
-std::wstring ToWideString(const std::string& str)
-{
- int stringLength = MultiByteToWideChar(CP_ACP, 0, str.data(), str.length(), 0, 0);
- std::wstring wstr(stringLength, 0);
- MultiByteToWideChar(CP_ACP, 0, str.data(), str.length(), &wstr[0], stringLength);
- return wstr;
-}
-
-void DataArchive::Extract(const char* name)
-{
- int index = FindEntry(name);
-
- if (index < 0) {
- printf("Could not extract '%s', not found\n", name);
- return;
- }
-
- BYTE* buf;
- ExpandEntry(index, buf);
-
- std::string dirname = directory[index].name;
- bool create_subdir = (dirname.find_first_of('/', 0) != std::string::npos);
- std::wstring wdirname = ToWideString(dirname.substr(0, dirname.find_first_of('/')));
- if (create_subdir)
- CreateDirectory(wdirname.c_str(), NULL);
- size_t offset = wdirname.length();
- while (dirname.find_first_of('/', offset + 1) != std::string::npos) {
- wdirname.push_back('/');
- wdirname += ToWideString(dirname.substr(offset + 1, dirname.find_first_of('/', offset + 1) - offset - 1));
- CreateDirectory(wdirname.c_str(), NULL);
- offset = wdirname.length();
- }
-
- FILE* f = fopen(directory[index].name, "w+b");
- if (f) {
- fwrite(buf, directory[index].size_orig, 1, f);
- fclose(f);
- }
- else
- printf("Could not extract '%s', could not open file for writing\n", name);
-
- delete [] buf;
-
- if (verbose) printf(" Extracted: %s\n", name);
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::Remove(const char* name)
-{
- int index = FindEntry(name);
-
- if (index < 0) {
- printf("Could not remove '%s', not found\n", name);
- return;
- }
-
- RemoveEntry(index);
- WriteEntry(index, 0);
-
- if (verbose) printf(" Removed: %s\n", name);
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::List()
-{
- int total_orig = 0;
- int total_comp = 0;
-
- printf("DATAFILE: %s\n", datafile);
- printf("Files: %d\n", header.nfiles); //-V576
- printf("\n");
- printf("Index Orig Size Comp Size Ratio Name\n");
- printf("----- --------- --------- ----- ----------------\n");
-
- for (DWORD i = 0; i < header.nfiles; i++) {
- DataEntry& e = directory[i];
- int ratio = (int) (100.0 * (double) e.size_comp / (double) e.size_orig);
-
- printf("%5d %9d %9d %2d%% %s\n", i+1, e.size_orig, e.size_comp, ratio, e.name); //-V576
-
- total_orig += e.size_orig;
- total_comp += e.size_comp;
- }
-
- int total_ratio = (int) (100.0 * (double) total_comp / (double) total_orig);
-
- printf("----- --------- --------- -----\n");
- printf("TOTAL %9d %9d %2d%%\n\n", total_orig, total_comp, total_ratio);
-}
-
-
-// +--------------------------------------------------------------------+
-
diff --git a/Datafile/Archive.h b/Datafile/Archive.h
deleted file mode 100644
index ec5791a..0000000
--- a/Datafile/Archive.h
+++ /dev/null
@@ -1,84 +0,0 @@
-/* Starshatter: The Open Source Project
- Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
- Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
- Copyright (c) 1997-2006, Destroyer Studios LLC.
-
- AUTHOR: John DiCamillo
-*/
-
-#ifndef ARCHIVE_HPP
-#define ARCHIVE_HPP
-
-// +------------------------------------------------------------------+
-
-#define VERSION 0x0010
-#define BLOCK_SIZE 1024
-#define MAX_FILES 8192
-#define FILE_BLOCK 1024
-#define NAMELEN 64
-
-// +------------------------------------------------------------------+
-
-struct DataHeader
-{
- DWORD version;
- DWORD nfiles;
- DWORD dir_blocks;
- DWORD dir_size_comp;
- DWORD dir_offset;
-};
-
-struct DataEntry
-{
- char name[NAMELEN];
- DWORD size_orig;
- DWORD size_comp;
- DWORD offset;
-};
-
-class DataArchive
-{
-public:
- // ctor:
- DataArchive(const char* name = 0);
- ~DataArchive();
-
- // operations:
- void LoadDatafile(const char* name);
- void Insert(const char* name);
- void Extract(const char* name);
- void Remove(const char* name);
- void List();
-
- void WriteEntry(int index, BYTE* buf);
- int FindEntry(const char* req_name);
- int ExpandEntry(int index, BYTE*& buf);
- BYTE* CompressEntry(int index);
- int InsertEntry(const char* name);
- void RemoveEntry(int index);
- DWORD Blocks(DWORD raw_size);
- DWORD DirBlocks();
- DWORD FileBlocks(int index);
- int FindDataBlocks(int blocks_needed);
- void CreateBlockMap();
-
- DWORD NumFiles() { return header.nfiles; }
- DataEntry* GetFile(int i) { if (i>=0 && i<(int)header.nfiles) return &directory[i]; return 0; }
-
-private:
- // persistent data members:
- DataHeader header;
- DataEntry directory[MAX_FILES];
- BYTE* dirbuf;
-
- // transient members:
- char datafile[NAMELEN];
-
- DWORD* block_map;
- DWORD nblocks;
-};
-
-extern std::wstring ToWideString(const std::string& str);
-
-
-#endif
diff --git a/Datafile/CMakeLists.txt b/Datafile/CMakeLists.txt
deleted file mode 100644
index 74d2ec4..0000000
--- a/Datafile/CMakeLists.txt
+++ /dev/null
@@ -1,19 +0,0 @@
-project(Datafile)
-add_executable(
- Datafile
- Archive.cpp
- Main.cpp
- )
-target_include_directories(
- Datafile
- PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
- )
-target_link_libraries(
- Datafile
- PUBLIC Zlib::zlib
- )
-target_compile_definitions(
- Datafile
- PUBLIC UNICODE
- )
-install(TARGETS Datafile RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
diff --git a/Datafile/Main.cpp b/Datafile/Main.cpp
deleted file mode 100644
index 004cd10..0000000
--- a/Datafile/Main.cpp
+++ /dev/null
@@ -1,422 +0,0 @@
-/* Starshatter: The Open Source Project
- Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
- Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
- Copyright (c) 1997-2006, Destroyer Studios LLC.
-
- AUTHOR: John DiCamillo
-*/
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string>
-#include <math.h>
-#include <time.h>
-#include <windows.h>
-#include <windowsx.h>
-#include "Archive.h"
-
-//#define MOD_MAKER 1
-
-// +------------------------------------------------------------------+
-
-void insertFile(DataArchive& a, const char* sPath, WIN32_FIND_DATA* find)
-{
- char sFile[256];
- char sFlat[256];
- std::string sTemp;
- DWORD find_attrib_forbidden =
- FILE_ATTRIBUTE_DIRECTORY |
- FILE_ATTRIBUTE_HIDDEN |
- FILE_ATTRIBUTE_SYSTEM |
- FILE_ATTRIBUTE_OFFLINE;
-
- if (sPath && *sPath) {
- sTemp = sPath;
- sTemp += '/';
- WideCharToMultiByte(CP_ACP,0,find->cFileName,-1, sFile,260, NULL, NULL);
- sTemp += sFile;
- strcpy(sFile, sTemp.c_str());
- } else {
- WideCharToMultiByte(CP_ACP,0,find->cFileName,-1, sFile,260, NULL, NULL);
- }
-
-
- if (find->dwFileAttributes & find_attrib_forbidden) {
- printf(" Skipping: %-48s \n", sFile);
- return;
- }
-
- int n = strlen(sFile);
-
- if (n >= NAMELEN) {
- printf(" Skipping: %-48s (NAME TOO LONG!)\n", sFile);
- return;
- }
-
- for (int i = 0; i < n; i++)
- sFlat[i] = tolower(sFile[i]);
-
- if (strstr(sFlat, ".exe")) {
- printf(" Skipping: %-48s (executable file)\n", sFile);
- }
- else if (strstr(sFlat, ".cmd")) {
- printf(" Skipping: %-48s (executable file)\n", sFile);
- }
- else if (strstr(sFlat, ".bat")) {
- printf(" Skipping: %-48s (executable file)\n", sFile);
- }
- else if (strstr(sFlat, ".bin")) {
- printf(" Skipping: %-48s (unknown file)\n", sFile);
- }
- else if (strstr(sFlat, ".db")) {
- printf(" Skipping: %-48s (unknown file)\n", sFile);
- }
- else if (strstr(sFlat, ".dat")) {
- printf(" Skipping: %-48s (data file)\n", sFile);
- }
- else if (strstr(sFlat, ".zip")) {
- printf(" Skipping: %-48s (zip file)\n", sFile);
- }
- else if (strstr(sFlat, ".arc")) {
- printf(" Skipping: %-48s (archive file)\n", sFile);
- }
- else if (strstr(sFlat, ".psd")) {
- printf(" Skipping: %-48s (PSD file)\n", sFile);
- }
- else {
- a.Insert(sFile);
- }
-}
-
-// +------------------------------------------------------------------+
-
-void ins(DataArchive& a, int argc, char* argv[])
-{
- char sPath[256];
- char* pDirSep = 0;
-
- for (int i = 0; i < argc; i++) {
- if (strchr(argv[i], '*')) {
- strcpy(sPath, argv[i]);
-
- if ((pDirSep = strrchr(sPath, '\\')) != 0)
- *pDirSep = 0;
- else if ((pDirSep = strrchr(sPath, '/')) != 0)
- *pDirSep = 0;
- else
- sPath[0] = 0;
-
- WIN32_FIND_DATA find;
- LPCWSTR tmp = (LPCWSTR)argv[i];
- HANDLE h = FindFirstFile(tmp, &find);
- if (h != INVALID_HANDLE_VALUE) {
- insertFile(a, sPath, &find);
-
- while (FindNextFile(h,&find)) {
- insertFile(a, sPath, &find);
- }
-
- FindClose(h);
- }
- }
- else {
- a.Insert(argv[i]);
- }
- }
-}
-
-// +--------------------------------------------------------------------+
-
-void build(DataArchive& a, const char* sBasePath);
-
-void buildFile(DataArchive& a, const char* sPath, WIN32_FIND_DATA& find)
-{
- if (find.cFileName[0] == '.') {
- } else if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- char subdir[256];
- std::string sTemp;
- if (sPath && *sPath) {
- sTemp = sPath;
- sTemp += '/';
- WideCharToMultiByte(CP_ACP,0,find.cFileName,-1, subdir,260, NULL, NULL);
- sTemp += subdir;
- strcpy(subdir, sTemp.c_str());
- }
- else
- WideCharToMultiByte(CP_ACP,0,find.cFileName,-1, subdir,256, NULL, NULL);
-
- build(a, subdir);
- } else {
- insertFile(a, sPath, &find);
- }
-}
-
-void build(DataArchive& a, const char* sBasePath)
-{
- char sPath[256];
- char sFind[256];
-
- if (sBasePath && *sBasePath) {
- strcpy(sPath, sBasePath);
- sprintf(sFind, "%s\\*.*", sPath);
- } else {
- sPath[0] = 0;
- strcpy(sFind, "*.*");
- }
-
- WIN32_FIND_DATA find;
- std::wstring sTemp;
- std::string sStd = sFind;
- sTemp = ToWideString(sStd);
- HANDLE h = FindFirstFile(sTemp.c_str(), &find);
- if (h != INVALID_HANDLE_VALUE) {
- do buildFile(a, sPath, find);
- while (FindNextFile(h, &find));
-
- FindClose(h);
- }
-}
-
-void mak(DataArchive& a)
-{
- build(a, 0);
-}
-
-// +--------------------------------------------------------------------+
-// for now, pattern must be either "*" or "*.???"
-
-int match(const char* sFile, const char* sPattern)
-{
- int nPatternType = 0;
- char* sExt = 0;
-
- const int PATTERN_NOWILD = 0;
- const int PATTERN_STAR = 1;
- const int PATTERN_STAR_DOT_STAR = 2;
- const int PATTERN_STAR_DOT_EXT = 3;
-
- // what kind of pattern matching?
- if (strchr(sPattern, '*')) {
- if (strchr(sPattern, '.')) {
- if (strcmp(sPattern, "*.*") == 0) {
- nPatternType = PATTERN_STAR_DOT_STAR;
- } else {
- nPatternType = PATTERN_STAR_DOT_EXT;
- sExt = const_cast<char*>(strchr(sPattern, '.'));
- }
- } else {
- nPatternType = PATTERN_STAR;
- }
- }
-
- int file_matches_pattern = 0;
-
- switch (nPatternType) {
- case PATTERN_NOWILD:
- default:
- file_matches_pattern = (_stricmp(sFile, sPattern) == 0);
- break;
-
- case PATTERN_STAR:
- case PATTERN_STAR_DOT_STAR:
- file_matches_pattern = 1;
- break;
-
- case PATTERN_STAR_DOT_EXT:
- file_matches_pattern = (strstr(sFile, sExt) != 0);
- break;
- }
-
- return file_matches_pattern;
-}
-
-void ext(DataArchive& a, int argc, char* argv[])
-{
- if (argc) {
- char sPath[256];
- char sPatt[256];
- char* pDirSep;
- int nPath;
-
- for (int i = 0; i < argc; i++) {
- if (strchr(argv[i], '*')) {
- strcpy(sPath, argv[i]);
-
- if ((pDirSep = strrchr(sPath, '\\')) != 0) {
- strcpy(sPatt, pDirSep+1);
- *pDirSep = 0;
- nPath = strlen(sPath);
- } else if ((pDirSep = strrchr(sPath, '/')) != 0) {
- strcpy(sPatt, pDirSep+1);
- *pDirSep = 0;
- nPath = strlen(sPath);
- } else {
- strcpy(sPatt, sPath);
- sPath[0] = 0;
- nPath = 0;
- }
-
- // for each file in the archive:
- for (unsigned j = 0; j < a.NumFiles(); j++) {
- DataEntry* pde = a.GetFile(j);
-
- if (pde) {
- // if we are extracting from a sub-directory,
- if (nPath) {
- // and this file is in the sub-directory,
- if (_strnicmp(pde->name, sPath, nPath) == 0) {
- // and this file matches the pattern:
- if (match(pde->name+nPath+1, sPatt)) {
- char sName[256];
- strcpy(sName, pde->name);
- a.Extract(sName);
- }
- }
- } else {
- // if we are extracting from the main directory,
- // and this file is in the main directory,
- if (strchr(pde->name, '/') == 0) {
- // and this file matches the pattern:
- if (match(pde->name, sPatt)) {
- char sName[256];
- strcpy(sName, pde->name);
- a.Extract(sName);
- }
- }
- }
- }
- }
- } else {
- // for each file in the archive:
- for (unsigned j = 0; j < a.NumFiles(); j++) {
- DataEntry* pde = a.GetFile(j);
-
- if (pde) {
- if (_stricmp(pde->name, argv[i]) == 0) {
- a.Extract(argv[i]);
- }
- }
- }
- }
- }
- } else { // full archive extraction:
- for (int i = 0; i < (int)a.NumFiles(); i++)
- a.Extract(a.GetFile(i)->name);
- }
-}
-
-void del(DataArchive& a, int argc, char* argv[])
-{
- char sPath[256];
- char sPatt[256];
- char* pDirSep;
- int nPath;
-
- for (int i = 0; i < argc; i++) {
- if (strchr(argv[i], '*')) {
- strcpy(sPath, argv[i]);
-
- if ((pDirSep = strrchr(sPath, '\\')) != 0) {
- strcpy(sPatt, pDirSep+1);
- *pDirSep = 0;
- nPath = strlen(sPath);
- } else if ((pDirSep = strrchr(sPath, '/')) != 0) {
- strcpy(sPatt, pDirSep+1);
- *pDirSep = 0;
- nPath = strlen(sPath);
- } else {
- strcpy(sPatt, sPath);
- sPath[0] = 0;
- nPath = 0;
- }
-
- // for each file in the archive:
- for (unsigned j = 0; j < a.NumFiles(); j++) {
- DataEntry* pde = a.GetFile(j);
-
- if (pde) {
- // if we are deleting from a sub-directory,
- if (nPath) {
- // and this file is in the sub-directory,
- if (_strnicmp(pde->name, sPath, nPath) == 0) {
- // and this file matches the pattern:
- if (match(pde->name+nPath+1, sPatt)) {
- char sName[256];
- strcpy(sName, pde->name);
- a.Remove(sName);
- }
- }
- } else {
- // if we are deleting from the main directory,
- // and this file is in the main directory,
- if (strchr(pde->name, '/') == 0) {
- // and this file matches the pattern:
- if (match(pde->name, sPatt)) {
- char sName[256];
- strcpy(sName, pde->name);
- a.Remove(sName);
- }
- }
- }
- }
- }
- } else {
- a.Remove(argv[i]);
- }
- }
-}
-
-
-// +--------------------------------------------------------------------+
-
-void Usage()
-{
- printf("Usage: datafile <dat-file> -option <file list>\n");
- printf("options: -ins (insert files into datafile)\n");
- printf(" -ext (extract files from datafile)\n");
- printf(" -del (delete files from datafile)\n");
- printf(" -mak (insert all files in current directory and all subdirectories)\n");
- printf(" -list (display list of entries in datafile)\n");
-
- exit(-1);
-}
-
-#define OPT_NONE 0
-#define OPT_INS 1
-#define OPT_EXT 2
-#define OPT_DEL 3
-#define OPT_MAK 4
-#define OPT_LIST 5
-
-int main(int argc, char* argv[])
-{
- printf("DATAFILE\n");
-
- if (argc < 3)
- Usage();
-
- DataArchive a(argv[1]);
- int option = OPT_NONE;
-
- if (!_stricmp(argv[2], "-ins")) option = OPT_INS;
- else if (!_stricmp(argv[2], "-ext")) option = OPT_EXT;
- else if (!_stricmp(argv[2], "-del")) option = OPT_DEL;
- else if (!_stricmp(argv[2], "-mak")) option = OPT_MAK;
- else if (!_stricmp(argv[2], "-list")) option = OPT_LIST;
-
- argc -= 3;
- argv += 3;
-
- switch (option) {
- default:
- case OPT_NONE: Usage(); break;
- case OPT_INS: ins(a, argc, argv); break;
- case OPT_EXT: ext(a, argc, argv); break;
- case OPT_DEL: del(a, argc, argv); break;
- case OPT_MAK: mak(a); break;
- case OPT_LIST: a.List(); break;
- }
-
- return 0;
-}
-
diff --git a/StarsEx/Archive.cpp b/StarsEx/Archive.cpp
deleted file mode 100644
index 46a4541..0000000
--- a/StarsEx/Archive.cpp
+++ /dev/null
@@ -1,597 +0,0 @@
-/* Starshatter: The Open Source Project
- Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
- Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
- Copyright (c) 1997-2006, Destroyer Studios LLC.
-
- AUTHOR: John DiCamillo
-
-*/
-
-#include "Archive.h"
-
-#include <fcntl.h>
-#include <io.h>
-#include <stdio.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <time.h>
-
-#include <cstdint>
-#include <cstring>
-
-#include <zlib.h>
-
-#include "Utils.h"
-
-
-// +--------------------------------------------------------------------+
-
-int verbose = 1;
-int err;
-
-#define CHECK_ERR(err, msg) { \
- if (err != Z_OK) { \
- fprintf(stderr, "%s error: %d\n", msg, err); \
- exit(1); \
- } \
- }
-
-// +--------------------------------------------------------------------+
-
-DataArchive::DataArchive(const char* name)
-{
- std::memset(this, 0, sizeof(DataArchive));
-
- if (name)
- LoadDatafile(name);
-}
-
-DataArchive::~DataArchive()
-{
- delete [] block_map;
- delete [] directory;
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::WriteEntry(int index, std::uint8_t* buf)
-{
- int f = _open(datafile, _O_RDWR|_O_CREAT|_O_BINARY, _S_IREAD|_S_IWRITE);
-
- if (f != -1) {
- header.dir_size_comp = DirBlocks() * Archive::BLOCK_SIZE;
- dirbuf = new std::uint8_t[header.dir_size_comp];
-
- if (!dirbuf) {
- err = Z_MEM_ERROR;
- }
-
- else {
- auto dir_size_comp = static_cast<uLongf>(header.dir_size_comp);
- err = compress(dirbuf, &dir_size_comp,
- (std::uint8_t*) directory, header.nfiles * sizeof(DataEntry));
- header.dir_size_comp = static_cast<std::uint32_t>(dir_size_comp);
- CHECK_ERR(err, "compress");
-
- header.dir_blocks = Blocks(header.dir_size_comp) * Archive::BLOCK_SIZE;
-
- _lseek(f, 0, SEEK_SET);
- _write(f, &header, sizeof(DataHeader));
- _lseek(f, sizeof(DataHeader) + header.dir_offset, SEEK_SET);
- _write(f, dirbuf, header.dir_blocks);
-
- delete [] dirbuf;
- dirbuf = 0;
- }
-
- if (buf && directory && directory[index].size_comp) {
- _lseek(f, sizeof(DataHeader) + directory[index].offset, SEEK_SET);
- _write(f, buf, directory[index].size_comp);
- }
- _close(f);
- }
- else
- perror("WriteEntry");
-}
-
-// +--------------------------------------------------------------------+
-
-std::uint32_t DataArchive::Blocks(std::uint32_t raw_size)
-{
- int full_blocks = raw_size / Archive::BLOCK_SIZE;
- int part_blocks = (raw_size % Archive::BLOCK_SIZE) > 0;
-
- return full_blocks + part_blocks;
-}
-
-std::uint32_t DataArchive::DirBlocks()
-{
- std::uint32_t result = Blocks(header.nfiles * sizeof(DataEntry));
- if (result == 0) result = 1;
- return result;
-}
-
-std::uint32_t DataArchive::FileBlocks(int index)
-{
- if (index >= 0 && index < (int) header.nfiles && directory)
- return Blocks(directory[index].size_comp);
-
- return 0;
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::CreateBlockMap()
-{
- delete [] block_map;
- block_map = 0;
-
- if (header.nfiles == 0) return;
-
- std::uint32_t i,j;
- std::uint32_t dir_usage = header.dir_offset + DirBlocks() * Archive::BLOCK_SIZE;
- std::uint32_t max_usage = dir_usage;
-
- for (i = 0; i < header.nfiles; i++) {
- std::uint32_t last_block = directory[i].offset + FileBlocks(i) * Archive::BLOCK_SIZE;
- if (last_block > max_usage)
- max_usage = last_block;
- }
-
- nblocks = max_usage/Archive::BLOCK_SIZE;
- block_map = new std::uint32_t[nblocks];
-
- if (!block_map) {
- nblocks = 0;
- }
-
- else {
- std::memset(block_map, 0, nblocks * sizeof(std::uint32_t));
-
- std::uint32_t first_block = header.dir_offset/Archive::BLOCK_SIZE +
- (header.dir_offset%Archive::BLOCK_SIZE > 0);
-
- for (j = 0; j < DirBlocks(); j++)
- block_map[first_block+j] = 1;
-
- for (i = 0; i < header.nfiles; i++) {
- std::uint32_t first_block = directory[i].offset/Archive::BLOCK_SIZE +
- (directory[i].offset%Archive::BLOCK_SIZE > 0);
-
- for (j = 0; j < FileBlocks(i); j++)
- block_map[first_block+j] = i+2;
- }
- }
-}
-
-// +--------------------------------------------------------------------+
-
-int DataArchive::FindDataBlocks(int need)
-{
- if ((int) (nblocks)-need > 0) {
- std::uint32_t start;
- int i;
-
- for (start = 0; start < nblocks-need; start++) {
- for (i = 0; block_map[start+i] == 0 && i < need; i++);
-
- if (i == need) return start*Archive::BLOCK_SIZE;
-
- start += i;
- }
- }
-
- return nblocks*Archive::BLOCK_SIZE;
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::LoadDatafile(const char* name)
-{
- if (!name) return;
-
- delete [] directory;
- delete [] block_map;
-
- std::memset(this, 0, sizeof(DataArchive));
- std::strncpy(datafile, name, Archive::NAMELEN-1);
-
- FILE* f;
- fopen_s(&f, datafile, "rb");
- if (f) {
- fread(&header, sizeof(DataHeader), 1, f);
-
- if (header.version != Archive::VERSION) {
- Print("ERROR: datafile '%s' invalid version '%d'\n",
- datafile, header.version);
- fclose(f);
- std::memset(&header, 0, sizeof(header));
- return;
- }
-
- uLongf len = DirBlocks() * Archive::BLOCK_SIZE;
- std::uint32_t dirsize = header.nfiles + 64;
-
- dirbuf = new std::uint8_t[len];
- directory = new DataEntry[dirsize];
-
- if (!dirbuf || !directory) {
- err = Z_MEM_ERROR;
- }
-
- else {
- std::memset(directory, 0, sizeof(DataEntry) * dirsize);
-
- fseek(f, sizeof(DataHeader) + header.dir_offset, SEEK_SET);
- fread(dirbuf, header.dir_size_comp, 1, f);
-
- int err = uncompress((std::uint8_t*) directory, &len,
-#pragma warning(suppress: 6029)
- dirbuf, header.dir_size_comp);
- if (err != Z_OK)
- std::memset(directory, 0, sizeof(DataEntry) * dirsize);
-
- delete [] dirbuf;
- dirbuf = 0;
-
- CreateBlockMap();
- }
- }
- else {
- Print("Creating Archive '%s'...\n", datafile);
-
- header.version = Archive::VERSION;
- header.nfiles = 0;
- header.dir_blocks = 0;
- header.dir_size_comp = 0;
- header.dir_offset = 0;
-
- nblocks = DirBlocks();
-
- delete [] block_map;
- block_map = 0;
- }
-}
-
-// +--------------------------------------------------------------------+
-
-int DataArchive::FindEntry(const char* req_name)
-{
- int entry = -1;
-
- if (req_name && *req_name && directory) {
- char path[256];
- int len = strlen(req_name);
-
- std::memset(path, 0, sizeof(path));
-
- for (int c = 0; c < len; c++) {
- if (req_name[c] == '\\')
- path[c] = '/';
- else
- path[c] = req_name[c];
- }
-
- for (std::uint32_t i = 0; i < header.nfiles; i++) {
- if (!_stricmp(directory[i].name, path))
- return i;
- }
- }
-
- return entry;
-}
-
-// +--------------------------------------------------------------------+
-
-std::uint8_t* DataArchive::CompressEntry(int i)
-{
- if (directory && i >= 0 && i < (int) header.nfiles) {
- char* name = directory[i].name;
-
- FILE* f;
- fopen_s(&f, name, "rb");
-
- if (f) {
- fseek(f, 0, SEEK_END);
- std::uint32_t len = ftell(f);
- fseek(f, 0, SEEK_SET);
-
- std::uint8_t* buf = new std::uint8_t[len];
-
- if (!buf) {
- err = Z_MEM_ERROR;
- }
-
- else {
- fread(buf, len, 1, f);
- fclose(f);
-
- directory[i].size_orig = len;
-
- directory[i].size_comp = (int) (len * 1.1);
- std::uint8_t* cbuf = new std::uint8_t[directory[i].size_comp];
-
- if (!cbuf) {
- err = Z_MEM_ERROR;
- }
- else {
- auto size_comp = static_cast<uLongf>(directory[i].size_comp);
- err = compress(cbuf, &size_comp, buf, len);
- directory[i].size_comp = static_cast<std::uint32_t>(size_comp);
- CHECK_ERR(err, "compress");
- }
-
- delete [] buf;
- return cbuf;
- }
- }
- }
-
- return 0;
-}
-
-// +--------------------------------------------------------------------+
-
-int DataArchive::ExpandEntry(int i, std::uint8_t*& buf, bool null_terminate)
-{
- uLongf len = 0;
-
- if (directory && i >= 0 && i < (int) header.nfiles) {
- FILE* f;
- fopen_s(&f, datafile, "rb");
-
- if (f) {
- std::uint32_t clen = directory[i].size_comp;
- std::uint8_t* cbuf = new std::uint8_t[clen];
-
- if (!cbuf) {
- err = Z_MEM_ERROR;
- }
-
- else {
- fseek(f, sizeof(DataHeader) + directory[i].offset, SEEK_SET);
- fread(cbuf, clen, 1, f);
-
- len = directory[i].size_orig;
-
- if (null_terminate) {
- buf = new std::uint8_t[len+1];
- if (buf) buf[len] = 0;
- }
-
- else {
- buf = new std::uint8_t[len];
- }
-
- if (!buf) {
- err = Z_MEM_ERROR;
- }
-
- else {
- err = uncompress(buf, &len, cbuf, clen);
- if (err != Z_OK) {
- delete [] buf;
- buf = 0;
- }
- }
-
- delete [] cbuf;
- fclose(f);
- }
- }
- }
-
- return len;
-}
-
-// +--------------------------------------------------------------------+
-
-int DataArchive::InsertEntry(const char* name)
-{
- if (name && *name) {
- char path[256];
- std::uint32_t len = strlen(name);
-
- std::memset(path, 0, sizeof(path));
-
- for (std::uint32_t c = 0; c < len; c++) {
- if (name[c] == '\\')
- path[c] = '/';
- else
- path[c] = name[c];
- }
-
- int dirsize = header.nfiles + 64;
-
- if (directory && dirsize) {
- for (int i = 0; i < dirsize; i++) {
- if (directory[i].size_orig == 0) {
- std::memset(directory[i].name, 0, Archive::NAMELEN);
- std::strncpy(directory[i].name, path, Archive::NAMELEN);
- directory[i].name[Archive::NAMELEN-1] = '\0';
- directory[i].size_orig = 1;
-
- return i;
- }
- }
- }
-
- DataEntry* dir = new DataEntry[dirsize + 64];
-
- if (directory && dirsize) {
- std::memset(dir, 0, (dirsize + 64) * sizeof(DataEntry));
- std::memcpy(dir, directory, dirsize * sizeof(DataEntry));
- }
-
- delete [] directory;
-
- header.nfiles = dirsize + 64;
- directory = dir;
-
- std::memset(directory[dirsize].name, 0, Archive::NAMELEN);
- std::strncpy(directory[dirsize].name, path, Archive::NAMELEN);
- directory[dirsize].name[Archive::NAMELEN-1] = '\0';
- directory[dirsize].size_orig = 1;
-
- return dirsize;
- }
-
- return -1;
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::RemoveEntry(int index)
-{
- if (directory && index >= 0 && index < (int) header.nfiles)
- std::memset(&directory[index], 0, sizeof(DataEntry));
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::Insert(const char* name)
-{
- std::uint32_t old_blocks = 0, old_offset = 0, new_blocks = 0;
- std::uint32_t old_dir_blocks = 0, old_dir_offset = 0, new_dir_blocks = 0;
- int added = 0;
-
- int index = FindEntry(name);
-
- if (index < 0) {
- old_dir_blocks = DirBlocks();
- old_dir_offset = header.dir_offset;
-
- index = InsertEntry(name);
-
- if (index >= (int) header.nfiles) {
- header.nfiles = index+1;
- added = 1;
- }
-
- new_dir_blocks = DirBlocks();
-
- if (new_dir_blocks > old_dir_blocks) {
- header.dir_offset = FindDataBlocks(new_dir_blocks);
- CreateBlockMap();
- }
- }
- else {
- old_blocks = FileBlocks(index);
- old_offset = directory[index].offset;
- }
-
- if (index >= 0) {
- DataEntry& e = directory[index];
-
- if (verbose) Print(" Inserting: %-16s ", e.name);
-
- std::uint8_t* buf = CompressEntry(index);
-
- if (!buf) {
- // this is (almost) unrecoverable,
- // so we quit before screwing things up:
- Print("ERROR: Could not compress %d:%s\n", index, directory[index].name);
- exit(1);
- }
-
- new_blocks = FileBlocks(index);
-
- // the file is new, or got bigger,
- // need to find room for the data:
- if (new_blocks > old_blocks) {
- directory[index].offset = FindDataBlocks(new_blocks);
- CreateBlockMap();
- }
-
- WriteEntry(index, buf);
- delete [] buf;
-
- if (verbose) {
- int ratio = (int) (100.0 * (double) e.size_comp / (double) e.size_orig);
- Print("%9d => %9d (%2d%%)\n", e.size_orig, e.size_comp, ratio);
- }
- }
- else if (added)
- header.nfiles--;
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::Extract(const char* name)
-{
- int index = FindEntry(name);
-
- if (!directory || index < 0 || index >= (int) header.nfiles) {
- Print("Could not extract '%s', not found\n", name);
- return;
- }
-
- std::uint8_t* buf;
- ExpandEntry(index, buf);
-
- FILE* f;
- fopen_s(&f, directory[index].name, "wb");
- if (f) {
- fwrite(buf, directory[index].size_orig, 1, f);
- fclose(f);
- }
- else
- Print("Could not extract '%s', could not open file for writing\n", name);
-
- delete [] buf;
-
- if (verbose) Print(" Extracted: %s\n", name);
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::Remove(const char* name)
-{
- int index = FindEntry(name);
-
- if (!directory || index < 0 || index >= (int) header.nfiles) {
- Print("Could not remove '%s', not found\n", name);
- return;
- }
-
- RemoveEntry(index);
- WriteEntry(index, 0);
-
- if (verbose) Print(" Removed: %s\n", name);
-}
-
-// +--------------------------------------------------------------------+
-
-void DataArchive::List()
-{
- int total_orig = 0;
- int total_comp = 0;
-
- printf("DATAFILE: %s\n", datafile);
- printf("Files: %d\n", header.nfiles); //-V576
- printf("\n");
-
- if (directory && header.nfiles) {
- printf("Index Orig Size Comp Size Ratio Name\n");
- printf("----- --------- --------- ----- ----------------\n");
-
- for (std::uint32_t i = 0; i < header.nfiles; i++) {
- DataEntry& e = directory[i];
- int ratio = (int) (100.0 * (double) e.size_comp / (double) e.size_orig);
-
- printf("%5d %9d %9d %2d%% %s\n", i+1, e.size_orig, e.size_comp, ratio, e.name); //-V576
-
- total_orig += e.size_orig;
- total_comp += e.size_comp;
- }
-
- int total_ratio = (int) (100.0 * (double) total_comp / (double) total_orig);
-
- printf("----- --------- --------- -----\n");
- printf("TOTAL %9d %9d %2d%%\n\n", total_orig, total_comp, total_ratio);
- }
-}
-
-
-// +--------------------------------------------------------------------+
-
diff --git a/StarsEx/Archive.h b/StarsEx/Archive.h
deleted file mode 100644
index 825cc1f..0000000
--- a/StarsEx/Archive.h
+++ /dev/null
@@ -1,93 +0,0 @@
-/* Starshatter: The Open Source Project
- Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
- Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
- Copyright (c) 1997-2006, Destroyer Studios LLC.
-
- AUTHOR: John DiCamillo
-
-*/
-
-#ifndef Archive_h
-#define Archive_h
-
-#include <cstdint>
-
-// +------------------------------------------------------------------+
-
-namespace Archive
-{
-static constexpr std::uint32_t VERSION {0x0010};
-static constexpr std::size_t BLOCK_SIZE {1024};
-static constexpr std::size_t NAMELEN {64};
-}
-
-// +------------------------------------------------------------------+
-
-struct DataHeader
-{
- static const char* TYPENAME() { return "DataHeader"; }
-
- std::uint32_t version;
- std::uint32_t nfiles;
- std::uint32_t dir_blocks;
- std::uint32_t dir_size_comp;
- std::uint32_t dir_offset;
-};
-
-struct DataEntry
-{
- static const char* TYPENAME() { return "DataEntry"; }
-
- char name[Archive::NAMELEN];
- std::uint32_t size_orig;
- std::uint32_t size_comp;
- std::uint32_t offset;
-};
-
-class DataArchive
-{
-public:
- static const char* TYPENAME() { return "DataArchive"; }
-
- // ctor:
- DataArchive(const char* name = 0);
- ~DataArchive();
-
- // operations:
- void LoadDatafile(const char* name);
- void Insert(const char* name);
- void Extract(const char* name);
- void Remove(const char* name);
- void List();
-
- void WriteEntry(int index, std::uint8_t* buf);
- int FindEntry(const char* req_name);
- int ExpandEntry(int index, std::uint8_t*& buf, bool null_terminate=false);
- std::uint8_t* CompressEntry(int index);
- int InsertEntry(const char* name);
- void RemoveEntry(int index);
- std::uint32_t Blocks(std::uint32_t raw_size);
- std::uint32_t DirBlocks();
- std::uint32_t FileBlocks(int index);
- int FindDataBlocks(int blocks_needed);
- void CreateBlockMap();
-
- std::uint32_t NumFiles() { return header.nfiles; }
- DataEntry* GetFile(int i) { if (i>=0 && i<(int)header.nfiles) return &directory[i]; return 0; }
-
- const char* Name() const { return datafile; }
-
-private:
- // persistent data members:
- DataHeader header;
- DataEntry* directory;
- std::uint8_t* dirbuf;
-
- // transient members:
- char datafile[Archive::NAMELEN];
-
- std::uint32_t* block_map;
- std::uint32_t nblocks;
-};
-
-#endif // Archive_h
diff --git a/StarsEx/CMakeLists.txt b/StarsEx/CMakeLists.txt
index 7e1ac53..b99f765 100644
--- a/StarsEx/CMakeLists.txt
+++ b/StarsEx/CMakeLists.txt
@@ -3,7 +3,6 @@ include(GitDescribe)
add_library(
StarsEx STATIC
ActiveWindow.cpp
- Archive.cpp
Asteroid.cpp
AudDlg.cpp
AudioConfig.cpp
@@ -277,6 +276,7 @@ target_include_directories(
)
target_link_libraries(
StarsEx
+ PUBLIC ArchiveEx
PUBLIC DefinitionEx
PUBLIC FoundationEx
PUBLIC NetEx
diff --git a/StarsEx/DataLoader.cpp b/StarsEx/DataLoader.cpp
index b4ee74b..69bdb14 100644
--- a/StarsEx/DataLoader.cpp
+++ b/StarsEx/DataLoader.cpp
@@ -9,7 +9,6 @@
#include "DataLoader.h"
#include "DataSource.h"
-#include "Archive.h"
#include "Color.h"
#include "D3DXImage.h"
#include "Bitmap.h"
@@ -99,8 +98,8 @@ DataLoader::MountDatafile(const char* name, int pos)
return FAILED;
}
fclose(f);
- auto archive = new DataArchive(name);
- if (!archive || archive->NumFiles() < 1) {
+ auto archive = new ArchiveEx::Archive(name);
+ if (!archive || archive->NumEntries() < 1) {
last_error = Text::format("Invalid datafile '%s'", name);
if (archive) delete archive;
return FAILED;
diff --git a/StarsEx/DataLoader.h b/StarsEx/DataLoader.h
index f3fbb32..13595dd 100644
--- a/StarsEx/DataLoader.h
+++ b/StarsEx/DataLoader.h
@@ -10,7 +10,6 @@
#ifndef DataLoader_h
#define DataLoader_h
-#include "Archive.h"
#include "DataSource.h"
#include "Types.h"
#include "List.h"
diff --git a/StarsEx/DataSource.cpp b/StarsEx/DataSource.cpp
index 3ace2ca..294f4a5 100644
--- a/StarsEx/DataSource.cpp
+++ b/StarsEx/DataSource.cpp
@@ -11,7 +11,7 @@
#include <cstring>
#include <filesystem>
-#include "Archive.h"
+#include <Archive.h>
#include "List.h"
#include "Text.h"
#include "Utils.h"
@@ -39,12 +39,12 @@ DataSource::Id() const
ArchiveDataSource::ArchiveDataSource(const char* name) :
- ArchiveDataSource(new DataArchive(name))
+ ArchiveDataSource(new ArchiveEx::Archive(name))
{
}
-ArchiveDataSource::ArchiveDataSource(DataArchive* archive) :
+ArchiveDataSource::ArchiveDataSource(ArchiveEx::Archive* archive) :
DataSource(),
m_archive {archive}
{
@@ -60,7 +60,7 @@ ArchiveDataSource::~ArchiveDataSource()
bool
ArchiveDataSource::Find(const Text& prefix, const char* name) const
{
- const int index = m_archive->FindEntry(prefix.concat(name));
+ const int index = m_archive->Find(prefix.concat(name));
return index > -1;
}
@@ -68,20 +68,18 @@ ArchiveDataSource::Find(const Text& prefix, const char* name) const
int
ArchiveDataSource::ListFiles(const Text& prefix, Text filter, List<Text>& items, bool recurse) const
{
- (void) recurse; // Lookup in DataArchives was always recursive so far
+ (void) recurse; // Lookup in Archives was always recursive so far
filter = filter.replace("*", ""); // Wildcards worked only on boundaries
const int first = prefix.length();
- const int count = m_archive->NumFiles();
- for (int i = 0; i < count; ++i) {
- const auto* entry = m_archive->GetFile(i);
- Text name = entry->name;
+ m_archive->ForEachEntry([&](const char* path){
+ Text name{path};
name.setSensitive(false);
if (name.contains(prefix) && name.contains(filter)) {
const auto without_prefix = name.substring(first, name.length());
if (!items.contains(&without_prefix))
items.append(new Text(without_prefix));
}
- }
+ });
return items.size();
}
@@ -89,10 +87,8 @@ ArchiveDataSource::ListFiles(const Text& prefix, Text filter, List<Text>& items,
int
ArchiveDataSource::Load(const Text& prefix, const char* name, std::uint8_t*& buf, bool null_terminate) const
{
- const int index = m_archive->FindEntry(prefix.concat(name));
- if (index > -1)
- return m_archive->ExpandEntry(index, buf, null_terminate);
- return 0; // -1 would be preferable, but 0 is from legacy
+ const int result = m_archive->Extract(prefix.concat(name), buf, null_terminate);
+ return -1 == result ? 0 : result; // -1 would be preferable, but 0 is legacy
}
diff --git a/StarsEx/DataSource.h b/StarsEx/DataSource.h
index e945dff..6809a44 100644
--- a/StarsEx/DataSource.h
+++ b/StarsEx/DataSource.h
@@ -9,7 +9,7 @@
#include <cstdint>
-#include "Archive.h"
+#include <Archive.h>
#include "List.h"
#include "Text.h"
@@ -38,7 +38,7 @@ class ArchiveDataSource : public DataSource
{
public:
explicit ArchiveDataSource(const char* name);
- explicit ArchiveDataSource(DataArchive* archive);
+ explicit ArchiveDataSource(ArchiveEx::Archive* archive);
~ArchiveDataSource() override;
bool Find(const Text& prefix, const char* name) const override;
@@ -46,7 +46,7 @@ public:
int Load(const Text& prefix, const char* name, std::uint8_t*& buf, bool null_terminate=false) const override;
protected:
- DataArchive* m_archive;
+ ArchiveEx::Archive* m_archive;
};
diff --git a/StarsEx/ModInfo.cpp b/StarsEx/ModInfo.cpp
index 271517c..d884dec 100644
--- a/StarsEx/ModInfo.cpp
+++ b/StarsEx/ModInfo.cpp
@@ -11,13 +11,13 @@
Information block for describing and deploying third party mods
*/
+#include <Archive.h>
#include "ModInfo.h"
#include "Campaign.h"
#include "ShipDesign.h"
#include "ParseUtil.h"
-#include "Archive.h"
#include "DataLoader.h"
#include "Pcx.h"
#include "Bitmap.h"
@@ -58,12 +58,12 @@ ModInfo::Load(const char* fname)
bool ok = false;
filename = fname;
- DataArchive a(filename);
+ ArchiveEx::Archive a(filename);
- int n = a.FindEntry("mod_info.def");
+ int n = a.Find("mod_info.def");
if (n > -1) {
BYTE* buf = 0;
- int len = a.ExpandEntry(n, buf, true);
+ int len = a.Extract(n, buf, true);
if (len > 0 && buf != 0) {
ok = ParseModInfo((const char*) buf);
@@ -76,10 +76,10 @@ ModInfo::Load(const char* fname)
logo = new Bitmap;
- n = a.FindEntry(logoname);
+ n = a.Find(logoname);
if (n > -1) {
BYTE* buf = 0;
- int len = a.ExpandEntry(n, buf, true);
+ int len = a.Extract(n, buf, true);
pcx.LoadBuffer(buf, len);
delete [] buf;
diff --git a/StarsEx/Starshatter.cpp b/StarsEx/Starshatter.cpp
index f53638d..b3a4be3 100644
--- a/StarsEx/Starshatter.cpp
+++ b/StarsEx/Starshatter.cpp
@@ -105,7 +105,6 @@
#include "TrackIR.h"
#include "EventDispatch.h"
#include "MultiController.h"
-#include "Archive.h"
#include "DataLoader.h"
#include "Random.h"
#include "Universe.h"
diff --git a/cmake/modules/AddDatafile.cmake b/cmake/modules/AddDatafile.cmake
index 6deda52..ea1c941 100644
--- a/cmake/modules/AddDatafile.cmake
+++ b/cmake/modules/AddDatafile.cmake
@@ -15,7 +15,7 @@
# additional directories with files that will also get included in the final datafile. In case of conflicts, files from
# source tree will be used. This is to allow working with binary assets located in source directory.
#
-# To create the archive the Datafile.exe is used - for non-Windows platforms an emulator that can run it is required.
+# To create the archive the dat.exe is used - for non-Windows platforms an emulator that can run it is required.
function(add_datafile DATAFILE_TARGET)
cmake_parse_arguments(
@@ -57,7 +57,7 @@ function(add_datafile DATAFILE_TARGET)
${CMAKE_COMMAND} -E chdir ${RESOURCE_DIRECTORY}
${CMAKE_COMMAND} -E env "${DATAFILE_VAR}='${DATAFILE_PATHS}'"
${CMAKE_CORSSCOMPILING_EMULATOR}
- $<TARGET_FILE:Datafile> $<SHELL_PATH:${DATAFILE_OUTPUT}> -mak
+ $<TARGET_FILE:dat> -cu $<SHELL_PATH:${DATAFILE_OUTPUT}> .
)
endforeach()
file(
@@ -73,7 +73,7 @@ function(add_datafile DATAFILE_TARGET)
${CMAKE_COMMAND} -E chdir ${DATAFILE_SOURCE}
${CMAKE_COMMAND} -E env "${DATAFILE_VAR}='${DATAFILE_PATHS}'"
${CMAKE_CORSSCOMPILING_EMULATOR}
- $<TARGET_FILE:Datafile> $<SHELL_PATH:${DATAFILE_OUTPUT}> -mak
+ $<TARGET_FILE:dat> -cu $<SHELL_PATH:${DATAFILE_OUTPUT}> .
)
add_custom_target(${DATAFILE_TARGET} ALL DEPENDS ${DATAFILE_OUTPUT})
endfunction()