Skip to content

Commit

Permalink
.NET: Fix some method invocations not working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Mar 18, 2024
1 parent bc89743 commit 3df93fc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
50 changes: 41 additions & 9 deletions csharp-api/REFrameworkNET/Method.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <bit>

#include "ManagedObject.hpp"
#include "NativeObject.hpp"

#include "Method.hpp"

Expand All @@ -8,11 +11,24 @@ REFrameworkNET::InvokeRet^ Method::Invoke(System::Object^ obj, array<System::Obj
std::vector<void*> args2{};
args2.resize(args->Length);

for (int i = 0; i < args->Length; ++i) {
for (int i = 0; i < args->Length; ++i) try {
if (args[i] == nullptr) {
args2[i] = nullptr;
continue;
}

//args2[i] = args[i]->ptr();
const auto t = args[i]->GetType();

if (t == System::Byte::typeid) {
if (t == REFrameworkNET::ManagedObject::typeid) {
args2[i] = safe_cast<REFrameworkNET::ManagedObject^>(args[i])->Ptr();
} else if (t == System::Boolean::typeid) {
bool v = System::Convert::ToBoolean(args[i]);
args2[i] = (void*)(intptr_t)v;
} else if (t == System::Int32::typeid) {
int32_t v = System::Convert::ToInt32(args[i]);
args2[i] = (void*)(intptr_t)v;
} else if (t == System::Byte::typeid) {
uint8_t v = System::Convert::ToByte(args[i]);
args2[i] = (void*)(uint64_t)v;
} else if (t == System::UInt16::typeid) {
Expand All @@ -22,32 +38,48 @@ REFrameworkNET::InvokeRet^ Method::Invoke(System::Object^ obj, array<System::Obj
uint32_t v = System::Convert::ToUInt32(args[i]);
args2[i] = (void*)(uint64_t)v;
} else if (t == System::Single::typeid) {
// this might seem counterintuitive, converting a float to a double
// but the invoke wrappers ALWAYS expect a double, so we need to do this
// even when they take a System.Single, the wrappers take in a double and convert it to a float
float v = System::Convert::ToSingle(args[i]);
args2[i] = (void*)(uint64_t)v;
auto d = (double)v;
auto n = *(int64_t*)&d;
args2[i] = (void*)(uint64_t)n;
} else if (t == System::UInt64::typeid) {
uint64_t v = System::Convert::ToUInt64(args[i]);
args2[i] = (void*)(uint64_t)v;
} else if (t == System::Double::typeid) {
double v = System::Convert::ToDouble(args[i]);
args2[i] = (void*)(uint64_t)v;
auto n = *(int64_t*)&v;
args2[i] = (void*)(uint64_t)n;
} else if (t == System::IntPtr::typeid) {
args2[i] = (void*)(uint64_t)System::Convert::ToInt64(args[i]);
} else {
//args2[i] = args[i]->ptr();
args2[i] = nullptr;
System::Console::WriteLine("Unknown type passed to method invocation @ arg " + i);
}
} catch (System::Exception^ e) {
System::Console::WriteLine("Error converting argument " + i + ": " + e->Message);
}

void* obj_ptr = nullptr;

if (obj != nullptr) {
if (obj != nullptr) try {
const auto obj_t = obj->GetType();
if (obj_t == System::IntPtr::typeid || obj_t == System::UIntPtr::typeid) {
obj_ptr = (void*)(uint64_t)System::Convert::ToInt64(obj);

if (obj_t == System::IntPtr::typeid) {
obj_ptr = (void*)(intptr_t)safe_cast<System::IntPtr>(obj).ToPointer();
} else if (obj_t == System::UIntPtr::typeid) {
obj_ptr = (void*)(uintptr_t)safe_cast<System::UIntPtr>(obj).ToUInt64();
} else if (obj_t == REFrameworkNET::ManagedObject::typeid) {
obj_ptr = safe_cast<REFrameworkNET::ManagedObject^>(obj)->Ptr();
} else if (obj_t == REFrameworkNET::NativeObject::typeid) {
obj_ptr = safe_cast<REFrameworkNET::NativeObject^>(obj)->Ptr();
} else {
//obj_ptr = obj->ptr();
System::Console::WriteLine("Unknown type passed to method invocation @ obj");
}
} catch (System::Exception^ e) {
System::Console::WriteLine("Error converting object: " + e->Message);
}

const auto native_result = m_method->invoke((reframework::API::ManagedObject*)obj_ptr, args2);
Expand Down
25 changes: 13 additions & 12 deletions csharp-api/test/Test/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,26 @@ public static void Main(REFrameworkNET.API api) {
Console.WriteLine("Testing REFrameworkAPI...");

REFrameworkNET.Callbacks.BeginRendering.Pre += () => {
Console.WriteLine("BeginRendering pre");
sw.Start();
};
REFrameworkNET.Callbacks.BeginRendering.Post += () => {
Console.WriteLine("BeginRendering post");
sw.Stop();
Console.WriteLine("BeginRendering took " + sw.ElapsedMilliseconds + "ms");

if (sw.ElapsedMilliseconds >= 6) {
Console.WriteLine("BeginRendering took " + sw.ElapsedMilliseconds + "ms");
}

sw.Reset();
};

REFrameworkNET.Callbacks.EndRendering.Post += () => {
Console.WriteLine("EndRendering");
};

REFrameworkNET.Callbacks.FinalizeRenderer.Pre += () => {
Console.WriteLine("Finalizing!!!!");
Console.WriteLine("Finalizing Renderer");
};

REFrameworkNET.Callbacks.PrepareRendering.Post += () => {
Console.WriteLine("PrepareRendering");
};

// Convert api.Get() type to pass to GenerateWrapper
Expand All @@ -129,7 +130,7 @@ public static void Main(REFrameworkNET.API api) {
// Open in explorer
System.Diagnostics.Process.Start("explorer.exe", currentDir);*/

var tdb = api.GetTDB();
var tdb = REFrameworkNET.API.GetTDB();

Console.WriteLine(tdb.GetNumTypes().ToString() + " types");

Expand Down Expand Up @@ -157,7 +158,7 @@ public static void Main(REFrameworkNET.API api) {

Console.WriteLine("Done with types");

var singletons = api.GetManagedSingletons();
var singletons = REFrameworkNET.API.GetManagedSingletons();

foreach (var singletonDesc in singletons) {
var singleton = singletonDesc.Instance;
Expand Down Expand Up @@ -187,13 +188,13 @@ public static void Main(REFrameworkNET.API api) {
}
}

/*var sceneManager = api.GetNativeSingleton("via.SceneManager");
var sceneManager = REFrameworkNET.API.GetNativeSingleton("via.SceneManager");
Console.WriteLine("sceneManager: " + sceneManager);
var sceneManager_t = tdb.FindType("via.SceneManager");
Console.WriteLine("sceneManager_t: " + sceneManager_t);
var get_CurrentScene = sceneManager_t.FindMethod("get_CurrentScene");
Console.WriteLine("get_CurrentScene: " + get_CurrentScene);
var scene = get_CurrentScene.Invoke(sceneManager, new object[]{}).Ptr;
var scene = get_CurrentScene.Invoke(sceneManager, []).Ptr;

Console.WriteLine("scene: " + scene);

Expand All @@ -203,7 +204,7 @@ public static void Main(REFrameworkNET.API api) {

Console.WriteLine("set_TimeScale: " + set_TimeScale);

set_TimeScale.Invoke(scene, new object[]{ 0.1f });
}*/
set_TimeScale.Invoke(scene, new object[]{0.1f});
}
}
};

0 comments on commit 3df93fc

Please sign in to comment.