Skip to content

Commit

Permalink
led sdk improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirteenAG committed Nov 9, 2023
1 parent bece055 commit f0abc77
Show file tree
Hide file tree
Showing 11 changed files with 834 additions and 802 deletions.
103 changes: 103 additions & 0 deletions includes/LED/LEDEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,112 @@
#include <chrono>
#include <vector>
#include <algorithm>
#include <CommCtrl.h>
#pragma comment(lib, "Comctl32.lib")

class LEDEffects
{
public:
static inline bool bLogiLedInitialized;
private:
static inline std::vector<HWND> AppWindows;
static inline WNDPROC DefaultWndProc = nullptr;
static inline std::vector<std::function<void()>> Callbacks;
static inline std::future<void> Future;

static void Init()
{
if (!bLogiLedInitialized)
bLogiLedInitialized = LogiLedInit();
}

static void Shutdown()
{
if (bLogiLedInitialized) {
LogiLedShutdown();
bLogiLedInitialized = false;
if (Future.valid())
Future.wait();
}
}

static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD lpdwProcessId;
GetWindowThreadProcessId(hwnd, &lpdwProcessId);
if (lpdwProcessId == lParam)
{
if (IsWindowVisible(hwnd))
AppWindows.push_back(hwnd);
return FALSE;
}
return TRUE;
}

static LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_QUIT:
case WM_DESTROY:
Shutdown();
break;
default:
break;
}

return CallWindowProcW(DefaultWndProc, hWnd, uMsg, wParam, lParam);
}

public:
static void Inject(std::function<void()> callback)
{
Init();

Callbacks.push_back(callback);

if (bLogiLedInitialized)
{
static std::once_flag flag;
std::call_once(flag, []()
{
Future = std::async(std::launch::async, []()
{
while (true)
{
std::this_thread::yield();
if (LEDEffects::bLogiLedInitialized)
{
for (auto& cb : Callbacks) { cb(); }
}
else
break;
}
});

if (AppWindows.empty())
{
std::thread([]()
{
while (AppWindows.empty())
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
EnumWindows(EnumWindowsProc, GetCurrentProcessId());
}
DefaultWndProc = (WNDPROC)SetWindowLongPtrW(AppWindows.back(), GWLP_WNDPROC, (LONG_PTR)WndProc);
}).detach();
}

IATHook::Replace(GetModuleHandleA(NULL), "KERNEL32.DLL",
std::forward_as_tuple("ExitProcess", static_cast<void(__stdcall*)(UINT)>([](UINT uExitCode) {
Shutdown();
ExitProcess(uExitCode);
}))
);
});
}
}

private:
static inline std::vector<LogiLed::KeyName> leftSide = {
LogiLed::KeyName::ESC,
Expand Down
34 changes: 15 additions & 19 deletions includes/dolphin/dolphin.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,34 @@ class Dolphin
}
}

static inline void FindEmulatorMemory(std::future<void>& futureObj)
static inline void FindEmulatorMemory()
{
while (GameMemoryStart == 0 && futureObj.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
uintptr_t curAddr = 0;
MEMORY_BASIC_INFORMATION MemoryInf;
while (VirtualQuery((LPCVOID)curAddr, &MemoryInf, sizeof(MemoryInf)))
{
uintptr_t curAddr = 0;
do
if (MemoryInf.AllocationProtect == PAGE_READWRITE && MemoryInf.State == MEM_COMMIT &&
MemoryInf.Protect == PAGE_READWRITE && MemoryInf.Type == MEM_MAPPED && MemoryInf.RegionSize == 0x2000000)
{
MEMORY_BASIC_INFORMATION MemoryInf;
if (VirtualQuery((LPCVOID)curAddr, &MemoryInf, sizeof(MemoryInf)) == 0) break;
if (MemoryInf.AllocationProtect == PAGE_READWRITE && MemoryInf.State == MEM_COMMIT &&
MemoryInf.Protect == PAGE_READWRITE && MemoryInf.Type == MEM_MAPPED && MemoryInf.RegionSize == 0x2000000)
if (GameMemoryStart == 0)
{
if (GameMemoryStart == 0)
{
GameMemoryStart = (uintptr_t)MemoryInf.BaseAddress;
GameMemoryEnd = GameMemoryStart + 0x01800000;
ImageBase = 0x80000000;
break;
}
GameMemoryStart = (uintptr_t)MemoryInf.BaseAddress;
GameMemoryEnd = GameMemoryStart + 0x01800000;
ImageBase = 0x80000000;
break;
}
curAddr += MemoryInf.RegionSize;
} while (futureObj.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout);
}
curAddr += MemoryInf.RegionSize;
}
}

static inline bool MemoryValid(std::future<void>& futureObj)
static inline bool MemoryValid()
{
static MEMORY_BASIC_INFORMATION MemoryInf;
if (GameMemoryStart == 0 || VirtualQuery((LPCVOID)GameMemoryStart, &MemoryInf, sizeof(MemoryInf)) == 0 || MemoryInf.AllocationProtect != PAGE_READWRITE)
{
GameMemoryStart = 0;
FindEmulatorMemory(futureObj);
FindEmulatorMemory();
return false;
}
return true;
Expand Down
75 changes: 73 additions & 2 deletions includes/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <set>
#include <map>
#include <iomanip>
#include <future>
#include "IniReader.h"
#include "injector\injector.hpp"
#include "injector\calling.hpp"
Expand Down Expand Up @@ -1020,15 +1021,15 @@ namespace WindowedModeWrapper
static HWND WINAPI CreateWindowExA_Hook(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
auto[WindowPosX, WindowPosY, newWidth, newHeight] = beforeCreateWindow(nWidth, nHeight);
GameHWND = CreateWindowExA(dwExStyle, lpClassName, lpWindowName, 0, WindowPosX, WindowPosY, newWidth, newHeight, hWndParent, hMenu, hInstance, lpParam);
GameHWND = CreateWindowExA(dwExStyle, lpClassName, lpWindowName, dwStyle, WindowPosX, WindowPosY, newWidth, newHeight, hWndParent, hMenu, hInstance, lpParam);
afterCreateWindow();
return GameHWND;
}

static HWND WINAPI CreateWindowExW_Hook(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
auto [WindowPosX, WindowPosY, newWidth, newHeight] = beforeCreateWindow(nWidth, nHeight);
GameHWND = CreateWindowExW(dwExStyle, lpClassName, lpWindowName, 0, WindowPosX, WindowPosY, newWidth, newHeight, hWndParent, hMenu, hInstance, lpParam);
GameHWND = CreateWindowExW(dwExStyle, lpClassName, lpWindowName, dwStyle, WindowPosX, WindowPosY, newWidth, newHeight, hWndParent, hMenu, hInstance, lpParam);
afterCreateWindow();
return GameHWND;
}
Expand Down Expand Up @@ -1192,3 +1193,73 @@ std::optional<T> PtrWalkthrough(auto addr, std::convertible_to<ptrdiff_t> auto&&
}
return std::nullopt;
};

class WFP
{
public:
template<typename... Args>
class Event : public std::function<void(Args...)>
{
public:
using std::function<void(Args...)>::function;

private:
std::vector<std::function<void(Args...)>> handlers;

public:
void operator+=(std::function<void(Args...)>&& handler)
{
handlers.push_back(handler);
}

void executeAll(Args... args) const
{
if (!handlers.empty())
{
for (auto& handler : handlers)
{
handler(args...);
}
}
}

std::reference_wrapper<std::vector<std::future<void>>> executeAllAsync(Args... args) const
{
static std::vector<std::future<void>> pendingFutures;
if (!handlers.empty())
{
for (auto& handler : handlers)
{
pendingFutures.push_back(std::async(std::launch::async, handler, args...));
}
}
return std::ref(pendingFutures);
}
};

public:
static Event<>& onInitEvent() {
static Event<> InitEvent;
return InitEvent;
}
static Event<>& onInitEventAsync() {
static Event<> InitEventAsync;
return InitEventAsync;
}
static Event<>& onAfterUALRestoredIATEvent() {
static Event<> AfterUALRestoredIATEvent;
return AfterUALRestoredIATEvent;
}
static Event<>& onShutdownEvent() {
static Event<> ShutdownEvent;
return ShutdownEvent;
}
static Event<>& onGameInitEvent() {
static Event<> GameInitEvent;
return GameInitEvent;
}
static Event<>& onGameProcessEvent() {
static Event<> GameProcessEvent;
return GameProcessEvent;
}
};
Loading

0 comments on commit f0abc77

Please sign in to comment.