feat: Allow custom CS# directory for flexible server deployment (aka. css_basepath) (#1033)

This commit is contained in:
宇宙 猫
2025-09-22 12:40:20 +08:00
committed by GitHub
parent 4869acac41
commit 31cedca2b7
2 changed files with 68 additions and 6 deletions

View File

@@ -2,6 +2,9 @@
#include <public/eiface.h>
#include <string>
#include <filesystem>
#include <regex>
#include <algorithm>
#include "core/globals.h"
@@ -9,7 +12,6 @@ namespace counterstrikesharp {
namespace utils {
static std::string gameDirectory;
inline std::string GameDirectory()
{
if (gameDirectory.empty())
@@ -22,10 +24,58 @@ inline std::string GameDirectory()
return gameDirectory;
}
inline std::string GetRootDirectory() { return GameDirectory() + "/addons/counterstrikesharp"; }
inline std::string PluginsDirectory() { return GameDirectory() + "/addons/counterstrikesharp/plugins"; }
inline std::string ConfigsDirectory() { return GameDirectory() + "/addons/counterstrikesharp/configs"; }
inline std::string GamedataDirectory() { return GameDirectory() + "/addons/counterstrikesharp/gamedata"; }
// clang-format off
inline std::string RelativeDirectory(const std::string& initPath = "")
{
static std::string storedPath;
static bool isInitialized = false;
if (!initPath.empty() && !isInitialized)
{
std::string processedPath = initPath;
processedPath.erase(processedPath.begin(), std::find_if(processedPath.begin(), processedPath.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
processedPath.erase(std::find_if(processedPath.rbegin(), processedPath.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), processedPath.end());
processedPath = std::regex_replace(processedPath, std::regex(R"([\\/]+)"), "/");
if (!processedPath.empty())
{
if (processedPath[0] != '/')
{
processedPath = "/" + processedPath;
}
if (processedPath.back() == '/' && processedPath.length() > 1)
{
processedPath.pop_back();
}
}
std::string fullPath = GameDirectory() + processedPath;
if (std::filesystem::exists(fullPath) && std::filesystem::is_directory(fullPath))
{
storedPath = processedPath;
isInitialized = true;
}
else
{
return "NotFound";
}
}
return isInitialized ? storedPath : "/addons/counterstrikesharp";
}
// clang-format on
inline std::string GetRootDirectory() { return GameDirectory() + RelativeDirectory(); }
inline std::string PluginsDirectory() { return GameDirectory() + RelativeDirectory() + "/plugins"; }
inline std::string ConfigsDirectory() { return GameDirectory() + RelativeDirectory() + "/configs"; }
inline std::string GamedataDirectory() { return GameDirectory() + RelativeDirectory() + "/gamedata"; }
} // namespace utils
} // namespace counterstrikesharp

View File

@@ -34,6 +34,10 @@
#include "scripting/dotnet_host.h"
#include "scripting/script_engine.h"
#include "tier0/vprof.h"
#include "tier0/icommandline.h"
#include "tier1/utlstringtoken.h"
DLL_IMPORT ICommandLine* CommandLine();
#define VERSION_STRING "v" SEMVER " @ " GITHUB_SHA
#define BUILD_TIMESTAMP __DATE__ " " __TIME__
@@ -95,7 +99,8 @@ bool CounterStrikeSharpMMPlugin::Load(PluginId id, ISmmAPI* ismm, char* error, s
Log::Init();
CSSHARP_CORE_INFO("Initializing");
CSSHARP_CORE_INFO("Initializing with command line: {}", CommandLine()->GetCmdLine());
const char* basePath = CommandLine()->ParmValue(MakeStringToken("+css_basepath"), "/addons/counterstrikesharp");
GET_V_IFACE_CURRENT(GetEngineFactory, globals::engineServer2, IVEngineServer2, SOURCE2ENGINETOSERVER_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetEngineFactory, globals::engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
@@ -113,6 +118,13 @@ bool CounterStrikeSharpMMPlugin::Load(PluginId id, ISmmAPI* ismm, char* error, s
g_pSource2GameEntities = globals::gameEntities;
interfaces::pGameResourceServiceServer = (CGameResourceService*)g_pGameResourceServiceServer;
if (utils::RelativeDirectory(std::string(basePath)) == "NotFound")
{
CSSHARP_CORE_ERROR("Invalid base path: {}", basePath);
return false;
}
CSSHARP_CORE_INFO("Current root directory: {}", utils::GetRootDirectory());
auto coreconfig_path = std::string(utils::ConfigsDirectory() + "/core");
globals::coreConfig = new CCoreConfig(coreconfig_path);
char coreconfig_error[255] = "";