예제 #1
0
 public static void Start()
 {
     timer = new DispatcherTimer(TimeSpan.FromMilliseconds(50), DispatcherPriority.Background, (sender, args) => OnTimer(), Dispatcher.CurrentDispatcher);
     timer.Start();
     foreach (var state in GameProcessStates)
     {
         OnGameInjectionStateChange?.Invoke(state.GameType, state.InjectionState);
     }
     GameMessageDispatcher.OnGameMessage += OnGameMessage;
 }
예제 #2
0
        private static void OnTimer()
        {
            foreach (var state in GameProcessStates)
            {
                var previousInjectionState = state.InjectionState;
//                var previousWindowState = state.WindowState;
//                var previousIsForeground = state.IsForeground;
//                var previousScreenCoordinates = state.ScreenCoordinates;

                if (state.Process?.HasExited == true)
                {
                    state.Reset();
                }

                switch (state.InjectionState)
                {
                case InjectionState.Idle:
                    var processes = Process.GetProcessesByName(state.ProcessName);
//                        foreach (var process in processes)
//                            Logger.LogDebug(state.GameType, $"Game process: {process.ProcessName} {process.MainModule.FileName}");
                    state.Process = processes.FirstOrDefault();
                    if (state.Process == null)
                    {
                        break;
                    }
                    state.InjectionState = InjectionState.Injecting;
                    break;

                case InjectionState.Injecting:
                    state.InjectionAttempt++;
                    state.SocketStreamClient.Start();
                    if (SendCommand(state.GameType, CommandType.Ping) == "Pong")
                    {
                        state.InjectionState = InjectionState.Injected;
                        break;
                    }
                    Logger.LogDebug(state.GameType, $"Injecting DeckTracker.InGame.Helper.dll, attempt #{state.InjectionAttempt}");
                    string injectionLibrary = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DeckTracker.InGame.Helper.dll");
                    state.InjectionState = DllInjector.InjectDll(state.GameType, (uint)state.Process.Id, injectionLibrary, out int _) ? InjectionState.Injected : InjectionState.Failed;
                    if (state.InjectionState == InjectionState.Injected)
                    {
                        Logger.LogDebug(state.GameType, "Injected DeckTracker.InGame.Helper.dll, waiting for response");
                        if (SendCommand(state.GameType, CommandType.Ping, 10000) != "Pong")
                        {
                            state.InjectionState = InjectionState.Failed;
                        }
                    }
                    break;

                case InjectionState.Injected:
                    pingCounter = (pingCounter + 1) % 200;
                    if (pingCounter == 0 && SendCommand(state.GameType, CommandType.Ping) != "Pong" && SendCommand(state.GameType, CommandType.Ping) != "Pong" && SendCommand(state.GameType, CommandType.Ping) != "Pong")
                    {
                        state.InjectionState = InjectionState.Disconnected;
                        break;
                    }
//                        if (state.Window == IntPtr.Zero)
//                            state.Window = WindowsHelper.FindUnityWindow(state.Process.Id);
//                        if (state.Window != IntPtr.Zero) {
//                            state.WindowState = WindowsHelper.GetWindowState(state.Window);
//                            state.IsForeground = WindowsHelper.IsForegroundWindow(state.Window);
//                            state.ScreenCoordinates = WindowsHelper.GetScreenCoordinates(state.Window);
//                        }
//                        if (state.OverlayWindow == null)
//                            state.OverlayWindow = new OverlayWindow(state.GameType, state.Window);
                    break;

                case InjectionState.Disconnected:
//                        state.OverlayWindow?.Close();
//                        state.OverlayWindow = null;
                    pingCounter = (pingCounter + 1) % 200;
                    if (pingCounter == 0 && SendCommand(state.GameType, CommandType.Ping, 1000) == "Pong")
                    {
                        state.InjectionState = InjectionState.Injected;
                    }
                    break;

                case InjectionState.Failed:
                    state.SocketStreamClient.Stop();
                    if (state.InjectionAttempt <= 3)
                    {
                        state.InjectionState = InjectionState.Injecting;
                    }
                    break;
                }

//                if (state.InjectionState != previousInjectionState || state.WindowState != previousWindowState || state.IsForeground != previousIsForeground || state.ScreenCoordinates != previousScreenCoordinates)
//                    state.OverlayWindow?.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => state.OverlayWindow.OnGameProcessStateChange()));

                if (state.InjectionState != previousInjectionState)
                {
                    Logger.LogDebug(state.GameType, $"Injection state changed: {state.InjectionState}");
                    OnGameInjectionStateChange?.Invoke(state.GameType, state.InjectionState);
                }
            }
        }