diff --git a/client/thirdparty/Log.h b/c-api/Log.h similarity index 78% rename from client/thirdparty/Log.h rename to c-api/Log.h index 49c6c3e..90bebe9 100644 --- a/client/thirdparty/Log.h +++ b/c-api/Log.h @@ -1,7 +1,7 @@ #pragma once #include -#include "cpp-sdk/ICore.h" +#include "../cpp-sdk/ICore.h" namespace cs { class Log @@ -23,6 +23,8 @@ class Log Log() = default; public: + static constexpr const char* LOG_PREFIX = "[C#]"; + Log(const Log&) = delete; Log(Log&&) = delete; Log& operator=(const Log&) = delete; @@ -104,11 +106,11 @@ class Log { switch(log.type) { - case INFO: alt::ICore::Instance().LogInfo(log.buf.str()); break; - case DEBUG: alt::ICore::Instance().LogDebug(log.buf.str().c_str()); break; - case WARNING: alt::ICore::Instance().LogWarning(log.buf.str().c_str()); break; - case ERR: alt::ICore::Instance().LogError(log.buf.str().c_str()); break; - case COLORED: alt::ICore::Instance().LogColored(log.buf.str().c_str()); break; + case INFO: alt::ICore::Instance().LogInfo(LOG_PREFIX, log.buf.str()); break; + case DEBUG: alt::ICore::Instance().LogDebug(LOG_PREFIX, log.buf.str().c_str()); break; + case WARNING: alt::ICore::Instance().LogWarning(LOG_PREFIX, log.buf.str().c_str()); break; + case ERR: alt::ICore::Instance().LogError(LOG_PREFIX, log.buf.str().c_str()); break; + case COLORED: alt::ICore::Instance().LogColored(LOG_PREFIX, log.buf.str().c_str()); break; } log.buf.str(""); diff --git a/c-api/core.cpp b/c-api/core.cpp index 71de297..b9444f4 100644 --- a/c-api/core.cpp +++ b/c-api/core.cpp @@ -12,23 +12,23 @@ CAPI_START() void Core_LogInfo(alt::ICore* core, const char* str) { - core->LogInfo(str); + core->LogInfo(cs::Log::LOG_PREFIX, str); } void Core_LogDebug(alt::ICore* core, const char* str) { - core->LogDebug(str); + core->LogDebug(cs::Log::LOG_PREFIX, str); } void Core_LogWarning(alt::ICore* core, const char* str) { - core->LogWarning(str); + core->LogWarning(cs::Log::LOG_PREFIX, str); } void Core_LogError(alt::ICore* core, const char* str) { - core->LogError(str); + core->LogError(cs::Log::LOG_PREFIX, str); } void Core_LogColored(alt::ICore* core, const char* str) { - core->LogColored(str); + core->LogColored(cs::Log::LOG_PREFIX, str); } alt::MValueConst* Core_CreateMValueNil(alt::ICore* core) { @@ -1218,7 +1218,7 @@ void Core_TriggerServerEventUnreliable(alt::ICore* core, const char* event, alt: void Core_ShowCursor(alt::ICore* core, alt::IResource* resource, uint8_t state) { if(!resource->ToggleCursor(state)) { - core->LogWarning("Cursor state can't go < 0"); + core->LogWarning(cs::Log::LOG_PREFIX, "Cursor state can't go < 0"); } } diff --git a/c-api/core.h b/c-api/core.h index 60f63b2..7c40d66 100644 --- a/c-api/core.h +++ b/c-api/core.h @@ -13,6 +13,7 @@ #include "data/ped_model_info.h" #include "data/weapon_model_info.h" #include "utils/export.h" +#include "Log.h" #ifdef ALT_SERVER_API #include diff --git a/client/src/coreclr/CoreClr.cpp b/client/src/coreclr/CoreClr.cpp index c5a605d..fc3d6c1 100644 --- a/client/src/coreclr/CoreClr.cpp +++ b/client/src/coreclr/CoreClr.cpp @@ -6,12 +6,12 @@ #include #include #include -#include #include #include "utils.h" #include "../../c-api/client.h" #include "../../c-api/func_table.h" +#include "../../c-api/Log.h" using namespace alt; using namespace std; diff --git a/client/src/coreclr/CoreClr.h b/client/src/coreclr/CoreClr.h index 9c15743..acccde4 100644 --- a/client/src/coreclr/CoreClr.h +++ b/client/src/coreclr/CoreClr.h @@ -5,7 +5,7 @@ #include "cpp-sdk/ICore.h" #include #include -#include +#include <../../c-api/Log.h> #include #include "nuget/NuGet.h" @@ -20,12 +20,12 @@ struct Progress float total; float current; progressfn_t& updateFn; - + void Update() const { updateFn(state, current, total, 1000); } - + void Advance(float value) { current += value; @@ -63,7 +63,7 @@ class CoreClr { coreclr_shutdown_2_ptr _shutdownCoreClr = nullptr; coreclr_create_delegate_ptr _createDelegate = nullptr; coreclr_execute_assembly_ptr _executeAssembly = nullptr; - + void* _runtimeHost = nullptr; unsigned int _domainId = 0; std::optional _nuget; @@ -78,9 +78,9 @@ class CoreClr { void DownloadHost(alt::IHttpClient* httpClient, Progress& progress) const; void DownloadNuGet(alt::IHttpClient* httpClient, nlohmann::json json, Progress& progress); void DownloadNuGets(alt::IHttpClient* httpClient, progressfn_t& progress); - + void InitializeCoreclr(); void Update(progressfn_t progressFn, int attempt); - + std::string GetBaseCdnUrl() const; }; diff --git a/client/src/coreclr/CoreClrValidation.cpp b/client/src/coreclr/CoreClrValidation.cpp index 98e4887..ec94116 100644 --- a/client/src/coreclr/CoreClrValidation.cpp +++ b/client/src/coreclr/CoreClrValidation.cpp @@ -1,5 +1,5 @@ #include -#include +#include <../../c-api/Log.h> #include #include #include diff --git a/client/src/main.cpp b/client/src/main.cpp index 9eb0a5b..aa03396 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -5,7 +5,7 @@ #ifdef ALTV_CSHARP_SHARED #include "cpp-sdk/version/version.h" #endif -#include +#include <../../c-api/Log.h> using namespace alt; diff --git a/client/src/nuget/NuGet.cpp b/client/src/nuget/NuGet.cpp index e79b05d..9db8bce 100644 --- a/client/src/nuget/NuGet.cpp +++ b/client/src/nuget/NuGet.cpp @@ -1,6 +1,6 @@ #include "NuGet.h" -#include +#include <../../c-api/Log.h> #include "utils.h" diff --git a/client/src/runtime/CSharpResourceImpl.cpp b/client/src/runtime/CSharpResourceImpl.cpp index b9273a3..90f1376 100644 --- a/client/src/runtime/CSharpResourceImpl.cpp +++ b/client/src/runtime/CSharpResourceImpl.cpp @@ -3,7 +3,7 @@ #include #include "CSharpScriptRuntime.h" #include "CSharpResourceImpl.h" -#include +#include <../../c-api/Log.h> #include "CRC.h" #include #include diff --git a/client/src/runtime/CSharpScriptRuntime.cpp b/client/src/runtime/CSharpScriptRuntime.cpp index 40774f4..ff0a865 100644 --- a/client/src/runtime/CSharpScriptRuntime.cpp +++ b/client/src/runtime/CSharpScriptRuntime.cpp @@ -1,4 +1,4 @@ -#include +#include <../../c-api/Log.h> #include "CSharpScriptRuntime.h" #include "CSharpResourceImpl.h" #include "natives.h" diff --git a/client/src/runtime/natives.cpp b/client/src/runtime/natives.cpp index 977a650..960c9f3 100644 --- a/client/src/runtime/natives.cpp +++ b/client/src/runtime/natives.cpp @@ -5,7 +5,7 @@ #include "../../c-api/data/types.h" #include "../../c-api/data/function_table.h" #include "../../c-api/client.h" -#include "Log.h" +#include "../../c-api/Log.h" #include #include #include diff --git a/client/src/utils.h b/client/src/utils.h index a8d7228..1918ef8 100644 --- a/client/src/utils.h +++ b/client/src/utils.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include <../../c-api/Log.h> EXTERN_C IMAGE_DOS_HEADER __ImageBase; @@ -59,7 +59,7 @@ namespace utils std::char_traits::copy(returnStr, str, ulSize); return returnStr; } - + template T *get_clr_value(Args &&...args) { @@ -99,27 +99,27 @@ namespace utils std::transform(str.begin(), str.end(), str.begin(), [](const unsigned char c){ return std::tolower(c); }); return str; } - + inline alt::IHttpClient::HttpResponse download_file_sync(alt::IHttpClient* httpClient, const std::string& url) { auto attempt = 0; - + while (true) { if (++attempt > 5) throw std::runtime_error("Failed to download file " + url); - + std::promise promise; std::future future = promise.get_future(); - + httpClient->Get([](alt::IHttpClient::HttpResponse response, const void* data) { const auto innerPromise = (std::promise*) data; - + if (response.statusCode != 200) { std::stringstream ss; ss << "HTTP " << response.statusCode << " " << response.body; innerPromise->set_exception(std::make_exception_ptr(std::runtime_error(ss.str()))); return; } - + innerPromise->set_value(response); }, url, &promise); diff --git a/cpp-sdk b/cpp-sdk index b1ced94..33a3a4c 160000 --- a/cpp-sdk +++ b/cpp-sdk @@ -1 +1 @@ -Subproject commit b1ced94027a7b3c0c2167b12e45da80b49c8527b +Subproject commit 33a3a4c6d95c5f293a42c5b51d59c476829d2238 diff --git a/server/src/CoreClr.cpp b/server/src/CoreClr.cpp index 69526a9..288908d 100644 --- a/server/src/CoreClr.cpp +++ b/server/src/CoreClr.cpp @@ -40,7 +40,7 @@ CoreClr::CoreClr(alt::ICore* core) int rc = get_hostfxr_path(buffer, &buffer_size, nullptr); if (rc != 0) { - core->LogError("invalid get_hostfxr_path " + std::to_string(rc)); + core->LogError(cs::Log::LOG_PREFIX, "invalid get_hostfxr_path " + std::to_string(rc)); } else { @@ -49,7 +49,7 @@ CoreClr::CoreClr(alt::ICore* core) _coreClrLib = LoadLibraryEx(std::string(bufferWString.begin(), bufferWString.end()).c_str(), nullptr, 0); if (_coreClrLib == nullptr) { - core->LogInfo(std::string("coreclr-module: Unable to find CoreCLR dll")); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Unable to find CoreCLR dll")); return; } @@ -63,7 +63,7 @@ CoreClr::CoreClr(alt::ICore* core) #else _coreClrLib = dlopen(std::string(buffer).c_str(), RTLD_NOW | RTLD_LOCAL); if (_coreClrLib == nullptr) { - core->LogInfo(std::string("coreclr-module: Unable to find CoreCLR dll [") + std::string(buffer) + "]: " + dlerror()); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Unable to find CoreCLR dll [") + std::string(buffer) + "]: " + dlerror()); return; } _initializeFxr = (hostfxr_initialize_for_runtime_config_fn) dlsym(_coreClrLib, @@ -77,7 +77,7 @@ CoreClr::CoreClr(alt::ICore* core) if (_initializeFxr == nullptr || _getDelegate == nullptr || _closeFxr == nullptr || _runApp == nullptr || _initForCmd == nullptr) { - core->LogInfo(std::string("coreclr-module: Unable to find CoreCLR dll methods")); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Unable to find CoreCLR dll methods")); return; } return; @@ -121,7 +121,7 @@ CoreClr::CoreClr(alt::ICore* core) delete[] fullPath; if (_coreClrLib == nullptr) { - core->LogInfo(std::string("coreclr-module: Unable to find CoreCLR dll")); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Unable to find CoreCLR dll")); return; } @@ -139,7 +139,7 @@ CoreClr::CoreClr(alt::ICore* core) strcat(fullPath, fileName); _coreClrLib = dlopen(fullPath, RTLD_NOW | RTLD_LOCAL); if (_coreClrLib == nullptr) { - core->LogInfo(std::string("coreclr-module: Unable to find CoreCLR dll [") + fullPath + "]: " + dlerror()); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Unable to find CoreCLR dll [") + fullPath + "]: " + dlerror()); return; } _initializeFxr = (hostfxr_initialize_for_runtime_config_fn) dlsym(_coreClrLib, @@ -153,7 +153,7 @@ CoreClr::CoreClr(alt::ICore* core) if (_initializeFxr == nullptr || _getDelegate == nullptr || _closeFxr == nullptr || _runApp == nullptr || _initForCmd == nullptr) { - core->LogInfo(std::string("coreclr-module: Unable to find CoreCLR dll methods")); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Unable to find CoreCLR dll methods")); return; } } @@ -181,7 +181,7 @@ CoreClr::~CoreClr() /*bool CoreClr::GetDelegate(alt::ICore* core, void* runtimeHost, unsigned int domainId, const char* moduleName, const char* classPath, const char* methodName, void** callback) { if (runtimeHost == nullptr || domainId == 0) { - server->LogInfo(std::string("coreclr-module: Core CLR host not loaded")); + server->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Core CLR host not loaded")); return false; } int result = _createDelegate(runtimeHost, domainId, moduleName, classPath, methodName, callback); @@ -189,7 +189,7 @@ CoreClr::~CoreClr() if (this->PrintError(server, result)) { return false; } - server->LogInfo( + server->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Unable to get ") + moduleName + ":" + classPath + "." + methodName + " domain:" + domainId); @@ -208,10 +208,10 @@ CoreClr::~CoreClr() for (auto path : directories) { auto directory = opendir(path); if (directory == nullptr) { - server->LogInfo(std::string("coreclr-module: Runtime directory not found")); + server->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Runtime directory not found")); return assemblies; } - server->LogInfo(std::string("coreclr-module: Runtime directory found")); + server->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Runtime directory found")); struct dirent* entry; struct stat sb{}; for (auto ext : tpaExtensions) { @@ -248,7 +248,7 @@ CoreClr::~CoreClr() // Check if the extension matches the one we are looking for size_t extPos = strlen(entry->d_name) - extLength; - //server->LogInfo(std::string(ext) + "," + extLength + "," + entry->d_name); + //server->LogInfo(cs::Log::LOG_PREFIX, std::string(ext) + "," + extLength + "," + entry->d_name); if (extPos <= 0 || memcmp(ext, entry->d_name + extPos, extLength) != 0) { continue; } @@ -315,7 +315,7 @@ CoreClr::~CoreClr() domainId); if (result < 0) { - server->LogInfo(std::string("coreclr-module: Unable to create app domain: 0x")); + server->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Unable to create app domain: 0x")); this->PrintError(server, result); } else { server->LogInfo(std::string("coreclr-module: Created app domain: 0x") + appPath); @@ -402,7 +402,7 @@ void CoreClr::GetPath(alt::ICore* core, const char* defaultPath) auto directory = opendir(defaultPath); if (directory == nullptr) { - core->LogInfo(std::string("coreclr-module: dotnet core sdk not found in ") + defaultPath); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: dotnet core sdk not found in ") + defaultPath); return; } struct dirent* entry; @@ -413,12 +413,12 @@ void CoreClr::GetPath(alt::ICore* core, const char* defaultPath) { if (entry->d_type == DT_DIR && memcmp(entry->d_name, ".", 1) != 0 && memcmp(entry->d_name, "..", 2) != 0) { - core->LogInfo(std::string("coreclr-module: version found: ") + entry->d_name); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: version found: ") + entry->d_name); if (greatest == nullptr) { if (semver_parse(entry->d_name, &greatest_version)) { - core->LogInfo(std::string("coreclr-module: invalid version found: ") + entry->d_name); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: invalid version found: ") + entry->d_name); continue; } greatest = entry->d_name; @@ -426,7 +426,7 @@ void CoreClr::GetPath(alt::ICore* core, const char* defaultPath) } if (semver_parse(entry->d_name, &compare_version)) { - core->LogInfo(std::string("coreclr-module: invalid version found: ") + entry->d_name); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: invalid version found: ") + entry->d_name); continue; } if (semver_compare(compare_version, greatest_version) > 0) @@ -452,14 +452,14 @@ void CoreClr::GetPath(alt::ICore* core, const char* defaultPath) } if (greatest == nullptr) { - core->LogInfo(std::string("coreclr-module: No dotnet sdk version found")); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: No dotnet sdk version found")); return; } else { semver_free(&greatest_version); } - core->LogInfo(std::string("coreclr-module: greatest version: ") + greatest); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: greatest version: ") + greatest); size_t size = strlen(defaultPath) + strlen(greatest) + 1; runtimeDirectory = (char*)malloc(size); memset(runtimeDirectory, '\0', size); @@ -520,7 +520,7 @@ void thread_proc(struct thread_user_data* userData) userData->closeFxr(userData->cxt); std::stringstream stream; stream << "Run App failed: " << std::hex << std::showbase << rc; - alt::ICore::Instance().LogError(stream.str()); + alt::ICore::Instance().LogError(cs::Log::LOG_PREFIX, stream.str()); } delete userData; } @@ -529,13 +529,13 @@ void CoreClr::GenerateRuntimeConfigText(std::ofstream* outfile) { if (version == nullptr) { - core->LogError("Unknown coreclr version"); + core->LogError(cs::Log::LOG_PREFIX, "Unknown coreclr version"); return; } semver_t sem_ver; if (semver_parse_version(version, &sem_ver) != 0) { - core->LogError("Couldn't parse coreclr version"); + core->LogError(cs::Log::LOG_PREFIX, "Couldn't parse coreclr version"); return; } auto minor_version = std::to_string(sem_ver.major) + std::string(".") + std::to_string(sem_ver.minor); @@ -592,12 +592,12 @@ void CoreClr::CreateManagedHost() { if (rc == 0x80008094) { - core->LogError( + core->LogError(cs::Log::LOG_PREFIX, "Make sure you have AltV.Net.Host.dll and AltV.Net.Host.runtimeconfig.json in the folder of the altv-server executable or binary."); } std::stringstream stream; stream << "Init for cmd failed: " << std::hex << std::showbase << rc; - core->LogError(stream.str()); + core->LogError(cs::Log::LOG_PREFIX, stream.str()); _closeFxr(cxt); if (result) { @@ -626,7 +626,7 @@ bool CoreClr::ExecuteManagedResource(const char* resourcePath, const char* resou while (hostResourceExecute == nullptr) { cv.wait(lck); } if (hostResourceExecute == nullptr) { - core->LogInfo(std::string("coreclr-module: Core CLR host not loaded")); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Core CLR host not loaded")); return false; } @@ -663,7 +663,7 @@ bool CoreClr::ExecuteManagedResourceUnload(const char* resourcePath, const char* while (hostResourceExecuteUnload == nullptr) { cv.wait(lck); } if (hostResourceExecuteUnload == nullptr) { - core->LogInfo(std::string("coreclr-module: Core CLR host not loaded")); + core->LogInfo(cs::Log::LOG_PREFIX, std::string("coreclr-module: Core CLR host not loaded")); return false; } diff --git a/server/windows-build.bat b/server/windows-build.bat new file mode 100644 index 0000000..76e76cc --- /dev/null +++ b/server/windows-build.bat @@ -0,0 +1,2 @@ +cmake . -Bcmake-build-release +cmake --build cmake-build-release --config RelWithDebInfo \ No newline at end of file