Skip to content

Commit

Permalink
.NET: Add callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Mar 18, 2024
1 parent 3bcf94f commit 7229ff3
Show file tree
Hide file tree
Showing 6 changed files with 504 additions and 5 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12893,6 +12893,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
list(APPEND csharp-api_SOURCES
"csharp-api/Plugin.cpp"
"csharp-api/REFrameworkNET/API.cpp"
"csharp-api/REFrameworkNET/Callbacks.cpp"
"csharp-api/REFrameworkNET/ManagedObject.cpp"
"csharp-api/REFrameworkNET/Method.cpp"
"csharp-api/REFrameworkNET/PluginManager.cpp"
Expand Down
13 changes: 9 additions & 4 deletions csharp-api/REFrameworkNET/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@

REFrameworkNET::API::API(const REFrameworkPluginInitializeParam* param)
{
Console::WriteLine("REFrameworkNET.API Constructor called.");
s_api = reframework::API::initialize(param).get();
Init_Internal(param);
}

REFrameworkNET::API::API(uintptr_t param)
{
Console::WriteLine("REFrameworkNET.API Constructor called.");
s_api = reframework::API::initialize((const REFrameworkPluginInitializeParam*)param).get();
Init_Internal(reinterpret_cast<const REFrameworkPluginInitializeParam*>(param));
}

void REFrameworkNET::API::Init_Internal(const REFrameworkPluginInitializeParam* param)
{
Console::WriteLine("REFrameworkNET.API Init_Internal called.");
s_api = reframework::API::initialize(param).get();
Callbacks::Impl::Setup(this);
}

REFrameworkNET::API::~API()
Expand Down
8 changes: 8 additions & 0 deletions csharp-api/REFrameworkNET/API.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "TDB.hpp"
#include "ManagedSingleton.hpp"

#include "Callbacks.hpp"

namespace reframework {
class API;
}
Expand Down Expand Up @@ -58,7 +60,13 @@ public ref class API
return result;
}

reframework::API* GetNativeImplementation() {
return s_api;
}

protected:
void Init_Internal(const REFrameworkPluginInitializeParam* param);

static reframework::API* s_api;
};
}
43 changes: 43 additions & 0 deletions csharp-api/REFrameworkNET/Callbacks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "API.hpp"

#include "Callbacks.hpp"

using namespace System;

namespace REFrameworkNET {
namespace Callbacks {
void Impl::Setup(REFrameworkNET::API^ api) {
Console::WriteLine("REFrameworkNET.Callbacks SetupCallbacks called.");

reframework::API* nativeApi = api->GetNativeImplementation();
System::Reflection::Assembly^ assembly = System::Reflection::Assembly::GetExecutingAssembly();
array<Type^>^ types = assembly->GetTypes();

// Look for REFrameworkNET.Callbacks.* classes
for each (Type^ type in types) {
if (type->Namespace != "REFrameworkNET.Callbacks") {
continue;
}

if (!type->IsClass) {
continue;
}

if (type->GetField("FUNCTION_PRE_CALLBACK_ADDRESS") == nullptr) {
continue;
}

if (type->GetField("FUNCTION_POST_CALLBACK_ADDRESS") == nullptr) {
continue;
}

auto eventName = type->Name;
auto eventNameStr = msclr::interop::marshal_as<std::string>(eventName);
auto eventHandlerPre = (uintptr_t)type->GetField("FUNCTION_PRE_CALLBACK_ADDRESS")->GetValue(nullptr);
auto eventHandlerPost = (uintptr_t)type->GetField("FUNCTION_POST_CALLBACK_ADDRESS")->GetValue(nullptr);
nativeApi->param()->functions->on_pre_application_entry(eventNameStr.c_str(), (REFOnPreApplicationEntryCb)eventHandlerPre);
nativeApi->param()->functions->on_post_application_entry(eventNameStr.c_str(), (REFOnPostApplicationEntryCb)eventHandlerPost);
}
}
}
}
Loading

0 comments on commit 7229ff3

Please sign in to comment.