Skip to content

Commit

Permalink
check files in correct folder (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh authored Jan 13, 2025
1 parent f4eb1db commit 139242e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 72 deletions.
6 changes: 1 addition & 5 deletions server/include/CoreClr.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,7 @@ class CoreClr {

void GetPath(alt::ICore* server, const char* defaultPath);

void GenerateRuntimeConfigText(std::ofstream* outfile);

bool CreateRuntimeConfigFile();

void DeleteRuntimeConfigFile();
bool CheckRuntimeConfigFile();

/**
* prints out error when error code in known
Expand Down
84 changes: 17 additions & 67 deletions server/src/CoreClr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <condition_variable>
#include <fstream>
#include <sstream>
#include <filesystem>

#include "c-api/func_table.h"

Expand Down Expand Up @@ -525,92 +526,41 @@ void thread_proc(struct thread_user_data* userData)
delete userData;
}

void CoreClr::GenerateRuntimeConfigText(std::ofstream* outfile)
bool CoreClr::CheckRuntimeConfigFile()
{
if (version == nullptr)
{
core->LogError(cs::Log::LOG_PREFIX, "Unknown coreclr version");
return;
}
semver_t sem_ver;
if (semver_parse_version(version, &sem_ver) != 0)
{
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);
auto patch_version = std::to_string(sem_ver.major) + std::string(".") + std::to_string(sem_ver.minor) +
std::string(".") + std::to_string(sem_ver.patch);
(*outfile) << std::string("{\n"
" \"runtimeOptions\": {\n"
" \"tfm\": \"netcoreapp" + minor_version + "\",\n"
" \"framework\": {\n"
" \"name\": \"Microsoft.NETCore.App\",\n"
" \"version\": \"") + patch_version + "\"\n"
" }\n"
" }\n"
"}";
semver_free(&sem_ver);
}

bool CoreClr::CreateRuntimeConfigFile()
{
auto fileName = (std::string(core->GetRootDirectory()) + DIR_SEPARATOR + std::string(
"AltV.Net.Host.runtimeconfig.json"));
struct stat buffer{};
auto exists = (stat(fileName.c_str(), &buffer) == 0);
if (exists) return false;
std::ofstream outfile;
outfile.open(fileName.c_str());
GenerateRuntimeConfigText(&outfile);
outfile << std::flush;
outfile.close();
return true;
}

void CoreClr::DeleteRuntimeConfigFile()
{
remove(
(std::string(core->GetRootDirectory()) + DIR_SEPARATOR + std::string("AltV.Net.Host.runtimeconfig.json")).
c_str());
auto fileName = std::string(core->GetRootDirectory()) + DIR_SEPARATOR +
std::string("modules") + DIR_SEPARATOR +
std::string("csharp-module") + DIR_SEPARATOR +
std::string("AltV.Net.Host.runtimeconfig.json");
return std::filesystem::exists(fileName);
}

void CoreClr::CreateManagedHost()
{
auto result = CreateRuntimeConfigFile();
// Load .NET Core
/*void* load_assembly_and_get_function_pointer = nullptr;
hostfxr_initialize_parameters initializeParameters = {};
initializeParameters.host_path = "";
initializeParameters.dotnet_root = dotnetDirectory;
initializeParameters.size = sizeof(hostfxr_initialize_parameters);*/
int rc;
if (!CheckRuntimeConfigFile())
{
core->LogError(cs::Log::LOG_PREFIX,
"Make sure you have AltV.Net.Host.runtimeconfig.json in the module/csharp-module folder.");
return;
}

const char_t* args[1];
args[0] = STR("AltV.Net.Host.dll");
rc = _initForCmd(1, args, nullptr, &cxt);
args[0] = STR("modules\\csharp-module\\AltV.Net.Host.dll");
int rc = _initForCmd(1, args, nullptr, &cxt);
if (rc != 0)
{
if (rc == 0x80008094)
{
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.");
"Make sure you have AltV.Net.Host.dll in the module/csharp-module folder.");
}
std::stringstream stream;
stream << "Init for cmd failed: " << std::hex << std::showbase << rc;
core->LogError(cs::Log::LOG_PREFIX, stream.str());
_closeFxr(cxt);
if (result)
{
DeleteRuntimeConfigFile();
}
return;
}

if (result)
{
DeleteRuntimeConfigFile();
}

auto userData = new struct thread_user_data;
userData->runApp = _runApp;
userData->closeFxr = _closeFxr;
Expand Down

0 comments on commit 139242e

Please sign in to comment.