Skip to content

Commit 2adc0d2

Browse files
Improve host init
1 parent 64b37c3 commit 2adc0d2

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

runtime/include/CoreClr.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
#ifdef _WIN32
2222
#define HostDll "\\AltV.Net.Host.dll"
2323
#define HostCfg "\\AltV.Net.Host.runtimeconfig.json"
24+
#define HostExe "\\altv-server.exe"
2425
#else
2526
#define HostDll "/AltV.Net.Host.dll"
2627
#define HostCfg "/AltV.Net.Host.runtimeconfig.json"
28+
#define HostExe "/altv-server"
2729
#endif
2830

2931
#ifdef __clang__
@@ -64,7 +66,8 @@ int tail_eq(char* lhs, char* rhs);
6466

6567
int tail_gt(char* lhs, char* rhs);
6668

67-
typedef void (* ExecuteResourceDelegate_t)(const char* resourcePath, const char* resourceName, const char* resourceMain, int resourceIndex);
69+
typedef void (* ExecuteResourceDelegate_t)(const char* resourcePath, const char* resourceName, const char* resourceMain,
70+
int resourceIndex);
6871

6972
class CoreClr {
7073
public:
@@ -80,8 +83,9 @@ class CoreClr {
8083
void CreateAppDomain(alt::IServer* server, alt::IResource* resource, const char* appPath, void** runtimeHost,
8184
unsigned int* domainId, bool executable, uint64_t resourceIndex, const char* domainName);
8285

83-
int Execute(alt::IServer* server, alt::IResource* resource, const char* appPath, uint64_t resourceIndex, void** runtimeHost,
84-
const unsigned int* domainId);
86+
int Execute(alt::IServer* server, alt::IResource* resource, const char* appPath, uint64_t resourceIndex,
87+
void** runtimeHost,
88+
const unsigned int* domainId);
8589

8690
void Shutdown(alt::IServer* server, void* runtimeHost,
8791
unsigned int domainId);
@@ -98,9 +102,11 @@ class CoreClr {
98102

99103
void CreateManagedHost(alt::IServer* server);
100104

101-
void ExecuteManagedResource(alt::IServer* server, const char* resourcePath, const char* resourceName, const char* resourceMain, alt::IResource* resource);
105+
void ExecuteManagedResource(alt::IServer* server, const char* resourcePath, const char* resourceName,
106+
const char* resourceMain, alt::IResource* resource);
102107

103-
load_assembly_and_get_function_pointer_fn get_dotnet_load_assembly(const char_t *config_path);
108+
load_assembly_and_get_function_pointer_fn
109+
get_dotnet_load_assembly(const char_t* config_path, const char_t* hostExe, const char_t* dotnetRoot);
104110

105111
private:
106112
#ifdef _WIN32
@@ -109,6 +115,7 @@ class CoreClr {
109115
void* _coreClrLib;
110116
#endif
111117
char* runtimeDirectory;
118+
char* dotnetDirectory;
112119
coreclr_initialize_ptr _initializeCoreCLR;
113120
coreclr_shutdown_2_ptr _shutdownCoreCLR;
114121
coreclr_create_delegate_ptr _createDelegate;

runtime/src/CoreClr.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ CoreClr::CoreClr(alt::IServer* server) {
2424
strcat(defaultPath, windowsProgramFilesPath);
2525
GetPath(server, defaultPath);
2626
delete[] defaultPath;
27+
28+
const char *dotnetProgramFilesPath = "/dotnet/";
29+
dotnetDirectory = new char[strlen(dotnetProgramFilesPath) + strlen(pf) + 1];
30+
strcpy(dotnetDirectory, pf);
31+
strcat(dotnetDirectory, dotnetProgramFilesPath);
2732
#else
2833
GetPath(server, "/usr/share/dotnet/host/fxr/");
34+
dotnetDirectory = new char[strlen("/usr/share/dotnet/") + 1];
35+
strcpy(dotnetDirectory, "/usr/share/dotnet/");
2936
#endif
3037
#ifdef _WIN32
3138
const char *fileName = "/hostfxr.dll";
@@ -67,7 +74,8 @@ CoreClr::CoreClr(alt::IServer* server) {
6774
}
6875

6976
CoreClr::~CoreClr() {
70-
free(runtimeDirectory);
77+
delete[] runtimeDirectory;
78+
delete[] dotnetDirectory;
7179
}
7280

7381
bool CoreClr::GetDelegate(alt::IServer* server, void* runtimeHost, unsigned int domainId, const char* moduleName,
@@ -374,8 +382,13 @@ void CoreClr::CreateManagedHost(alt::IServer* server) {
374382
auto wd = server->GetRootDirectory().CStr();
375383
auto hostCfgPath = alt::String(wd) + HostCfg;
376384
auto hostDllPath = alt::String(wd) + HostDll;
385+
auto hostExePath = alt::String(wd) + HostExe;
377386
server->LogInfo(alt::String("coreclr-module: Prepare for loading config:") + hostCfgPath);
378-
auto load_assembly_and_get_function_pointer = get_dotnet_load_assembly((const char_t*) hostCfgPath.CStr());
387+
server->LogInfo(alt::String("coreclr-module: dotnet root:") + dotnetDirectory);
388+
server->LogInfo(alt::String("coreclr-module: host exe:") + hostExePath);
389+
auto load_assembly_and_get_function_pointer = get_dotnet_load_assembly((const char_t*) hostCfgPath.CStr(),
390+
(const char_t*) hostExePath.CStr(),
391+
(const char_t*) dotnetDirectory);
379392
if (load_assembly_and_get_function_pointer == nullptr) return;
380393
server->LogInfo(alt::String("coreclr-module: Prepare for executing host:") + hostDllPath);
381394

@@ -423,11 +436,16 @@ void CoreClr::ExecuteManagedResource(alt::IServer* server, const char* resourceP
423436
ExecuteResourceDelegate(&args, sizeof(args));
424437
}
425438

426-
load_assembly_and_get_function_pointer_fn CoreClr::get_dotnet_load_assembly(const char_t* config_path) {
439+
load_assembly_and_get_function_pointer_fn
440+
CoreClr::get_dotnet_load_assembly(const char_t* config_path, const char_t* hostExe, const char_t* dotnetRoot) {
427441
// Load .NET Core
428442
void* load_assembly_and_get_function_pointer = nullptr;
429443
hostfxr_handle cxt = nullptr;
430-
int rc = _initializeFxr(config_path, nullptr, &cxt);
444+
hostfxr_initialize_parameters initializeParameters{};
445+
initializeParameters.size = sizeof(hostfxr_initialize_parameters);
446+
initializeParameters.host_path = hostExe;
447+
initializeParameters.dotnet_root = dotnetRoot;
448+
int rc = _initializeFxr(config_path, &initializeParameters, &cxt);
431449
if (rc != 0 || cxt == nullptr) {
432450
std::cerr << "Init failed: " << std::hex << std::showbase << rc << std::endl;
433451
_closeFxr(cxt);

0 commit comments

Comments
 (0)