summaryrefslogtreecommitdiffhomepage
path: root/StarsEx
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-04-10 15:24:32 +0200
committerAki <please@ignore.pl>2022-04-10 15:25:02 +0200
commit034aa81895b201164004d79aa090e882e3e66945 (patch)
tree502ad20a5488c3258c866514e9a286e89321f8f4 /StarsEx
parente8c40a99cb00ea7f68dcb1b8414065785c89df9c (diff)
downloadstarshatter-034aa81895b201164004d79aa090e882e3e66945.zip
starshatter-034aa81895b201164004d79aa090e882e3e66945.tar.gz
starshatter-034aa81895b201164004d79aa090e882e3e66945.tar.bz2
Moved archives list from static part of impl to loader member
Including Archive.h in DataLoader.h created name conflicts and created an error due to missing types from Types.h in the Archive.h itself.
Diffstat (limited to 'StarsEx')
-rw-r--r--StarsEx/Archive.cpp46
-rw-r--r--StarsEx/Archive.h18
-rw-r--r--StarsEx/DataLoader.cpp9
-rw-r--r--StarsEx/DataLoader.h3
4 files changed, 43 insertions, 33 deletions
diff --git a/StarsEx/Archive.cpp b/StarsEx/Archive.cpp
index 9d859c2..ca73ec5 100644
--- a/StarsEx/Archive.cpp
+++ b/StarsEx/Archive.cpp
@@ -55,7 +55,7 @@ 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;
+ header.dir_size_comp = DirBlocks() * Archive::BLOCK_SIZE;
dirbuf = new BYTE[header.dir_size_comp];
if (!dirbuf) {
@@ -67,7 +67,7 @@ void DataArchive::WriteEntry(int index, BYTE* buf)
(BYTE*) directory, header.nfiles * sizeof(DataEntry));
CHECK_ERR(err, "compress");
- header.dir_blocks = Blocks(header.dir_size_comp) * BLOCK_SIZE;
+ header.dir_blocks = Blocks(header.dir_size_comp) * Archive::BLOCK_SIZE;
_lseek(f, 0, SEEK_SET);
_write(f, &header, sizeof(DataHeader));
@@ -92,8 +92,8 @@ void DataArchive::WriteEntry(int index, BYTE* buf)
DWORD DataArchive::Blocks(DWORD raw_size)
{
- int full_blocks = raw_size / BLOCK_SIZE;
- int part_blocks = (raw_size % BLOCK_SIZE) > 0;
+ int full_blocks = raw_size / Archive::BLOCK_SIZE;
+ int part_blocks = (raw_size % Archive::BLOCK_SIZE) > 0;
return full_blocks + part_blocks;
}
@@ -123,16 +123,16 @@ void DataArchive::CreateBlockMap()
if (header.nfiles == 0) return;
DWORD i,j;
- DWORD dir_usage = header.dir_offset + DirBlocks() * BLOCK_SIZE;
+ DWORD dir_usage = header.dir_offset + DirBlocks() * Archive::BLOCK_SIZE;
DWORD max_usage = dir_usage;
for (i = 0; i < header.nfiles; i++) {
- DWORD last_block = directory[i].offset + FileBlocks(i) * BLOCK_SIZE;
+ DWORD last_block = directory[i].offset + FileBlocks(i) * Archive::BLOCK_SIZE;
if (last_block > max_usage)
max_usage = last_block;
}
- nblocks = max_usage/BLOCK_SIZE;
+ nblocks = max_usage/Archive::BLOCK_SIZE;
block_map = new DWORD[nblocks];
if (!block_map) {
@@ -142,15 +142,15 @@ void DataArchive::CreateBlockMap()
else {
ZeroMemory(block_map, nblocks*sizeof(DWORD));
- DWORD first_block = header.dir_offset/BLOCK_SIZE +
- (header.dir_offset%BLOCK_SIZE > 0);
+ DWORD 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++) {
- DWORD first_block = directory[i].offset/BLOCK_SIZE +
- (directory[i].offset%BLOCK_SIZE > 0);
+ DWORD 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;
@@ -169,13 +169,13 @@ int DataArchive::FindDataBlocks(int need)
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;
+ if (i == need) return start*Archive::BLOCK_SIZE;
start += i;
}
}
- return nblocks*BLOCK_SIZE;
+ return nblocks*Archive::BLOCK_SIZE;
}
// +--------------------------------------------------------------------+
@@ -188,14 +188,14 @@ void DataArchive::LoadDatafile(const char* name)
delete [] block_map;
ZeroMemory(this, sizeof(DataArchive));
- strncpy_s(datafile, name, NAMELEN-1);
+ strncpy_s(datafile, name, Archive::NAMELEN-1);
FILE* f;
fopen_s(&f, datafile, "rb");
if (f) {
fread(&header, sizeof(DataHeader), 1, f);
- if (header.version != VERSION) {
+ if (header.version != Archive::VERSION) {
Print("ERROR: datafile '%s' invalid version '%d'\n",
datafile, header.version);
fclose(f);
@@ -203,7 +203,7 @@ void DataArchive::LoadDatafile(const char* name)
return;
}
- DWORD len = DirBlocks() * BLOCK_SIZE;
+ DWORD len = DirBlocks() * Archive::BLOCK_SIZE;
DWORD dirsize = header.nfiles + 64;
dirbuf = new BYTE[len];
@@ -234,7 +234,7 @@ void DataArchive::LoadDatafile(const char* name)
else {
Print("Creating Archive '%s'...\n", datafile);
- header.version = VERSION;
+ header.version = Archive::VERSION;
header.nfiles = 0;
header.dir_blocks = 0;
header.dir_size_comp = 0;
@@ -398,9 +398,9 @@ int DataArchive::InsertEntry(const char* name)
if (directory && dirsize) {
for (int i = 0; i < dirsize; i++) {
if (directory[i].size_orig == 0) {
- ZeroMemory(directory[i].name, NAMELEN);
- strncpy_s(directory[i].name, path, NAMELEN);
- directory[i].name[NAMELEN-1] = '\0';
+ ZeroMemory(directory[i].name, Archive::NAMELEN);
+ strncpy_s(directory[i].name, path, Archive::NAMELEN);
+ directory[i].name[Archive::NAMELEN-1] = '\0';
directory[i].size_orig = 1;
return i;
@@ -420,9 +420,9 @@ int DataArchive::InsertEntry(const char* name)
header.nfiles = dirsize + 64;
directory = dir;
- ZeroMemory(directory[dirsize].name, NAMELEN);
- strncpy_s(directory[dirsize].name, path, NAMELEN);
- directory[dirsize].name[NAMELEN-1] = '\0';
+ ZeroMemory(directory[dirsize].name, Archive::NAMELEN);
+ strncpy_s(directory[dirsize].name, path, Archive::NAMELEN);
+ directory[dirsize].name[Archive::NAMELEN-1] = '\0';
directory[dirsize].size_orig = 1;
return dirsize;
diff --git a/StarsEx/Archive.h b/StarsEx/Archive.h
index ac22f28..1cf76e9 100644
--- a/StarsEx/Archive.h
+++ b/StarsEx/Archive.h
@@ -10,12 +10,18 @@
#ifndef Archive_h
#define Archive_h
+#include <cstdint>
+
+#include "Types.h"
+
// +------------------------------------------------------------------+
-#define VERSION 0x0010
-#define BLOCK_SIZE 1024
-#define FILE_BLOCK 1024
-#define NAMELEN 64
+namespace Archive
+{
+static constexpr std::uint32_t VERSION {0x0010};
+static constexpr std::size_t BLOCK_SIZE {1024};
+static constexpr std::size_t NAMELEN {64};
+}
// +------------------------------------------------------------------+
@@ -34,7 +40,7 @@ struct DataEntry
{
static const char* TYPENAME() { return "DataEntry"; }
- char name[NAMELEN];
+ char name[Archive::NAMELEN];
DWORD size_orig;
DWORD size_comp;
DWORD offset;
@@ -80,7 +86,7 @@ private:
BYTE* dirbuf;
// transient members:
- char datafile[NAMELEN];
+ char datafile[Archive::NAMELEN];
DWORD* block_map;
DWORD nblocks;
diff --git a/StarsEx/DataLoader.cpp b/StarsEx/DataLoader.cpp
index 4e35cab..72eaf4b 100644
--- a/StarsEx/DataLoader.cpp
+++ b/StarsEx/DataLoader.cpp
@@ -22,14 +22,17 @@
DataLoader* DataLoader::loader = 0;
-static List<DataArchive> archives;
-
// +--------------------------------------------------------------------+
DataLoader::DataLoader()
: datapath(""), video(0), use_file_system(true), enable_media(true)
{ }
+DataLoader::~DataLoader()
+{
+ archives.destroy();
+}
+
// +--------------------------------------------------------------------+
void
@@ -45,14 +48,12 @@ DataLoader::Initialize()
{
if (!loader) {
loader = new DataLoader;
- archives.destroy();
}
}
void
DataLoader::Close()
{
- archives.destroy();
Bitmap::ClearCache();
delete loader;
loader = nullptr;
diff --git a/StarsEx/DataLoader.h b/StarsEx/DataLoader.h
index 0331dcb..eaf4f28 100644
--- a/StarsEx/DataLoader.h
+++ b/StarsEx/DataLoader.h
@@ -10,6 +10,7 @@
#ifndef DataLoader_h
#define DataLoader_h
+#include "Archive.h"
#include "Types.h"
#include "List.h"
#include "Text.h"
@@ -30,6 +31,7 @@ public:
enum { DATAFILE_OK, DATAFILE_INVALID, DATAFILE_NOTEXIST };
DataLoader();
+ virtual ~DataLoader();
static DataLoader* GetLoader() { return loader; }
static void Initialize();
@@ -76,6 +78,7 @@ private:
bool use_file_system;
bool enable_media;
+ List<DataArchive> archives;
static DataLoader* loader;
};