Skip to content

Commit

Permalink
Doing proper multiplatform agnostic macros
Browse files Browse the repository at this point in the history
Because of the way this mess was done, for macOS and linux the old WINAPI functions were used but were replaced with POSIX calls.  So we will use the WINAPI for Windows and the 'fake' WINAPI for UNIX systems (with AVP in the name).
  • Loading branch information
atsb committed Apr 14, 2023
1 parent 7e9aa03 commit ca63f55
Show file tree
Hide file tree
Showing 12 changed files with 424 additions and 101 deletions.
52 changes: 42 additions & 10 deletions src/avp/win95/projload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3131,10 +3131,15 @@ void LoadModuleData()
HANDLE file = AVPCreateFile ("avp_rifs/module.bbb", GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
FILE_FLAG_RANDOM_ACCESS, 0);
unsigned long byteswritten;
AVPWriteFile(file,&Global_VDB_Ptr->VDB_World,sizeof(VECTORCH),&byteswritten,0);
AVPWriteFile(file,&Global_VDB_Ptr->VDB_Mat,sizeof(MATRIXCH),&byteswritten,0);

AVPCloseHandle(file);
#ifdef _WIN32
WriteFile(file, &Global_VDB_Ptr->VDB_World, sizeof(VECTORCH), &byteswritten, 0);
WriteFile(file, &Global_VDB_Ptr->VDB_Mat, sizeof(MATRIXCH), &byteswritten, 0);
CloseHandle(file);
#else
AVPWriteFile(file, &Global_VDB_Ptr->VDB_World, sizeof(VECTORCH), &byteswritten, 0);
AVPWriteFile(file, &Global_VDB_Ptr->VDB_Mat, sizeof(MATRIXCH), &byteswritten, 0);
AVPCloseHandle(file);
#endif

/* TODO: dir separator */
file = AVPCreateFile ("avp_rifs/module.aaa", GENERIC_READ, 0, 0, OPEN_EXISTING,
Expand Down Expand Up @@ -3354,7 +3359,11 @@ static BOOL WarnedAboutDiskSpace=FALSE;
static void MakeBackupFile(File_Chunk* fc)
{
unsigned long spc,bps,numclust,total;
if(AVPGetDiskFreeSpace(0,&spc,&bps,&numclust,&total))
#ifdef _WIN32
if (GetDiskFreeSpace(0, &spc, &bps, &numclust, &total))
#else
if (AVPGetDiskFreeSpace(0, &spc, &bps, &numclust, &total))
#endif
{
unsigned int freespace=spc*bps*numclust;
if(freespace<40000000)
Expand All @@ -3370,7 +3379,12 @@ static void MakeBackupFile(File_Chunk* fc)
}
WarnedAboutDiskSpace=FALSE;

AVPCreateDirectory("avp_rifs\\Backup",0);
#ifdef _WIN32
CreateDirectory("avp_rifs\\Backup", 0);
#else
AVPCreateDirectory("avp_rifs\\Backup", 0);
#endif

int length=strlen(fc->filename);
int pos=length;
while(pos>=0 && fc->filename[pos]!='\\')pos--;
Expand All @@ -3386,16 +3400,31 @@ static void MakeBackupFile(File_Chunk* fc)
strncpy(&Name1[length],"B0.rif",7);
strncpy(&Name2[length],"B1.rif",7);

AVPDeleteFile(Name1);
#ifdef _WIN32
DeleteFile(Name1);
#else
AVPDeleteFile(Name1);
#endif

for (int i=0;i<9;i++)
{
Name1[length+1]=i+'0';
Name2[length+1]=i+'1';
AVPMoveFile(Name2,Name1);

#ifdef _WIN32
MoveFile(Name2, Name1);
#else
AVPMoveFile(Name2, Name1);
#endif

}
Name2[length+1]='9';
AVPCopyFile(fc->filename,Name2,FALSE);

#ifdef _WIN32
CopyFile(fc->filename, Name2, FALSE);
#else
AVPCopyFile(fc->filename, Name2, FALSE);
#endif

delete [] Name1;
delete [] Name2;
Expand All @@ -3415,8 +3444,11 @@ void save_preplaced_decals()
}

{

#ifdef _WIN32
DWORD attributes = GetFileAttributes(env_rif->fc->filename);
#else
DWORD attributes = AVPGetFileAttributes(env_rif->fc->filename);
#endif
if (0xffffffff!=attributes)
{
if (attributes & FILE_ATTRIBUTE_READONLY)
Expand Down
12 changes: 10 additions & 2 deletions src/avp/win95/strachnk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ BOOL AVP_Strategy_Chunk::output_chunk (HANDLE &hand)

data_block = this->make_data_block_from_chunk();

ok = AVPWriteFile (hand, (long *) data_block, (unsigned long) chunk_size, &junk, 0);
#ifdef _WIN32
ok = WriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#else
ok = AVPWriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#endif

delete [] data_block;

Expand Down Expand Up @@ -252,7 +256,11 @@ BOOL AVP_External_Strategy_Chunk::output_chunk (HANDLE &hand)

data_block = this->make_data_block_from_chunk();

ok = AVPWriteFile (hand, (long *) data_block, (unsigned long) chunk_size, &junk, 0);
#ifdef _WIN32
ok = WriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#else
ok = AVPWriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#endif

delete [] data_block;

Expand Down
6 changes: 5 additions & 1 deletion src/win95/animchnk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ BOOL Animation_Chunk::output_chunk (HANDLE &hand)

data_block = this->make_data_block_from_chunk();

ok = AVPWriteFile (hand, (long *) data_block, (unsigned long) chunk_size, &junk, 0);
#ifdef _WIN32
ok = WriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#else
ok = AVPWriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#endif

delete [] data_block;

Expand Down
60 changes: 49 additions & 11 deletions src/win95/chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,56 @@ void list_chunks_in_file(List<int> * pList, HANDLE hand, char const * chunk_id)

// assuming we start at the front of a parent chunk,
// containing the child chunk specified
#ifdef _WIN32
int init_file_pos = SetFilePointer(hand, 0, 0, FILE_CURRENT);
int file_pos;
int file_length = GetFileSize(hand, 0);

SetFilePointer(hand, 8, 0, FILE_CURRENT);

int init_file_pos = AVPSetFilePointer (hand,0,0,FILE_CURRENT);
int chunk_length;
int sub_chunk_ln;

ReadFile(hand, (long*)&chunk_length, 4, &bytes_read, 0);
#else
int init_file_pos = AVPSetFilePointer(hand, 0, 0, FILE_CURRENT);
int file_pos;
int file_length = AVPGetFileSize(hand, 0);

AVPSetFilePointer (hand,8,0,FILE_CURRENT);
AVPSetFilePointer(hand, 8, 0, FILE_CURRENT);

int chunk_length;
int sub_chunk_ln;

AVPReadFile (hand, (long *) &chunk_length, 4, &bytes_read, 0);
AVPReadFile(hand, (long*)&chunk_length, 4, &bytes_read, 0);
#endif

if ((init_file_pos + chunk_length) > file_length) return;

while ((file_pos = AVPSetFilePointer (hand,0,0,FILE_CURRENT))
< (init_file_pos + chunk_length) && ok) {
#ifdef _WIN32
while ((file_pos = SetFilePointer(hand, 0, 0, FILE_CURRENT))
< (init_file_pos + chunk_length) && ok) {

ok = ReadFile(hand, (long*)buffer, 8, &bytes_read, 0);
#else
while ((file_pos = AVPSetFilePointer(hand, 0, 0, FILE_CURRENT))
< (init_file_pos + chunk_length) && ok) {

ok = AVPReadFile(hand, (long*)buffer, 8, &bytes_read, 0);
#endif

ok = AVPReadFile (hand, (long *) buffer, 8, &bytes_read, 0);
if (strncmp(buffer, chunk_id, 8) == 0)
pList->add_entry(file_pos);

ok = AVPReadFile (hand, (long *) &sub_chunk_ln, 4, &bytes_read, 0);
#ifdef _WIN32
ok = ReadFile(hand, (long*)&sub_chunk_ln, 4, &bytes_read, 0);

AVPSetFilePointer (hand,sub_chunk_ln-12,0,FILE_CURRENT);
SetFilePointer(hand, sub_chunk_ln - 12, 0, FILE_CURRENT);
#else
ok = AVPReadFile(hand, (long*)&sub_chunk_ln, 4, &bytes_read, 0);

AVPSetFilePointer(hand, sub_chunk_ln - 12, 0, FILE_CURRENT);
#endif
}
}

Expand Down Expand Up @@ -121,7 +147,11 @@ BOOL Chunk::output_chunk (HANDLE & hand)

if (data_block)
{
ok = AVPWriteFile (hand, (long *) data_block, (unsigned long) chunk_size, &junk, 0);
#ifdef _WIN32
ok = WriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#else
ok = AVPWriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#endif
delete [] data_block;

if (!ok) return FALSE;
Expand Down Expand Up @@ -279,11 +309,19 @@ BOOL Chunk_With_Children::output_chunk (HANDLE &hand)
Chunk * child_ptr = children;
BOOL ok;

ok = AVPWriteFile (hand, (long *) identifier, 8, &junk, 0);
#ifdef _WIN32
ok = WriteFile(hand, (long*)identifier, 8, &junk, 0);
#else
ok = AVPWriteFile(hand, (long*)identifier, 8, &junk, 0);
#endif

if (!ok) return FALSE;

ok = AVPWriteFile (hand, (long *) &chunk_size, 4, &junk, 0);
#ifdef _WIN32
ok = WriteFile(hand, (long*)&chunk_size, 4, &junk, 0);
#else
ok = AVPWriteFile(hand, (long*)&chunk_size, 4, &junk, 0);
#endif

if (!ok) return FALSE;

Expand Down
6 changes: 5 additions & 1 deletion src/win95/enumchnk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ BOOL Enum_Header_Chunk::output_chunk (HANDLE & hand)

data_block = make_data_block_from_chunk();

ok = AVPWriteFile (hand, (long *) data_block, (unsigned long) chunk_size, &junk, 0);
#ifdef _WIN32
ok = WriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#else
ok = AVPWriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#endif

delete [] data_block;

Expand Down
6 changes: 5 additions & 1 deletion src/win95/envchunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ BOOL Environment_Data_Header_Chunk::output_chunk (HANDLE & hand)

data_block = make_data_block_from_chunk();

ok = AVPWriteFile (hand, (long *) data_block, (unsigned long) chunk_size, &junk, 0);
#ifdef _WIN32
ok = WriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#else
ok = AVPWriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#endif

delete [] data_block;

Expand Down
6 changes: 5 additions & 1 deletion src/win95/gsprchnk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ BOOL AllSprites_Header_Chunk::output_chunk (HANDLE & hand)

data_block = make_data_block_from_chunk();

ok = AVPWriteFile (hand, (long *) data_block, (unsigned long) chunk_size, &junk, 0);
#ifdef _WIN32
ok = WriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#else
ok = AVPWriteFile(hand, (long*)data_block, (unsigned long)chunk_size, &junk, 0);
#endif

delete [] data_block;

Expand Down
Loading

0 comments on commit ca63f55

Please sign in to comment.