Skip to content

Commit

Permalink
Back to C++17 (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed May 8, 2024
1 parent 928bc3f commit fb5967d
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 213 deletions.
31 changes: 16 additions & 15 deletions makefiles/linux.base.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#set(_ITERATOR_DEBUG_LEVEL 2)
add_definitions(-D_LINUX -DPOSIX -DLINUX -DGNUC -DCOMPILER_GCC -DPLATFORM_64BITS)

add_definitions(-D_LINUX -DPOSIX -DLINUX -DGNUC -DCOMPILER_GCC -DPLATFORM_64BITS -D_FILE_OFFSET_BITS=64)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Dstricmp=strcasecmp -D_stricmp=strcasecmp -D_strnicmp=strncasecmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Dstrnicmp=strncasecmp -D_snprintf=snprintf")
Expand All @@ -16,16 +15,18 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof -Wno-reorder")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpmath=sse -msse -fno-strict-aliasing")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-threadsafe-statics -v -fvisibility=default")

SET(
COUNTER_STRIKE_SHARP_LINK_LIBRARIES
${SOURCESDK_LIB}/linux64/libtier0.so
${SOURCESDK_LIB}/linux64/tier1.a
${SOURCESDK_LIB}/linux64/interfaces.a
${SOURCESDK_LIB}/linux64/mathlib.a
spdlog
dynload_s
dyncall_s
distorm
funchook-static
dynohook
)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc -static-libstdc++")

set(
COUNTER_STRIKE_SHARP_LINK_LIBRARIES
${SOURCESDK_LIB}/linux64/libtier0.so
${SOURCESDK_LIB}/linux64/tier1.a
${SOURCESDK_LIB}/linux64/interfaces.a
${SOURCESDK_LIB}/linux64/mathlib.a
spdlog
dynload_s
dyncall_s
distorm
funchook-static
dynohook
)
10 changes: 5 additions & 5 deletions makefiles/shared.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
if (UNIX AND NOT APPLE)
if(UNIX AND NOT APPLE)
set(LINUX TRUE)
endif()

if (WIN32 AND NOT MSVC)
if(WIN32 AND NOT MSVC)
message(FATAL "MSVC restricted.")
endif()

Expand All @@ -11,9 +11,9 @@ set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING
FORCE
)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 17)

if (LINUX)
if(LINUX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
endif()
Expand Down Expand Up @@ -67,4 +67,4 @@ include_directories(
libraries
)

include(${CMAKE_CURRENT_LIST_DIR}/metamod/configure_metamod.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/metamod/configure_metamod.cmake)
135 changes: 78 additions & 57 deletions src/core/gameconfig.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "core/gameconfig.h"

#include <fstream>

#include "log.h"
Expand All @@ -13,7 +14,8 @@ CGameConfig::~CGameConfig() = default;
bool CGameConfig::Init(char* conf_error, int conf_error_size)
{
std::ifstream ifs(m_sPath);
if (!ifs) {
if (!ifs)
{
V_snprintf(conf_error, conf_error_size, "Gamedata file not found.");
return false;
}
Expand All @@ -26,44 +28,53 @@ bool CGameConfig::Init(char* conf_error, int conf_error_size)
constexpr auto platform = "linux";
#endif

try {
for (auto& [k, v] : m_json.items()) {
if (v.contains("signatures")) {
if (auto library = v["signatures"]["library"]; library.is_string()) {
try
{
for (auto& [k, v] : m_json.items())
{
if (v.contains("signatures"))
{
if (auto library = v["signatures"]["library"]; library.is_string())
{
m_umLibraries[k] = library.get<std::string>();
}
if (auto signature = v["signatures"][platform]; signature.is_string()) {
if (auto signature = v["signatures"][platform]; signature.is_string())
{
m_umSignatures[k] = signature.get<std::string>();
}
}
if (v.contains("offsets")) {
if (auto offset = v["offsets"][platform]; offset.is_number_integer()) {
if (v.contains("offsets"))
{
if (auto offset = v["offsets"][platform]; offset.is_number_integer())
{
m_umOffsets[k] = offset.get<std::int64_t>();
}
}
if (v.contains("patches")) {
if (auto patch = v["patches"][platform]; patch.is_string()) {
if (v.contains("patches"))
{
if (auto patch = v["patches"][platform]; patch.is_string())
{
m_umPatches[k] = patch.get<std::string>();
}
}
}
} catch (const std::exception& ex) {
}
catch (const std::exception& ex)
{
V_snprintf(conf_error, conf_error_size, "Failed to parse gamedata file: %s", ex.what());
return false;
}
return true;
}

const std::string CGameConfig::GetPath()
{
return m_sPath;
}
const std::string CGameConfig::GetPath() { return m_sPath; }

const char* CGameConfig::GetLibrary(const std::string& name)
{
// My recommendation is switch to C++20.
auto it = m_umLibraries.find(name);
if (it == m_umLibraries.end()) {
if (it == m_umLibraries.end())
{
return nullptr;
}
return it->second.c_str();
Expand All @@ -72,7 +83,8 @@ const char* CGameConfig::GetLibrary(const std::string& name)
const char* CGameConfig::GetSignature(const std::string& name)
{
auto it = m_umSignatures.find(name);
if (it == m_umSignatures.end()) {
if (it == m_umSignatures.end())
{
return nullptr;
}
return it->second.c_str();
Expand All @@ -82,7 +94,8 @@ const char* CGameConfig::GetSymbol(const char* name)
{
const char* symbol = this->GetSignature(name);

if (!symbol || strlen(symbol) <= 1) {
if (!symbol || strlen(symbol) <= 1)
{
CSSHARP_CORE_ERROR("Missing symbol: {}\n", name);
return nullptr;
}
Expand All @@ -92,7 +105,8 @@ const char* CGameConfig::GetSymbol(const char* name)
const char* CGameConfig::GetPatch(const std::string& name)
{
auto it = m_umPatches.find(name);
if (it == m_umPatches.end()) {
if (it == m_umPatches.end())
{
return nullptr;
}
return it->second.c_str();
Expand All @@ -101,14 +115,14 @@ const char* CGameConfig::GetPatch(const std::string& name)
int CGameConfig::GetOffset(const std::string& name)
{
auto it = m_umOffsets.find(name);
if (it == m_umOffsets.end()) {
if (it == m_umOffsets.end())
{
return -1;
}
return it->second;
}

void* CGameConfig::GetAddress(const std::string& name, void* engine, void* server, char* error,
int maxlen)
void* CGameConfig::GetAddress(const std::string& name, void* engine, void* server, char* error, int maxlen)
{
CSSHARP_CORE_ERROR("Not implemented.");
return nullptr;
Expand All @@ -117,11 +131,9 @@ void* CGameConfig::GetAddress(const std::string& name, void* engine, void* serve
modules::CModule** CGameConfig::GetModule(const char* name)
{
const char* library = this->GetLibrary(name);
if (!library)
return nullptr;
if (!library) return nullptr;

if (strcmp(library, "engine") == 0)
return &modules::engine;
if (strcmp(library, "engine") == 0) return &modules::engine;
else if (strcmp(library, "server") == 0)
return &modules::server;
else if (strcmp(library, "vscript") == 0)
Expand All @@ -135,7 +147,8 @@ modules::CModule** CGameConfig::GetModule(const char* name)
bool CGameConfig::IsSymbol(const char* name)
{
const char* sigOrSymbol = this->GetSignature(name);
if (!sigOrSymbol || strlen(sigOrSymbol) <= 0) {
if (!sigOrSymbol || strlen(sigOrSymbol) <= 0)
{
CSSHARP_CORE_ERROR("Missing signature or symbol: {}\n", name);
return false;
}
Expand All @@ -145,30 +158,37 @@ bool CGameConfig::IsSymbol(const char* name)
void* CGameConfig::ResolveSignature(const char* name)
{
modules::CModule** module = this->GetModule(name);
if (!module || !(*module)) {
if (!module || !(*module))
{
CSSHARP_CORE_ERROR("Invalid Module {}\n", name);
return nullptr;
}

void* address = nullptr;
if (this->IsSymbol(name)) {
if (this->IsSymbol(name))
{
const char* symbol = this->GetSymbol(name);
if (!symbol) {
if (!symbol)
{
CSSHARP_CORE_ERROR("Invalid symbol for {}\n", name);
return nullptr;
}
address = (*module)->FindSymbol(symbol);
} else {
}
else
{
const char* signature = this->GetSignature(name);
if (!signature) {
if (!signature)
{
CSSHARP_CORE_ERROR("Failed to find signature for {}\n", name);
return nullptr;
}

address = (*module)->FindSignature(signature);
}

if (!address) {
if (!address)
{
CSSHARP_CORE_ERROR("Failed to find address for {}\n", name);
return nullptr;
}
Expand All @@ -180,25 +200,24 @@ std::string CGameConfig::GetDirectoryName(const std::string& directoryPathInput)
std::string directoryPath = std::string(directoryPathInput);

size_t found = std::string(directoryPath).find_last_of("/\\");
if (found != std::string::npos) {
if (found != std::string::npos)
{
return std::string(directoryPath, found + 1);
}
return "";
}

std::vector<int16_t> CGameConfig::HexToByte(std::string_view src)
{
if (src.empty()) {
if (src.empty())
{
return {};
}

auto hex_char_to_byte = [](char c) -> int16_t {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
if (c >= 'a' && c <= 'f') return c - 'a' + 10;

// a valid hex char can never go up to 0xFF
return -1;
Expand All @@ -211,36 +230,38 @@ std::vector<int16_t> CGameConfig::HexToByte(std::string_view src)
const std::string_view pattern = is_code_style ? R"(\x)" : " ";
const std::string_view wildcard = is_code_style ? "2A" : "?";

auto split = std::views::split(src, pattern);
std::string::size_type pos = 0;

for (auto&& str : split) {
if (str.empty()) [[unlikely]] {
continue;
while (pos < src.size())
{
std::string::size_type found = src.find(pattern, pos);
if (found == std::string::npos)
{
found = src.size();
}
std::string_view str = src.substr(pos, found - pos);
pos = found + pattern.size();

if (str.empty()) continue;

// std::string_view(std::subrange) constructor only exists in C++23 or above
// use this when compiler is GCC >= 12.1 or Clang >= 16
// const std::string_view byte(str.begin(), str.end());

// a workaround for GCC < 12.1, it doesn't work with Clang < 16
// https://stackoverflow.com/a/48403210
std::string_view byte (&*str.begin(), std::ranges::distance(str));
std::string byte(str.data(), str.size());

if (byte.starts_with(wildcard)) {
if (byte.substr(0, wildcard.size()) == wildcard)
{
result.emplace_back(-1);
continue;
}

if (byte.size() < 2) [[unlikely]] {
if (byte.size() < 2)
{
return {};
}

const auto high = hex_char_to_byte(byte[0]);
const auto low = hex_char_to_byte(byte[1]);

// if then is malformed, return nothing
// maybe print error message here?
if (high == 0xFF || low == 0xFF) [[unlikely]] {
if (high == 0xFF || low == 0xFF)
{
return {};
}

Expand Down
Loading

0 comments on commit fb5967d

Please sign in to comment.