/// <summary> /// Handles the message. /// </summary> /// <param name="message">The message.</param> public void Handle(NewRound message) { if (EnableSlowOpponentMode && message.MyTurn) { HearthstoneHelper.SetWindowToForeground(); } }
public void StartHearthstone() { try { var wnd = HearthstoneHelper.GetHearthstoneWindow(); if (wnd != IntPtr.Zero) { return; } var hsLocation = String.Empty; using (var settings = new HearthstoneRegistrySettings()) { hsLocation = settings.BattleNetLocation; if (String.IsNullOrEmpty(hsLocation) || !File.Exists(hsLocation)) { // try default var def = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Battle.net"); def = Path.Combine(def, "Battle.net Launcher.exe"); if (File.Exists(def)) { settings.BattleNetLocation = hsLocation = def; } else { if (AskHearthstoneLocation(out hsLocation)) { settings.BattleNetLocation = hsLocation; } } } } if (File.Exists(hsLocation)) { var start = new ProcessStartInfo(hsLocation) { Arguments = "--exec=\"launch WTCG\"" }; Process.Start(start); } } catch (Exception) { // TODO: check, show error message instead of ignoring exception } }
public void CreateScreenShot() { var wnd = HearthstoneHelper.GetHearthstoneWindow(); if (wnd != IntPtr.Zero) { var shot = new ScreenCapture(); this.screenshotBitmap = new Bitmap(shot.GetDesktopBitmap(wnd)); var ms = new MemoryStream(); var bi = new BitmapImage(); bi.BeginInit(); this.screenshotBitmap.Save(ms, ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); bi.StreamSource = ms; bi.EndInit(); this.Screenshot = bi; } else { this.Notification = "failed to get window"; } // this.hearthstoneWindowImage.Dispose(); }
private void Start() { if (this.running) { Log.Warn("already running"); return; } this.running = true; Log.Debug("Capture method: {0}, Version: {1}", this.CaptureMethod, Assembly.GetEntryAssembly().GetName().Version); stopRequested = false; while (stopRequested == false) { if (!injected) { IntPtr wnd = IntPtr.Zero; try { wnd = HearthstoneHelper.GetHearthstoneWindow(); } catch (Exception ex) { Log.Error(ex); } // Verify window exists if (wnd == IntPtr.Zero) { OnWindowLost(); Thread.Sleep(1000); continue; } Thread.Sleep(3000); this.InitServerChannel(); int injectresult = -1; var injecttask = Task.Run( () => { injectresult = Inject("hearthstone.exe", "HearthCapLogger.dll", "HearthCapLogger", "Loader", "Load", this.probe); }); if (!injecttask.Wait(5000)) { } if (injectresult != 0) { Log.Warn("Window found, but could not find hearthstone.exe"); OnWindowLost(); Thread.Sleep(1000); continue; } Log.Info("Injected into hearthstone.exe"); Thread.Sleep(500); injected = true; } try { bool error = false; try { // .NET Remoting exceptions will throw RemotingException if (!this._interface.Ping(1000)) { Log.Error("Failed to ping log engine."); error = true; } } catch (Exception ex) { Log.Error(ex); error = true; } if (error) { injected = false; } else { OnWindowFound(); Thread.Sleep(1000); } } catch (Exception e) { Log.Error(e.ToString); // this._interface.Message(MessageType.Error, "An unexpected error occured: {0}", e.ToString()); } } if (_interface != null) { try { this._interface.Disconnect(); } finally { RemotingServices.Disconnect(this._interface); } } running = false; }
private void CaptureLoop() { IntPtr wnd; try { wnd = HearthstoneHelper.GetHearthstoneWindow(); } catch (Exception ex) { Log.Error(ex); OnWindowLost(); return; } // Verify window exists if (wnd == IntPtr.Zero) { OnWindowLost(); return; } ScreenshotResource img = null; try { if (currentCaptureMethod == null) { currentCaptureMethod = CaptureMethod; } if (preferredCaptureMethod != CaptureMethod) { // user changed capture method in settings currentCaptureMethod = CaptureMethod; preferredCaptureMethod = CaptureMethod; } if (dontUseDirectX && currentCaptureMethod.Value == CaptureMethod.DirectX) { Log.Info("DirectX gave too much errors, switching to Wdm."); currentCaptureMethod = CaptureMethod.Wdm; } switch (currentCaptureMethod) { case CaptureMethod.AutoDetect: var detectedMethod = DetectCaptureMethod(wnd, out img); if (detectedMethod == null) { OnWindowMinimized(); extraDelay = 200; // small delay, just enough to not be noticable, but less cpu Thread.Sleep(extraDelay); return; } currentCaptureMethod = detectedMethod.Value; break; case CaptureMethod.DirectX: img = CaptureDirectX(wnd); break; case CaptureMethod.Wdm: img = CaptureWdm(wnd); break; case CaptureMethod.BitBlt: img = CaptureWdm(wnd, false, true); break; } if (lastCaptureMethod != currentCaptureMethod.Value) { Log.Debug("Capture method changed from {0} to {1}", lastCaptureMethod, currentCaptureMethod.Value); // Do not detach hook, so we can quickly switch again //if (lastCaptureMethod == CaptureMethod.DirectX) //{ // DettachHookFromProcess(); //} lastCaptureMethod = currentCaptureMethod.Value; } if (img == null || img.Bitmap == null) { TraceLog.Log("No image data found."); OnWindowMinimized(); extraDelay = 200; // small delay, just enough to not be noticable, but less cpu Thread.Sleep(extraDelay); return; } if (CaptureMethod == CaptureMethod.AutoDetect) { if (lastImageHeight > 0 && lastImageHeight != img.Bitmap.Height) { // reset capture method so we detect again on next run Log.Debug("Auto-detect: Image resolution changed from {0} to {1}, reset auto-detect.", lastImageHeight, img.Bitmap.Height); currentCaptureMethod = null; } lastImageHeight = img.Bitmap.Height; } extraDelay = 0; OnWindowFound(); if (this.PublishCapturedWindow) { // Log.Diag("Window captured"); var bmpcpy = new Bitmap(img.Bitmap); this.Publish(new WindowCaptured(bmpcpy), log: false); } var start = DateTime.Now.Ticks; try { // scan areas, publish events; foreach (var scanner in this.imageScanners) { scanner.Run(img.Bitmap, null); } } finally { try { if (img != null) { img.Dispose(); } } catch { } var stop = DateTime.Now.Ticks; var proc = TimeSpan.FromTicks(stop - start).Milliseconds; TraceLog.Log("Scanner speed: {0}", proc); } // wait with passing next capture to scanner and signal scanner thread to process //this.captureWaitHandle.Wait(); //this.captureWaitHandle.Reset(); //if (this.currentImage != null) //{ // this.currentImage.Dispose(); // this.currentImage = null; //} //this.currentImage = img; //this.scannerWaitHandle.Set(); } catch (ScreenshotCaptureException ex) { Log.Debug(ex.ToString()); OnWindowLost(); extraDelay = 2000; Thread.Sleep(extraDelay); } catch (Exception ex) { Log.Error(ex); } }
private void CaptureLoop() { if (!injected) { var wnd = IntPtr.Zero; try { wnd = HearthstoneHelper.GetHearthstoneWindow(); } catch (Exception ex) { Log.Error(ex); } // Verify window exists if (wnd == IntPtr.Zero) { OnWindowLost(); Thread.Sleep(1000); return; } Thread.Sleep(3000); InitServerChannel(); var injectresult = -1; var injecttask = Task.Run( () => { injectresult = Inject("hearthstone.exe", "HearthCapLogger.dll", "HearthCapLogger", "Loader", "Load", probe); }); if (!injecttask.Wait(5000)) { } if (injectresult != 0) { Log.Warn("Window found, but could not find hearthstone.exe"); OnWindowLost(); Thread.Sleep(1000); return; } Log.Info("Injected into hearthstone.exe"); Thread.Sleep(500); injected = true; } try { var error = false; try { // .NET Remoting exceptions will throw RemotingException if (!_interface.Ping(1000)) { Log.Error("Failed to ping log engine."); error = true; } } catch (Exception ex) { Log.Error(ex); error = true; } if (error) { injected = false; } else { OnWindowFound(); Thread.Sleep(1000); } } catch (Exception e) { Log.Error(e.ToString); // this._interface.Message(MessageType.Error, "An unexpected error occured: {0}", e.ToString()); } }