Skip to content

Commit

Permalink
WIP: Started adding error handling when modules are not loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
BramTeurlings committed Aug 29, 2024
1 parent 8112d58 commit 0a2447d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 53 deletions.
1 change: 0 additions & 1 deletion gamebridge_reshade/CMakelists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ add_library(${PROJECT_NAME} SHARED
src/hotkeymanager.cpp
src/hotkeymanager.h
src/delayLoader.h
src/pathGetter.h
)

if (WIN32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
Expand Down
23 changes: 14 additions & 9 deletions gamebridge_reshade/src/delayLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

#pragma once

#include "directx10weaver.h"
#include "pathGetter.h"

#include <Windows.h>
#include <delayimp.h>

static bool allDLLsLoaded = true;
// Todo: This can just be a single string instead, checking this for each DLL seems horribly inefficient.
std::vector<std::string> sr_dll_names = {"Glog.dll", "Opencv_world343.dll", "DimencoWeaving.dll", "SimulatedRealityCore.dll", "SimulatedRealityDisplays.dll", "SimulatedRealityFacetrackers.dll", "SimulatedRealityDirectX.dll", "DimencoWeaving32.dll", "SimulatedRealityCore32.dll", "SimulatedRealityDisplays32.dll", "SimulatedRealityFacetrackers32.dll", "SimulatedRealityDirectX32.dll"};

FARPROC WINAPI delayHook(unsigned dliNotify, PDelayLoadInfo pdli) {
std::string requested_dll;
switch (dliNotify) {
case dliStartProcessing :
case dliStartProcessing:

// If you want to return control to the helper, return 0.
// Otherwise, return a pointer to a FARPROC helper function
Expand All @@ -31,15 +30,20 @@ FARPROC WINAPI delayHook(unsigned dliNotify, PDelayLoadInfo pdli) {
// helper instead of having it call LoadLibrary itself.

// Check if the DLL in question is one we want to delayed load.
std::string requested_dll = pdli->szDll;
requested_dll = pdli->szDll;
for (int i = 0; i < sr_dll_names.size(); i++) {
if (sr_dll_names[i].find(requested_dll) != std::string::npos) {
// DLL matches one we want to load, let's load it
auto path_getter = PathGetter();
std::string sr_bin_path = path_getter.getSRBinPath();
HMODULE hModule LoadLibraryA((requested_dll + ".dll").c_str());
const HMODULE hModule = LoadLibraryA((requested_dll + ".dll").c_str());
const DWORD errorCode = GetLastError();
if (errorCode == ERROR_MOD_NOT_FOUND) {
std::cout << "Module not found (ERROR_MOD_NOT_FOUND)" << std::endl;
allDLLsLoaded = false;
// Todo: Stop the addon somehow and put it in an inactive state + display an error message.
return 0;
}
if (hModule) {
return (FARPROC)hModule;
return reinterpret_cast<FARPROC>(hModule);
}
}
}
Expand All @@ -65,6 +69,7 @@ FARPROC WINAPI delayHook(unsigned dliNotify, PDelayLoadInfo pdli) {
// the alternate DLL. The helper will continue execution with
// this alternate DLL and attempt to find the
// requested entrypoint via GetProcAddress.
throw std::runtime_error("Failed to load library");

break;

Expand Down
13 changes: 13 additions & 0 deletions gamebridge_reshade/src/directx11weaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ bool DirectX11Weaver::create_effect_copy_buffer(const reshade::api::resource_des
}

bool DirectX11Weaver::init_weaver(reshade::api::effect_runtime* runtime, reshade::api::resource rtv, reshade::api::command_list* cmd_list) {
if (!sr_ddls_loaded) {
// Trick the addon into
return false;
}

if (weaver_initialized) {
return weaver_initialized;
}
Expand Down Expand Up @@ -90,6 +95,14 @@ bool DirectX11Weaver::init_weaver(reshade::api::effect_runtime* runtime, reshade
reshade::log_message(reshade::log_level::info, e.what());
return false;
}
catch (std::runtime_error &e) {
if (e.what() == "Failed to load library") {
// Todo: Disable the addon since we are missing an SR DLL.
// Todo: Set the error message in the overlay to INACTIVE with reason "SR Platform not found, please (re)install it".
// Todo: Do this by chaning the method signature of this method to an int for error codes and then the dllmain can set dll_failed_to_load to true.
sr_ddls_loaded = false;
}
}
catch (...) {
reshade::log_message(reshade::log_level::info, "Couldn't initialize weaver");
return false;
Expand Down
1 change: 1 addition & 0 deletions gamebridge_reshade/src/directx11weaver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DirectX11Weaver: public IGraphicsApi {
uint32_t last_latency_frame_time_set = default_weaver_latency;
uint32_t effect_frame_copy_x = 0, effect_frame_copy_y = 0;

bool sr_ddls_loaded = true;
bool weaver_initialized = false;
bool weaving_enabled = false;
bool popup_window_visible = false;
Expand Down
9 changes: 8 additions & 1 deletion gamebridge_reshade/src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "hotkeymanager.h"
#include "directx10weaver.h"
#include "directx9weaver.h"
#include "delayLoader.h"

#include <chrono>
#include <functional>
Expand All @@ -34,6 +35,7 @@ SR::SwitchableLensHint* lens_hint = nullptr;
HotKeyManager* hotKey_manager = nullptr;

// Currently we use this string to determine if we should toggle this shader on press of the shortcut. We can expand this to a list later.
static bool dll_failed_to_load = false;
static const std::string depth_3D_shader_name = "SuperDepth3D";
static const std::string sr_shader_name = "SR";
static char char_buffer[CHAR_BUFFER_SIZE];
Expand Down Expand Up @@ -183,7 +185,12 @@ static void draw_status_overlay(reshade::api::effect_runtime* runtime) {
std::string status_string = "Status: \n";
if (sr_context == nullptr) {
// Unable to connect to the SR Service. Fall back to drawing the overlay UI ourselves.
status_string += "INACTIVE - NO SR SERVICE DETECTED, MAKE SURE THE SR PLATFORM IS INSTALLED AND RUNNING\nwww.srappstore.com\n";
status_string += "INACTIVE - NO SR SERVICE DETECTED, MAKE SURE THE SR PLATFORM IS INSTALLED AND RUNNING\nhttps://github.com/LeiaInc/leiainc.github.io/tree/master/SRSDK\n";
printStatusInWeaver = false;
}
else if (dll_failed_to_load) {
// Unable to load at least one of the SR DLLs
status_string += "INACTIVE - UNABLE TO LOAD ALL SR DLLS, MAKE SURE THE SR PLATFORM IS INSTALLED AND RUNNING\nhttps://github.com/LeiaInc/leiainc.github.io/tree/master/SRSDK\n";
printStatusInWeaver = false;
}
else if (weaver_implementation) {
Expand Down
42 changes: 0 additions & 42 deletions gamebridge_reshade/src/pathGetter.h

This file was deleted.

0 comments on commit 0a2447d

Please sign in to comment.