diff --git a/projects/WinDurango.Common/src/Config.cpp b/projects/WinDurango.Common/src/Config.cpp index eb2fecbc..3c055d59 100644 --- a/projects/WinDurango.Common/src/Config.cpp +++ b/projects/WinDurango.Common/src/Config.cpp @@ -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, diff --git a/projects/WinDurango.KernelX/CMakeLists.txt b/projects/WinDurango.KernelX/CMakeLists.txt index 2bac843d..a71febea 100644 --- a/projects/WinDurango.KernelX/CMakeLists.txt +++ b/projects/WinDurango.KernelX/CMakeLists.txt @@ -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 diff --git a/projects/WinDurango.KernelX/include/WinDurango.KernelX/EraStaWait.h b/projects/WinDurango.KernelX/include/WinDurango.KernelX/EraStaWait.h new file mode 100644 index 00000000..1d05da98 --- /dev/null +++ b/projects/WinDurango.KernelX/include/WinDurango.KernelX/EraStaWait.h @@ -0,0 +1,7 @@ +#pragma once +#include + +// 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); diff --git a/projects/WinDurango.KernelX/src/EraStaWait.cpp b/projects/WinDurango.KernelX/src/EraStaWait.cpp new file mode 100644 index 00000000..61d0824b --- /dev/null +++ b/projects/WinDurango.KernelX/src/EraStaWait.cpp @@ -0,0 +1,112 @@ +#include "EraStaWait.h" +#include "Hooks.h" +#include +#include + +// 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(GetProcAddress(msvcp, "_Cnd_wait")); + TrueCndTimedWait = reinterpret_cast(GetProcAddress(msvcp, "_Cnd_timedwait")); + TrueMtxLock = reinterpret_cast(GetProcAddress(msvcp, "_Mtx_lock")); + TrueMtxUnlock = reinterpret_cast(GetProcAddress(msvcp, "_Mtx_unlock")); + if (!TrueCndWait || !TrueCndTimedWait || !TrueMtxLock || !TrueMtxUnlock) + return; + + XWinePatchImport(titleModule, msvcp, "_Cnd_wait", reinterpret_cast(&EraCndWait)); +} diff --git a/projects/WinDurango.KernelX/src/dllmain.cpp b/projects/WinDurango.KernelX/src/dllmain.cpp index 9658e26d..55ea2f48 100644 --- a/projects/WinDurango.KernelX/src/dllmain.cpp +++ b/projects/WinDurango.KernelX/src/dllmain.cpp @@ -1,4 +1,5 @@ #include "Hooks.h" +#include "EraStaWait.h" #include "kernelx.h" #include "Logan.h" #include "ForzaThreadHook.h" @@ -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) diff --git a/projects/WinDurango.KernelX/src/kernelx.cpp b/projects/WinDurango.KernelX/src/kernelx.cpp index f9ae32de..b3428c7d 100644 --- a/projects/WinDurango.KernelX/src/kernelx.cpp +++ b/projects/WinDurango.KernelX/src/kernelx.cpp @@ -5,6 +5,7 @@ #include #include "EraCoreApplication.h" #include +#include EXTERN_C CONSOLE_TYPE __stdcall GetConsoleType() { @@ -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\\.pck the title asks for TPACK: and TPACK:\.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 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 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); @@ -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); diff --git a/projects/WinDurango.Kinect/CMakeLists.txt b/projects/WinDurango.Kinect/CMakeLists.txt index 3600e5b1..75a6c05f 100644 --- a/projects/WinDurango.Kinect/CMakeLists.txt +++ b/projects/WinDurango.Kinect/CMakeLists.txt @@ -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}" diff --git a/projects/WinDurango.Kinect/include/WinDurango.Kinect/Windows/Kinect/Windows.Kinect.KinectSensor.h b/projects/WinDurango.Kinect/include/WinDurango.Kinect/Windows/Kinect/Windows.Kinect.KinectSensor.h index be6c0704..c61114d3 100644 --- a/projects/WinDurango.Kinect/include/WinDurango.Kinect/Windows/Kinect/Windows.Kinect.KinectSensor.h +++ b/projects/WinDurango.Kinect/include/WinDurango.Kinect/Windows/Kinect/Windows.Kinect.KinectSensor.h @@ -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() { - GetDefaultKinectSensor(&m_pKinectSensor); + if (IsKinectRuntimeAvailable()) + GetDefaultKinectSensor(&m_pKinectSensor); } static winrt::Windows::Kinect::KinectSensor GetDefault(); diff --git a/projects/WinDurango.Kinect/src/Windows/Kinect/Windows.Kinect.KinectSensor.cpp b/projects/WinDurango.Kinect/src/Windows/Kinect/Windows.Kinect.KinectSensor.cpp index b90084e9..89507c45 100644 --- a/projects/WinDurango.Kinect/src/Windows/Kinect/Windows.Kinect.KinectSensor.cpp +++ b/projects/WinDurango.Kinect/src/Windows/Kinect/Windows.Kinect.KinectSensor.cpp @@ -4,6 +4,19 @@ namespace winrt::Windows::Kinect::implementation { + bool IsKinectRuntimeAvailable() + { + static const bool available = [] { + // Kept loaded so the delay-load helper binds to this module. + HMODULE runtime = LoadLibraryExW(L"Kinect20.dll", nullptr, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); + if (runtime == nullptr) + p_wd->log.Warn("WinDurango::Kinect::Windows::Kinect::KinectSensor", + "Kinect20.dll (Kinect for Windows Runtime 2.0) not found; reporting no Kinect sensor"); + return runtime != nullptr; + }(); + return available; + } + Kinect::AudioSource AudioBeam::AudioSource() { return m_audioSource; @@ -1093,30 +1106,37 @@ namespace winrt::Windows::Kinect::implementation void KinectSensor::Open() { - m_pKinectSensor->Open(); + if (m_pKinectSensor) + m_pKinectSensor->Open(); } void KinectSensor::Close() { - m_pKinectSensor->Close(); + if (m_pKinectSensor) + m_pKinectSensor->Close(); } bool KinectSensor::IsOpen() { BOOLEAN isOpen = 0; - m_pKinectSensor->get_IsOpen(&isOpen); + if (m_pKinectSensor) + m_pKinectSensor->get_IsOpen(&isOpen); return (bool)isOpen; } bool KinectSensor::IsAvailable() { BOOLEAN isAvailable = 0; - m_pKinectSensor->get_IsAvailable(&isAvailable); + if (m_pKinectSensor) + m_pKinectSensor->get_IsAvailable(&isAvailable); return (bool)isAvailable; } Kinect::ColorFrameSource KinectSensor::ColorFrameSource() { + if (!m_pKinectSensor) + return nullptr; + ::IColorFrameSource *pColorFrameSource = nullptr; HRESULT hresult = m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource); @@ -1126,6 +1146,9 @@ namespace winrt::Windows::Kinect::implementation Kinect::DepthFrameSource KinectSensor::DepthFrameSource() { + if (!m_pKinectSensor) + return nullptr; + ::IDepthFrameSource *pDepthFrameSource = nullptr; HRESULT hresult = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource); @@ -1137,6 +1160,9 @@ namespace winrt::Windows::Kinect::implementation Kinect::BodyFrameSource KinectSensor::BodyFrameSource() { + if (!m_pKinectSensor) + return nullptr; + ::IBodyFrameSource *pBodyFrameSource = nullptr; HRESULT hresult = m_pKinectSensor->get_BodyFrameSource(&pBodyFrameSource); @@ -1146,6 +1172,9 @@ namespace winrt::Windows::Kinect::implementation Kinect::BodyIndexFrameSource KinectSensor::BodyIndexFrameSource() { + if (!m_pKinectSensor) + return nullptr; + ::IBodyIndexFrameSource *pBodyIndexFrameSource = nullptr; HRESULT hresult = m_pKinectSensor->get_BodyIndexFrameSource(&pBodyIndexFrameSource); @@ -1155,6 +1184,9 @@ namespace winrt::Windows::Kinect::implementation Kinect::InfraredFrameSource KinectSensor::InfraredFrameSource() { + if (!m_pKinectSensor) + return nullptr; + ::IInfraredFrameSource *pInfraredFrameSource = nullptr; HRESULT hresult = m_pKinectSensor->get_InfraredFrameSource(&pInfraredFrameSource); @@ -1185,6 +1217,9 @@ namespace winrt::Windows::Kinect::implementation Kinect::CoordinateMapper KinectSensor::CoordinateMapper() { + if (!m_pKinectSensor) + return nullptr; + ::ICoordinateMapper *pCoordinateMapper = nullptr; HRESULT hresult = m_pKinectSensor->get_CoordinateMapper(&pCoordinateMapper); @@ -1203,7 +1238,8 @@ namespace winrt::Windows::Kinect::implementation { DWORD kinectCapabilities = 0; - HRESULT hresult = m_pKinectSensor->get_KinectCapabilities(&kinectCapabilities); + if (m_pKinectSensor) + m_pKinectSensor->get_KinectCapabilities(&kinectCapabilities); return static_cast(kinectCapabilities); }