Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions src/core/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

#include <public/eiface.h>
#include <string>
#include <filesystem>
#include <regex>
#include <algorithm>

#include "core/globals.h"

namespace counterstrikesharp {
namespace utils {

static std::string gameDirectory;

inline std::string GameDirectory()
{
if (gameDirectory.empty())
Expand All @@ -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
14 changes: 13 additions & 1 deletion src/mm_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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);
Expand All @@ -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] = "";
Expand Down
Loading