Skip to content

Commit

Permalink
Plugins: Add FMalloc functions and TArray safety deletion upon destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Feb 20, 2024
1 parent 89f4762 commit f48ed67
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
12 changes: 11 additions & 1 deletion include/uevr/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SOFTWARE.
#define UEVR_OUT

#define UEVR_PLUGIN_VERSION_MAJOR 2
#define UEVR_PLUGIN_VERSION_MINOR 11
#define UEVR_PLUGIN_VERSION_MINOR 12
#define UEVR_PLUGIN_VERSION_PATCH 0

#define UEVR_RENDERER_D3D11 0
Expand Down Expand Up @@ -73,6 +73,7 @@ DECLARE_UEVR_HANDLE(UEVR_IConsoleObjectHandle);
DECLARE_UEVR_HANDLE(UEVR_IConsoleCommandHandle);
DECLARE_UEVR_HANDLE(UEVR_IConsoleVariableHandle);
DECLARE_UEVR_HANDLE(UEVR_TArrayHandle);
DECLARE_UEVR_HANDLE(UEVR_FMallocHandle);

/* OpenXR stuff */
DECLARE_UEVR_HANDLE(UEVR_XrInstance);
Expand Down Expand Up @@ -319,6 +320,14 @@ typedef struct {
void (*constructor)(UEVR_FNameHandle name, const wchar_t* data, unsigned int find_type);
} UEVR_FNameFunctions;

typedef struct {
UEVR_FMallocHandle (*get)();

void* (*malloc)(UEVR_FMallocHandle instance, unsigned int size, unsigned int alignment);
void* (*realloc)(UEVR_FMallocHandle instance, void* ptr, unsigned int size, unsigned int alignment);
void (*free)(UEVR_FMallocHandle instance, void* ptr);
} UEVR_FMallocFunctions;

typedef struct {
const UEVR_SDKFunctions* functions;
const UEVR_SDKCallbacks* callbacks;
Expand All @@ -333,6 +342,7 @@ typedef struct {
const UEVR_FFieldClassFunctions* ffield_class;
const UEVR_FNameFunctions* fname;
const UEVR_ConsoleFunctions* console;
const UEVR_FMallocFunctions* malloc;
} UEVR_SDKData;

DECLARE_UEVR_HANDLE(UEVR_IVRSystem);
Expand Down
45 changes: 44 additions & 1 deletion include/uevr/API.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,43 @@ class API {
return (FConsoleManager*)fn();
}

struct FMalloc {
static FMalloc* get() {
static const auto fn = initialize()->get;
return (FMalloc*)fn();
}

inline UEVR_FMallocHandle to_handle() { return (UEVR_FMallocHandle)this; }
inline UEVR_FMallocHandle to_handle() const { return (UEVR_FMallocHandle)this; }

using FMallocSizeT = uint32_t; // because of C89

void* malloc(FMallocSizeT size, uint32_t alignment = 0) {
static const auto fn = initialize()->malloc;
return fn(to_handle(), size, alignment);
}

void* realloc(void* original, FMallocSizeT size, uint32_t alignment = 0) {
static const auto fn = initialize()->realloc;
return fn(to_handle(), original, size, alignment);
}

void free(void* original) {
static const auto fn = initialize()->free;
fn(to_handle(), original);
}

private:
static inline const UEVR_FMallocFunctions* s_functions{nullptr};
static inline const UEVR_FMallocFunctions* initialize() {
if (s_functions == nullptr) {
s_functions = API::get()->sdk()->malloc;
}

return s_functions;
}
};

struct FName {
inline UEVR_FNameHandle to_handle() { return (UEVR_FNameHandle)this; }
inline UEVR_FNameHandle to_handle() const { return (UEVR_FNameHandle)this; }
Expand Down Expand Up @@ -657,13 +694,19 @@ class API {

// One of the very few non-opaque structs
// because these have never changed, if they do its because of bespoke code
// TODO: fully implement, GMalloc and everything?
template <typename T>
struct TArray {
T* data;
int32_t count;
int32_t capacity;

~TArray() {
if (data != nullptr) {
FMalloc::get()->free(data);
data = nullptr;
}
}

T* begin() {
return data;
}
Expand Down
2 changes: 1 addition & 1 deletion src/CommitHash.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#define UEVR_COMMIT_HASH "a7998cea4b19f6f2d77df8d2abfa4aa4b5988373"
#define UEVR_COMMIT_HASH "89f476243d2ebacea6f941a8458dd571a181b568"
#define UEVR_BUILD_DATE "20.02.2024"
#define UEVR_BUILD_TIME "00:00"
30 changes: 29 additions & 1 deletion src/mods/PluginLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,33 @@ UEVR_ConsoleFunctions g_console_functions {
uevr::console::command_execute
};

namespace uevr {
namespace malloc {
UEVR_FMallocHandle get() {
return (UEVR_FMallocHandle)sdk::FMalloc::get();
}

void* malloc(UEVR_FMallocHandle malloc, unsigned int size, uint32_t alignment) {
return ((sdk::FMalloc*)malloc)->malloc(size, alignment);
}

void* realloc(UEVR_FMallocHandle malloc, void* original, unsigned int size, uint32_t alignment) {
return ((sdk::FMalloc*)malloc)->realloc(original, size, alignment);
}

void free(UEVR_FMallocHandle malloc, void* original) {
return ((sdk::FMalloc*)malloc)->free(original);
}
}
}

UEVR_FMallocFunctions g_malloc_functions {
uevr::malloc::get,
uevr::malloc::malloc,
uevr::malloc::realloc,
uevr::malloc::free
};

UEVR_SDKData g_sdk_data {
&g_sdk_functions,
&g_sdk_callbacks,
Expand All @@ -663,7 +690,8 @@ UEVR_SDKData g_sdk_data {
&g_uobjecthook_functions,
&g_ffield_class_functions,
&g_fname_functions,
&g_console_functions
&g_console_functions,
&g_malloc_functions
};

namespace uevr {
Expand Down

0 comments on commit f48ed67

Please sign in to comment.