fun with modding

This commit is contained in:
Erris
2026-01-23 15:41:59 +01:00
parent 58ea4554c7
commit 1a584bcd2f

View File

@@ -8,8 +8,6 @@
#include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.0/runtimes/arch-x64/native/nethost.h" #include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.0/runtimes/arch-x64/native/nethost.h"
#include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.0/runtimes/arch-x64/native/hostfxr.h" #include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.0/runtimes/arch-x64/native/hostfxr.h"
#include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.0/runtimes/arch-x64/native/coreclr_delegates.h" #include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.0/runtimes/arch-x64/native/coreclr_delegates.h"
#include "open_engine/core/time.hpp"
#include "open_engine/logging.hpp"
class Modding : public OpenEngine::Layer class Modding : public OpenEngine::Layer
{ {
@@ -29,7 +27,8 @@ class Modding : public OpenEngine::Layer
OE_ERROR("Failed to find symbol: {} Error: {}", name, dlerror()); OE_ERROR("Failed to find symbol: {} Error: {}", name, dlerror());
} }
return f; return f;
} };
// Using the nethost library, discover the location of hostfxr and get exports // Using the nethost library, discover the location of hostfxr and get exports
bool load_hostfxr() bool load_hostfxr()
{ {
@@ -47,7 +46,7 @@ class Modding : public OpenEngine::Layer
close_fptr = (hostfxr_close_fn)get_export(lib, "hostfxr_close"); close_fptr = (hostfxr_close_fn)get_export(lib, "hostfxr_close");
return (init_fptr && get_delegate_fptr && close_fptr); return (init_fptr && get_delegate_fptr && close_fptr);
} };
load_assembly_and_get_function_pointer_fn get_dotnet_load_assembly(const char_t *config_path) load_assembly_and_get_function_pointer_fn get_dotnet_load_assembly(const char_t *config_path)
{ {
@@ -74,29 +73,39 @@ class Modding : public OpenEngine::Layer
return (load_assembly_and_get_function_pointer_fn)load_assembly_and_get_function_pointer; return (load_assembly_and_get_function_pointer_fn)load_assembly_and_get_function_pointer;
} }
component_entry_point_fn LoadMethod(const char* method)
{
component_entry_point_fn method_ptr;
int rc = load_assembly_and_get_function_pointer(
dotnet_lib_path,
dotnet_type,
method,
UNMANAGEDCALLERSONLY_METHOD /*delegate_type_name*/,
nullptr,
(void**)&method_ptr);
if (rc != 0) {
OE_ERROR("Failed to load method {}", method);
return nullptr;
}
return method_ptr;
}
void OnAttach() override void OnAttach() override
{ {
load_hostfxr(); load_hostfxr();
auto load_assembly_and_get_function_pointer = get_dotnet_load_assembly("assets/scripts/mod_loader.runtimeconfig.json"); load_assembly_and_get_function_pointer = get_dotnet_load_assembly("assets/scripts/mod_loader.runtimeconfig.json");
component_entry_point_fn init = nullptr; component_entry_point_fn init = LoadMethod("Init");
int rc = load_assembly_and_get_function_pointer(
dotnet_lib_path,
dotnet_type,
dotnet_type_method,
UNMANAGEDCALLERSONLY_METHOD /*delegate_type_name*/,
nullptr,
(void**)&init);
init(nullptr, 1); init(nullptr, 0);
rc = load_assembly_and_get_function_pointer( hello = LoadMethod("Hello");
dotnet_lib_path,
dotnet_type, load_mod = LoadMethod("LoadMod");
"Hello", load_mod(nullptr, 0);
UNMANAGEDCALLERSONLY_METHOD /*delegate_type_name*/,
nullptr,
(void**)&hello);
} }
void OnDetach() override void OnDetach() override
@@ -107,10 +116,10 @@ class Modding : public OpenEngine::Layer
{ {
static float time = 0; static float time = 0;
time += OpenEngine::Time::Get().DeltaTime(); time += OpenEngine::Time::DeltaTime();
if (time >= 1) { if (time >= 1) {
hello(nullptr, 1); hello(nullptr, 0);
time = 0; time = 0;
} }
} }
@@ -130,8 +139,10 @@ class Modding : public OpenEngine::Layer
hostfxr_initialize_for_runtime_config_fn init_fptr; hostfxr_initialize_for_runtime_config_fn init_fptr;
hostfxr_get_runtime_delegate_fn get_delegate_fptr; hostfxr_get_runtime_delegate_fn get_delegate_fptr;
hostfxr_close_fn close_fptr; hostfxr_close_fn close_fptr;
load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer;
component_entry_point_fn hello = nullptr; component_entry_point_fn hello = nullptr;
component_entry_point_fn load_mod = nullptr;
const char_t* dotnet_lib_path = "assets/scripts/mod_loader.dll"; const char_t* dotnet_lib_path = "assets/scripts/mod_loader.dll";
const char_t* dotnet_type = "OpenEngine.ModLoader, mod_loader"; const char_t* dotnet_type = "OpenEngine.ModLoader, mod_loader";