diff --git a/csharp-api/REFrameworkNET/Method.cpp b/csharp-api/REFrameworkNET/Method.cpp index b7a56a612..e22acbc0f 100644 --- a/csharp-api/REFrameworkNET/Method.cpp +++ b/csharp-api/REFrameworkNET/Method.cpp @@ -1,4 +1,7 @@ +#include + #include "ManagedObject.hpp" +#include "NativeObject.hpp" #include "Method.hpp" @@ -8,11 +11,24 @@ REFrameworkNET::InvokeRet^ Method::Invoke(System::Object^ obj, array 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(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) { @@ -22,32 +38,48 @@ REFrameworkNET::InvokeRet^ Method::Invoke(System::Object^ obj, arrayptr(); + 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(obj).ToPointer(); + } else if (obj_t == System::UIntPtr::typeid) { + obj_ptr = (void*)(uintptr_t)safe_cast(obj).ToUInt64(); } else if (obj_t == REFrameworkNET::ManagedObject::typeid) { obj_ptr = safe_cast(obj)->Ptr(); + } else if (obj_t == REFrameworkNET::NativeObject::typeid) { + obj_ptr = safe_cast(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); diff --git a/csharp-api/test/Test/Test.cs b/csharp-api/test/Test/Test.cs index 9c92524a7..53572c7aa 100644 --- a/csharp-api/test/Test/Test.cs +++ b/csharp-api/test/Test/Test.cs @@ -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 @@ -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"); @@ -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; @@ -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); @@ -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}); + } } }; \ No newline at end of file