#include <stdio.h>
#include <stdlib.h>
#include "C:\Users\i7Matisse\Documents\1 informatique\DEV\C\UTILES\Multi-Process-Thread\multi-threading.h"

void print_error(void)
{
    printf("Error: ");
    printf("%lu\n", GetLastError());
    system("pause");
}

int main(int argc, char *argv[])
{
    /*if(argc < 3)
    {
        printf("Usage:\nhooker.exe [The_game_to_hook] [The_autoclicker]");
        system("pause");
    }*/

    // Game Process
    PROCESS_INFORMATION Game_Process_Info = {0};
    BOOL Game_Process_Success = create_a_process_with_name("C:\\GOG Games\\Stronghold Crusader Extreme HD\\Stronghold Crusader.exe", "C:\\GOG Games\\Stronghold Crusader Extreme HD\\", &Game_Process_Info);

    if(Game_Process_Success == 1)
    {
        printf("Launched Game !\n");
        printf("Process ID: ");
        printf("%lu\n", Game_Process_Info.dwProcessId);
    }else
    {
        print_error();
    }

    // Autoclicker Process
    PROCESS_INFORMATION Autoclicker_Process_Info = {0};
    BOOL Autoclicker_Process_Success = create_a_process_with_command("C:\\Users\\i7Matisse\\Documents\\1 informatique\\DEV\\C\\UTILES\\AutoClicker\\a.exe 2 0x41 13", "C:\\Users\\i7Matisse\\Documents\\1 informatique\\DEV\\C\\UTILES\\AutoClicker\\", &Autoclicker_Process_Info);

    if(Autoclicker_Process_Success == 1)
    {
        printf("Autoclicker Hooked !\n");
        printf("Process ID: ");
        printf("%lu\n", Autoclicker_Process_Info.dwProcessId);
    }else
    {
        print_error();
    }


// --------------------------------------------------------------------------------------
    // main loop
    DWORD exitCode;
    while(Game_Process_Success)
    {
        Sleep(20);

        // managing game process
        if(GetExitCodeProcess(Game_Process_Info.hProcess, &exitCode) == 0)
        {
            print_error();
        }else if(exitCode != STILL_ACTIVE)
        {
            printf("Game Closed.\n");

            if(TerminateProcess(Autoclicker_Process_Info.hProcess, 0) == 0)
            {
                print_error();
            }
            Game_Process_Success = 0;
        }
    }

    printf("ending.\n");
    return 0;
}