started the clean-up of cpp files

This commit is contained in:
Borja Ferrer 2005-09-10 00:59:24 +00:00
parent e67457440d
commit 16ad8739e7
2 changed files with 67 additions and 37 deletions

View File

@ -35,19 +35,20 @@
/********************** /**********************
****** AMXXFILE ****** ****** AMXXFILE ******
**********************/ **********************/
#if defined __GNUC__ #if defined __GNUC__
#define PACKED __attribute__((packed)) #define PACKED __attribute__((packed))
#else #else
#define PACKED #define PACKED
#endif #endif
#if defined __linux__ #if defined __linux__
#pragma pack(1) /* structures must be packed (byte-aligned) */ #pragma pack(1) /* structures must be packed (byte-aligned) */
#else #else
#pragma pack(1) /* structures must be packed (byte-aligned) */ #pragma pack(1) /* structures must be packed (byte-aligned) */
#if defined __TURBOC__ #if defined __TURBOC__
#pragma option -a- /* "pack" pragma for older Borland compilers */ #pragma option -a- /* "pack" pragma for older Borland compilers */
#endif #endif
#endif #endif
struct TableEntry struct TableEntry
@ -73,6 +74,7 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
{ {
m_Bh.plugins = NULL; m_Bh.plugins = NULL;
m_AmxxFile = false; m_AmxxFile = false;
if (!filename) if (!filename)
{ {
m_Status = Err_InvalidParam; m_Status = Err_InvalidParam;
@ -83,6 +85,7 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
m_CellSize = cellsize; m_CellSize = cellsize;
m_pFile = fopen(filename, "rb"); m_pFile = fopen(filename, "rb");
if (!m_pFile) if (!m_pFile)
{ {
m_Status = Err_FileOpen; m_Status = Err_FileOpen;
@ -93,14 +96,19 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
DATAREAD(&magic, sizeof(magic), 1); DATAREAD(&magic, sizeof(magic), 1);
m_OldFile = false; m_OldFile = false;
if ( magic == 0x524C4542 ) {
if (magic == 0x524C4542)
{
//we have an invalid, old, RLEB file //we have an invalid, old, RLEB file
m_Status = Err_OldFile; m_Status = Err_OldFile;
fclose(m_pFile); fclose(m_pFile);
m_pFile = NULL; m_pFile = NULL;
return; return;
} else if ( magic == MAGIC_HEADER2 ) { } else if (magic == MAGIC_HEADER2)
{
DATAREAD(&m_Bh.version, sizeof(int16_t), 1); DATAREAD(&m_Bh.version, sizeof(int16_t), 1);
if (m_Bh.version != MAGIC_VERSION) if (m_Bh.version != MAGIC_VERSION)
{ {
m_Status = Err_OldFile; m_Status = Err_OldFile;
@ -108,13 +116,15 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
m_pFile = NULL; m_pFile = NULL;
return; return;
} }
m_AmxxFile = true; m_AmxxFile = true;
DATAREAD(&m_Bh.numPlugins, sizeof(mint8_t), 1); DATAREAD(&m_Bh.numPlugins, sizeof(mint8_t), 1);
m_Bh.plugins = new PluginEntry[m_Bh.numPlugins]; m_Bh.plugins = new PluginEntry[m_Bh.numPlugins];
PluginEntry *pe; PluginEntry *pe;
m_SectionHdrOffset = 0; m_SectionHdrOffset = 0;
m_Entry = -1; m_Entry = -1;
for (mint8_t i=0; i<m_Bh.numPlugins; i++)
for (mint8_t i = 0; i < m_Bh.numPlugins; i++)
{ {
pe = &(m_Bh.plugins[i]); pe = &(m_Bh.plugins[i]);
DATAREAD(&pe->cellsize, sizeof(mint8_t), 1); DATAREAD(&pe->cellsize, sizeof(mint8_t), 1);
@ -123,15 +133,18 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
DATAREAD(&pe->memsize, sizeof(int32_t), 1); DATAREAD(&pe->memsize, sizeof(int32_t), 1);
DATAREAD(&pe->offs, sizeof(int32_t), 1); DATAREAD(&pe->offs, sizeof(int32_t), 1);
} }
for (mint8_t i=0; i<m_Bh.numPlugins; i++)
for (mint8_t i = 0; i < m_Bh.numPlugins; i++)
{ {
pe = &(m_Bh.plugins[i]); pe = &(m_Bh.plugins[i]);
if (pe->cellsize == m_CellSize) if (pe->cellsize == m_CellSize)
{ {
m_Entry = i; m_Entry = i;
break; break;
} }
} }
if (m_Entry == -1) if (m_Entry == -1)
{ {
m_Status = Err_SectionNotFound; m_Status = Err_SectionNotFound;
@ -139,10 +152,11 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
m_pFile = NULL; m_pFile = NULL;
return; return;
} }
pe = &(m_Bh.plugins[m_Entry]); pe = &(m_Bh.plugins[m_Entry]);
m_SectionLength = pe->disksize; m_SectionLength = pe->disksize;
} else if (magic == MAGIC_HEADER) { } else if (magic == MAGIC_HEADER)
{
// try to find the section // try to find the section
mint8_t numOfPlugins; mint8_t numOfPlugins;
DATAREAD(&numOfPlugins, sizeof(numOfPlugins), 1); DATAREAD(&numOfPlugins, sizeof(numOfPlugins), 1);
@ -151,6 +165,7 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
m_SectionHdrOffset = 0; m_SectionHdrOffset = 0;
int i = 0; int i = 0;
for (i = 0; i < static_cast<int>(numOfPlugins); ++i) for (i = 0; i < static_cast<int>(numOfPlugins); ++i)
{ {
DATAREAD(&entry, sizeof(entry), 1); DATAREAD(&entry, sizeof(entry), 1);
@ -160,6 +175,7 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
break; break;
} }
} }
if (!m_SectionHdrOffset) if (!m_SectionHdrOffset)
{ {
m_Status = Err_SectionNotFound; m_Status = Err_SectionNotFound;
@ -175,9 +191,7 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
TableEntry nextEntry; TableEntry nextEntry;
DATAREAD(&nextEntry, sizeof(nextEntry), 1); DATAREAD(&nextEntry, sizeof(nextEntry), 1);
m_SectionLength = nextEntry.offset - entry.offset; m_SectionLength = nextEntry.offset - entry.offset;
} } else {
else
{
fseek(m_pFile, 0, SEEK_END); fseek(m_pFile, 0, SEEK_END);
m_SectionLength = ftell(m_pFile) - (long)entry.offset; m_SectionLength = ftell(m_pFile) - (long)entry.offset;
} }
@ -187,6 +201,7 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
rewind(m_pFile); rewind(m_pFile);
fread(&hdr, sizeof(hdr), 1, m_pFile); fread(&hdr, sizeof(hdr), 1, m_pFile);
amx_Align16(&hdr.magic); amx_Align16(&hdr.magic);
if (hdr.magic == AMX_MAGIC) if (hdr.magic == AMX_MAGIC)
{ {
if (cellsize != 4) if (cellsize != 4)
@ -194,18 +209,19 @@ CAmxxReader::CAmxxReader(const char *filename, int cellsize)
m_Status = Err_SectionNotFound; m_Status = Err_SectionNotFound;
fclose(m_pFile); fclose(m_pFile);
m_pFile = NULL; m_pFile = NULL;
return; return;
} }
m_OldFile = true; m_OldFile = true;
return; return;
} } else {
else
{
// no known file format // no known file format
m_Status = Err_FileInvalid; m_Status = Err_FileInvalid;
fclose(m_pFile); fclose(m_pFile);
m_pFile = NULL; m_pFile = NULL;
return; return;
} }
} }
@ -218,6 +234,7 @@ CAmxxReader::~CAmxxReader()
fclose(m_pFile); fclose(m_pFile);
m_pFile = NULL; m_pFile = NULL;
} }
if (m_Bh.plugins) if (m_Bh.plugins)
{ {
delete [] m_Bh.plugins; delete [] m_Bh.plugins;
@ -257,10 +274,13 @@ size_t CAmxxReader::GetBufferSize()
DATAREAD(&hdr, sizeof(hdr), 1); DATAREAD(&hdr, sizeof(hdr), 1);
fseek(m_pFile, save, SEEK_SET); fseek(m_pFile, save, SEEK_SET);
return hdr.stp; return hdr.stp;
} else if (m_AmxxFile) { } else if (m_AmxxFile)
{
PluginEntry *pe = &(m_Bh.plugins[m_Entry]); PluginEntry *pe = &(m_Bh.plugins[m_Entry]);
if (pe->imagesize > pe->memsize) if (pe->imagesize > pe->memsize)
return pe->imagesize + 1; return pe->imagesize + 1;
return pe->memsize + 1; return pe->memsize + 1;
} }
@ -269,6 +289,7 @@ size_t CAmxxReader::GetBufferSize()
TableEntry entry; TableEntry entry;
DATAREAD(&entry, sizeof(entry), 1); DATAREAD(&entry, sizeof(entry), 1);
fseek(m_pFile, save, SEEK_SET); fseek(m_pFile, save, SEEK_SET);
return entry.origSize + 1; // +1 : safe return entry.origSize + 1; // +1 : safe
} }
@ -298,22 +319,25 @@ CAmxxReader::Error CAmxxReader::GetSection(void *buffer)
rewind(m_pFile); rewind(m_pFile);
DATAREAD(buffer, 1, filesize); DATAREAD(buffer, 1, filesize);
m_Status = Err_None; m_Status = Err_None;
return m_Status; return m_Status;
} else if (m_AmxxFile) { } else if (m_AmxxFile)
{
PluginEntry *pe = &(m_Bh.plugins[m_Entry]); PluginEntry *pe = &(m_Bh.plugins[m_Entry]);
char *tempBuffer = new char[m_SectionLength + 1]; char *tempBuffer = new char[m_SectionLength + 1];
fseek(m_pFile, pe->offs, SEEK_SET); fseek(m_pFile, pe->offs, SEEK_SET);
DATAREAD((void *)tempBuffer, 1, m_SectionLength); DATAREAD((void *)tempBuffer, 1, m_SectionLength);
uLongf destLen = GetBufferSize(); uLongf destLen = GetBufferSize();
int result = uncompress((Bytef *)buffer, &destLen, int result = uncompress((Bytef *)buffer, &destLen, (Bytef *)tempBuffer, m_SectionLength);
(Bytef *)tempBuffer, m_SectionLength);
delete [] tempBuffer; delete [] tempBuffer;
if (result != Z_OK) if (result != Z_OK)
{ {
AMXXLOG_Log("[AMXX] Zlib error encountered: %d(%d)", result, m_SectionLength); AMXXLOG_Log("[AMXX] Zlib error encountered: %d(%d)", result, m_SectionLength);
m_Status = Err_Decompress; m_Status = Err_Decompress;
return Err_Decompress; return Err_Decompress;
} }
return Err_None; return Err_None;
} else { } else {
// new file type: go to the section table entry // new file type: go to the section table entry
@ -328,15 +352,17 @@ CAmxxReader::Error CAmxxReader::GetSection(void *buffer)
//fread(tempBuffer, sizeof(char), m_SectionLength, m_pFile); //fread(tempBuffer, sizeof(char), m_SectionLength, m_pFile);
DATAREAD((void*)tempBuffer, 1, m_SectionLength); DATAREAD((void*)tempBuffer, 1, m_SectionLength);
// decompress // decompress
int result = uncompress((Bytef *)buffer, &destLen, int result = uncompress((Bytef *)buffer, &destLen, (Bytef *)tempBuffer, m_SectionLength);
(Bytef *)tempBuffer, m_SectionLength);
delete [] tempBuffer; delete [] tempBuffer;
if (result != Z_OK) if (result != Z_OK)
{ {
AMXXLOG_Log("[AMXX] Zlib error encountered: %d(%d)", result, m_SectionLength); AMXXLOG_Log("[AMXX] Zlib error encountered: %d(%d)", result, m_SectionLength);
m_Status = Err_Decompress; m_Status = Err_Decompress;
return Err_Decompress; return Err_Decompress;
} }
return Err_None; return Err_None;
} }
} }

View File

@ -36,12 +36,12 @@
#include <time.h> #include <time.h>
#ifndef __linux__ #ifndef __linux__
#include <io.h> #include <io.h>
#endif #endif
#include "amxmodx.h" #include "amxmodx.h"
#ifndef __linux__ #ifndef __linux__
#define vsnprintf _vsnprintf #define vsnprintf _vsnprintf
#endif #endif
CLog::CLog() CLog::CLog()
@ -61,6 +61,7 @@ void CLog::CloseFile()
if (!m_LogFile.empty()) if (!m_LogFile.empty())
{ {
FILE *fp = fopen(m_LogFile.c_str(), "r"); FILE *fp = fopen(m_LogFile.c_str(), "r");
if (fp) if (fp)
{ {
fclose(fp); fclose(fp);
@ -77,6 +78,7 @@ void CLog::CloseFile()
fprintf(fp, "L %s: %s\n", date, "Log file closed."); fprintf(fp, "L %s: %s\n", date, "Log file closed.");
fclose(fp); fclose(fp);
} }
m_LogFile.clear(); m_LogFile.clear();
} }
} }
@ -90,23 +92,26 @@ void CLog::CreateNewFile()
tm *curTime = localtime(&td); tm *curTime = localtime(&td);
int i = 0; int i = 0;
while (true) while (true)
{ {
FILE *pTmpFile = fopen(m_LogFile.c_str(), "r"); // open for reading to check whether the file exists FILE *pTmpFile = fopen(m_LogFile.c_str(), "r"); // open for reading to check whether the file exists
if (!pTmpFile) if (!pTmpFile)
break; break;
fclose(pTmpFile); fclose(pTmpFile);
++i; ++i;
} }
// Log logfile start // Log logfile start
FILE *fp = fopen(m_LogFile.c_str(), "w"); FILE *fp = fopen(m_LogFile.c_str(), "w");
if (!fp) if (!fp)
{ {
ALERT(at_logged, "[AMXX] Unexpected fatal logging error. AMXX Logging disabled.\n"); ALERT(at_logged, "[AMXX] Unexpected fatal logging error. AMXX Logging disabled.\n");
SET_LOCALINFO("amxx_logging", "0"); SET_LOCALINFO("amxx_logging", "0");
} } else {
else
{
fprintf(fp, "AMX Mod X log file started (file \"%s/L%02d%02d%03d.log\") (version \"%s\")\n", g_log_dir.c_str(), curTime->tm_mon + 1, curTime->tm_mday, i, AMX_VERSION); fprintf(fp, "AMX Mod X log file started (file \"%s/L%02d%02d%03d.log\") (version \"%s\")\n", g_log_dir.c_str(), curTime->tm_mon + 1, curTime->tm_mday, i, AMX_VERSION);
fclose(fp); fclose(fp);
} }
@ -129,6 +134,7 @@ void CLog::MapChange()
#endif #endif
m_LogType = atoi(get_localinfo("amxx_logging", "1")); m_LogType = atoi(get_localinfo("amxx_logging", "1"));
if (m_LogType < 0 || m_LogType > 3) if (m_LogType < 0 || m_LogType > 3)
{ {
SET_LOCALINFO("amxx_logging", "1"); SET_LOCALINFO("amxx_logging", "1");
@ -152,6 +158,7 @@ void CLog::MapChange()
void CLog::Log(const char *fmt, ...) void CLog::Log(const char *fmt, ...)
{ {
static char file[256]; static char file[256];
if (m_LogType == 1 || m_LogType == 2) if (m_LogType == 1 || m_LogType == 2)
{ {
// get time // get time
@ -178,6 +185,7 @@ void CLog::Log(const char *fmt, ...)
{ {
CreateNewFile(); CreateNewFile();
pF = fopen(m_LogFile.c_str(), "a+"); pF = fopen(m_LogFile.c_str(), "a+");
if (!pF) if (!pF)
{ {
ALERT(at_logged, "[AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Logging disabled for this map.\n", m_LogFile.c_str()); ALERT(at_logged, "[AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Logging disabled for this map.\n", m_LogFile.c_str());
@ -185,9 +193,7 @@ void CLog::Log(const char *fmt, ...)
return; return;
} }
} }
} } else {
else
{
build_pathname_r(file, sizeof(file)-1, "%s/L%02d%02d.log", g_log_dir.c_str(), curTime->tm_mon + 1, curTime->tm_mday); build_pathname_r(file, sizeof(file)-1, "%s/L%02d%02d.log", g_log_dir.c_str(), curTime->tm_mon + 1, curTime->tm_mday);
pF = fopen(file, "a+"); pF = fopen(file, "a+");
} }
@ -195,9 +201,7 @@ void CLog::Log(const char *fmt, ...)
{ {
fprintf(pF, "L %s: %s\n", date, msg); fprintf(pF, "L %s: %s\n", date, msg);
fclose(pF); fclose(pF);
} } else {
else
{
ALERT(at_logged, "[AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Logging disabled for this map.\n", file); ALERT(at_logged, "[AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Logging disabled for this map.\n", file);
m_LogType = 0; m_LogType = 0;
return; return;