| 
 | 1 | +#include "unrealsdk/pch.h"  | 
 | 2 | +#include "unrealsdk/game/bl1/bl1.h"  | 
 | 3 | +#include "unrealsdk/memory.h"  | 
 | 4 | +#include "unrealsdk/utils.h"  | 
 | 5 | + | 
 | 6 | +#if UNREALSDK_FLAVOUR == UNREALSDK_FLAVOUR_WILLOW && !defined(UNREALSDK_IMPORTING)  | 
 | 7 | + | 
 | 8 | +using namespace unrealsdk::utils;  | 
 | 9 | +using namespace unrealsdk::memory;  | 
 | 10 | + | 
 | 11 | +namespace unrealsdk::game {  | 
 | 12 | + | 
 | 13 | +namespace {  | 
 | 14 | + | 
 | 15 | +// This is ___tmainCRTStartup, so expect it's very stable  | 
 | 16 | +const constinit Pattern<14> UNPACKED_ENTRY_SIG{"6A 58 68 ?? ?? ?? ?? E8 ?? ?? ?? ?? 33 DB"};  | 
 | 17 | + | 
 | 18 | +std::atomic<bool> ready = false;  | 
 | 19 | +std::mutex ready_mutex;  | 
 | 20 | +std::condition_variable ready_cv;  | 
 | 21 | + | 
 | 22 | +// This is probably orders of magnitude bigger than we need, but best be safe  | 
 | 23 | +const constexpr std::chrono::seconds FALLBACK_DELAY{5};  | 
 | 24 | + | 
 | 25 | +// NOLINTBEGIN(readability-identifier-naming)  | 
 | 26 | + | 
 | 27 | +// NOLINTNEXTLINE(modernize-use-using)  - need a typedef for calling conventions in msvc  | 
 | 28 | +typedef void(WINAPI* GetStatupInfoA_func)(LPSTARTUPINFOA);  | 
 | 29 | +GetStatupInfoA_func GetStatupInfoA_ptr;  | 
 | 30 | + | 
 | 31 | +void GetStartupInfoA_hook(LPSTARTUPINFOA lpStartupInfo) {  | 
 | 32 | +    GetStatupInfoA_ptr(lpStartupInfo);  | 
 | 33 | + | 
 | 34 | +    static_assert(decltype(ready)::is_always_lock_free, "need to lock on checking ready flag too");  | 
 | 35 | +    if (ready.load()) {  | 
 | 36 | +        return;  | 
 | 37 | +    }  | 
 | 38 | + | 
 | 39 | +    const std::lock_guard<std::mutex> lock{ready_mutex};  | 
 | 40 | +    ready.store(true);  | 
 | 41 | +    ready_cv.notify_all();  | 
 | 42 | +}  | 
 | 43 | + | 
 | 44 | +// NOLINTEND(readability-identifier-naming)  | 
 | 45 | + | 
 | 46 | +}  // namespace  | 
 | 47 | + | 
 | 48 | +void BL1Hook::wait_for_steam_drm(void) {  | 
 | 49 | +    {  | 
 | 50 | +        // Immediately suspend the other threads  | 
 | 51 | +        const ThreadSuspender suspend{};  | 
 | 52 | + | 
 | 53 | +        if (UNPACKED_ENTRY_SIG.sigscan_nullable() != 0) {  | 
 | 54 | +            // If we found a match, we're already unpacked  | 
 | 55 | +            return;  | 
 | 56 | +        }  | 
 | 57 | + | 
 | 58 | +        LOG(MISC, "Waiting for steam drm unpack");  | 
 | 59 | + | 
 | 60 | +        // Set up a hook for GetStartupInfoA, which is the one of the first things the unpacked  | 
 | 61 | +        // entry function calls, which we'll use to tell once it's been unpacked  | 
 | 62 | +        MH_STATUS status = MH_OK;  | 
 | 63 | + | 
 | 64 | +        status = MH_CreateHook(reinterpret_cast<LPVOID>(&GetStartupInfoA),  | 
 | 65 | +                               reinterpret_cast<LPVOID>(&GetStartupInfoA_hook),  | 
 | 66 | +                               reinterpret_cast<LPVOID*>(&GetStatupInfoA_ptr));  | 
 | 67 | +        if (status != MH_OK) {  | 
 | 68 | +            LOG(ERROR, "Failed to create GetStartupInfoA hook: {:x}",  | 
 | 69 | +                static_cast<uint32_t>(status));  | 
 | 70 | + | 
 | 71 | +            LOG(ERROR, "Falling back to a static delay");  | 
 | 72 | +            std::this_thread::sleep_for(FALLBACK_DELAY);  | 
 | 73 | +            return;  | 
 | 74 | +        }  | 
 | 75 | + | 
 | 76 | +        status = MH_EnableHook(reinterpret_cast<LPVOID>(&GetStartupInfoA));  | 
 | 77 | +        if (status != MH_OK) {  | 
 | 78 | +            LOG(ERROR, "Failed to enable GetStartupInfoA hook: {:x}",  | 
 | 79 | +                static_cast<uint32_t>(status));  | 
 | 80 | + | 
 | 81 | +            LOG(ERROR, "Falling back to a static delay");  | 
 | 82 | +            std::this_thread::sleep_for(FALLBACK_DELAY);  | 
 | 83 | +            return;  | 
 | 84 | +        }  | 
 | 85 | + | 
 | 86 | +        // Drop out of this scope and unsuspend the other threads, let the unpacker run  | 
 | 87 | +    }  | 
 | 88 | + | 
 | 89 | +    std::unique_lock lock(ready_mutex);  | 
 | 90 | +    ready_cv.wait(lock, [] { return ready.load(); });  | 
 | 91 | + | 
 | 92 | +    MH_STATUS status = MH_OK;  | 
 | 93 | +    status = MH_DisableHook(reinterpret_cast<LPVOID>(&GetStartupInfoA));  | 
 | 94 | +    if (status != MH_OK) {  | 
 | 95 | +        LOG(ERROR, "Failed to disable GetStartupInfoA hook: {:x}", static_cast<uint32_t>(status));  | 
 | 96 | + | 
 | 97 | +        // If it fails, there isn't really any harm in leaving it active, just return  | 
 | 98 | +        return;  | 
 | 99 | +    }  | 
 | 100 | + | 
 | 101 | +    status = MH_RemoveHook(reinterpret_cast<LPVOID>(&GetStartupInfoA));  | 
 | 102 | +    if (status != MH_OK) {  | 
 | 103 | +        LOG(ERROR, "Failed to remove GetStartupInfoA hook: {:x}", static_cast<uint32_t>(status));  | 
 | 104 | +        return;  | 
 | 105 | +    }  | 
 | 106 | +}  | 
 | 107 | + | 
 | 108 | +}  // namespace unrealsdk::game  | 
 | 109 | + | 
 | 110 | +#endif  | 
0 commit comments