Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions projects/WinDurango.Common/src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ std::string jsonTemplate = R"({
"B": 88,
"X": 67,
"Y": 86,
"Up": 26,
"Down": 28,
"Left": 25,
"Right": 27,
"Up": 38,
"Down": 40,
"Left": 37,
"Right": 39,
"Menu": 13,
"View": 27,
"LeftThumb": 160,
Expand Down
2 changes: 1 addition & 1 deletion projects/WinDurango.KernelX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ set(FILES
include/WinDurango.KernelX/Hooks.h
)

add_library(WinDurango.KernelX SHARED ${FILES} "include/WinDurango.KernelX/EraCoreApplication.h" "src/EraCoreApplication.cpp" "include/WinDurango.KernelX/EraCoreWindow.h" "src/EraCoreWindow.cpp" "include/WinDurango.KernelX/MMDeviceEnumerator.h" "src/MMDeviceEnumerator.cpp" "include/WinDurango.KernelX/ForzaThreadHook.h" "include/WinDurango.KernelX/D3D11Peggle2Hook.h" "src/Logan.cpp" "include/WinDurango.KernelX/ACPLoganTypes.h" "src/XmaDecoder.cpp" "include/WinDurango.KernelX/XmaDecoder.h" "include/WinDurango.KernelX/EraNetworking.h" "src/EraNetworking.cpp")
add_library(WinDurango.KernelX SHARED ${FILES} "include/WinDurango.KernelX/EraCoreApplication.h" "src/EraCoreApplication.cpp" "include/WinDurango.KernelX/EraCoreWindow.h" "src/EraCoreWindow.cpp" "include/WinDurango.KernelX/MMDeviceEnumerator.h" "src/MMDeviceEnumerator.cpp" "include/WinDurango.KernelX/ForzaThreadHook.h" "include/WinDurango.KernelX/D3D11Peggle2Hook.h" "src/Logan.cpp" "include/WinDurango.KernelX/ACPLoganTypes.h" "src/XmaDecoder.cpp" "include/WinDurango.KernelX/XmaDecoder.h" "include/WinDurango.KernelX/EraNetworking.h" "src/EraNetworking.cpp" "include/WinDurango.KernelX/EraStaWait.h" "src/EraStaWait.cpp")

target_link_libraries(WinDurango.KernelX PRIVATE
WinDurango.Common
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include <windows.h>

// Replaces the title executable's msvcp140 _Cnd_wait import so that a condition-variable wait on
// an application single-threaded apartment (the CoreApplication view thread) keeps servicing
// incoming COM calls. See EraStaWait.cpp for why.
void EraInstallStaWaitPatch(HMODULE titleModule);
112 changes: 112 additions & 0 deletions projects/WinDurango.KernelX/src/EraStaWait.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "EraStaWait.h"
#include "Hooks.h"
#include <objbase.h>
#include <ctime>

// On the console a title can block its CoreApplication view thread in task::wait() (a
// std::condition_variable wait). The console's app C++ runtime refuses such a wait on a Windows
// Runtime STA ("Illegal to wait on a task in a Windows Runtime STA"), so that thread does not
// behave as an STA there. On PC the real CoreApplication makes it an application STA (ASTA) and
// desktop msvcp140 lets the wait block. If the work being waited on needs a COM call into that
// thread, for example a callback that was registered from it, the title deadlocks.
//
// Minecraft: Xbox One Edition 1.61 freezes this way while loading a new world: the view thread
// waits on a Microsoft.Xbox.GameChat operation, and GameChat's worker is waiting on a delegate
// call marshaled back to the view thread.
//
// On an ASTA this turns one long wait into short timed waits and lets incoming COM calls run in
// between. Returning without a notify is a spurious wakeup, which callers of _Cnd_wait already
// handle by rechecking their predicate.

namespace
{
using CndT = void *;
using MtxT = void *;

// Layout of both xtime and _timespec64, the absolute UTC time _Cnd_timedwait takes.
struct EraXtime
{
__time64_t sec;
long nsec;
};

using CndWaitFn = int(__cdecl *)(CndT, MtxT);
using CndTimedWaitFn = int(__cdecl *)(CndT, MtxT, const EraXtime *);
using MtxFn = int(__cdecl *)(MtxT);

constexpr int ThrdSuccess = 0; // _Thrd_success
constexpr int ThrdTimedout = 2; // _Thrd_timedout
constexpr long SliceNs = 10'000'000;
constexpr long NsPerSecond = 1'000'000'000;

CndWaitFn TrueCndWait = nullptr;
CndTimedWaitFn TrueCndTimedWait = nullptr;
MtxFn TrueMtxLock = nullptr;
MtxFn TrueMtxUnlock = nullptr;

bool IsApplicationSta()
{
APTTYPE type;
APTTYPEQUALIFIER qualifier;
if (FAILED(CoGetApartmentType(&type, &qualifier)))
return false;

return type == APTTYPE_STA && qualifier == APTTYPEQUALIFIER_APPLICATION_STA;
}

void ServiceIncomingCalls()
{
// Never signaled: the wait only exists so COM can dispatch calls while it lasts.
thread_local HANDLE idle = CreateEventW(nullptr, TRUE, FALSE, nullptr);
if (idle == nullptr)
{
Sleep(1);
return;
}

DWORD index;
CoWaitForMultipleHandles(COWAIT_DISPATCH_CALLS, 1, 1, &idle, &index);
}

int __cdecl EraCndWait(CndT cnd, MtxT mtx)
{
if (!IsApplicationSta())
return TrueCndWait(cnd, mtx);

timespec now{};
if (timespec_get(&now, TIME_UTC) != TIME_UTC)
return TrueCndWait(cnd, mtx);

EraXtime target{now.tv_sec, now.tv_nsec + SliceNs};
if (target.nsec >= NsPerSecond)
{
target.sec += 1;
target.nsec -= NsPerSecond;
}

int result = TrueCndTimedWait(cnd, mtx, &target);
if (result != ThrdTimedout)
return result;

TrueMtxUnlock(mtx);
ServiceIncomingCalls();
TrueMtxLock(mtx);
return ThrdSuccess;
}
} // namespace

void EraInstallStaWaitPatch(HMODULE titleModule)
{
HMODULE msvcp = GetModuleHandleW(L"msvcp140.dll");
if (msvcp == nullptr)
return;

TrueCndWait = reinterpret_cast<CndWaitFn>(GetProcAddress(msvcp, "_Cnd_wait"));
TrueCndTimedWait = reinterpret_cast<CndTimedWaitFn>(GetProcAddress(msvcp, "_Cnd_timedwait"));
TrueMtxLock = reinterpret_cast<MtxFn>(GetProcAddress(msvcp, "_Mtx_lock"));
TrueMtxUnlock = reinterpret_cast<MtxFn>(GetProcAddress(msvcp, "_Mtx_unlock"));
if (!TrueCndWait || !TrueCndTimedWait || !TrueMtxLock || !TrueMtxUnlock)
return;

XWinePatchImport(titleModule, msvcp, "_Cnd_wait", reinterpret_cast<PVOID>(&EraCndWait));
}
4 changes: 4 additions & 0 deletions projects/WinDurango.KernelX/src/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Hooks.h"
#include "EraStaWait.h"
#include "kernelx.h"
#include "Logan.h"
#include "ForzaThreadHook.h"
Expand Down Expand Up @@ -33,6 +34,9 @@ void KernelxInitialize(HINSTANCE hinstDLL)
"?GetActivationFactoryByPCWSTR@@YAJPEAXAEAVGuid@Platform@@PEAPEAX@Z",
GetActivationFactoryRedirect);

// Keep the CoreApplication view thread servicing COM calls while the title blocks it.
EraInstallStaWaitPatch(GetModuleHandleW(nullptr));

//This is needed for DXVK.
if (!User32) User32 = LoadLibraryA("user32.dll");
if (User32)
Expand Down
78 changes: 78 additions & 0 deletions projects/WinDurango.KernelX/src/kernelx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <winternl.h>
#include "EraCoreApplication.h"
#include <Psapi.h>
#include <mutex>

EXTERN_C CONSOLE_TYPE __stdcall GetConsoleType()
{
Expand Down Expand Up @@ -730,8 +731,82 @@ EXTERN_C void __stdcall XMemReleaseAuxiliaryTitleMemory(_In_opt_ HANDLE hHandle)
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
}

// Minecraft: Xbox One Edition 1.61 reads DLC packs through a "TPACK:" root, a name kept from the
// Xbox 360 content-mount API. It refers to the DLC pack being loaded: after listing and opening
// Mount\DLC\<pack>\<file>.pck the title asks for TPACK: and TPACK:\<file>.pck. Left unmapped, the
// path becomes Mount\TPACK:\..., Windows rejects it as an invalid name, and the title crashes while
// freeing the half-loaded pack. Remember the last DLC pack folder the title touched and resolve
// TPACK: against it.
static std::mutex DlcPackRootLock;
static std::wstring LastDlcPackRoot;

static void RememberDlcPackRoot(std::wstring_view path)
{
size_t start = 0;
while (start < path.size())
{
size_t end = path.find_first_of(L"\\/", start);
if (end == std::wstring_view::npos)
return;

std::wstring_view component = path.substr(start, end - start);
if (component.size() == 3 && _wcsnicmp(component.data(), L"DLC", 3) == 0)
{
size_t packEnd = path.find_first_of(L"\\/", end + 1);
std::wstring_view pack =
path.substr(end + 1, packEnd == std::wstring_view::npos ? std::wstring_view::npos : packEnd - end - 1);
// Skip a listing of the DLC folder itself (DLC\*) and empty components.
if (pack.empty() || pack.find_first_of(L"*?") != std::wstring_view::npos)
return;

std::wstring root(path.substr(0, packEnd == std::wstring_view::npos ? path.size() : packEnd));
std::replace(root.begin(), root.end(), L'/', L'\\');
std::lock_guard<std::mutex> guard(DlcPackRootLock);
LastDlcPackRoot = std::move(root);
return;
}
start = end + 1;
}
}

static bool MapTPackPath(LPCWSTR &lpFileName)
{
if (_wcsnicmp(lpFileName, L"TPACK:", 6) != 0)
return false;

thread_local std::wstring mapped;
{
std::lock_guard<std::mutex> guard(DlcPackRootLock);
if (LastDlcPackRoot.empty())
return false;
mapped = LastDlcPackRoot;
}

std::wstring_view rest(lpFileName + 6);
if (!rest.empty() && rest[0] != L'\\' && rest[0] != L'/')
mapped.push_back(L'\\');
mapped.append(rest);
std::replace(mapped.begin(), mapped.end(), L'/', L'\\');

lpFileName = mapped.c_str();
return true;
}

static void FixRelativePathCore(LPCWSTR &lpFileName);

void FixRelativePath(LPCWSTR &lpFileName)
{
if (lpFileName == nullptr || lpFileName[0] == L'\0')
return;

if (MapTPackPath(lpFileName))
return;

FixRelativePathCore(lpFileName);
RememberDlcPackRoot(lpFileName);
}

static void FixRelativePathCore(LPCWSTR &lpFileName)
{
static std::wstring convert{};
std::wstring_view fileName(lpFileName);
Expand Down Expand Up @@ -881,6 +956,9 @@ EXTERN_C HANDLE __stdcall EraCreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAcce
FixRelativePath(FileName);
}

// Paths handled by the branches above bypass FixRelativePath, so record DLC pack folders here too.
RememberDlcPackRoot(FileName);

HANDLE Result = TrueCreateFileW(FileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition,
dwFlagsAndAttributes, hTemplateFile);

Expand Down
6 changes: 5 additions & 1 deletion projects/WinDurango.Kinect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ target_link_directories(WinDurango.Kinect PRIVATE
"${CMAKE_SOURCE_DIR}/build/vcpkg_installed/x64-windows/lib"
)

target_link_libraries(WinDurango.Kinect PRIVATE WinDurango.Common windowsapp runtimeobject unofficial::kinectsdk2::kinectsdk2)
target_link_libraries(WinDurango.Kinect PRIVATE WinDurango.Common windowsapp runtimeobject unofficial::kinectsdk2::kinectsdk2 delayimp)

# Kinect20.dll comes from the Kinect for Windows Runtime 2.0, which most PCs do not have. Delay-load
# it so windows.kinect.dll still loads without it; KinectSensor then reports no sensor.
target_link_options(WinDurango.Kinect PRIVATE "/DELAYLOAD:Kinect20.dll")

target_compile_definitions(WinDurango.Kinect PUBLIC
WINDURANGO_WINRT_COMPILER_NAME="${CMAKE_CXX_COMPILER_ID}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,17 @@ namespace winrt::Windows::Kinect::implementation
::IFrameDescription *m_pFrameDescription = nullptr;
};

// Kinect20.dll (the Kinect for Windows Runtime 2.0) is delay-loaded. Without it the sensor is
// reported as not available, as a console with no Kinect attached does, instead of the class
// failing to activate.
bool IsKinectRuntimeAvailable();

struct KinectSensor : KinectSensorT<KinectSensor>
{
KinectSensor()
{
GetDefaultKinectSensor(&m_pKinectSensor);
if (IsKinectRuntimeAvailable())
GetDefaultKinectSensor(&m_pKinectSensor);
}

static winrt::Windows::Kinect::KinectSensor GetDefault();
Expand Down
Loading
Loading