Skip to content

Commit

Permalink
.NET: Allow proxies to be casted to IObject
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Mar 27, 2024
1 parent 17a5fe6 commit f8408d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
36 changes: 35 additions & 1 deletion csharp-api/REFrameworkNET/Proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

#include "./API.hpp"
#include "ManagedObject.hpp"
#include "TypeDefinition.hpp"

using namespace System;

// Classes for proxying interfaces from auto generated assemblies to our dynamic types
namespace REFrameworkNET {
public interface class IProxy {
// Inherit IObject's interface as well
// so we can easily pretend that this is an IObject, even though it just forwards the calls to the actual object
public interface class IProxy : IObject {
IObject^ GetInstance();
void SetInstance(IObject^ instance);
};

Expand All @@ -18,12 +22,42 @@ where T2 : ref class
public ref class Proxy : public Reflection::DispatchProxy, public IProxy, public System::IEquatable<Proxy<T, T2>^>
{
public:
virtual REFrameworkNET::TypeDefinition^ GetTypeDefinition() {
return Instance->GetTypeDefinition();
}

virtual void* Ptr() {
return Instance->Ptr();
}

virtual uintptr_t GetAddress() {
return Instance->GetAddress();
}

virtual REFrameworkNET::InvokeRet^ Invoke(System::String^ methodName, array<System::Object^>^ args) {
return Instance->Invoke(methodName, args);
}

virtual bool HandleInvokeMember_Internal(System::String^ methodName, array<System::Object^>^ args, System::Object^% result) {
return Instance->HandleInvokeMember_Internal(methodName, args, result);
}

// For interface types
generic <typename T>
virtual T As() {
return Instance->As<T>();
}

static T Create(IObject^ target) {
auto proxy = Reflection::DispatchProxy::Create<T, Proxy<T, T2>^>();
((IProxy^)proxy)->SetInstance(target);
return proxy;
}

virtual IObject^ GetInstance() {
return Instance;
}

virtual void SetInstance(IObject^ instance) {
Instance = instance;
}
Expand Down
2 changes: 1 addition & 1 deletion csharp-api/test/Test/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void Entry() {
REFrameworkNET.API.LogInfo("Scene name: " + name);

// Testing autocomplete for the concrete ManagedObject
REFrameworkNET.API.LogInfo("Scene: " + scene.ToString() + ": " + (scene as REFrameworkNET.ManagedObject)?.GetTypeDefinition()?.GetFullName()?.ToString());
REFrameworkNET.API.LogInfo("Scene: " + scene.ToString() + ": " + (scene as REFrameworkNET.IObject).GetTypeDefinition()?.GetFullName()?.ToString());

// Testing dynamic invocation
float currentTimescale = scene.get_TimeScale();
Expand Down

0 comments on commit f8408d2

Please sign in to comment.