2009-04-30 07:36:57 +02:00
|
|
|
/*
|
|
|
|
* ============================================================================
|
|
|
|
*
|
2008-10-04 22:59:11 +02:00
|
|
|
* Zombie:Reloaded
|
2009-04-30 07:36:57 +02:00
|
|
|
*
|
2009-05-06 02:28:09 +02:00
|
|
|
* File: models.inc
|
|
|
|
* Type: Core
|
|
|
|
* Description: Model validation and API
|
2009-04-30 07:36:57 +02:00
|
|
|
*
|
|
|
|
* ============================================================================
|
2008-10-04 22:59:11 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
/**
|
|
|
|
* Maximum folder depth a model file can be located.
|
|
|
|
*/
|
|
|
|
#define MODELS_PATH_MAX_DEPTH 8
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
/**
|
|
|
|
* Maximum string length of a folder a model file is located under.
|
|
|
|
*/
|
|
|
|
#define MODELS_PATH_DIR_MAX_LENGTH 32
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
/**
|
|
|
|
* Array that stores a list of validated models.
|
2009-05-01 07:09:18 +02:00
|
|
|
*
|
|
|
|
* @redir config.inc
|
2009-04-30 07:36:57 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
ModelsLoad()
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-04-30 07:36:57 +02:00
|
|
|
// Add models to downloads table and validate.
|
|
|
|
ModelsPrepModels();
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
// Add download entries to downloads table and validate.
|
|
|
|
ModelsPrepDownloads();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate model paths and add to downloads table.
|
|
|
|
*/
|
|
|
|
ModelsPrepModels()
|
|
|
|
{
|
2009-05-09 17:45:19 +02:00
|
|
|
// Initialize log boolean.
|
|
|
|
new bool:enablelog = LogCheckFlag(LOG_CORE_EVENTS, LOG_MODULE_CORE);
|
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
// Get models file path.
|
|
|
|
decl String:pathmodels[PLATFORM_MAX_PATH];
|
|
|
|
new bool:exists = ConfigGetFilePath(CVAR_CONFIG_PATH_MODELS, pathmodels);
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
// If file doesn't exist, then log and stop.
|
|
|
|
if (!exists)
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-04-30 07:36:57 +02:00
|
|
|
// Log failure and stop plugin.
|
2009-05-09 17:45:19 +02:00
|
|
|
LogMessageFormatted(-1, "Models", "Config Validation", "Fatal error: Missing models file: \"%s\"", LOG_FORMAT_TYPE_FATALERROR, pathmodels);
|
2008-10-04 22:59:11 +02:00
|
|
|
}
|
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
// If model array exists, then destroy it.
|
|
|
|
if (arrayModelsList != INVALID_HANDLE)
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-04-30 07:36:57 +02:00
|
|
|
CloseHandle(arrayModelsList);
|
2008-10-04 22:59:11 +02:00
|
|
|
}
|
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
arrayModelsList = ConfigLinesToArray(pathmodels);
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
// If array couldn't be created, then fail.
|
|
|
|
if (arrayModelsList == INVALID_HANDLE)
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-05-09 17:45:19 +02:00
|
|
|
LogMessageFormatted(-1, "Models", "Config Validation", "Fatal error: Error parsing \"%s\"", LOG_FORMAT_TYPE_FATALERROR, pathmodels);
|
2008-10-04 22:59:11 +02:00
|
|
|
}
|
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
new modelcount;
|
|
|
|
new modelvalidcount;
|
|
|
|
new modelfilecount;
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
decl String:modelbase[PLATFORM_MAX_PATH];
|
|
|
|
decl String:modelpath[PLATFORM_MAX_PATH];
|
|
|
|
decl String:modelname[MODELS_PATH_DIR_MAX_LENGTH];
|
|
|
|
decl String:modelfile[MODELS_PATH_DIR_MAX_LENGTH];
|
|
|
|
decl String:modeldiskname[MODELS_PATH_DIR_MAX_LENGTH];
|
|
|
|
decl String:modelfullpath[PLATFORM_MAX_PATH];
|
|
|
|
|
|
|
|
new String:baseexploded[MODELS_PATH_MAX_DEPTH][MODELS_PATH_DIR_MAX_LENGTH];
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
new FileType:type;
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
new models = modelcount = GetArraySize(arrayModelsList);
|
|
|
|
|
|
|
|
// x = model array index.
|
|
|
|
for (new x = 0; x < models; x++)
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-04-30 07:36:57 +02:00
|
|
|
// Get base model path (rawline in models.txt)
|
|
|
|
GetArrayString(arrayModelsList, x, modelbase, sizeof(modelbase));
|
|
|
|
|
|
|
|
// Explode path into pieces. (separated by "/")
|
|
|
|
new strings = ExplodeString(modelbase, "/", baseexploded, MODELS_PATH_MAX_DEPTH, MODELS_PATH_DIR_MAX_LENGTH);
|
|
|
|
|
|
|
|
// Get model file name.
|
|
|
|
strcopy(modelname, sizeof(modelname), baseexploded[strings - 1]);
|
|
|
|
|
|
|
|
// Get the path to the file.
|
|
|
|
// Works by truncating original path by the length of the file name.
|
|
|
|
strcopy(modelpath, strlen(modelbase) - strlen(modelname), modelbase);
|
|
|
|
|
|
|
|
// Open dir containing model files.
|
|
|
|
new Handle:modeldir = OpenDirectory(modelpath);
|
|
|
|
|
|
|
|
// Reset model file count.
|
|
|
|
modelfilecount = 0;
|
|
|
|
|
|
|
|
while (ReadDirEntry(modeldir, modelfile, sizeof(modelfile), type))
|
|
|
|
{
|
|
|
|
// If entry isn't a file, then stop.
|
|
|
|
if (type != FileType_File)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find break point index in the string to get model name.
|
|
|
|
// Add one because it seems to break on the character before.
|
|
|
|
new breakpoint = FindCharInString(modelfile, '.') + 1;
|
|
|
|
strcopy(modeldiskname, breakpoint, modelfile);
|
|
|
|
|
|
|
|
// If this file doesn't match, then stop.
|
|
|
|
if (!StrEqual(modelname, modeldiskname, false))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format a full path string.
|
|
|
|
strcopy(modelfullpath, sizeof(modelfullpath), modelpath);
|
|
|
|
Format(modelfullpath, sizeof(modelfullpath), "%s/%s", modelfullpath, modelfile);
|
|
|
|
|
|
|
|
// Precache model file and add to downloads table.
|
|
|
|
PrecacheModel(modelfullpath);
|
|
|
|
AddFileToDownloadsTable(modelfullpath);
|
|
|
|
|
|
|
|
// Increment modelfilecount
|
|
|
|
modelfilecount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment modelvalidcount if model files are valid.
|
|
|
|
if (modelfilecount)
|
|
|
|
{
|
|
|
|
modelvalidcount++;
|
|
|
|
}
|
|
|
|
else
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-04-30 07:36:57 +02:00
|
|
|
// Remove client from array.
|
|
|
|
RemoveFromArray(arrayModelsList, x);
|
|
|
|
|
|
|
|
// Subtract one from count.
|
|
|
|
models--;
|
|
|
|
|
|
|
|
// Backtrack one index, because we deleted it out from under the loop.
|
|
|
|
x--;
|
|
|
|
|
|
|
|
// Log missing model files.
|
2009-05-09 17:45:19 +02:00
|
|
|
if (enablelog)
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-04-30 07:36:57 +02:00
|
|
|
LogMessageFormatted(-1, "Models", "Config Validation", "Missing model files on server (%s)", LOG_FORMAT_TYPE_ERROR, modelbase);
|
2008-10-04 22:59:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-30 07:36:57 +02:00
|
|
|
|
|
|
|
// Log model validation info.
|
2009-05-09 17:45:19 +02:00
|
|
|
if (enablelog)
|
|
|
|
{
|
|
|
|
LogMessageFormatted(-1, "Models", "Config Validation", "Total: %d | Successful: %d | Unsuccessful: %d", LOG_FORMAT_TYPE_FULL, modelcount, modelvalidcount, modelcount - modelvalidcount);
|
|
|
|
}
|
2009-04-30 07:36:57 +02:00
|
|
|
|
|
|
|
// If none of the model paths are valid, then log and fail.
|
|
|
|
if (!modelvalidcount)
|
|
|
|
{
|
2009-05-09 17:45:19 +02:00
|
|
|
if (enablelog)
|
2009-04-30 07:36:57 +02:00
|
|
|
{
|
|
|
|
LogMessageFormatted(-1, "Models", "Config Validation", "No usable model paths in %s", LOG_FORMAT_TYPE_FATALERROR, pathmodels);
|
|
|
|
}
|
|
|
|
}
|
2008-10-04 22:59:11 +02:00
|
|
|
}
|
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
/**
|
|
|
|
* Validate custom download paths and add to downloads table.
|
|
|
|
*/
|
|
|
|
ModelsPrepDownloads()
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-05-09 17:45:19 +02:00
|
|
|
// Initialize log boolean.
|
|
|
|
new bool:enablelog = LogCheckFlag(LOG_CORE_EVENTS, LOG_MODULE_CORE);
|
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
// Get downloads file path.
|
|
|
|
decl String:pathdownloads[PLATFORM_MAX_PATH];
|
|
|
|
new bool:exists = ConfigGetFilePath(CVAR_CONFIG_PATH_DOWNLOADS, pathdownloads);
|
|
|
|
|
|
|
|
// If file doesn't exist, then log.
|
|
|
|
if (!exists)
|
|
|
|
{
|
|
|
|
// Log error, then stop.
|
2009-05-09 17:45:19 +02:00
|
|
|
if (enablelog)
|
2009-04-30 07:36:57 +02:00
|
|
|
{
|
2009-05-09 17:45:19 +02:00
|
|
|
LogMessageFormatted(-1, "Downloads", "Config Validation", "Missing downloads file: \"%s\"", LOG_FORMAT_TYPE_ERROR, pathdownloads);
|
2009-04-30 07:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
new Handle:arrayDownloadsList = ConfigLinesToArray(pathdownloads);
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
// If array couldn't be created, then fail.
|
|
|
|
if (arrayModelsList == INVALID_HANDLE)
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-05-09 17:45:19 +02:00
|
|
|
if (enablelog)
|
2009-04-30 07:36:57 +02:00
|
|
|
{
|
2009-05-09 17:45:19 +02:00
|
|
|
LogMessageFormatted(-1, "Downloads", "Config Validation", "Error parsing \"%s\"", LOG_FORMAT_TYPE_ERROR, pathdownloads);
|
2009-04-30 07:36:57 +02:00
|
|
|
}
|
2008-10-04 22:59:11 +02:00
|
|
|
}
|
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
new downloadcount;
|
|
|
|
new downloadvalidcount;
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
decl String:downloadpath[PLATFORM_MAX_PATH];
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
new downloads = downloadcount = GetArraySize(arrayDownloadsList);
|
2008-10-04 22:59:11 +02:00
|
|
|
|
2009-04-30 07:36:57 +02:00
|
|
|
// x = download array index.
|
|
|
|
for (new x = 0; x < downloads; x++)
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-04-30 07:36:57 +02:00
|
|
|
// Get base model path (rawline in models.txt)
|
|
|
|
GetArrayString(arrayDownloadsList, x, downloadpath, sizeof(downloadpath));
|
|
|
|
|
|
|
|
// If file doesn't exist, then remove, log, and stop.
|
|
|
|
if (!FileExists(downloadpath))
|
2008-10-04 22:59:11 +02:00
|
|
|
{
|
2009-04-30 07:36:57 +02:00
|
|
|
// Remove client from array.
|
|
|
|
RemoveFromArray(arrayDownloadsList, x);
|
|
|
|
|
|
|
|
// Subtract one from count.
|
|
|
|
downloads--;
|
|
|
|
|
|
|
|
// Backtrack one index, because we deleted it out from under the loop.
|
|
|
|
x--;
|
|
|
|
|
2009-05-09 17:45:19 +02:00
|
|
|
if (enablelog)
|
2009-04-30 07:36:57 +02:00
|
|
|
{
|
2009-05-09 17:45:19 +02:00
|
|
|
LogMessageFormatted(-1, "Downloads", "Config Validation", "Missing file \"%s\"", LOG_FORMAT_TYPE_ERROR, downloadpath);
|
2009-04-30 07:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2008-10-04 22:59:11 +02:00
|
|
|
}
|
2009-04-30 07:36:57 +02:00
|
|
|
|
|
|
|
// Increment downloadvalidcount
|
|
|
|
downloadvalidcount++;
|
|
|
|
|
|
|
|
// Precache model file and add to downloads table.
|
|
|
|
AddFileToDownloadsTable(downloadpath);
|
2008-10-04 22:59:11 +02:00
|
|
|
}
|
2009-04-30 07:36:57 +02:00
|
|
|
|
|
|
|
// Log model validation info.
|
2009-05-09 17:45:19 +02:00
|
|
|
if (enablelog)
|
|
|
|
{
|
|
|
|
LogMessageFormatted(-1, "Downloads", "Config Validation", "Total: %d | Successful: %d | Unsuccessful: %d", LOG_FORMAT_TYPE_FULL, downloadcount, downloadvalidcount, downloadcount - downloadvalidcount);
|
|
|
|
}
|
2009-05-01 11:22:45 +02:00
|
|
|
}
|