fun with modding
This commit is contained in:
@@ -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/hostfxr.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
|
||||
{
|
||||
@@ -29,7 +27,8 @@ class Modding : public OpenEngine::Layer
|
||||
OE_ERROR("Failed to find symbol: {} Error: {}", name, dlerror());
|
||||
}
|
||||
return f;
|
||||
}
|
||||
};
|
||||
|
||||
// Using the nethost library, discover the location of hostfxr and get exports
|
||||
bool load_hostfxr()
|
||||
{
|
||||
@@ -47,7 +46,7 @@ class Modding : public OpenEngine::Layer
|
||||
close_fptr = (hostfxr_close_fn)get_export(lib, "hostfxr_close");
|
||||
|
||||
return (init_fptr && get_delegate_fptr && close_fptr);
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
load_hostfxr();
|
||||
|
||||
auto load_assembly_and_get_function_pointer = get_dotnet_load_assembly("assets/scripts/mod_loader.runtimeconfig.json");
|
||||
component_entry_point_fn init = nullptr;
|
||||
int rc = load_assembly_and_get_function_pointer(
|
||||
dotnet_lib_path,
|
||||
dotnet_type,
|
||||
dotnet_type_method,
|
||||
UNMANAGEDCALLERSONLY_METHOD /*delegate_type_name*/,
|
||||
nullptr,
|
||||
(void**)&init);
|
||||
load_assembly_and_get_function_pointer = get_dotnet_load_assembly("assets/scripts/mod_loader.runtimeconfig.json");
|
||||
component_entry_point_fn init = LoadMethod("Init");
|
||||
|
||||
init(nullptr, 1);
|
||||
init(nullptr, 0);
|
||||
|
||||
rc = load_assembly_and_get_function_pointer(
|
||||
dotnet_lib_path,
|
||||
dotnet_type,
|
||||
"Hello",
|
||||
UNMANAGEDCALLERSONLY_METHOD /*delegate_type_name*/,
|
||||
nullptr,
|
||||
(void**)&hello);
|
||||
hello = LoadMethod("Hello");
|
||||
|
||||
load_mod = LoadMethod("LoadMod");
|
||||
load_mod(nullptr, 0);
|
||||
}
|
||||
|
||||
void OnDetach() override
|
||||
@@ -107,10 +116,10 @@ class Modding : public OpenEngine::Layer
|
||||
{
|
||||
static float time = 0;
|
||||
|
||||
time += OpenEngine::Time::Get().DeltaTime();
|
||||
time += OpenEngine::Time::DeltaTime();
|
||||
|
||||
if (time >= 1) {
|
||||
hello(nullptr, 1);
|
||||
hello(nullptr, 0);
|
||||
time = 0;
|
||||
}
|
||||
}
|
||||
@@ -130,8 +139,10 @@ class Modding : public OpenEngine::Layer
|
||||
hostfxr_initialize_for_runtime_config_fn init_fptr;
|
||||
hostfxr_get_runtime_delegate_fn get_delegate_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 load_mod = nullptr;
|
||||
|
||||
const char_t* dotnet_lib_path = "assets/scripts/mod_loader.dll";
|
||||
const char_t* dotnet_type = "OpenEngine.ModLoader, mod_loader";
|
||||
|
||||
Reference in New Issue
Block a user