Skip to content

Commit

Permalink
Fix crash if ghosting fix enabled before view extensions installed
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jun 22, 2023
1 parent 6e880fd commit a7b66f3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/mods/vr/FFakeStereoRenderingHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,7 @@ void FFakeStereoRenderingHook::attempt_hook_fsceneview_constructor() {
return;
}

auto& vr = VR::get();

if (!vr->is_ghosting_fix_enabled() && !vr->is_splitscreen_compatibility_enabled()) {
return;
}

// just try to find it before ghosting fix is even enabled
if (m_asynchronous_scan->value()) {
static std::future<bool> future = std::async(std::launch::async, detail::pre_find_fsceneview_constructor);

Expand All @@ -379,6 +374,12 @@ void FFakeStereoRenderingHook::attempt_hook_fsceneview_constructor() {
}
}

auto& vr = VR::get();

if (!vr->is_ghosting_fix_enabled() && !vr->is_splitscreen_compatibility_enabled()) {
return;
}

utility::ScopeGuard _{[&]() {
m_attempted_hook_fsceneview_constructor = true;
}};
Expand Down Expand Up @@ -2494,7 +2495,10 @@ sdk::FSceneView* FFakeStereoRenderingHook::sceneview_constructor(sdk::FSceneView
return g_hook->m_sceneview_data.constructor_hook.call<sdk::FSceneView*>(view, init_options, a3, a4);
}

std::scoped_lock _{g_hook->m_sceneview_data.mtx};
if (g_hook->m_analyzing_view_extensions || !g_hook->m_has_view_extensions_installed) {
SPDLOG_INFO_ONCE("FSceneView constructor was called before view extensions were installed, aborting");
return g_hook->m_sceneview_data.constructor_hook.call<sdk::FSceneView*>(view, init_options, a3, a4);
}

auto init_options_ue5 = (sdk::FSceneViewInitOptionsUE5*)init_options;

Expand Down Expand Up @@ -3327,8 +3331,9 @@ bool FFakeStereoRenderingHook::setup_view_extensions() try {
}

// Will get called when the view extensions are finally hooked.
RenderThreadWorker::get().enqueue([]() {
g_hook->m_analyzing_view_extensions = false;
RenderThreadWorker::get().enqueue([this]() {
this->m_analyzing_view_extensions = false;
this->m_has_view_extensions_installed = true;
});

// overwrite the vtable
Expand Down Expand Up @@ -4306,11 +4311,14 @@ uint32_t FFakeStereoRenderingHook::get_desired_number_of_views_hook(FFakeStereoR
}

if (!is_stereo_enabled || (vr->is_using_afr() && !vr->is_splitscreen_compatibility_enabled())) {
// We need to know about the second scene state to fix ghosting, so set the view count to 2
// after we know about it, we can continue returning 1.
if (is_stereo_enabled && vr->is_ghosting_fix_enabled() && vr->is_using_afr() &&
g_hook->m_sceneview_data.known_scene_states.size() < 2 && g_hook->m_fixed_localplayer_view_count &&
!!g_hook->m_sceneview_data.constructor_hook)
!!g_hook->m_sceneview_data.constructor_hook && g_hook->m_has_view_extensions_installed)
{
return 2; // We need to know about the second scene state to fix ghosting
// Only works correctly if view extensions are installed, so we can reset the view count to 1 without crashing
return 2;
}

return 1;
Expand Down
1 change: 1 addition & 0 deletions src/mods/vr/FFakeStereoRenderingHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ class FFakeStereoRenderingHook : public ModComponent {


bool m_analyzing_view_extensions{false};
bool m_has_view_extensions_installed{false};

std::chrono::time_point<std::chrono::high_resolution_clock> m_analyze_view_extensions_start_time{};

Expand Down

0 comments on commit a7b66f3

Please sign in to comment.