Skip to content

Commit

Permalink
feat(cvar): maps config directory (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushaway authored Oct 2, 2024
1 parent 5b09348 commit 555d23c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 15 deletions.
97 changes: 82 additions & 15 deletions src/addons/sourcemod/scripting/zr/config.inc
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,24 @@ void ConfigLoad()
{
char mapname[256];
char mapconfig[PLATFORM_MAX_PATH];

char path[PLATFORM_MAX_PATH];

// Get map name and format into config path.
GetCurrentMap(mapname, sizeof(mapname));
Format(mapconfig, sizeof(mapconfig), "sourcemod/zombiereloaded/%s.cfg", mapname);

// Prepend cfg to path.
char path[PLATFORM_MAX_PATH];
Format(path, sizeof(path), "cfg/%s", mapconfig);


bool bSMPath = GetConVarBool(g_hCvarsList.CVAR_CONFIG_SM_PATH);

if (bSMPath)
{
Format(mapconfig, sizeof(mapconfig), "configs/zr/maps/%s.cfg", mapname);
BuildPath(Path_SM, path, PLATFORM_MAX_PATH, mapconfig);
}
else
{
Format(mapconfig, sizeof(mapconfig), "sourcemod/zombiereloaded/%s.cfg", mapname);
Format(path, sizeof(path), "cfg/%s", mapconfig);
}

// Check if the file exist.
bool cfgexists = FileExists(path);
if (!cfgexists)
Expand All @@ -222,21 +231,79 @@ void ConfigLoad()
{
mapname[i] = CharToLower(mapname[i]);
}
Format(mapconfig, sizeof(mapconfig), "sourcemod/zombiereloaded/%s.cfg", mapname);
Format(path, sizeof(path), "cfg/%s", mapconfig);

if (bSMPath)
{
Format(mapconfig, sizeof(mapconfig), "configs/zr/maps/%s.cfg", mapname);
BuildPath(Path_SM, path, PLATFORM_MAX_PATH, mapconfig);
}
else
{
Format(mapconfig, sizeof(mapconfig), "sourcemod/zombiereloaded/%s.cfg", mapname);
Format(path, sizeof(path), "cfg/%s", mapconfig);
}

// Check if the new file path exist.
cfgexists = FileExists(path);
// File doesn't exist, then stop.
if (!cfgexists)
return;
}

// Execute config file.
ServerCommand("exec %s", mapconfig);

// Log action.
LogEvent(false, LogType_Normal, LOG_CORE_EVENTS, LogModule_Config, "Map Configs", "Executed map config file: %s", path);

// Execute config file - Log action if successful.
if (LoadConfigAndExec(path, bSMPath))
LogEvent(false, LogType_Normal, LOG_CORE_EVENTS, LogModule_Config, "Map Configs", "Executed map config file: %s", path);
}

/**
* Workaround to execute file from game/cfgs
* Or from addons/sourcemod/configs/zr/maps
*/
bool LoadConfigAndExec(char[] filepath, bool bSMPath = false)
{
if (!bSMPath)
{
ServerCommand("exec %s", filepath);
return true;
}

char line[PLATFORM_MAX_PATH];
File hFile = OpenFile(filepath, "r");

// Check if the file was opened successfully
if (hFile == null)
return false;

// Read each line and execute it
while (ReadFileLine(hFile, line, sizeof(line)))
{
// Remove any newline or whitespace characters
TrimString(line);

// Skip empty lines
if (line[0] == '\0')
continue;

// Find the position of "//" in the line
int commentIndex = StrContains(line, "//", false);

// If a comment is found, truncate the line at that point
if (commentIndex != -1)
{
line[commentIndex] = '\0';
TrimString(line);
}

// Skip lines that are empty after removing comments
if (line[0] == '\0')
continue;

// Execute the command
ServerCommand("%s", line);
}

CloseHandle(hFile);
return true;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/addons/sourcemod/scripting/zr/cvars.inc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum struct CvarsList
Handle CVAR_LOG_ERROR_OVERRIDE;
Handle CVAR_LOG_PRINT_ADMINS;
Handle CVAR_LOG_PRINT_CHAT;
Handle CVAR_CONFIG_SM_PATH;
Handle CVAR_CONFIG_PATH_MODELS;
Handle CVAR_CONFIG_PATH_DOWNLOADS;
Handle CVAR_CONFIG_PATH_CLASSES;
Expand Down Expand Up @@ -244,6 +245,7 @@ void CvarsCreate()
// ===========================
// Config (core)
// ===========================
g_hCvarsList.CVAR_CONFIG_SM_PATH = CreateConVar("zr_config_sm_path", "0", "Path, to Zombie:Reloaded MAPS config files. [0 = cfgs/sourcemod/zombiereloaded | 1 = addons/sourcemod/configs/zr/maps]");
g_hCvarsList.CVAR_CONFIG_PATH_MODELS = CreateConVar("zr_config_path_models", "configs/zr/models.txt", "Path, relative to root sourcemod directory, to models config file.");
g_hCvarsList.CVAR_CONFIG_PATH_DOWNLOADS = CreateConVar("zr_config_path_downloads", "configs/zr/downloads.txt", "Path, relative to root sourcemod directory, to downloads file.");
g_hCvarsList.CVAR_CONFIG_PATH_CLASSES = CreateConVar("zr_config_path_playerclasses", "configs/zr/playerclasses.txt", "Path, relative to root sourcemod directory, to playerclasses config file.");
Expand Down

0 comments on commit 555d23c

Please sign in to comment.