Skip to content

Commit

Permalink
.NET: Initial support for creating ValueTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed May 12, 2024
1 parent e86651e commit bd4634e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions csharp-api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -223,6 +224,7 @@ set(csharp-api_SOURCES
"REFrameworkNET/UnifiedObject.hpp"
"REFrameworkNET/Utility.hpp"
"REFrameworkNET/VM.hpp"
"REFrameworkNET/ValueType.hpp"
cmake.toml
)

Expand Down
17 changes: 17 additions & 0 deletions csharp-api/REFrameworkNET/ValueType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "Proxy.hpp"
#include "TDB.hpp"
#include "ValueType.hpp"

namespace REFrameworkNET {
generic <typename T>
T ValueType::As() {
return AnyProxy<T>::Create(this);
}

generic <typename T>
T ValueType::New() {
auto t = TDB::TypeCacher<T>::GetCachedType();
auto valueType = gcnew ValueType(t);
return valueType->As<T>();
}
}
42 changes: 42 additions & 0 deletions csharp-api/REFrameworkNET/ValueType.hpp
Original file line number Diff line number Diff line change
@@ -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<uint8_t>(t->ValueTypeSize);
pin_ptr<uint8_t> data = &m_data[0];
}

/// <returns>The type of the object</returns>
virtual TypeDefinition^ GetTypeDefinition() override {
return m_type;
}

/// <returns>The address of the object as a void* pointer</returns>
virtual void* Ptr() override {
pin_ptr<uint8_t> data = &m_data[0];
return data;
}

/// <returns>The address of the object</returns>
virtual uintptr_t GetAddress() override {
return (uintptr_t)Ptr();
}

generic <typename T>
virtual T As() override;

generic <typename T>
where T : ref class
static T New();

private:
array<uint8_t>^ m_data{};
TypeDefinition^ m_type{};
};
}
16 changes: 15 additions & 1 deletion csharp-api/test/Test/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static REFrameworkNET.PreHookResult isInsidePreHook(Span<ulong> 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());
}
Expand All @@ -39,6 +39,8 @@ public static REFrameworkNET.PreHookResult processNormalAttackPreHook(Span<ulong
}

public static void Entry() {
via.render.RayTracingManager.set_EnableLod(false);
via.render.RayTracingManager.set_PreferShadowCast(true);
var mouse = REFrameworkNET.API.GetNativeSingletonT<via.hid.Mouse>();

via.hid.Mouse.set_ShowCursor(false);
Expand Down Expand Up @@ -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<via.RangeI>();
var range = REFrameworkNET.ValueType.New<via.RangeI>();
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<via.render.Mesh>();
mesh.set_DrawRaytracing(true);
}
}

public static void TryEnableFrameGeneration() {
Expand Down

0 comments on commit bd4634e

Please sign in to comment.