#include "injection_panel.hpp" #include #include #include "inject/injector.hpp" namespace coop { namespace { const ImVec4 kGreen(0.4f, 1.0f, 0.4f, 1.0f); const ImVec4 kRed(1.0f, 0.45f, 0.4f, 1.0f); const ImVec4 kGrey(0.7f, 0.7f, 0.7f, 1.0f); std::string narrow(const std::wstring& w) { if (w.empty()) { return {}; } const int len = WideCharToMultiByte(CP_UTF8, 0, w.c_str(), static_cast(w.size()), nullptr, 0, nullptr, nullptr); std::string out(static_cast(len), '\0'); WideCharToMultiByte(CP_UTF8, 0, w.c_str(), static_cast(w.size()), out.data(), len, nullptr, nullptr); return out; } bool contains_ci(const std::wstring& haystack, const char* needle_utf8) { if (needle_utf8 == nullptr || needle_utf8[0] == '\0') { return true; } const std::string hay = narrow(haystack); std::string h = hay, n = needle_utf8; for (char& c : h) { c = static_cast(::tolower(static_cast(c))); } for (char& c : n) { c = static_cast(::tolower(static_cast(c))); } return h.find(n) != std::string::npos; } // Absolute path to coop_hook.dll, assumed to sit next to the host executable. std::wstring hook_dll_path() { wchar_t buffer[MAX_PATH] = {}; const DWORD len = GetModuleFileNameW(nullptr, buffer, MAX_PATH); std::wstring path(buffer, len); const std::size_t slash = path.find_last_of(L"\\/"); if (slash != std::wstring::npos) { path.resize(slash + 1); } path += L"coop_hook.dll"; return path; } } // namespace InjectionPanel::InjectionPanel() { refresh_processes(); } void InjectionPanel::refresh_processes() { processes_ = list_processes(); } void InjectionPanel::inject_selected() { if (selected_pid_ == 0) { status_ = "Select a target process first."; status_color_ = kRed; return; } // Bring up the shared-memory channel before injecting so the hook finds it // immediately on load. if (!server_.start(selected_pid_)) { status_ = "Failed to create shared memory."; status_color_ = kRed; return; } const InjectResult result = inject_dll(selected_pid_, hook_dll_path()); if (result.status == InjectStatus::Ok) { status_ = "Injected into " + narrow(selected_name_) + " (pid " + std::to_string(selected_pid_) + ")."; status_color_ = kGreen; } else { server_.stop(); status_ = std::string("Injection failed: ") + to_string(result.status); if (result.os_error != 0) { status_ += " [err " + std::to_string(result.os_error) + "]"; } status_color_ = kRed; } } void InjectionPanel::publish(const std::array& pads) { if (!test_input_) { server_.publish(pads); return; } // Synthetic, unmistakably non-human pattern: left stick sweeps a circle and // A is pressed every other second. If the game moves on its own to this, the // forwarding pipeline is proven end-to-end. const unsigned long long ms = GetTickCount64(); const double t = static_cast(ms) / 1000.0; std::array synthetic{}; PadInfo& pad = synthetic[0]; pad.connected = true; pad.source = "synthetic test"; pad.state.connected = 1; pad.state.packet = static_cast(ms); pad.state.thumb_lx = static_cast(std::cos(t) * 30000.0); pad.state.thumb_ly = static_cast(std::sin(t) * 30000.0); if ((ms / 1000) % 2 == 0) { pad.state.buttons |= 0x1000; // XINPUT_GAMEPAD_A } server_.publish(synthetic); } void InjectionPanel::draw_hook_status() { if (!server_.running()) { return; } const HookStatusView status = server_.hook_status(); // Convert the cumulative query counter into a rate every half second. const double now = ImGui::GetTime(); if (now - last_sample_time_ >= 0.5) { const double dt = now - last_sample_time_; const unsigned long long delta = status.xinput_queries >= last_query_count_ ? status.xinput_queries - last_query_count_ : 0; query_rate_ = dt > 0.0 ? static_cast(delta) / dt : 0.0; last_query_count_ = status.xinput_queries; last_sample_time_ = now; } ImGui::SeparatorText("Hook status"); if (!status.attached) { ImGui::TextColored(kGrey, "Waiting for hook to attach in the game..."); return; } ImGui::TextColored(kGreen, "Attached (game pid %u, hwnd 0x%llX)", status.game_pid, static_cast(status.game_hwnd)); ImGui::Text("XInput polled: %.0f/s (last slot %u)", query_rate_, status.last_user_index); if (query_rate_ > 0.0) { ImGui::SameLine(); ImGui::TextColored(kGreen, " <- game is reading our input"); } ImGui::TextColored(status.focus_spoof ? kGreen : kGrey, "Focus spoof: %s", status.focus_spoof ? "active" : "inactive"); } void InjectionPanel::draw() { ImGui::SetNextWindowPos(ImVec2(24, 360), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(420, 380), ImGuiCond_FirstUseEver); ImGui::Begin("Injection"); if (server_.running()) { ImGui::TextColored(kGreen, "Forwarding input to pid %lu", server_.target_pid()); if (ImGui::Button("Stop forwarding")) { server_.stop(); status_ = "Stopped."; status_color_ = kGrey; } ImGui::Separator(); } ImGui::TextUnformatted("Target process"); if (ImGui::Button("Refresh")) { refresh_processes(); } ImGui::SameLine(); ImGui::SetNextItemWidth(-1.0f); ImGui::InputTextWithHint("##filter", "filter by name...", filter_, sizeof(filter_)); if (ImGui::BeginListBox("##processes", ImVec2(-1.0f, 200.0f))) { for (const ProcessEntry& entry : processes_) { if (!contains_ci(entry.exe_name, filter_)) { continue; } const bool selected = entry.pid == selected_pid_; char label[300]; snprintf(label, sizeof(label), "%-40s %lu", narrow(entry.exe_name).c_str(), entry.pid); if (ImGui::Selectable(label, selected)) { selected_pid_ = entry.pid; selected_name_ = entry.exe_name; } } ImGui::EndListBox(); } const bool can_inject = selected_pid_ != 0; ImGui::BeginDisabled(!can_inject); if (ImGui::Button("Inject & Connect", ImVec2(-1.0f, 0.0f))) { inject_selected(); } ImGui::EndDisabled(); if (!status_.empty()) { ImGui::TextColored(status_color_, "%s", status_.c_str()); } ImGui::Checkbox("Forward synthetic test input", &test_input_); if (test_input_) { ImGui::SameLine(); ImGui::TextDisabled("(ignores your controller)"); } draw_hook_status(); ImGui::End(); } } // namespace coop