From bd4634ec07700593d0108545a5df015543741f28 Mon Sep 17 00:00:00 2001 From: praydog Date: Sat, 11 May 2024 17:01:40 -0700 Subject: [PATCH] .NET: Initial support for creating ValueTypes --- csharp-api/CMakeLists.txt | 2 ++ csharp-api/REFrameworkNET/ValueType.cpp | 17 ++++++++++ csharp-api/REFrameworkNET/ValueType.hpp | 42 +++++++++++++++++++++++++ csharp-api/test/Test/Test.cs | 16 +++++++++- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 csharp-api/REFrameworkNET/ValueType.cpp create mode 100644 csharp-api/REFrameworkNET/ValueType.hpp diff --git a/csharp-api/CMakeLists.txt b/csharp-api/CMakeLists.txt index 0648a8d36..b0525aa7e 100644 --- a/csharp-api/CMakeLists.txt +++ b/csharp-api/CMakeLists.txt @@ -192,6 +192,7 @@ set(csharp-api_SOURCES "REFrameworkNET/TypeDefinition.cpp" "REFrameworkNET/UnifiedObject.cpp" "REFrameworkNET/VM.cpp" + "REFrameworkNET/ValueType.cpp" "REFrameworkNET/API.hpp" "REFrameworkNET/Attributes/Callback.hpp" "REFrameworkNET/Attributes/Method.hpp" @@ -223,6 +224,7 @@ set(csharp-api_SOURCES "REFrameworkNET/UnifiedObject.hpp" "REFrameworkNET/Utility.hpp" "REFrameworkNET/VM.hpp" + "REFrameworkNET/ValueType.hpp" cmake.toml ) diff --git a/csharp-api/REFrameworkNET/ValueType.cpp b/csharp-api/REFrameworkNET/ValueType.cpp new file mode 100644 index 000000000..4c8d90a41 --- /dev/null +++ b/csharp-api/REFrameworkNET/ValueType.cpp @@ -0,0 +1,17 @@ +#include "Proxy.hpp" +#include "TDB.hpp" +#include "ValueType.hpp" + +namespace REFrameworkNET { +generic +T ValueType::As() { + return AnyProxy::Create(this); +} + +generic +T ValueType::New() { + auto t = TDB::TypeCacher::GetCachedType(); + auto valueType = gcnew ValueType(t); + return valueType->As(); +} +} \ No newline at end of file diff --git a/csharp-api/REFrameworkNET/ValueType.hpp b/csharp-api/REFrameworkNET/ValueType.hpp new file mode 100644 index 000000000..60bc87493 --- /dev/null +++ b/csharp-api/REFrameworkNET/ValueType.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include "UnifiedObject.hpp" +#include "TypeDefinition.hpp" + +namespace REFrameworkNET { +public ref class ValueType : public UnifiedObject { +public: + ValueType(TypeDefinition^ t) + { + m_data = gcnew array(t->ValueTypeSize); + pin_ptr data = &m_data[0]; + } + + /// The type of the object + virtual TypeDefinition^ GetTypeDefinition() override { + return m_type; + } + + /// The address of the object as a void* pointer + virtual void* Ptr() override { + pin_ptr data = &m_data[0]; + return data; + } + + /// The address of the object + virtual uintptr_t GetAddress() override { + return (uintptr_t)Ptr(); + } + + generic + virtual T As() override; + + generic + where T : ref class + static T New(); + +private: + array^ m_data{}; + TypeDefinition^ m_type{}; +}; +} \ No newline at end of file diff --git a/csharp-api/test/Test/Test.cs b/csharp-api/test/Test/Test.cs index 558b4d6d9..ec3f60a4e 100644 --- a/csharp-api/test/Test/Test.cs +++ b/csharp-api/test/Test/Test.cs @@ -19,7 +19,7 @@ public static REFrameworkNET.PreHookResult isInsidePreHook(Span args) { [MethodHook(typeof(app.CameraManager), nameof(app.CameraManager.isInside), MethodHookType.Post)] public static void isInsidePostHook(ref ulong retval) { if ((retval & 1) != 0) { - REFrameworkNET.API.LogInfo("Camera is inside"); + //REFrameworkNET.API.LogInfo("Camera is inside"); } //Console.WriteLine("Inside post hook (From C#), retval: " + (retval & 1).ToString()); } @@ -39,6 +39,8 @@ public static REFrameworkNET.PreHookResult processNormalAttackPreHook(Span(); via.hid.Mouse.set_ShowCursor(false); @@ -109,6 +111,18 @@ public static void Entry() { var stringInDotNetVM = stringTest.ToString(); // Back in .NET REFrameworkNET.API.LogInfo("Managed string back in .NET: " + stringInDotNetVM); + + var meshes = via.SceneManager.get_MainScene().findComponents(via.render.Mesh.REFType.RuntimeType.As<_System.Type>()); + //var range = via.RangeI.REFType.CreateInstance(0).As(); + var range = REFrameworkNET.ValueType.New(); + range.setMinMax(0, 10); + // print min max to test if this works + REFrameworkNET.API.LogInfo("Range min: " + range.getMin().ToString()); + REFrameworkNET.API.LogInfo("Range max: " + range.getMax().ToString()); + for (int i = 0; i < meshes.get_Length(); i++) { + var mesh = (meshes.get_Item(i) as IObject).As(); + mesh.set_DrawRaytracing(true); + } } public static void TryEnableFrameGeneration() {